package kamkeel.npcs.controllers.data; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import noppes.npcs.NoppesUtilServer; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.handler.data.IProfile; import noppes.npcs.api.handler.data.ISlot; import java.util.HashMap; import java.util.Map; import java.util.Set; public class Profile implements IProfile { public EntityPlayer player; public int currentSlotId; private Map slots = new HashMap<>(); private boolean locked = false; public Profile(EntityPlayer player, NBTTagCompound compound) { this.player = player; if(compound.hasKey("CurrentSlotId")){ this.currentSlotId = compound.getInteger("CurrentSlotId"); } else { this.currentSlotId = 0; } if(compound.hasKey("Slots")){ NBTTagCompound slotsCompound = compound.getCompoundTag("Slots"); Set keys = slotsCompound.func_150296_c(); for(String key : keys) { try { int slotId = Integer.parseInt(key); NBTTagCompound slotNBT = slotsCompound.getCompoundTag(key); ISlot slot = Slot.fromNBT(slotId, slotNBT); slots.put(slotId, slot); } catch (NumberFormatException e) { // Skip keys that are not valid slot IDs. } } } } public Profile(EntityPlayer player) { this.player = player; this.currentSlotId = 0; // Create a default slot. Slot defaultSlot = new Slot(0, "Default Slot"); defaultSlot.setLastLoaded(System.currentTimeMillis()); slots.put(0, defaultSlot); } @Override public IPlayer getPlayer() { return NoppesUtilServer.getIPlayer(player); } @Override public int getCurrentSlotId() { return currentSlotId; } @Override public Map getSlots() { return slots; } public boolean isLocked() { return locked; } public void setLocked(boolean locked) { this.locked = locked; } @Override public NBTTagCompound writeToNBT() { NBTTagCompound compound = new NBTTagCompound(); if(player != null) compound.setString("Name", player.getCommandSenderName()); compound.setInteger("CurrentSlotId", currentSlotId); NBTTagCompound slotsCompound = new NBTTagCompound(); for(Map.Entry entry : slots.entrySet()) { // Save only non-temporary slots. if(entry.getValue().isTemporary()) continue; slotsCompound.setTag(String.valueOf(entry.getKey()), entry.getValue().toNBT()); } compound.setTag("Slots", slotsCompound); return compound; } }