-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathAOneBlock.java
More file actions
425 lines (386 loc) · 14.4 KB
/
AOneBlock.java
File metadata and controls
425 lines (386 loc) · 14.4 KB
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
package world.bentobox.aoneblock;
import java.io.IOException;
import java.util.Objects;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.entity.SpawnCategory;
import org.bukkit.generator.ChunkGenerator;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.aoneblock.commands.admin.AdminCommand;
import world.bentobox.aoneblock.commands.island.PlayerCommand;
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
import world.bentobox.aoneblock.generators.ChunkGeneratorWorld;
import world.bentobox.aoneblock.listeners.BlockListener;
import world.bentobox.aoneblock.listeners.BlockProtect;
import world.bentobox.aoneblock.listeners.BossBarListener;
import world.bentobox.aoneblock.listeners.HoloListener;
import world.bentobox.aoneblock.listeners.InfoListener;
import world.bentobox.aoneblock.listeners.ItemsAdderListener;
import world.bentobox.aoneblock.listeners.JoinLeaveListener;
import world.bentobox.aoneblock.listeners.NoBlockHandler;
import world.bentobox.aoneblock.listeners.StartSafetyListener;
import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlockCreator;
import world.bentobox.aoneblock.oneblocks.OneBlocksManager;
import world.bentobox.aoneblock.oneblocks.customblock.ItemsAdderCustomBlock;
import world.bentobox.aoneblock.requests.IslandStatsHandler;
import world.bentobox.aoneblock.requests.LocationStatsHandler;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.Flag.Mode;
import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
/**
* Main OneBlock class - provides an island minigame in the sky
*
* @author tastybento
*/
public class AOneBlock extends GameModeAddon {
/** Suffix for the nether world */
private static final String NETHER = "_nether";
/** Suffix for the end world */
private static final String THE_END = "_the_end";
/** Whether ItemsAdder is present on the server */
private boolean hasItemsAdder = false;
/** The addon settings */
private Settings settings;
/** The custom chunk generator for OneBlock worlds */
private ChunkGeneratorWorld chunkGenerator;
/** The configuration object for settings */
private final Config<Settings> configObject = new Config<>(this, Settings.class);
/** The listener for block-related events */
private BlockListener blockListener;
/** The manager for OneBlock phases and blocks */
private OneBlocksManager oneBlockManager;
/** The placeholder manager for AOneBlock */
private AOneBlockPlaceholders phManager;
/** The listener for hologram-related events */
private HoloListener holoListener;
/**
* Flag to enable or disable start safety for players.
*/
public final Flag START_SAFETY = new Flag.Builder("START_SAFETY", Material.BAMBOO_BLOCK)
.mode(Mode.BASIC)
.type(Type.WORLD_SETTING)
.listener(new StartSafetyListener(this))
.defaultSetting(false)
.build();
/** The listener for the boss bar */
private BossBarListener bossBar = new BossBarListener(this);
/**
* Flag to enable or disable the OneBlock boss bar.
*/
public final Flag ONEBLOCK_BOSSBAR = new Flag.Builder("ONEBLOCK_BOSSBAR", Material.DRAGON_HEAD)
.mode(Mode.BASIC)
.type(Type.SETTING)
.listener(bossBar)
.defaultSetting(true)
.build();
/**
* Flag to enable or disable the OneBlock action bar.
*/
public final Flag ONEBLOCK_ACTIONBAR = new Flag.Builder("ONEBLOCK_ACTIONBAR", Material.IRON_BARS)
.mode(Mode.BASIC)
.type(Type.SETTING)
.listener(bossBar)
.defaultSetting(true)
.build();
/**
* Flag to set who can break the magic block.
*/
public final Flag MAGIC_BLOCK = new Flag.Builder("MAGIC_BLOCK", Material.GRASS_BLOCK)
.mode(Mode.BASIC)
.type(Type.PROTECTION)
.defaultRank(RanksManager.COOP_RANK)
.build();
@Override
public void onLoad() {
// Check if ItemsAdder exists, if yes register listener
if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) {
registerListener(new ItemsAdderListener(this));
OneBlockCustomBlockCreator.register(ItemsAdderCustomBlock::fromId);
OneBlockCustomBlockCreator.register("itemsadder", ItemsAdderCustomBlock::fromMap);
hasItemsAdder = true;
}
// Save the default config from config.yml
saveDefaultConfig();
// Load settings from config.yml. This will check if there are any issues with
// it too.
if (loadSettings()) {
// Chunk generator
chunkGenerator = settings.isUseOwnGenerator() ? null : new ChunkGeneratorWorld(this);
// Register commands
playerCommand = new PlayerCommand(this);
adminCommand = new AdminCommand(this);
// Register flag with BentoBox
// Register protection flag with BentoBox
getPlugin().getFlagsManager().registerFlag(this, START_SAFETY);
// Bossbar
if (getSettings().isBossBar()) {
getPlugin().getFlagsManager().registerFlag(this, this.ONEBLOCK_BOSSBAR);
}
// Actionbar
if (getSettings().isActionBar()) {
getPlugin().getFlagsManager().registerFlag(this, this.ONEBLOCK_ACTIONBAR);
}
// Magic Block protection
getPlugin().getFlagsManager().registerFlag(this, this.MAGIC_BLOCK);
}
}
/**
* Loads the settings from the config file.
* @return true if settings were loaded successfully, false otherwise.
*/
private boolean loadSettings() {
// Load settings again to get worlds
settings = configObject.loadConfigObject();
if (settings == null) {
// Disable
logError("AOneBlock settings could not load! Addon disabled.");
setState(State.DISABLED);
return false;
} else {
// Save the settings
configObject.saveConfigObject(settings);
}
return true;
}
@Override
public void onEnable() {
// Initialize the OneBlock manager
oneBlockManager = new OneBlocksManager(this);
// Load phase data
if (loadData()) {
// Failed to load - don't register anything
return;
}
// Initialize and register listeners
blockListener = new BlockListener(this);
registerListener(blockListener);
registerListener(new NoBlockHandler(this));
registerListener(new BlockProtect(this));
registerListener(new JoinLeaveListener(this));
registerListener(new InfoListener(this));
if (getSettings().isBossBar() && getSettings().isActionBar()) {
registerListener(bossBar);
}
// Register placeholders
phManager = new AOneBlockPlaceholders(this, getPlugin().getPlaceholdersManager());
// Register request handlers
registerRequestHandler(new IslandStatsHandler(this));
registerRequestHandler(new LocationStatsHandler(this));
// Register Holograms
holoListener = new HoloListener(this);
registerListener(holoListener);
}
/**
* Load phase data from oneblock.yml.
* @return true if there was an error, false otherwise.
*/
public boolean loadData() {
try {
oneBlockManager.loadPhases();
} catch (IOException e) {
// Disable the addon if phase data cannot be loaded
logError("AOneBlock settings could not load (oneblock.yml error)! Addon disabled.");
logError(e.getMessage());
setState(State.DISABLED);
return true;
}
return false;
}
@Override
public void onDisable() {
// save cache
if (blockListener != null) {
blockListener.saveCache();
}
// Clear holograms
if (holoListener != null) {
holoListener.onDisable();
}
}
@Override
public void onReload() {
// save cache
blockListener.saveCache();
// Reload settings and phase data
if (loadSettings()) {
log("Reloaded AOneBlock settings");
loadData();
}
}
/**
* @return the settings
*/
public Settings getSettings() {
return settings;
}
@Override
public void createWorlds() {
String worldName = settings.getWorldName().toLowerCase();
if (getServer().getWorld(worldName) == null) {
log("Creating AOneBlock world ...");
}
// Create the world if it does not exist
islandWorld = getWorld(worldName, World.Environment.NORMAL, chunkGenerator);
// Make the nether if it does not exist
if (settings.isNetherGenerate()) {
if (getServer().getWorld(worldName + NETHER) == null) {
log("Creating AOneBlock's Nether...");
}
netherWorld = settings.isNetherIslands() ? getWorld(worldName, World.Environment.NETHER, chunkGenerator)
: getWorld(worldName, World.Environment.NETHER, null);
}
// Make the end if it does not exist
if (settings.isEndGenerate()) {
if (getServer().getWorld(worldName + THE_END) == null) {
log("Creating AOneBlock's End World...");
}
endWorld = settings.isEndIslands() ? getWorld(worldName, World.Environment.THE_END, chunkGenerator)
: getWorld(worldName, World.Environment.THE_END, null);
}
}
/**
* Gets a world or generates a new world if it does not exist
*
* @param worldName2 - the overworld name
* @param env - the environment
* @param chunkGenerator2 - the chunk generator. If <tt>null</tt> then the
* generator will not be specified
* @return world loaded or generated
*/
private World getWorld(String worldName2, Environment env, ChunkGeneratorWorld chunkGenerator2) {
// Set world name
worldName2 = env.equals(World.Environment.NETHER) ? worldName2 + NETHER : worldName2;
worldName2 = env.equals(World.Environment.THE_END) ? worldName2 + THE_END : worldName2;
WorldCreator wc = WorldCreator.name(worldName2).environment(env);
// Use custom generator if configured, otherwise default
World w = settings.isUseOwnGenerator() ? wc.createWorld() : wc.generator(chunkGenerator2).createWorld();
// Set spawn rates
if (w != null) {
setSpawnRates(w);
}
return w;
}
/**
* Sets the spawn rates for a given world based on the addon's settings.
* @param w The world to set spawn rates for.
*/
private void setSpawnRates(World w) {
if (getSettings().getSpawnLimitMonsters() > 0) {
w.setSpawnLimit(SpawnCategory.MONSTER, getSettings().getSpawnLimitMonsters());
}
if (getSettings().getSpawnLimitAmbient() > 0) {
w.setSpawnLimit(SpawnCategory.AMBIENT, getSettings().getSpawnLimitAmbient());
}
if (getSettings().getSpawnLimitAnimals() > 0) {
w.setSpawnLimit(SpawnCategory.ANIMAL, getSettings().getSpawnLimitAnimals());
}
if (getSettings().getSpawnLimitWaterAnimals() > 0) {
w.setSpawnLimit(SpawnCategory.WATER_ANIMAL, getSettings().getSpawnLimitWaterAnimals());
}
if (getSettings().getTicksPerAnimalSpawns() > 0) {
w.setTicksPerSpawns(SpawnCategory.ANIMAL, getSettings().getTicksPerAnimalSpawns());
}
if (getSettings().getTicksPerMonsterSpawns() > 0) {
w.setTicksPerSpawns(SpawnCategory.MONSTER, getSettings().getTicksPerMonsterSpawns());
}
}
@Override
public WorldSettings getWorldSettings() {
return getSettings();
}
@Override
public @Nullable ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
return chunkGenerator;
}
@Override
public void saveWorldSettings() {
if (settings != null) {
configObject.saveConfigObject(settings);
}
}
@Override
public void saveDefaultConfig() {
super.saveDefaultConfig();
// Save default phases panel
this.saveResource("panels/phases_panel.yml", false);
}
/*
* (non-Javadoc)
*
* @see world.bentobox.bentobox.api.addons.Addon#allLoaded()
*/
@Override
public void allLoaded() {
// save settings. This will occur after all addons have loaded
this.saveWorldSettings();
}
/**
* @param i - island
* @return one block island data
*/
@NonNull
public OneBlockIslands getOneBlocksIsland(@NonNull Island i) {
return blockListener.getIsland(Objects.requireNonNull(i));
}
/**
* @return The OneBlock manager.
*/
public OneBlocksManager getOneBlockManager() {
return oneBlockManager;
}
/**
* @return the blockListener
*/
public BlockListener getBlockListener() {
return blockListener;
}
/**
* Get the placeholder manager
*
* @return the phManager
*/
public AOneBlockPlaceholders getPlaceholdersManager() {
return phManager;
}
/**
* @return the holoListener
*/
public HoloListener getHoloListener() {
return holoListener;
}
/**
* @return true if ItemsAdder is on the server
*/
public boolean hasItemsAdder() {
return hasItemsAdder;
}
/**
* Set the addon's world. Used only for testing.
* @param world world
*/
public void setIslandWorld(World world) {
this.islandWorld = world;
}
/**
* Sets the addon's settings. Used only for testing.
* @param settings The settings to set.
*/
public void setSettings(Settings settings) {
this.settings = settings;
}
/**
* @return the bossBar
*/
public BossBarListener getBossBar() {
return bossBar;
}
}