Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jayhill.xlife.common.world.stats;
- import com.jayhill.xlife.XLife;
- import com.jayhill.xlife.common.capability.stats.DefaultStatsCapability;
- import com.jayhill.xlife.common.capability.stats.StatsCapability;
- import com.jayhill.xlife.common.capability.stats.StatsCapabilityProvider;
- import net.minecraft.client.Minecraft;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.nbt.ListNBT;
- import net.minecraft.world.server.ServerWorld;
- import net.minecraft.world.storage.DimensionSavedDataManager;
- import net.minecraft.world.storage.WorldSavedData;
- import net.minecraftforge.event.world.WorldEvent;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- import java.util.function.Supplier;
- public class StatsManager extends WorldSavedData implements Supplier {
- public Map<UUID, DefaultStatsCapability> players = new HashMap<>();
- public PlayerEntity player = Minecraft.getInstance().player;
- public StatsManager() {
- super(XLife.MOD_ID);
- }
- public void load(CompoundNBT nbt) {
- players.put(player.getUUID(), StatsCapabilityProvider.stats);
- }
- public CompoundNBT save(CompoundNBT nbt) {
- ListNBT listNBT = new ListNBT();
- for (DefaultStatsCapability stats : players.values()) {
- stats.save(nbt);
- listNBT.add(nbt);
- }
- nbt.put("Players", listNBT);
- return nbt;
- }
- public static StatsManager onWorld(ServerWorld world) {
- DimensionSavedDataManager storage = world.getDataStorage();
- StatsManager sup = new StatsManager();
- StatsManager saver = (StatsManager) storage.computeIfAbsent(sup, XLife.MOD_ID);
- storage.set(saver);
- return saver;
- }
- public static void onSaved(WorldEvent.Save event) {
- if (!event.getWorld().isClientSide() && event.getWorld() instanceof ServerWorld) {
- StatsManager saver = StatsManager.onWorld((ServerWorld) event.getWorld());
- saver.setDirty();
- }
- }
- @Override
- public Object get() {
- return this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement