-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArenaManager.java
271 lines (211 loc) · 9.71 KB
/
ArenaManager.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
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
package io.tofpu.speedbridge2.model.island.arena;
import com.sk89q.worldedit.WorldEditException;
import io.tofpu.speedbridge2.model.common.config.manager.ConfigurationManager;
import io.tofpu.speedbridge2.model.common.util.BridgeUtil;
import io.tofpu.speedbridge2.model.island.object.Island;
import io.tofpu.speedbridge2.model.island.object.GameIsland;
import io.tofpu.speedbridge2.model.island.object.land.IslandLand;
import io.tofpu.speedbridge2.model.support.worldedit.Vector;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public final class ArenaManager {
private final @NotNull Map<Integer, Collection<IslandLand>> ISLAND_PLOTS =
new HashMap<>();
private final AtomicInteger COUNTER = new AtomicInteger(100);
private File worldDirectory;
private World world;
public void load() {
this.world = Bukkit.createWorld(WorldCreator.name("speedbridge2")
.generator(new EmptyChunkGenerator()));
this.worldDirectory = new File( "speedbridge2");
protectWorld(world);
}
private void protectWorld(final @NotNull World world) {
world.setFullTime(1000);
world.setWeatherDuration(0);
world.setStorm(false);
world.setThundering(false);
world.setPVP(false);
world.setAutoSave(false);
world.setMonsterSpawnLimit(0);
world.setGameRuleValue("doDaylightCycle", "false");
}
public @NotNull IslandLand justReservePlot(final GameIsland gameIsland) {
return justGetPlot(gameIsland.getIsland(), gameIsland);
}
public @Nullable IslandLand reservePlot(final GameIsland gameIsland) {
if (world == null) {
Bukkit.getLogger()
.severe("The SpeedBridge2 world cannot be found! cancelled player's request to reserve a plot.");
return null;
}
final Island island = gameIsland.getIsland();
// return the available plot
return getPlot(island, gameIsland);
}
private IslandLand justGetPlot(final Island island, final GameIsland gameIsland) {
final int islandSlot = island.getSlot();
// retrieving a collection of plots that is associated with the given island slot
final Collection<IslandLand> islandLands = retrieve(islandSlot);
// attempt to get an available plot with the given island slot
IslandLand islandLand = getAvailablePlot(islandLands, islandSlot);
// if we found an available plot, start reserving the plot
if (islandLand != null) {
islandLand.reserveWith(gameIsland);
} else {
// otherwise, create our own island plot
islandLand = createIslandPlotWithNoGeneration(islandLands, island, gameIsland);
}
return islandLand;
}
private IslandLand getPlot(final Island island, final GameIsland gameIsland) {
final int islandSlot = island.getSlot();
// retrieving a collection of plots that is associated with the given island slot
final Collection<IslandLand> islandLands = retrieve(islandSlot);
// attempt to get an available plot with the given island slot
IslandLand islandLand = getAvailablePlot(islandLands, islandSlot);
// if we found an available plot, start reserving the plot
if (islandLand != null) {
islandLand.reserveWith(gameIsland);
} else {
// otherwise, create our own island plot
islandLand = createIslandPlot(islandLands, island, gameIsland);
}
return islandLand;
}
private IslandLand getAvailablePlot(final Collection<IslandLand> islandLands,
final int slot) {
for (final IslandLand islandLand : islandLands) {
// if it's not the same island plot, or the plot is not free; continue
if (islandLand.getIsland().getSlot() != slot || !islandLand.isFree()) {
continue;
}
BridgeUtil.debug("SchematicManager#getAvailablePlot: Found a free plot for " + slot + " slot!");
return islandLand;
}
return null;
}
// private IslandLand getNewPlot(final Island target) {
// return getNewPlot(target, getPositions());
// }
private IslandLand getNewPlot(final Island target, double[] positions) {
return new IslandLand(target, world, positions);
}
private IslandLand createIslandPlotWithNoGeneration(final Collection<IslandLand> islandLandList
, final Island target, final GameIsland gameIsland) {
BridgeUtil.debug("SchematicManager#createIslandPlot: Creating a new island plot for " + target.getSlot() + " slot!");
final double[] positions = getPositions();
positions[0] += Math.abs(positions[0] - new IslandLand(target, world, positions).region().getMinimumPoint().getX());
BridgeUtil.debug("=== island " + target.getSlot() + " ===");
BridgeUtil.debug("Placing schematic at: " + Arrays.toString(positions));
final IslandLand islandLand = getNewPlot(target, positions);
BridgeUtil.debug("Island width is: " + islandLand.getWidth());
COUNTER.getAndAdd(islandLand.getWidth() + ConfigurationManager.INSTANCE.getGeneralCategory().getIslandSpaceGap());
BridgeUtil.debug("minimumPoint=" + serializeVector(islandLand.region().getMinimumPoint()));
BridgeUtil.debug("maximumPoint=" + serializeVector(islandLand.region().getMaximumPoint()));
BridgeUtil.debug("==========");
// reserving the plot to player
islandLand.reserveWith(gameIsland);
// adding the plot for usability
islandLandList.add(islandLand);
// adding the new island plot to the schematic plot map
ISLAND_PLOTS.put(target.getSlot(), islandLandList);
return islandLand;
}
private IslandLand createIslandPlot(final Collection<IslandLand> islandLandList
, final Island target, final GameIsland gameIsland) {
BridgeUtil.debug("SchematicManager#createIslandPlot: Creating a new island plot for " + target.getSlot() + " slot!");
final double[] positions = getPositions();
positions[0] += Math.abs(positions[0] - new IslandLand(target, world, positions).region().getMinimumPoint().getX());
BridgeUtil.debug("=== island " + target.getSlot() + " ===");
BridgeUtil.debug("Placing schematic at: " + Arrays.toString(positions));
final IslandLand islandLand = getNewPlot(target, positions);
BridgeUtil.debug("Island width is: " + islandLand.getWidth());
COUNTER.getAndAdd(islandLand.getWidth() + ConfigurationManager.INSTANCE.getGeneralCategory().getIslandSpaceGap());
BridgeUtil.debug("minimumPoint=" + serializeVector(islandLand.region().getMinimumPoint()));
BridgeUtil.debug("maximumPoint=" + serializeVector(islandLand.region().getMaximumPoint()));
BridgeUtil.debug("==========");
// reserving the plot to player
islandLand.reserveWith(gameIsland);
try {
// attempt to generate the plot
islandLand.generatePlot();
} catch (WorldEditException e) {
throw new IllegalStateException(e);
}
// adding the plot for usability
islandLandList.add(islandLand);
// adding the new island plot to the schematic plot map
ISLAND_PLOTS.put(target.getSlot(), islandLandList);
return islandLand;
}
public double[] getPositions() {
return new double[]{COUNTER.get(), 100, 100};
}
@NotNull
private static String serializeVector(Vector vector) {
return String.format("%s, %s, %s", vector.getX(), vector.getY(), vector.getZ());
}
public void resetWorld() {
final File worldFile = getWorldDirectory();
if (worldFile != null && worldFile.exists()) {
try {
// delete the world outright
FileUtils.forceDelete(worldFile);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
public Collection<IslandLand> retrieve(final int slot) {
return ISLAND_PLOTS.getOrDefault(slot, new ArrayList<>());
}
public void clearPlot(final int slot) {
ISLAND_PLOTS.remove(slot);
}
public File getWorldDirectory() {
return worldDirectory;
}
public void unloadWorld() {
if (world == null) {
return;
}
movePlayersFromWorld();
Bukkit.unloadWorld(world, false);
}
private void movePlayersFromWorld() {
for (final Player player : Bukkit.getOnlinePlayers()) {
if (!player.getWorld()
.getName()
.equals(world.getName())) {
continue;
}
Location lobbyLocation = ConfigurationManager.INSTANCE.getLobbyCategory()
.getLobbyLocation();
if (lobbyLocation == null) {
lobbyLocation = Bukkit.getServer().getWorlds().get(0).getSpawnLocation();
}
player.teleport(lobbyLocation);
}
}
public World getWorld() {
return this.world;
}
private static final class EmptyChunkGenerator extends ChunkGenerator {
@Override
public @NotNull ChunkData generateChunkData(final World world, final Random random, final int x, final int z, final BiomeGrid biome) {
return createChunkData(world);
}
}
}