diff options
Diffstat (limited to 'src/main/java/com/encrox/zombie/Zombie.java')
-rwxr-xr-x | src/main/java/com/encrox/zombie/Zombie.java | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/src/main/java/com/encrox/zombie/Zombie.java b/src/main/java/com/encrox/zombie/Zombie.java new file mode 100755 index 0000000..d687f6e --- /dev/null +++ b/src/main/java/com/encrox/zombie/Zombie.java @@ -0,0 +1,245 @@ +package com.encrox.zombie;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+import org.bukkit.Bukkit;
+import org.bukkit.ChatColor;
+import org.bukkit.Material;
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.Plugin;
+import org.bukkit.plugin.PluginDescriptionFile;
+import org.bukkit.plugin.java.JavaPlugin;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import com.sk89q.worldedit.EditSession;
+import com.sk89q.worldedit.bukkit.WorldEditPlugin;
+
+public class Zombie extends JavaPlugin {
+
+ public static Plugin plugin;
+ public static Logger logger;
+ public static PluginDescriptionFile pdf;
+ public static Properties lang;
+ public static JSONObject config;
+ public static JSONArray schematicDescriptors;
+ public static File schematicsDirectory;
+ public static WorldEditPlugin we;
+ public static ArrayList<Map> maps;
+ public static ArrayList<Lobby> lobbies;
+ public static ArrayList<Game> games;
+ public static ZombieWorld zombieWorld;
+ public static Commander commander;
+
+ public void onEnable() {
+ pdf = getDescription();
+ logger = Logger.getLogger("Minecraft");
+ if(setupMyself() && setupWorldEdit()) {
+ plugin = this;
+ commander = new Commander();
+ getCommand("zombie").setExecutor(commander);
+ logger.info(pdf.getName() + " " + pdf.getVersion() + " has been enabled.");
+ } else {
+ logger.info(pdf.getName() + " " + pdf.getVersion() + " has been disabled.");
+ }
+ }
+
+ public boolean setupWorldEdit() {
+ we = (WorldEditPlugin)Bukkit.getPluginManager().getPlugin("WorldEdit");
+ return (we != null);
+ }
+
+ public boolean setupMyself() {
+ if(!this.getDataFolder().exists())
+ this.getDataFolder().mkdirs();
+ schematicsDirectory = new File(this.getDataFolder(), "schematics");
+ if(!schematicsDirectory.exists())
+ schematicsDirectory.mkdirs();
+ File configFile = new File(this.getDataFolder(), "config.json");
+ if(!configFile.exists()) {
+ BufferedInputStream bis;
+ FileOutputStream out;
+ try {
+ bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("config.json"));
+ out = new FileOutputStream(configFile);
+ int current;
+ while((current = bis.read()) != -1) {
+ out.write(current);
+ }
+ bis.close();
+ out.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ BufferedReader br;
+ StringBuilder sb = new StringBuilder();
+ String line;
+ try {
+ br = new BufferedReader(new FileReader(configFile));
+ while((line = br.readLine()) != null)
+ sb.append(line);
+ br.close();
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ return false;
+ }
+ config = new JSONObject(sb.toString());
+ zombieWorld = new ZombieWorld(this, Bukkit.getWorld(config.getString("world")));
+ File schematicDescriptorFile = new File(schematicsDirectory, "descriptor.json");
+ if(!schematicDescriptorFile.exists()) {
+ int current;
+ try {
+ BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("descriptor.json"));
+ FileOutputStream out = new FileOutputStream(schematicDescriptorFile);
+ while((current = bis.read()) != -1) {
+ out.write(current);
+ }
+ bis.close();
+ out.close();
+ } catch(Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ try {
+ BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/test.schematic"));
+ FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "test.schematic"));
+ while((current = bis.read()) != -1) {
+ out.write(current);
+ }
+ bis.close();
+ out.close();
+ } catch(Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ sb = new StringBuilder();
+ try {
+ br = new BufferedReader(new FileReader(schematicDescriptorFile));
+ while((line = br.readLine()) != null)
+ sb.append(line);
+ br.close();
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ return false;
+ }
+ schematicDescriptors = new JSONArray(sb.toString());
+ lang = new Properties();
+ try {
+ //TODO
+ lang.load(getClass().getClassLoader().getResourceAsStream("lang/" + config.getString("locale") + ".properties"));
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return false;
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ maps = new ArrayList<Map>();
+ lobbies = new ArrayList<Lobby>();
+ games = new ArrayList<Game>();
+ for(int i = 0, len = schematicDescriptors.length(); i<len; i++) {
+ maps.add(new Map(schematicDescriptors.getJSONObject(i)));
+ }
+ Bukkit.getPluginManager().registerEvents(new GameListener(), this);
+ return true;
+ }
+
+ public void onDisable() {
+ logger.info(pdf.getName() + " " + pdf.getVersion() + " has been disabled.");
+ }
+
+ public static String createLobby(Player creator, String identifier, String map) {
+ for(int i = 0, size = lobbies.size(); i<size; i++) {
+ if(lobbies.get(i).getIdentifier().equals(identifier)) {
+ return (ChatColor.RED + Zombie.lang.getProperty("lobby_create_identifier"));
+ }
+ if(lobbies.get(i).containsPlayer(creator)) {
+ return (ChatColor.RED + Zombie.lang.getProperty("lobby_create_membership"));
+ }
+ }
+ for(int i = 0, size = maps.size(); i<size; i++) {
+ if(maps.get(i).getName().equals(map)) {
+ lobbies.add(new Lobby(creator, identifier, maps.get(i)));
+ return (ChatColor.GREEN + Zombie.lang.getProperty("lobby_create_success"));
+ }
+ }
+ return (ChatColor.RED + Zombie.lang.getProperty("lobby_create_map"));
+ }
+
+ public static String removePlayer(Player player) {
+ for(int i = 0, size = lobbies.size(); i<size; i++) {
+ if(lobbies.get(i).containsPlayer(player)) {
+ lobbies.get(i).removePlayer(player);
+ return (ChatColor.GREEN + Zombie.lang.getProperty("lobby_leave_success"));
+ }
+ }
+ return (ChatColor.RED + Zombie.lang.getProperty("lobby_leave_membership"));
+ }
+
+ public static String joinLobby(Player player, String identifier) {
+ int size = lobbies.size();
+ Lobby current;
+ for(int i = 0; i<size; i++) {
+ current = lobbies.get(i);
+ if(current.containsPlayer(player)) {
+ current.removePlayer(player);
+ }
+ }
+ for(int i = 0; i<size; i++) {
+ current = lobbies.get(i);
+ if(current.getIdentifier().equals(identifier)) {
+ if(current.getCurrent() < current.getMax()) {
+ current.addPlayer(player);
+ return (ChatColor.GREEN + Zombie.lang.getProperty("lobby_join_success"));
+ }
+ return (ChatColor.RED + Zombie.lang.getProperty("lobby_join_full"));
+ }
+ }
+ return (ChatColor.RED + Zombie.lang.getProperty("lobby_join_identifier"));
+ }
+
+ public static String listLobbies() {
+ String out = ChatColor.WHITE + lang.getProperty("lobby_list") + "\n";
+ Lobby current;
+ for(int i = 0, size = lobbies.size(); i<size; i++) {
+ current = lobbies.get(i);
+ if(current.getCurrent() < current.getMax()) {
+ out += ChatColor.GREEN;
+ } else {
+ out += ChatColor.RED;
+ }
+ out += current.getIdentifier() + ChatColor.WHITE + ", ";
+ }
+ return out;
+ }
+
+ public static String start(Player player) {
+ Lobby current;
+ for(int i = 0, size = lobbies.size(); i<size; i++) {
+ current = lobbies.get(i);
+ if(current.getCreator().equals(player)) {
+ current.start();
+ return (ChatColor.GREEN + lang.getProperty("lobby_start_success"));
+ }
+ }
+ return (ChatColor.GREEN + lang.getProperty("lobby_start_failed"));
+ }
+
+}
\ No newline at end of file |