Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/data/BankData.java
T

120 lines
4.4 KiB
Java
Raw Normal View History

package noppes.npcs.controllers.data;
2018-09-13 12:03:28 -04:00
import kamkeel.npcs.network.PacketUtil;
2018-09-13 12:03:28 -04:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.NBTTags;
import noppes.npcs.NoppesUtilServer;
import noppes.npcs.NpcMiscInventory;
import noppes.npcs.constants.EnumGuiType;
import noppes.npcs.containers.ContainerNPCBankInterface;
import noppes.npcs.controllers.BankController;
2018-09-13 12:03:28 -04:00
import noppes.npcs.entity.EntityNPCInterface;
2023-01-12 21:33:06 -05:00
import java.util.HashMap;
2018-09-13 12:03:28 -04:00
public class BankData {
2024-02-15 17:54:28 -05:00
public HashMap<Integer,NpcMiscInventory> itemSlots;
public HashMap<Integer,Boolean> upgradedSlots;
2018-09-13 12:03:28 -04:00
public int unlockedSlots = 0;
public int bankId = -1;
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
public BankData(){
itemSlots = new HashMap<Integer,NpcMiscInventory>();
upgradedSlots = new HashMap<Integer,Boolean>();
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
for(int i = 0; i < 6; i++){
itemSlots.put(i, new NpcMiscInventory(54));
upgradedSlots.put(i, false);
}
}
public void readNBT(NBTTagCompound nbttagcompound) {
bankId = nbttagcompound.getInteger("DataBankId");
unlockedSlots = nbttagcompound.getInteger("UnlockedSlots");
itemSlots = getItemSlots(nbttagcompound.getTagList("BankInv", 10));
upgradedSlots = NBTTags.getBooleanList(nbttagcompound.getTagList("UpdatedSlots", 10));
}
private HashMap<Integer, NpcMiscInventory> getItemSlots(NBTTagList tagList) {
HashMap<Integer,NpcMiscInventory> list = new HashMap<Integer,NpcMiscInventory>();
for(int i = 0; i < tagList.tagCount(); i++)
{
NBTTagCompound nbttagcompound = tagList.getCompoundTagAt(i);
int slot = nbttagcompound.getInteger("Slot");
NpcMiscInventory inv = new NpcMiscInventory(54);
inv.setFromNBT(nbttagcompound.getCompoundTag("BankItems"));
list.put(slot, inv);
}
return list;
}
public void writeNBT(NBTTagCompound nbttagcompound) {
nbttagcompound.setInteger("DataBankId", bankId);
nbttagcompound.setInteger("UnlockedSlots", unlockedSlots);
nbttagcompound.setTag("UpdatedSlots", NBTTags.nbtBooleanList(upgradedSlots));
nbttagcompound.setTag("BankInv", nbtItemSlots(itemSlots));
}
private NBTTagList nbtItemSlots(HashMap<Integer, NpcMiscInventory> items) {
NBTTagList list = new NBTTagList();
for(int slot: items.keySet()){
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setInteger("Slot", slot);
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
nbttagcompound.setTag("BankItems",items.get(slot).getToNBT());
list.appendTag(nbttagcompound);
}
return list;
}
public boolean isUpgraded(Bank bank, int slot) {
if(bank.isUpgraded(slot))
return true;
return bank.canBeUpgraded(slot) && upgradedSlots.get(slot);
}
2024-02-15 17:54:28 -05:00
public void openBankGui(final EntityPlayer player, EntityNPCInterface npc, int bankId, int slot) {
final Bank bank = BankController.getInstance().getBank(bankId);
2018-09-13 12:03:28 -04:00
if(bank.getMaxSlots() <= slot)
return;
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
if(bank.startSlots > unlockedSlots)
unlockedSlots = bank.startSlots;
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
ItemStack currency = null;
if(unlockedSlots <= slot){
currency = bank.currencyInventory.getStackInSlot(slot);
NoppesUtilServer.sendOpenGui(player, EnumGuiType.PlayerBankUnlock, npc, slot, bank.id, 0);
}
else if(isUpgraded(bank,slot)){
NoppesUtilServer.sendOpenGui(player, EnumGuiType.PlayerBankLarge, npc, slot, bank.id, 0);
}
else if(bank.canBeUpgraded(slot)){
currency = bank.upgradeInventory.getStackInSlot(slot);
NoppesUtilServer.sendOpenGui(player, EnumGuiType.PlayerBankUprade, npc, slot, bank.id, 0);
}
else{
NoppesUtilServer.sendOpenGui(player, EnumGuiType.PlayerBankSmall, npc, slot, bank.id, 0);
}
final ItemStack item = currency;
2023-05-16 19:16:34 -04:00
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("MaxSlots", bank.getMaxSlots());
compound.setInteger("UnlockedSlots", unlockedSlots);
if (item != null) {
compound.setTag("Currency", NoppesUtilServer.writeItem(item, new NBTTagCompound()));
ContainerNPCBankInterface container = getContainer(player);
if (container != null)
container.setCurrency(item);
}
PacketUtil.sendGuiData((EntityPlayerMP) player, compound);
2018-09-13 12:03:28 -04:00
}
private ContainerNPCBankInterface getContainer(EntityPlayer player){
Container con = player.openContainer;
if(con == null || !(con instanceof ContainerNPCBankInterface))
return null;
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
return (ContainerNPCBankInterface) con;
}
}