summaryrefslogtreecommitdiff
path: root/src/main/java/com/encrox/zombie/Zombie.java
blob: d687f6e63f72fc6161eaf20a8c09a5a7b38d9ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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"));
	}

}