/
IslandBuilderImpl.java
468 lines (405 loc) · 18.6 KB
/
IslandBuilderImpl.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
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
package com.bgsoftware.superiorskyblock.island.builder;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.enums.Rating;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.IslandFlag;
import com.bgsoftware.superiorskyblock.api.island.IslandPrivilege;
import com.bgsoftware.superiorskyblock.api.island.PlayerRole;
import com.bgsoftware.superiorskyblock.api.island.bank.BankTransaction;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.key.KeyMap;
import com.bgsoftware.superiorskyblock.api.missions.Mission;
import com.bgsoftware.superiorskyblock.api.upgrades.Upgrade;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.key.KeyMapImpl;
import com.bgsoftware.superiorskyblock.island.SIsland;
import com.bgsoftware.superiorskyblock.island.container.value.Value;
import com.bgsoftware.superiorskyblock.island.privilege.PlayerPrivilegeNode;
import com.google.common.base.Preconditions;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class IslandBuilderImpl implements Island.Builder {
private static final BigDecimal SYNCED_BANK_LIMIT_VALUE = BigDecimal.valueOf(-2);
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
@Nullable
public SuperiorPlayer owner;
public UUID uuid = null;
public Location center = null;
public String islandName = "";
@Nullable
public String islandType;
public long creationTime = System.currentTimeMillis() / 1000;
public String discord = "None";
public String paypal = "None";
public BigDecimal bonusWorth = BigDecimal.ZERO;
public BigDecimal bonusLevel = BigDecimal.ZERO;
public boolean isLocked = false;
public boolean isIgnored = false;
public String description = "";
public int generatedSchematicsMask = 0;
public int unlockedWorldsMask = 0;
public long lastTimeUpdated = System.currentTimeMillis() / 1000;
public final Set<ChunkPosition> dirtyChunks = new LinkedHashSet<>();
public final KeyMap<BigInteger> blockCounts = KeyMapImpl.createHashMap();
public final EnumMap<World.Environment, Location> islandHomes = new EnumMap<>(World.Environment.class);
public final List<SuperiorPlayer> members = new LinkedList<>();
public final List<SuperiorPlayer> bannedPlayers = new LinkedList<>();
public final Map<SuperiorPlayer, PlayerPrivilegeNode> playerPermissions = new LinkedHashMap<>();
public final Map<IslandPrivilege, PlayerRole> rolePermissions = new LinkedHashMap<>();
public final Map<String, Integer> upgrades = new LinkedHashMap<>();
public final KeyMap<Value<Integer>> blockLimits = KeyMapImpl.createHashMap();
public final Map<UUID, Rating> ratings = new LinkedHashMap<>();
public final Map<Mission<?>, Integer> completedMissions = new LinkedHashMap<>();
public final Map<IslandFlag, Byte> islandFlags = new LinkedHashMap<>();
public final EnumMap<World.Environment, KeyMap<Value<Integer>>> cobbleGeneratorValues = new EnumMap<>(World.Environment.class);
public final List<SIsland.UniqueVisitor> uniqueVisitors = new LinkedList<>();
public final KeyMap<Value<Integer>> entityLimits = KeyMapImpl.createHashMap();
public final Map<PotionEffectType, Value<Integer>> islandEffects = new LinkedHashMap<>();
public final List<ItemStack[]> islandChests = new ArrayList<>(plugin.getSettings().getIslandChests().getDefaultPages());
public final Map<PlayerRole, Value<Integer>> roleLimits = new LinkedHashMap<>();
public final EnumMap<World.Environment, Location> visitorHomes = new EnumMap<>(World.Environment.class);
public Value<Integer> islandSize = Value.syncedFixed(-1);
public Value<Integer> warpsLimit = Value.syncedFixed(-1);
public Value<Integer> teamLimit = Value.syncedFixed(-1);
public Value<Integer> coopLimit = Value.syncedFixed(-1);
public Value<Double> cropGrowth = Value.syncedFixed(-1D);
public Value<Double> spawnerRates = Value.syncedFixed(-1D);
public Value<Double> mobDrops = Value.syncedFixed(-1D);
public Value<BigDecimal> bankLimit = Value.syncedFixed(SYNCED_BANK_LIMIT_VALUE);
public BigDecimal balance = BigDecimal.ZERO;
public long lastInterestTime = 0L;
public List<WarpRecord> warps = new LinkedList<>();
public List<WarpCategoryRecord> warpCategories = new LinkedList<>();
public List<BankTransaction> bankTransactions = new LinkedList<>();
public byte[] persistentData = new byte[0];
public IslandBuilderImpl() {
}
@Override
public Island.Builder setOwner(@Nullable SuperiorPlayer owner) {
this.owner = owner;
return this;
}
@Override
public Island.Builder setUniqueId(UUID uuid) {
Preconditions.checkNotNull(uuid, "uuid parameter cannot be null.");
Preconditions.checkState(plugin.getGrid().getIslandByUUID(uuid) == null, "The provided uuid is not unique.");
this.uuid = uuid;
return this;
}
@Override
public Island.Builder setCenter(Location center) {
Preconditions.checkNotNull(center, "center parameter cannot be null.");
Preconditions.checkState(isValidCenter(center), "The provided center is not centered.");
this.center = center;
return this;
}
@Override
public Island.Builder setName(String islandName) {
Preconditions.checkNotNull(islandName, "islandName parameter cannot be null.");
this.islandName = islandName;
return this;
}
@Override
public Island.Builder setSchematicName(String islandType) {
Preconditions.checkNotNull(islandType, "islandType parameter cannot be null.");
this.islandType = islandType;
return this;
}
@Override
public Island.Builder setCreationTime(long creationTime) {
this.creationTime = creationTime;
return this;
}
@Override
public Island.Builder setDiscord(String discord) {
Preconditions.checkNotNull(discord, "discord parameter cannot be null.");
this.discord = discord;
return this;
}
@Override
public Island.Builder setPaypal(String paypal) {
Preconditions.checkNotNull(paypal, "paypal parameter cannot be null.");
this.paypal = paypal;
return this;
}
@Override
public Island.Builder setBonusWorth(BigDecimal bonusWorth) {
Preconditions.checkNotNull(bonusWorth, "bonusWorth parameter cannot be null.");
this.bonusWorth = bonusWorth;
return this;
}
@Override
public Island.Builder setBonusLevel(BigDecimal bonusLevel) {
Preconditions.checkNotNull(bonusLevel, "bonusLevel parameter cannot be null.");
this.bonusLevel = bonusLevel;
return this;
}
@Override
public Island.Builder setLocked(boolean isLocked) {
this.isLocked = isLocked;
return this;
}
@Override
public Island.Builder setIgnored(boolean isIgnored) {
this.isIgnored = isIgnored;
return this;
}
@Override
public Island.Builder setDescription(String description) {
Preconditions.checkNotNull(description, "description parameter cannot be null.");
this.description = description;
return this;
}
@Override
public Island.Builder setGeneratedSchematics(int generatedSchematicsMask) {
this.generatedSchematicsMask = generatedSchematicsMask;
return this;
}
@Override
public Island.Builder setUnlockedWorlds(int unlockedWorldsMask) {
this.unlockedWorldsMask = unlockedWorldsMask;
return this;
}
@Override
public Island.Builder setLastTimeUpdated(long lastTimeUpdated) {
this.lastTimeUpdated = lastTimeUpdated;
return this;
}
@Override
public Island.Builder setDirtyChunk(String worldName, int chunkX, int chunkZ) {
Preconditions.checkNotNull(worldName, "worldName parameter cannot be null.");
this.dirtyChunks.add(ChunkPosition.of(worldName, chunkX, chunkZ));
return this;
}
@Override
public Island.Builder setBlockCount(Key block, BigInteger count) {
Preconditions.checkNotNull(block, "block parameter cannot be null.");
Preconditions.checkNotNull(count, "count parameter cannot be null.");
this.blockCounts.put(block, count);
return this;
}
@Override
public Island.Builder setIslandHome(Location location, World.Environment environment) {
Preconditions.checkNotNull(location, "location parameter cannot be null.");
Preconditions.checkNotNull(environment, "environment parameter cannot be null.");
this.islandHomes.put(environment, location);
return this;
}
@Override
public Island.Builder addIslandMember(SuperiorPlayer superiorPlayer) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
this.members.add(superiorPlayer);
return this;
}
@Override
public Island.Builder addBannedPlayer(SuperiorPlayer superiorPlayer) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
this.bannedPlayers.add(superiorPlayer);
return this;
}
@Override
public Island.Builder setPlayerPermission(SuperiorPlayer superiorPlayer, IslandPrivilege islandPrivilege, boolean value) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
Preconditions.checkNotNull(islandPrivilege, "islandPrivilege parameter cannot be null.");
this.playerPermissions.computeIfAbsent(superiorPlayer, p -> new PlayerPrivilegeNode(superiorPlayer, null))
.loadPrivilege(islandPrivilege, (byte) (value ? 1 : 0));
return this;
}
@Override
public Island.Builder setRolePermission(IslandPrivilege islandPrivilege, PlayerRole requiredRole) {
Preconditions.checkNotNull(islandPrivilege, "islandPrivilege parameter cannot be null.");
Preconditions.checkNotNull(requiredRole, "requiredRole parameter cannot be null.");
this.rolePermissions.put(islandPrivilege, requiredRole);
return this;
}
@Override
public Island.Builder setUpgrade(Upgrade upgrade, int level) {
Preconditions.checkNotNull(upgrade, "upgrade parameter cannot be null.");
this.upgrades.put(upgrade.getName(), level);
return this;
}
@Override
public Island.Builder setBlockLimit(Key block, int limit) {
Preconditions.checkNotNull(block, "block parameter cannot be null.");
this.blockLimits.put(block, limit < 0 ? Value.syncedFixed(limit) : Value.fixed(limit));
return this;
}
@Override
public Island.Builder setRating(SuperiorPlayer superiorPlayer, Rating rating) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
Preconditions.checkNotNull(rating, "rating parameter cannot be null.");
this.ratings.put(superiorPlayer.getUniqueId(), rating);
return this;
}
@Override
public Island.Builder setCompletedMission(Mission<?> mission, int finishCount) {
Preconditions.checkNotNull(mission, "mission parameter cannot be null.");
this.completedMissions.put(mission, finishCount);
return this;
}
@Override
public Island.Builder setIslandFlag(IslandFlag islandFlag, boolean value) {
Preconditions.checkNotNull(islandFlag, "islandFlag parameter cannot be null.");
this.islandFlags.put(islandFlag, (byte) (value ? 1 : 0));
return this;
}
@Override
public Island.Builder setGeneratorRate(Key block, int rate, World.Environment environment) {
Preconditions.checkNotNull(block, "block parameter cannot be null.");
Preconditions.checkNotNull(environment, "environment parameter cannot be null.");
this.cobbleGeneratorValues.computeIfAbsent(environment, e -> KeyMapImpl.createHashMap())
.put(block, rate < 0 ? Value.syncedFixed(rate) : Value.fixed(rate));
return this;
}
@Override
public Island.Builder addUniqueVisitor(SuperiorPlayer superiorPlayer, long visitTime) {
Preconditions.checkNotNull(superiorPlayer, "superiorPlayer parameter cannot be null.");
this.uniqueVisitors.add(new SIsland.UniqueVisitor(superiorPlayer, visitTime));
return this;
}
@Override
public Island.Builder setEntityLimit(Key entity, int limit) {
Preconditions.checkNotNull(entity, "entity parameter cannot be null.");
this.entityLimits.put(entity, limit < 0 ? Value.syncedFixed(limit) : Value.fixed(limit));
return this;
}
@Override
public Island.Builder setIslandEffect(PotionEffectType potionEffectType, int level) {
Preconditions.checkNotNull(potionEffectType, "potionEffectType parameter cannot be null.");
this.islandEffects.put(potionEffectType, level < 0 ? Value.syncedFixed(level) : Value.fixed(level));
return this;
}
@Override
public Island.Builder setIslandChest(int index, ItemStack[] contents) {
Preconditions.checkNotNull(contents, "contents parameter cannot be null.");
if (index >= this.islandChests.size()) {
while (index > this.islandChests.size()) {
this.islandChests.add(new ItemStack[plugin.getSettings().getIslandChests().getDefaultSize() * 9]);
}
this.islandChests.add(contents);
} else {
this.islandChests.set(index, contents);
}
return this;
}
@Override
public Island.Builder setRoleLimit(PlayerRole playerRole, int limit) {
Preconditions.checkNotNull(playerRole, "playerRole parameter cannot be null.");
this.roleLimits.put(playerRole, limit < 0 ? Value.syncedFixed(limit) : Value.fixed(limit));
return this;
}
@Override
public Island.Builder setVisitorHome(Location location, World.Environment environment) {
Preconditions.checkNotNull(location, "location parameter cannot be null.");
Preconditions.checkNotNull(environment, "environment parameter cannot be null.");
this.visitorHomes.put(environment, location);
return this;
}
@Override
public Island.Builder setIslandSize(int islandSize) {
this.islandSize = islandSize < 0 ? Value.syncedFixed(islandSize) : Value.fixed(islandSize);
return this;
}
@Override
public Island.Builder setTeamLimit(int teamLimit) {
this.teamLimit = teamLimit < 0 ? Value.syncedFixed(teamLimit) : Value.fixed(teamLimit);
return this;
}
@Override
public Island.Builder setWarpsLimit(int warpsLimit) {
this.warpsLimit = warpsLimit < 0 ? Value.syncedFixed(warpsLimit) : Value.fixed(warpsLimit);
return this;
}
@Override
public Island.Builder setCropGrowth(double cropGrowth) {
this.cropGrowth = cropGrowth < 0 ? Value.syncedFixed(cropGrowth) : Value.fixed(cropGrowth);
return this;
}
@Override
public Island.Builder setSpawnerRates(double spawnerRates) {
this.spawnerRates = spawnerRates < 0 ? Value.syncedFixed(spawnerRates) : Value.fixed(spawnerRates);
return this;
}
@Override
public Island.Builder setMobDrops(double mobDrops) {
this.mobDrops = mobDrops < 0 ? Value.syncedFixed(mobDrops) : Value.fixed(mobDrops);
return this;
}
@Override
public Island.Builder setCoopLimit(int coopLimit) {
this.coopLimit = coopLimit < 0 ? Value.syncedFixed(coopLimit) : Value.fixed(coopLimit);
return this;
}
@Override
public Island.Builder setBankLimit(BigDecimal bankLimit) {
Preconditions.checkNotNull(bankLimit, "bankLimit parameter cannot be null.");
this.bankLimit = bankLimit.compareTo(SYNCED_BANK_LIMIT_VALUE) <= 0 ? Value.syncedFixed(bankLimit) : Value.fixed(bankLimit);
return this;
}
@Override
public Island.Builder setBalance(BigDecimal balance) {
Preconditions.checkNotNull(balance, "balance parameter cannot be null.");
this.balance = balance;
return this;
}
@Override
public Island.Builder setLastInterestTime(long lastInterestTime) {
this.lastInterestTime = lastInterestTime;
return this;
}
@Override
public Island.Builder addWarp(String name, String category, Location location, boolean isPrivate, @Nullable ItemStack icon) {
Preconditions.checkNotNull(name, "name parameter cannot be null.");
Preconditions.checkNotNull(category, "category parameter cannot be null.");
Preconditions.checkNotNull(location, "location parameter cannot be null.");
this.warps.add(new WarpRecord(name, category, location, isPrivate, icon));
return this;
}
@Override
public Island.Builder addWarpCategory(String name, int slot, @Nullable ItemStack icon) {
Preconditions.checkNotNull(name, "name parameter cannot be null.");
Preconditions.checkArgument(slot >= 0, "slot must be positive.");
this.warpCategories.add(new WarpCategoryRecord(name, slot, icon));
return this;
}
@Override
public Island.Builder addBankTransaction(BankTransaction bankTransaction) {
Preconditions.checkNotNull(bankTransaction, "bankTransaction parameter cannot be null.");
this.bankTransactions.add(bankTransaction);
return this;
}
@Override
public Island.Builder setPersistentData(byte[] persistentData) {
Preconditions.checkNotNull(persistentData, "persistentData parameter cannot be null.");
this.persistentData = persistentData;
return this;
}
@Override
public Island build() {
if (this.uuid == null)
throw new IllegalStateException("Cannot create an island with no valid uuid.");
if (this.center == null)
throw new IllegalStateException("Cannot create an island with no valid location.");
return plugin.getFactory().createIsland(this);
}
private static boolean isValidCenter(Location center) {
int maxIslandSize = plugin.getSettings().getMaxIslandSize() * 3;
return center.getBlockX() % maxIslandSize == 0 && center.getBlockZ() % maxIslandSize == 0 &&
plugin.getGrid().getIslandAt(center) == null;
}
}