Advertisement
jayhillx

StatsManager

Oct 15th, 2021
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. package com.jayhill.xlife.common.world.stats;
  2.  
  3. import com.jayhill.xlife.XLife;
  4. import com.jayhill.xlife.common.capability.stats.DefaultStatsCapability;
  5. import com.jayhill.xlife.common.capability.stats.StatsCapability;
  6. import com.jayhill.xlife.common.capability.stats.StatsCapabilityProvider;
  7. import net.minecraft.client.Minecraft;
  8. import net.minecraft.entity.player.PlayerEntity;
  9. import net.minecraft.nbt.CompoundNBT;
  10. import net.minecraft.nbt.ListNBT;
  11. import net.minecraft.world.server.ServerWorld;
  12. import net.minecraft.world.storage.DimensionSavedDataManager;
  13. import net.minecraft.world.storage.WorldSavedData;
  14. import net.minecraftforge.event.world.WorldEvent;
  15.  
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.UUID;
  19. import java.util.function.Supplier;
  20.  
  21. public class StatsManager extends WorldSavedData implements Supplier {
  22.     public Map<UUID, DefaultStatsCapability> players = new HashMap<>();
  23.     public PlayerEntity player = Minecraft.getInstance().player;
  24.  
  25.     public StatsManager() {
  26.         super(XLife.MOD_ID);
  27.     }
  28.  
  29.     public void load(CompoundNBT nbt) {
  30.         players.put(player.getUUID(), StatsCapabilityProvider.stats);
  31.     }
  32.  
  33.     public CompoundNBT save(CompoundNBT nbt) {
  34.         ListNBT listNBT = new ListNBT();
  35.  
  36.         for (DefaultStatsCapability stats : players.values()) {
  37.             stats.save(nbt);
  38.             listNBT.add(nbt);
  39.         }
  40.  
  41.         nbt.put("Players", listNBT);
  42.         return nbt;
  43.     }
  44.  
  45.     public static StatsManager onWorld(ServerWorld world) {
  46.         DimensionSavedDataManager storage = world.getDataStorage();
  47.         StatsManager sup = new StatsManager();
  48.         StatsManager saver = (StatsManager) storage.computeIfAbsent(sup, XLife.MOD_ID);
  49.  
  50.         storage.set(saver);
  51.  
  52.         return saver;
  53.     }
  54.  
  55.     public static void onSaved(WorldEvent.Save event) {
  56.         if (!event.getWorld().isClientSide() && event.getWorld() instanceof ServerWorld) {
  57.             StatsManager saver = StatsManager.onWorld((ServerWorld) event.getWorld());
  58.  
  59.             saver.setDirty();
  60.         }
  61.     }
  62.  
  63.     @Override
  64.     public Object get() {
  65.         return this;
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement