summaryrefslogtreecommitdiff
path: root/src/main/java/com/encrox/instanceddungeons/Section.java
blob: 282a554b21960a658689ce159b6fd187653a2120 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package com.encrox.instanceddungeons;

import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.Plugin;
import org.json.JSONArray;
import org.json.JSONObject;

import com.encrox.instancedregions.InstancedProtectedCuboidRegion;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.CuboidClipboard;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.data.DataException;
import com.sk89q.worldedit.schematic.MCEditSchematicFormat;

public class Section {
	
	private Schematic schematic;
	private String schematicFileName;
	private Map<BlockVector,Exit> exitMap;
	private ArrayList<Player> players;
	private BlockVector[] exits;
	private BlockVector size, nearestPositiveX, nearestNegativeX, nearestPositiveY, nearestNegativeY, nearestPositiveZ, nearestNegativeZ;
	private int index, depth;
	private InstancedProtectedCuboidRegion region;
	private Listener trigger;
	private volatile boolean justEntered, portal = false;
	private JSONObject descriptor;
	
	public Section(JSONObject descriptor, int index, int depth) {
		this.index = index;
		this.depth = depth;
		players = new ArrayList<Player>();
		exitMap = new HashMap<BlockVector,Exit>();
		schematicFileName = descriptor.getString("file");
		JSONArray size = descriptor.getJSONArray("size");
		this.size = new BlockVector(size.getInt(0), size.getInt(1), size.getInt(2));
		JSONArray exits = descriptor.getJSONArray("exits");
		this.exits = new BlockVector[exits.length()];
		for(int i = 0; i<this.exits.length; i++) {
			JSONArray current = exits.getJSONArray(i);
			this.exits[i] = new BlockVector(current.getInt(0), current.getInt(1), current.getInt(2));
		}
		this.descriptor = descriptor;
	}
	
	public void load() throws Exception {
		this.schematic = new Schematic(new File(InstancedDungeons.schematicsDirectory, schematicFileName));
		if(index <= depth) {
			Section next;
			for(int i = 0; i<exits.length; i++) {
				ArrayList<Exit> usable = new ArrayList<Exit>();
				if(exits[i].getBlockX() == 0.0 || exits[i].getBlockX() == size.getBlockX()-1 || exits[i].getBlockZ() == 0.0 || exits[i].getBlockZ() == size.getBlockZ()-1) {
					if(index == depth) {
						for(int c = 0, length = InstancedDungeons.endDescriptors.length(); c<length; c++) {
							if((next = new Section(InstancedDungeons.endDescriptors.getJSONObject(c), index+1, depth)).hasSideExit()) {
								usable.addAll(next.getSideExits());
							}
						}
					} else {
						for(int c = 0, length = InstancedDungeons.normalDescriptors.length(); c<length; c++) {
							if((next = new Section(InstancedDungeons.normalDescriptors.getJSONObject(c), index+1, depth)).hasSideExit()) {
								usable.addAll(next.getSideExits());
							}
						}
					}
				} else if(exits[i].getBlockY() == 0.0) {
					if(index == depth) {
						for(int c = 0, length = InstancedDungeons.endDescriptors.length(); c<length; c++) {
							if((next = new Section(InstancedDungeons.endDescriptors.getJSONObject(c), index+1, depth)).hasTopExit()) {
								usable.addAll(next.getTopExits());
							}
						}
					} else {
						for(int c = 0, length = InstancedDungeons.normalDescriptors.length(); c<length; c++) {
							if((next = new Section(InstancedDungeons.normalDescriptors.getJSONObject(c), index+1, depth)).hasTopExit()) {
								usable.addAll(next.getTopExits());
							}
						}
					}
				} else if(exits[i].getBlockY() == size.getBlockY()-1) {
					if(index == depth) {
						for(int c = 0, length = InstancedDungeons.endDescriptors.length(); c<length; c++) {
							if((next = new Section(InstancedDungeons.endDescriptors.getJSONObject(c), index+1, depth)).hasBottomExit()) {
								usable.addAll(next.getBottomExits());
							}
						}
					} else {
						for(int c = 0, length = InstancedDungeons.normalDescriptors.length(); c<length; c++) {
							if((next = new Section(InstancedDungeons.normalDescriptors.getJSONObject(c), index+1, depth)).hasBottomExit()) {
								usable.addAll(next.getBottomExits());
							}
						}
					}
				} else {
					throw new Exception("No schematic with a valid exit found.");
				}
				exitMap.put(exits[i], usable.get((int)Math.round(Math.random()*(usable.size()-1))));
			}
		}
	}
	
	public BlockVector[] getExits() {
		return exits;
	}
	
	public BlockVector[] getAbsoluteExits() {
		BlockVector[] out = new BlockVector[exits.length];
		BlockVector min = region.getMinimumPoint();
		for(int i = 0; i<exits.length; i++) {
			out[i] = new BlockVector(min.getBlockX()+exits[i].getBlockX(), 128+exits[i].getBlockY(), min.getBlockZ()+exits[i].getBlockZ());
		}
		return out;
	}
	
	public BlockVector absoluteToRelativeExit(BlockVector bv) {
		BlockVector min = region.getMinimumPoint();
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockX() == bv.getBlockX()-min.getBlockX()
					&& exits[i].getBlockY() == bv.getBlockY()-128
					&& exits[i].getBlockZ() == bv.getBlockZ()-min.getBlockZ())
				return exits[i];
		}
		return null;
	}
	
	public BlockVector relativeToAbsoluteExit(BlockVector bv) {
		BlockVector min = region.getMinimumPoint();
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockX() == bv.getBlockX()
					&& exits[i].getBlockY() == bv.getBlockY()
					&& exits[i].getBlockZ() == bv.getBlockZ())
				return new BlockVector(min.getBlockX()+exits[i].getBlockX(), 128+exits[i].getBlockY(), min.getBlockZ()+exits[i].getBlockZ());
		}
		return null;
	}
	
	public ArrayList<Exit> getSideExits() {
		ArrayList<Exit> out = new ArrayList<Exit>();
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockX() == 0.0 || exits[i].getBlockX() == size.getBlockX()-1.0
					|| exits[i].getBlockZ() == .0 || exits[i].getBlockZ() == size.getBlockZ()-1.0) {
				out.add(new Exit(this, exits[i]));
			}
		}
		return out;
	}
	
	public ArrayList<Exit> getTopExits() {
		ArrayList<Exit> out = new ArrayList<Exit>();
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockY() == size.getBlockY()-1.0) {
				out.add(new Exit(this, exits[i]));
			}
		}
		return out;
	}
	
	public ArrayList<Exit> getBottomExits() {
		ArrayList<Exit> out = new ArrayList<Exit>();
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockY() == 0.0) {
				out.add(new Exit(this, exits[i]));;
			}
		}
		return out;
	}
	
	public BlockVector getNearestExitPositiveX(org.bukkit.util.Vector relative) {
		BlockVector nearest = null;
		double dist = 0.0, current;
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockX() == size.getBlockX()-1) {
				if((current = exits[i].distance(new Vector(relative.getX(), relative.getY(), relative.getZ()))) > dist) {
					dist = current;
					nearest = exits[i];
				}
			}
		}
		return nearest;
	}
	
	public BlockVector getNearestExitNegativeX(org.bukkit.util.Vector relative) {
		BlockVector nearest = null;
		double dist = 0.0, current;
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockX() == 0.0) {
				if((current = exits[i].distance(new Vector(relative.getX(), relative.getY(), relative.getZ()))) > dist) {
					dist = current;
					nearest = exits[i];
				}
			}
		}
		return nearest;
	}
	
	public BlockVector getNearestExitPositiveY(org.bukkit.util.Vector relative) {
		BlockVector nearest = null;
		double dist = 0.0, current;
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockY() == size.getBlockY()-1) {
				if((current = exits[i].distance(new Vector(relative.getX(), relative.getY(), relative.getZ()))) > dist) {
					dist = current;
					nearest = exits[i];
				}
			}
		}
		return nearest;
	}
	
	public BlockVector getNearestExitNegativeY(org.bukkit.util.Vector relative) {
		BlockVector nearest = null;
		double dist = 0.0, current;
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockY() == 0.0) {
				if((current = exits[i].distance(new Vector(relative.getX(), relative.getY(), relative.getZ()))) > dist) {
					dist = current;
					nearest = exits[i];
				}
			}
		}
		return nearest;
	}
	
	public BlockVector getNearestExitPositiveZ(org.bukkit.util.Vector relative) {
		BlockVector nearest = null;
		double dist = 0.0, current;
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockZ() == size.getBlockZ()-1) {
				if((current = exits[i].distance(new Vector(relative.getX(), relative.getY(), relative.getZ()))) > dist) {
					dist = current;
					nearest = exits[i];
				}
			}
		}
		return nearest;
	}
	
	public BlockVector getNearestExitNegativeZ(org.bukkit.util.Vector relative) {
		BlockVector nearest = null;
		double dist = 0.0, current;
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockZ() == 0.0) {
				if((current = exits[i].distance(new Vector(relative.getX(), relative.getY(), relative.getZ()))) > dist) {
					dist = current;
					nearest = exits[i];
				}
			}
		}
		return nearest;
	}
	
	public void instantiate() {
		InstancedDungeons.logger.info("Instantiating section (file: " + descriptor.getString("file") + ", index: " + index + ", depth: " + depth + ")");
		justEntered = true;
		portal = false;
		region = InstancedDungeons.dungeonWorld.allocate(size.getBlockX(), size.getBlockZ());
		Iterator<Player> iter = players.iterator();
		while(iter.hasNext())
			region.addPlayer(iter.next());
		//write schematic to map
		final BlockVector min = region.getMinimumPoint();
		
		for(int i = 0; i<exits.length; i++) {
			try {
				exitMap.get(exits[i]).getSection().load();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//DEBUG
		for(int i = 0; i<exits.length; i++) {
			InstancedDungeons.logger.info("Exit at: (" + (min.getBlockX()+exits[i].getBlockX()) + ", " + (128+exits[i].getBlockY()) + ", " + (min.getBlockZ()+exits[i].getBlockZ()));
		}
		
		for(int y = 0; y<schematic.height; y++) {
			for(int z = 0; z<schematic.length; z++) {
				for(int x = 0; x<schematic.width; x++) {
					region.addToChangeWhitelist(new BlockVector(min.getBlockX()+x, 128+y, min.getBlockZ()+z));
				}
			}
		}
		
		EditSession es = InstancedDungeons.we.getWorldEdit().getEditSessionFactory().getEditSession(new BukkitWorld(InstancedDungeons.dungeonWorld.getWorld()), Integer.MAX_VALUE);
		try {
			MCEditSchematicFormat.getFormat(schematic.file).load(schematic.file).paste(es, new Vector(min.getBlockX()-1, 128, min.getBlockZ()), false);
		} catch (MaxChangedBlocksException | DataException | IOException e1) {
			e1.printStackTrace();
		}
		
		/*
		byte next;
		for(int y = 0; y<schematic.height; y++) {
			for(int z = 0; z<schematic.length; z++) {
				for(int x = 0; x<schematic.width; x++) {
					next = schematic.getNextBlock();
					region.addToChangeWhitelist(new BlockVector(min.getBlockX()+x, 128+y, min.getBlockZ()+z));
					InstancedDungeons.dungeonWorld.getWorld().getBlockAt(min.getBlockX()+x, 128+y, min.getBlockZ()+z).setType(Material.getMaterial(next));
					
				}
			}
		}
		*/
		Bukkit.getScheduler().scheduleSyncDelayedTask(InstancedDungeons.plugin, new Runnable() {
			@Override
			public void run() {
				InstancedDungeons.logger.info("portal activated");
				justEntered = false;
			}
		}, 120L);
		trigger = new Listener() {
			@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
			public void onMove(PlayerMoveEvent event) {
				Location to = event.getTo();
				Player player = event.getPlayer();
				org.bukkit.util.Vector tov = to.toVector();
				BlockVector nearest;
				//draw x+ if changed
				if((nearest = getNearestExitPositiveX(tov)) != null) {
					if(nearest != nearestPositiveX) {
						nearestPositiveX = nearest;
						BlockVector exit = exitMap.get(nearest).getRelativeBlockVector(), absolute = relativeToAbsoluteExit(nearest);
						Section section = exitMap.get(nearest).getSection();
						Schematic schematic = section.getSchematic();
						if(exit.getBlockX() == 0.0) {
							//rotate along y axis by 0 degree
							InstancedDungeons.logger.info("negative x");
							for(int y = 0; y<schematic.height; y++) {
								for(int z = 0; z<schematic.length; z++) {
									for(int x = 0; x<schematic.width; x++) {
										region.addToChangeWhitelist(new BlockVector(absolute.getBlockX()+1+x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()-exit.getBlockZ()+z));
										player.sendBlockChange(new Location(InstancedDungeons.dungeonWorld.getWorld(), absolute.getBlockX()+1+x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()-exit.getBlockZ()+z), Material.getMaterial(schematic.getNextBlock()), (byte) 0);
									}
								}
							}
						} else if(exit.getBlockX() == section.size.getBlockX()-1) { //works
							InstancedDungeons.logger.info("positive x");
							for(int y = 0; y<schematic.height; y++) {
								for(int z = 0; z<schematic.length; z++) {
									for(int x = 0; x<schematic.width; x++) {
										region.addToChangeWhitelist(new BlockVector(absolute.getBlockX()+schematic.width-x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()-exit.getBlockZ()+z));
										player.sendBlockChange(new Location(InstancedDungeons.dungeonWorld.getWorld(), absolute.getBlockX()+schematic.width-x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()-exit.getBlockZ()+z), Material.getMaterial(schematic.getNextBlock()), (byte) 0);
									}
								}
							}
						} else if(exit.getBlockZ() == 0.0) {
							InstancedDungeons.logger.info("negative z");
							for(int y = 0; y<schematic.height; y++) {
								for(int z = 0; z<schematic.length; z++) {
									for(int x = 0; x<schematic.width; x++) {
										region.addToChangeWhitelist(new BlockVector(absolute.getBlockX()+1+x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()+1+z));
										player.sendBlockChange(new Location(InstancedDungeons.dungeonWorld.getWorld(), absolute.getBlockX()+1+x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()+1+z), Material.getMaterial(schematic.getNextBlock()), (byte) 0);
									}
								}
							}
						} else if(exit.getBlockZ() == section.size.getBlockZ()-1) {
							InstancedDungeons.logger.info("positive z");
							for(int y = 0; y<schematic.height; y++) {
								for(int z = 0; z<schematic.length; z++) {
									for(int x = 0; x<schematic.width; x++) {
										region.addToChangeWhitelist(new BlockVector(absolute.getBlockX()+schematic.length-x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()-exit.getBlockX()+z));
										player.sendBlockChange(new Location(InstancedDungeons.dungeonWorld.getWorld(), absolute.getBlockX()+schematic.length-x, absolute.getBlockY()-exit.getBlockY()+y, absolute.getBlockZ()-exit.getBlockX()+z), Material.getMaterial(schematic.getNextBlock()), (byte) 0);
									}
								}
							}
						}
					}
				}
				if(!justEntered) {
					if(portal) {
						for(int i = 0; i<exits.length; i++) {
							if(to.getWorld().equals(InstancedDungeons.dungeonWorld.getWorld())
									&& to.getBlockX() == min.getBlockX() + exits[i].getBlockX()
									&& to.getBlockY() == 128 + exits[i].getBlockY()
									&& to.getBlockZ() == min.getBlockZ() + exits[i].getBlockZ()) {
								InstancedDungeons.logger.info("Player in loading zone");
								portal = false;
								Section next = exitMap.get(exits[i]).getSection();
								try {
									region.dispose();
									for(int c = 0, size = players.size(); c<size; c++) {
										next.addPlayer(players.get(c));
									}
									next.instantiate();
									BlockVector destination = next.relativeToAbsoluteExit(exitMap.get(exits[i]).getRelativeBlockVector());
									//next.assignExit(destination, new Section(descriptor, index, depth));
									event.getPlayer().teleport(new Location(Bukkit.getWorld("world_nether"), -100000, 200, -100000));
									event.getPlayer().teleport(new Location(InstancedDungeons.dungeonWorld.getWorld(), destination.getBlockX(), destination.getBlockY(), destination.getBlockZ()).add(.5, 0, .5));
									InstancedDungeons.dungeonWorld.deallocate(region);
									HandlerList.unregisterAll(this);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						}
					} else {
						for(int i = 0; i<exits.length; i++) {
							if(to.getWorld().equals(InstancedDungeons.dungeonWorld.getWorld())
									&& to.getBlockX() == min.getBlockX() + exits[i].getBlockX()
									&& to.getBlockY() == 128 + exits[i].getBlockY()
									&& to.getBlockZ() == min.getBlockZ() + exits[i].getBlockZ()) {
								return;
							}
						}
						portal = true;
					}
				}
			}
		};
		Bukkit.getPluginManager().registerEvents(trigger, InstancedDungeons.plugin);
		region.apply();
	}
	
	public Schematic getSchematic() {
		return schematic;
	}
	
	public void activateExits() {
		
	}
	
	public void assignExit(BlockVector v, Exit exit) {
		exitMap.put(v, exit);
	}
	
	public boolean hasSideExit() {
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockX() == 0
					|| exits[i].getBlockX() == size.getBlockX()-1
					|| exits[i].getBlockZ() == 0
					|| exits[i].getBlockZ() == size.getBlockZ()-1) {
				return true;
			}
		}
		return false;
	}
	
	public boolean hasTopExit() {
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockY() == size.getBlockY()-1) {
				return true;
			}
		}
		return false;
	}
	
	public boolean hasBottomExit() {
		for(int i = 0; i<exits.length; i++) {
			if(exits[i].getBlockY() == 0) {
				return true;
			}
		}
		return false;
	}
	
	public void addPlayer(Player player) {
		players.add(player);
		if(region != null)
			region.addPlayer(player);
		if(exitMap != null) {
			Iterator<Exit> exits = exitMap.values().iterator();
			while(exits.hasNext())
				exits.next().getSection().addPlayer(player);
		}
	}
	
	public void removePlayer(Player player) {
		players.add(player);
		if(region != null)
			region.removePlayer(player);
		if(exitMap != null) {
			Iterator<Exit> exits = exitMap.values().iterator();
			while(exits.hasNext())
				exits.next().getSection().removePlayer(player);
		}
	}

}