aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kubatech/savedata/PlayerDataManager.java
blob: d14a261542953167ea3e3c05bc10b6d7e99e8c5e (plain)
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
/*
 * KubaTech - Gregtech Addon Copyright (C) 2022 - 2023 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/>.
 */

package kubatech.savedata;

import java.util.HashMap;
import java.util.Map;

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;

public class PlayerDataManager extends WorldSavedData {

    private static final String playerDataName = "KubaTech_PlayerData";
    static PlayerDataManager Instance = null;
    private final HashMap<String, 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);
            players.put(playerNBTData.getString("username"), new PlayerData(playerNBTData.getCompoundTag("data")));
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound NBTData) {
        NBTData.setInteger("size", players.size());
        int i = 0;
        for (Map.Entry<String, PlayerData> playerDataEntry : players.entrySet()) {
            NBTTagCompound playerNBTData = new NBTTagCompound();
            playerNBTData.setString("username", playerDataEntry.getKey());
            playerNBTData.setTag("data", playerDataEntry.getValue().toNBTData());
            NBTData.setTag("Player." + (i++), playerNBTData);
        }
    }

    public static PlayerData getPlayer(String username) {
        if (Instance == null) return null;
        return Instance.players.computeIfAbsent(username, s -> new PlayerData());
    }

    @SuppressWarnings("unused")
    @SubscribeEvent
    public void onWorldLoad(WorldEvent.Load event) {
        if (event.world.isRemote || event.world.provider.dimensionId != 0) return;
        Initialize(event.world);
    }
}