summaryrefslogtreecommitdiff
path: root/src/main/java/com/encrox/instanceddungeons/InstancedDungeons.java
blob: 751e252daa6dcbb465365f5cd979cf7e5d028cf9 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.encrox.instanceddungeons;

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.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 InstancedDungeons 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 resetDungeons, schematicDescriptors, startDescriptors, endDescriptors, normalDescriptors;
	public static DungeonWorld dungeonWorld;
	public static File schematicsDirectory;
	public static ArrayList<Dungeon> dungeons;
	public static WorldEditPlugin we;
	
	public void onEnable() {
		pdf = getDescription();
		logger = Logger.getLogger("Minecraft");
		if(setupMyself() && setupWorldEdit()) {
			plugin = this;
			getCommand("dungeon").setExecutor(new 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());
		resetDungeons = config.getJSONArray("resetDungeons");
		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/endBottom.schematic"));
				FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "endBottom.schematic"));
				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/endSide.schematic"));
				FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "endSide.schematic"));
				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/endTop.schematic"));
				FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "endTop.schematic"));
				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/startBottom.schematic"));
				FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "startBottom.schematic"));
				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/startSide.schematic"));
				FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "startSide.schematic"));
				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/startTop.schematic"));
				FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "startTop.schematic"));
				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 JSONObject(sb.toString()).getJSONArray("schematics");
		startDescriptors = new JSONArray();
		endDescriptors = new JSONArray();
		normalDescriptors = new JSONArray();
		JSONObject current;
		for(int i = 0, length = schematicDescriptors.length(); i<length; i++) {
			switch((current = schematicDescriptors.getJSONObject(i)).getInt("modifier")) {
			case 0:
				normalDescriptors.put(current);
				break;
			case 1:
				startDescriptors.put(current);
				break;
			case 2:
				endDescriptors.put(current);
				break;
			}
		}
		/*lang = new Properties();
		try {
			//TODO
			lang.load(getClass().getClassLoader().getResourceAsStream("lang/" + config.getString("locale")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		*/
		dungeonWorld = new DungeonWorld(this, Bukkit.getWorld(config.getString("world")));
		dungeons = new ArrayList<Dungeon>();
		return true;
	}
	
	public void onDisable() {
		logger.info(pdf.getName() + " " + pdf.getVersion() + " has been disabled.");
	}
	
	private void addClassPath(final URL url) throws IOException {
        final URLClassLoader sysloader = (URLClassLoader) ClassLoader
                .getSystemClassLoader();
        final Class<URLClassLoader> sysclass = URLClassLoader.class;
        try {
            final Method method = sysclass.getDeclaredMethod("addURL",
                    new Class[] { URL.class });
            method.setAccessible(true);
            method.invoke(sysloader, new Object[] { url });
        } catch (final Throwable t) {
            t.printStackTrace();
            throw new IOException("Error adding " + url
                    + " to system classloader");
        }
    }

}