-
Notifications
You must be signed in to change notification settings - Fork 7
/
SkyblockSavedData.java
555 lines (447 loc) · 20.1 KB
/
SkyblockSavedData.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package de.melanx.skyblockbuilder.data;
import com.google.common.collect.*;
import de.melanx.skyblockbuilder.SkyblockBuilder;
import de.melanx.skyblockbuilder.client.GameProfileCache;
import de.melanx.skyblockbuilder.config.StartingInventory;
import de.melanx.skyblockbuilder.config.common.SpawnConfig;
import de.melanx.skyblockbuilder.config.common.TemplatesConfig;
import de.melanx.skyblockbuilder.template.ConfiguredTemplate;
import de.melanx.skyblockbuilder.util.Spiral;
import de.melanx.skyblockbuilder.util.WorldUtil;
import de.melanx.skyblockbuilder.world.IslandPos;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.storage.DimensionDataStorage;
import net.minecraftforge.common.util.BlockSnapshot;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
/*
* Credits go to Botania authors
* https://github.com/Vazkii/Botania/blob/master/src/main/java/vazkii/botania/common/world/SkyblockSavedData.java
*/
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class SkyblockSavedData extends SavedData {
private static final String NAME = "skyblock_builder";
private static SkyblockSavedData clientInstance;
public static final UUID SPAWN_ID = Util.NIL_UUID;
private ServerLevel level;
private Map<UUID, SkyMeta> metaInfo = Maps.newHashMap();
private Map<UUID, Team> skyblocks = Maps.newHashMap();
private BiMap<String, UUID> skyblockIds = HashBiMap.create();
private BiMap<UUID, IslandPos> skyblockPositions = HashBiMap.create();
private Spiral spiral = new Spiral();
public static SkyblockSavedData get(Level level) {
if (!level.isClientSide) {
MinecraftServer server = ((ServerLevel) level).getServer();
DimensionDataStorage storage = server.overworld().getDataStorage();
SkyblockSavedData data = storage.computeIfAbsent(nbt -> new SkyblockSavedData().load(nbt), SkyblockSavedData::new, NAME);
data.level = WorldUtil.getConfiguredLevel(server);
data.getOrCreateMetaInfo(Util.NIL_UUID);
return data;
} else {
return clientInstance == null ? new SkyblockSavedData() : clientInstance;
}
}
public static void updateClient(SkyblockSavedData data) {
clientInstance = data;
}
public Team getSpawn() {
if (this.skyblocks.get(SPAWN_ID) != null) {
return this.skyblocks.get(SPAWN_ID);
}
SkyblockBuilder.getLogger().info("Successfully generated spawn.");
Team team = this.createTeam("Spawn", TemplatesConfig.spawn.flatMap(templateInfo -> Optional.of(new ConfiguredTemplate(templateInfo))).orElse(TemplateData.get(this.level).getConfiguredTemplate()));
//noinspection ConstantConditions
team.addPlayer(Util.NIL_UUID);
this.setDirty();
return team;
}
public Optional<Team> getSpawnOption() {
return Optional.ofNullable(this.skyblocks.get(SPAWN_ID));
}
public Pair<IslandPos, Team> create(String teamName, ConfiguredTemplate template) {
IslandPos islandPos;
Team team;
if (teamName.equalsIgnoreCase("spawn")) {
int[] pos = new int[]{0, 0};
if (SpawnConfig.skipCenterIslandCreation) {
pos = this.spiral.next();
}
islandPos = new IslandPos(this.level, pos[0], pos[1], template);
team = new Team(this, islandPos, SPAWN_ID);
} else {
do {
int[] pos = this.spiral.next();
islandPos = new IslandPos(this.level, pos[0], pos[1], template);
} while (this.skyblockPositions.containsValue(islandPos));
team = new Team(this, islandPos);
}
Set<TemplatesConfig.Spawn> positions = initialPossibleSpawns(islandPos.getCenter(), template);
team.setPossibleSpawns(positions);
team.setName(teamName);
this.skyblocks.put(team.getId(), team);
this.skyblockIds.put(team.getName().toLowerCase(Locale.ROOT), team.getId());
this.skyblockPositions.put(team.getId(), islandPos);
this.setDirty();
return Pair.of(islandPos, team);
}
public SkyblockSavedData load(CompoundTag nbt) {
Map<UUID, SkyMeta> metaInfo = Maps.newHashMap();
Map<UUID, Team> skyblocks = Maps.newHashMap();
BiMap<String, UUID> skyblockIds = HashBiMap.create();
BiMap<UUID, IslandPos> skyblockPositions = HashBiMap.create();
for (Tag inbt : nbt.getList("Islands", Tag.TAG_COMPOUND)) {
CompoundTag tag = (CompoundTag) inbt;
IslandPos island = IslandPos.fromTag(tag.getCompound("Island"));
Team team = Team.create(this, tag);
skyblocks.put(team.getId(), team);
skyblockIds.put(team.getName().toLowerCase(Locale.ROOT), team.getId());
skyblockPositions.put(team.getId(), island);
}
for (Tag inbt : nbt.getList("MetaInformation", Tag.TAG_COMPOUND)) {
CompoundTag tag = (CompoundTag) inbt;
UUID player = tag.getUUID("Player");
SkyMeta meta = SkyMeta.get(this, tag.getCompound("Meta"));
metaInfo.put(player, meta);
}
this.metaInfo = metaInfo;
this.skyblocks = skyblocks;
this.skyblockIds = skyblockIds;
this.skyblockPositions = skyblockPositions;
this.spiral = Spiral.fromArray(nbt.getIntArray("SpiralState"));
return this;
}
@Nonnull
@Override
public CompoundTag save(@Nonnull CompoundTag compound) {
ListTag islands = new ListTag();
for (Team team : this.skyblocks.values()) {
islands.add(team.serializeNBT());
}
ListTag metaInfo = new ListTag();
for (Map.Entry<UUID, SkyMeta> entry : this.metaInfo.entrySet()) {
SkyMeta meta = entry.getValue();
CompoundTag entryTag = new CompoundTag();
entryTag.putUUID("Player", entry.getKey());
entryTag.put("Meta", meta.save());
metaInfo.add(entryTag);
}
compound.putIntArray("SpiralState", this.spiral.toIntArray());
compound.put("Islands", islands);
compound.put("MetaInformation", metaInfo);
return compound;
}
@Nullable
public IslandPos getTeamIsland(UUID teamId) {
return this.skyblockPositions.get(teamId);
}
public boolean hasPlayerTeam(Player player) {
return this.hasPlayerTeam(player.getGameProfile().getId());
}
public boolean hasPlayerTeam(UUID player) {
Team team = this.getTeamFromPlayer(player);
return team != null && !team.isSpawn();
}
public boolean addPlayerToTeam(UUID teamId, Player player) {
return this.addPlayerToTeam(teamId, player.getGameProfile().getId());
}
public boolean addPlayerToTeam(UUID teamId, UUID playerId) {
Team team = this.skyblocks.get(teamId);
if (team != null) {
return team.addPlayer(playerId);
}
return false;
}
public boolean addPlayerToTeam(String teamName, Player player) {
return this.addPlayerToTeam(teamName, player.getGameProfile().getId());
}
public boolean addPlayerToTeam(String teamName, UUID player) {
UUID teamId = this.skyblockIds.get(teamName.toLowerCase(Locale.ROOT));
return this.addPlayerToTeam(teamId, player);
}
public boolean addPlayerToTeam(Team team, Player player) {
return this.addPlayerToTeam(team, player.getGameProfile().getId());
}
public boolean addPlayerToTeam(Team team, UUID player) {
if (!team.isSpawn()) {
team.broadcast(Component.translatable("skyblockbuilder.event.player_joined", GameProfileCache.getName(player)), Style.EMPTY.applyFormat(ChatFormatting.GOLD));
}
ServerLevel level = team.getLevel();
if (level != null && !this.metaInfo.get(player).getPreviousTeamIds().contains(team.getId())) {
ServerPlayer onlinePlayer = level.getServer().getPlayerList().getPlayer(player);
if (onlinePlayer != null) {
StartingInventory.getStarterItems().forEach(entry -> {
if (entry.getLeft() == EquipmentSlot.MAINHAND) {
onlinePlayer.getInventory().add(entry.getRight().copy());
} else {
onlinePlayer.setItemSlot(entry.getLeft(), entry.getRight().copy());
}
});
}
}
this.getSpawn().removePlayer(player);
team.addPlayer(player);
this.setDirty();
return true;
}
@Nullable
public Team createTeam(String teamName) {
if (this.level == null) {
return null;
}
return this.createTeam(teamName, TemplateData.get(this.level).getConfiguredTemplate());
}
@Nullable
public Team createTeam(String teamName, ConfiguredTemplate template) {
if (this.teamExists(teamName) || this.level == null) {
return null;
}
Pair<IslandPos, Team> pair = this.create(teamName, template);
Team team = pair.getRight();
List<TemplatesConfig.Spawn> possibleSpawns = new ArrayList<>(this.getPossibleSpawns(team.getIsland(), template));
team.setPossibleSpawns(possibleSpawns);
StructurePlaceSettings settings = new StructurePlaceSettings().setKnownShape(true).setKeepLiquids(false);
BlockPos center = team.getIsland().getCenter();
List<BlockSnapshot> capturedBlockSnapshots = (List<BlockSnapshot>) this.level.capturedBlockSnapshots.clone();
this.level.captureBlockSnapshots = true;
template.getTemplate().placeInWorld(this.level, center, center, settings, RandomSource.create(), Block.UPDATE_CLIENTS);
SkyblockSavedData.surround(this.level, team.getIsland().getCenter(), template);
this.level.captureBlockSnapshots = false;
this.level.capturedBlockSnapshots.clear();
this.level.capturedBlockSnapshots.addAll(capturedBlockSnapshots);
this.skyblocks.put(team.getId(), team);
this.skyblockIds.put(team.getName().toLowerCase(Locale.ROOT), team.getId());
this.skyblockPositions.put(team.getId(), team.getIsland());
this.setDirty();
return team;
}
@Nullable
public Team createTeamAndJoin(String teamName, Player player) {
return this.createTeamAndJoin(teamName, player.getGameProfile().getId());
}
@Nullable
public Team createTeamAndJoin(String teamName, UUID player) {
Team team = this.createTeam(teamName);
if (team == null) return null;
team.addPlayer(player);
this.setDirty();
return team;
}
public boolean removePlayerFromTeam(Player player) {
return this.removePlayerFromTeam(player.getGameProfile().getId());
}
public boolean removePlayerFromTeam(UUID player) {
for (Map.Entry<UUID, Team> entry : this.skyblocks.entrySet()) {
Team team = entry.getValue();
if (team.isSpawn()) continue;
if (team.hasPlayer(player)) {
boolean removed = team.removePlayer(player);
if (removed) {
team.broadcast(Component.translatable("skyblockbuilder.event.remove_player", GameProfileCache.getName(player)), Style.EMPTY.applyFormat(ChatFormatting.RED));
//noinspection ConstantConditions
this.getTeam(SPAWN_ID).addPlayer(player);
this.getOrCreateMetaInfo(player).setTeamId(SPAWN_ID);
}
return removed;
}
}
return false;
}
public void removeAllPlayersFromTeam(@Nonnull Team team) {
Set<UUID> players = Sets.newHashSet(team.getPlayers());
team.removeAllPlayers();
Team spawn = this.getSpawn();
for (UUID player : players) {
this.addPlayerToTeam(spawn, player);
}
this.setDirty();
}
@Nullable
public Team getTeam(String name) {
return this.getTeam(this.skyblockIds.get(name.toLowerCase(Locale.ROOT)));
}
@Nullable
public Team getTeam(UUID teamId) {
return this.skyblocks.get(teamId);
}
public boolean deleteTeam(String team) {
UUID teamId = this.skyblockIds.get(team.toLowerCase(Locale.ROOT));
return this.deleteTeam(teamId);
}
public boolean deleteTeam(UUID teamId) {
Team removedTeam = this.skyblocks.remove(teamId);
if (removedTeam == null) {
return false;
}
this.skyblockIds.inverse().remove(teamId);
this.skyblockPositions.inverse().remove(removedTeam.getIsland());
this.skyblocks.get(SPAWN_ID).addPlayers(removedTeam.getPlayers());
return true;
}
@Nullable
public Team getTeamFromPlayer(Player player) {
return this.getTeamFromPlayer(player.getGameProfile().getId());
}
@Nullable
public Team getTeamFromPlayer(UUID player) {
SkyMeta meta = this.metaInfo.get(player);
if (meta == null) {
return null;
}
Team team = this.skyblocks.getOrDefault(meta.getTeamId(), this.skyblocks.get(SPAWN_ID));
return team == null || team.isSpawn() ? null : team;
}
public boolean teamExists(String name) {
return this.skyblockIds.containsKey(name.toLowerCase(Locale.ROOT));
}
public boolean teamExists(UUID teamId) {
return this.skyblocks.containsKey(teamId);
}
public Collection<Team> getTeams() {
return this.skyblocks.values();
}
public void addInvite(Team team, Player invitor, Player player) {
this.addInvite(team, invitor, player.getGameProfile().getId());
}
public void addInvite(Team team, Player invitor, UUID id) {
SkyMeta meta = this.metaInfo.get(id);
if (!meta.getInvites().contains(team.getId())) {
meta.addInvite(team.getId());
team.broadcast(Component.translatable("skyblockbuilder.event.invite_player", invitor.getDisplayName(), GameProfileCache.getName(id)), Style.EMPTY.applyFormat(ChatFormatting.GOLD));
}
this.setDirty();
}
public boolean hasInvites(Player player) {
return this.hasInvites(player.getGameProfile().getId());
}
public boolean hasInvites(UUID player) {
SkyMeta meta = this.metaInfo.get(player);
return meta != null && !meta.getInvites().isEmpty();
}
public boolean hasInviteFrom(Team team, Player player) {
return this.hasInviteFrom(team, player.getGameProfile().getId());
}
public boolean hasInviteFrom(Team team, UUID player) {
SkyMeta meta = this.metaInfo.get(player);
return meta != null && meta.getInvites().contains(team.getId());
}
public List<UUID> getInvites(Player player) {
return this.getInvites(player.getGameProfile().getId());
}
public List<UUID> getInvites(UUID player) {
SkyMeta meta = this.metaInfo.get(player);
return meta == null ? Lists.newArrayList() : meta.getInvites();
}
public boolean acceptInvite(Team team, Player player) {
return this.acceptInvite(team, player.getGameProfile().getId());
}
public boolean acceptInvite(Team team, UUID id) {
SkyMeta meta = this.metaInfo.get(id);
if (meta == null) {
return false;
}
if (meta.getInvites().contains(team.getId())) {
team.broadcast(Component.translatable("skyblockbuilder.event.accept_invite", GameProfileCache.getName(id)), Style.EMPTY.applyFormat(ChatFormatting.GOLD));
this.addPlayerToTeam(team.getName(), id);
meta.resetInvites();
//noinspection ConstantConditions
WorldUtil.teleportToIsland(this.level.getServer().getPlayerList().getPlayer(id), team);
this.setDirty();
return true;
}
return false;
}
public boolean declineInvite(Team team, Player player) {
return this.declineInvite(team, player.getGameProfile().getId());
}
public boolean declineInvite(Team team, UUID id) {
SkyMeta meta = this.metaInfo.get(id);
if (meta == null) {
return false;
}
meta.removeInvite(team.getId());
this.setDirty();
return true;
}
public void renameTeam(Team team, @Nullable ServerPlayer player, String name) {
String oldName = team.getName().toLowerCase();
this.skyblockIds.remove(oldName);
team.setName(name);
this.skyblockIds.put(name.toLowerCase(Locale.ROOT), team.getId());
Component playerName = player != null ? player.getDisplayName() : Component.literal("Server");
team.broadcast(Component.translatable("skyblockbuilder.event.rename_team", playerName, oldName, name), Style.EMPTY.applyFormat(ChatFormatting.DARK_RED));
this.setDirty();
}
public SkyMeta getOrCreateMetaInfo(Player player) {
return this.getOrCreateMetaInfo(player.getGameProfile().getId());
}
public SkyMeta getOrCreateMetaInfo(UUID id) {
return this.metaInfo.computeIfAbsent(id, meta -> new SkyMeta(this, id));
}
public Set<TemplatesConfig.Spawn> getPossibleSpawns(IslandPos pos, ConfiguredTemplate template) {
if (!this.skyblockPositions.containsValue(pos)) {
return initialPossibleSpawns(pos.getCenter(), template);
}
return this.skyblocks.get(this.skyblockPositions.inverse().get(pos)).getPossibleSpawns();
}
public static Set<TemplatesConfig.Spawn> initialPossibleSpawns(BlockPos center, ConfiguredTemplate template) {
Set<TemplatesConfig.Spawn> positions = new HashSet<>();
for (TemplatesConfig.Spawn spawn : template.getDefaultSpawns()) {
positions.add(new TemplatesConfig.Spawn(center.offset(spawn.pos().immutable()), spawn.direction()));
}
return positions;
}
private static void surround(ServerLevel level, BlockPos zero, ConfiguredTemplate configuredTemplate) {
if (configuredTemplate.getSurroundingBlocks().isEmpty() || configuredTemplate.getSurroundingMargin() <= 0) {
return;
}
StructureTemplate template = configuredTemplate.getTemplate();
BoundingBox box = new BoundingBox(zero.getX(), zero.getY(), zero.getZ(),
zero.getX() + template.size.getX() - 1, zero.getY() + template.size.getY() - 1, zero.getZ() + template.size.getZ() - 1);
BoundingBox outside = box.inflatedBy(configuredTemplate.getSurroundingMargin());
RandomSource random = RandomSource.create();
BlockPos.betweenClosedStream(outside).forEach(blockPos -> {
if (!box.isInside(blockPos)) {
Block block = configuredTemplate.getSurroundingBlocks().get(random.nextInt(configuredTemplate.getSurroundingBlocks().size()));
level.setBlock(blockPos, block.defaultBlockState(), Block.UPDATE_CLIENTS);
}
});
}
@Override
public void setDirty() {
super.setDirty();
if (this.level != null) {
SkyblockBuilder.getNetwork().updateData(this.level, this);
for (ServerPlayer player : this.level.getServer().getPlayerList().getPlayers()) {
player.refreshTabListName();
}
}
}
public void setDirtySilently() {
super.setDirty();
}
@Nullable
public ServerLevel getLevel() {
return this.level;
}
}