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
|
/*
* spotless:off
* KubaTech - Gregtech Addon
* Copyright (C) 2022 - 2024 kuba6000
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
* spotless:on
*/
package kubatech.savedata;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import net.minecraftforge.event.world.WorldEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import kubatech.api.helpers.UUIDFinder;
public class PlayerDataManager extends WorldSavedData {
private static final String playerDataName = "KubaTech_PlayerData";
static PlayerDataManager Instance = null;
private final HashMap<UUID, PlayerData> players = new HashMap<>();
public static void Initialize(World world) {
if (Instance != null) {
Instance.players.clear();
}
Instance = (PlayerDataManager) world.mapStorage.loadData(PlayerDataManager.class, playerDataName);
if (Instance == null) {
Instance = new PlayerDataManager();
world.mapStorage.setData(playerDataName, Instance);
}
Instance.markDirty();
}
@SuppressWarnings("unused")
public PlayerDataManager(String p_i2141_1_) {
super(p_i2141_1_);
}
public PlayerDataManager() {
super(playerDataName);
}
@Override
public void readFromNBT(NBTTagCompound NBTData) {
if (!NBTData.hasKey("size")) return;
players.clear();
for (int i = 0, imax = NBTData.getInteger("size"); i < imax; i++) {
NBTTagCompound playerNBTData = NBTData.getCompoundTag("Player." + i);
if (!playerNBTData.hasKey("uuid")) continue;
UUID uuid = UUID.fromString(playerNBTData.getString("uuid"));
PlayerData pData = new PlayerData(playerNBTData.getCompoundTag("data"));
players.put(uuid, pData);
if (!pData.username.isEmpty()) UUIDFinder.updateMapping(pData.username, uuid);
}
}
@Override
public void writeToNBT(NBTTagCompound NBTData) {
NBTData.setInteger("size", players.size());
int i = 0;
for (Map.Entry<UUID, PlayerData> playerDataEntry : players.entrySet()) {
NBTTagCompound playerNBTData = new NBTTagCompound();
playerNBTData.setString(
"uuid",
playerDataEntry.getKey()
.toString());
playerNBTData.setTag(
"data",
playerDataEntry.getValue()
.toNBTData());
NBTData.setTag("Player." + (i++), playerNBTData);
}
}
public static PlayerData getPlayer(UUID player) {
if (Instance == null) return null; // probably client side
return Instance.players.computeIfAbsent(player, s -> new PlayerData());
}
public static void initializePlayer(EntityPlayerMP player) {
if (Instance == null) return;
if (!Instance.players.containsKey(player.getPersistentID())) {
PlayerData pData = new PlayerData();
pData.username = player.getCommandSenderName();
Instance.players.put(player.getPersistentID(), pData);
Instance.markDirty();
}
}
@SuppressWarnings("unused")
@SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) {
if (event.world.isRemote || event.world.provider.dimensionId != 0) return;
Initialize(event.world);
}
}
|