-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathServerState.java
More file actions
68 lines (49 loc) · 2.33 KB
/
ServerState.java
File metadata and controls
68 lines (49 loc) · 2.33 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
package com.gmail.picono435.randomtp.data;
import com.gmail.picono435.randomtp.RandomTPMod;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.storage.DimensionDataStorage;
import java.util.HashMap;
import java.util.UUID;
public class ServerState extends SavedData {
public HashMap<UUID, PlayerState> players = new HashMap<>();
@Override
public CompoundTag save(CompoundTag compoundTag, HolderLookup.Provider provider) {
CompoundTag playersNbtCompound = new CompoundTag();
players.forEach((UUID, playerSate) -> {
CompoundTag playerStateNbt = new CompoundTag();
playerStateNbt.putBoolean("hasJoined", playerSate.hasJoined);
playersNbtCompound.put(String.valueOf(UUID), playerStateNbt);
});
compoundTag.put("players", playersNbtCompound);
return compoundTag;
}
public static ServerState createFromNbt(CompoundTag compoundTag, HolderLookup.Provider provider) {
ServerState serverState = new ServerState();
CompoundTag playersTag = compoundTag.getCompound("players");
playersTag.getAllKeys().forEach(key -> {
PlayerState playerState = new PlayerState();
playerState.hasJoined = playersTag.getCompound(key).getBoolean("hasJoined");
UUID uuid = UUID.fromString(key);
serverState.players.put(uuid, playerState);
});
return serverState;
}
public static ServerState getServerState(MinecraftServer server) {
DimensionDataStorage persistentStateManager = server
.overworld().getDataStorage();
ServerState serverState = persistentStateManager.computeIfAbsent(
new SavedData.Factory<>(ServerState::new, ServerState::createFromNbt, null),
RandomTPMod.MOD_ID);
serverState.setDirty();
return serverState;
}
public static PlayerState getPlayerState(LivingEntity player) {
ServerState serverState = getServerState(player.getServer());
PlayerState playerState = serverState.players.computeIfAbsent(player.getUUID(), uuid -> new PlayerState());
return playerState;
}
}