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
|
package com.encrox.instanceddungeons;
import java.io.File;
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;
public class Section {
private Schematic schematic;
private String schematicFileName;
private Map<BlockVector,Section> exitMap;
private ArrayList<Player> players;
private BlockVector[] exits;
private BlockVector size;
private int index, depth;
private InstancedProtectedCuboidRegion region;
private Listener moveOutOfLoadingZoneListener, moveIntoLoadingZoneListener;
private volatile boolean justEntered, portal;
public Section(JSONObject descriptor, int index, int depth) {
this.index = index;
this.depth = depth;
players = new ArrayList<Player>();
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));
}
}
public void load() throws Exception {
exitMap = new HashMap<BlockVector,Section>();
this.schematic = new Schematic(new File(InstancedDungeons.schematicsDirectory, schematicFileName));
if(index <= depth) {
Section next;
ArrayList<Section> usable = new ArrayList<Section>();
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) {
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.add(next);
}
}
} 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.add(next);
}
}
}
} else if(exits[i].getBlockY() == 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.add(next);
}
}
} 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.add(next);
}
}
}
} 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.add(next);
}
}
} 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.add(next);
}
}
}
} 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[] 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 void instantiate() {
justEntered = true;
portal = false;
region = InstancedDungeons.dungeonWorld.allocate(size.getBlockX(), size.getBlockZ());
//write schematic to map
final BlockVector min = region.getMinimumPoint();
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));
region.addToChangeWhitelist(new BlockVector(min.getBlockX()+x, 128+y, min.getBlockZ()+z));
for(int i = 0, size = players.size(); i<size; i++) {
players.get(i).sendBlockChange(new Location(InstancedDungeons.dungeonWorld.getWorld(), min.getBlockX()+x, 128+y, min.getBlockZ()+z), Material.getMaterial(next), (byte) 0);
}
}
}
}
Bukkit.getScheduler().scheduleSyncDelayedTask(InstancedDungeons.plugin, new Runnable() {
@Override
public void run() {
InstancedDungeons.logger.info("portal activated");
justEntered = false;
portal = true;
}
}, 40L);
moveOutOfLoadingZoneListener = new Listener() {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
public void onMove(PlayerMoveEvent event) {
InstancedDungeons.logger.info("Player moved");
Location to = event.getTo();
for(int i = 0; i<exits.length; i++) {
if((!justEntered) && (!(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 exited loading zone");
moveIntoLoadingZoneListener = new Listener() {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
public void onMove(PlayerMoveEvent event) {
Location to = event.getTo();
for(int i = 0; i<exits.length; i++) {
if(portal && (!justEntered) && (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]);
try {
HandlerList.unregisterAll(moveIntoLoadingZoneListener);
next.load();
next.instantiate();
for(int c = 0, size = players.size(); c<size; c++) {
next.addPlayer(players.get(c));
}
BlockVector[] destinations = next.getAbsoluteExits();
BlockVector destination = destinations[(int)Math.round(Math.random()*destinations.length)];
event.getPlayer().teleport(new Location(InstancedDungeons.dungeonWorld.getWorld(), -10000, 200, -10000));
event.getPlayer().teleport(new Location(InstancedDungeons.dungeonWorld.getWorld(), destination.getBlockX(), destination.getBlockY(), destination.getBlockZ()));
region.dispose();
InstancedDungeons.dungeonWorld.deallocate(region);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
};
Bukkit.getPluginManager().registerEvents(moveIntoLoadingZoneListener, InstancedDungeons.plugin);
HandlerList.unregisterAll(moveOutOfLoadingZoneListener);
}
}
}
};
Bukkit.getPluginManager().registerEvents(moveOutOfLoadingZoneListener, InstancedDungeons.plugin);
}
public Schematic getSchematic() {
return schematic;
}
public void activateExits() {
}
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<Section> sections = exitMap.values().iterator();
while(sections.hasNext())
sections.next().addPlayer(player);
}
}
public void removePlayer(Player player) {
players.add(player);
if(region != null)
region.removePlayer(player);
if(exitMap != null) {
Iterator<Section> sections = exitMap.values().iterator();
while(sections.hasNext())
sections.next().removePlayer(player);
}
}
}
|