/
TARDISBuildSilurianStructure.java
195 lines (185 loc) · 8.24 KB
/
TARDISBuildSilurianStructure.java
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
/*
* Copyright (C) 2020 eccentric_nz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package me.eccentric_nz.TARDIS.planets;
import me.eccentric_nz.TARDIS.JSON.JSONArray;
import me.eccentric_nz.TARDIS.JSON.JSONObject;
import me.eccentric_nz.TARDIS.TARDIS;
import me.eccentric_nz.TARDIS.TARDISConstants;
import me.eccentric_nz.TARDIS.enumeration.COMPASS;
import me.eccentric_nz.TARDIS.schematic.TARDISSchematicGZip;
import me.eccentric_nz.TARDIS.utility.TARDISBlockSetters;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.util.Vector;
import java.io.File;
/**
* The TARDIS was prone to a number of technical faults, ranging from depleted resources to malfunctioning controls to a
* simple inability to arrive at the proper time or location. While the Doctor did not build the TARDIS from scratch, he
* has substantially modified/rebuilt it.
*
* @author eccentric_nz
*/
class TARDISBuildSilurianStructure {
private final TARDIS plugin;
TARDISBuildSilurianStructure(TARDIS plugin) {
this.plugin = plugin;
}
/**
* Builds a Siluria structure.
*
* @param startx the start coordinate on the x-axis
* @param starty the start coordinate on the y-axis
* @param startz the start coordinate on the z-axis
* @return false when the build task has finished
*/
boolean buildCity(int startx, int starty, int startz) {
String[] paths = {plugin.getDataFolder() + File.separator + "schematics" + File.separator + "siluria_large.tschm", plugin.getDataFolder() + File.separator + "schematics" + File.separator + "siluria_cross.tschm", plugin.getDataFolder() + File.separator + "schematics" + File.separator + "siluria_north_south.tschm", plugin.getDataFolder() + File.separator + "schematics" + File.separator + "siluria_east_west.tschm"};
File file = new File(paths[0]);
if (!file.exists()) {
plugin.debug("Could not find the Silurian schematics!");
return false;
}
plugin.debug("Building Silurian structure @ " + startx + ", " + starty + ", " + startz);
World world = plugin.getServer().getWorld("Siluria");
structure(paths[0], world, startx, starty, startz);
// choose a random direction
COMPASS compass = COMPASS.values()[TARDISConstants.RANDOM.nextInt(4)];
// see if the chunk is loaded
Vector v1 = isChunkLoaded(compass, world.getBlockAt(startx, starty, startz));
if (v1 != null) {
startx += v1.getBlockX();
starty += v1.getBlockY();
startz += v1.getBlockZ();
if (TARDISConstants.RANDOM.nextBoolean()) {
// cross - paths[1]
structure(paths[1], world, startx, starty, startz);
} else {
// straight
if (compass.equals(COMPASS.NORTH) || compass.equals(COMPASS.SOUTH)) {
// east west - paths[2]
structure(paths[2], world, startx, starty, startz);
} else {
// north south - paths[3]
structure(paths[3], world, startx, starty, startz);
}
}
}
return false;
}
private void structure(String path, World world, int startx, int starty, int startz) {
// get JSON
JSONObject obj = TARDISSchematicGZip.unzip(path);
// get dimensions
JSONObject dimensions = (JSONObject) obj.get("dimensions");
int h = dimensions.getInt("height");
int w = dimensions.getInt("width");
int l = dimensions.getInt("length");
Block chest;
Material type;
BlockData data;
// get input array
JSONArray arr = (JSONArray) obj.get("input");
// loop like crazy
for (int level = 0; level < h; level++) {
JSONArray floor = (JSONArray) arr.get(level);
for (int row = 0; row < w; row++) {
JSONArray r = (JSONArray) floor.get(row);
for (int col = 0; col < l; col++) {
JSONObject c = (JSONObject) r.get(col);
int x = startx + row;
int y = starty + level;
int z = startz + col;
data = plugin.getServer().createBlockData(c.getString("data"));
type = data.getMaterial();
switch (type) {
case AIR:
// only set air blocks if the structure part is above the 'stilts'
if (level > h - 6) {
TARDISBlockSetters.setBlock(world, x, y, z, data);
}
break;
case CHEST:
TARDISBlockSetters.setBlock(world, x, y, z, data);
chest = world.getBlockAt(x, y, z);
if (chest != null && chest.getType().equals(Material.CHEST)) {
try {
// set chest contents
Chest container = (Chest) chest.getState();
container.setLootTable(TARDISConstants.LOOT.get(TARDISConstants.RANDOM.nextInt(11)));
container.update();
} catch (ClassCastException e) {
plugin.debug("Could not cast " + chest.getType() + "to Silurian Chest." + e.getMessage());
}
}
break;
case SPONGE:
Block swap_block = world.getBlockAt(x, y, z);
if (!swap_block.getType().isOccluding()) {
TARDISBlockSetters.setBlock(world, x, y, z, Material.AIR);
}
break;
case SPAWNER:
Block spawner = world.getBlockAt(x, y, z);
spawner.setBlockData(data);
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
CreatureSpawner cs = (CreatureSpawner) spawner.getState();
cs.setSpawnedType(EntityType.SKELETON);
cs.update();
}, 2L);
break;
default:
TARDISBlockSetters.setBlock(world, x, y, z, data);
break;
}
}
}
}
}
private Vector isChunkLoaded(COMPASS compass, Block block) {
Chunk chunk = block.getChunk();
int x = chunk.getX();
int z = chunk.getZ();
Vector vector;
switch (compass) {
case WEST:
vector = new Vector(-16, 17, 0);
x -= 1;
break;
case NORTH:
vector = new Vector(0, 17, -16);
z -= 1;
break;
case EAST:
vector = new Vector(16, 17, 0);
x += 1;
break;
default: //SOUTH
vector = new Vector(0, 17, 16);
z += 1;
break;
}
// see if the chunk is loaded
Chunk newChunk = chunk.getWorld().getChunkAt(x, z);
return newChunk.isLoaded() ? vector : null;
}
}