diff --git a/src/main/java/noppes/npcs/NBTTags.java b/src/main/java/noppes/npcs/NBTTags.java index 85b7eb08b..cb39bab95 100644 --- a/src/main/java/noppes/npcs/NBTTags.java +++ b/src/main/java/noppes/npcs/NBTTags.java @@ -131,6 +131,34 @@ public class NBTTags { } return list; } + + public static HashMap getStringIntegerArrayMap(NBTTagList tagList) { + HashMap list = new HashMap(); + for(int i = 0; i < tagList.tagCount(); i++) + { + NBTTagCompound nbttagcompound = tagList.getCompoundTagAt(i); + list.put(nbttagcompound.getString("Slot"), nbttagcompound.getIntArray("Value")); + } + return list; + } + + /** + * @param tagList + * @param arraySize if an int array is not this length a new int array with this length is made + * @return + */ + public static HashMap getStringIntegerArrayMap(NBTTagList tagList, int arrayLength) { + HashMap list = new HashMap(); + for(int i = 0; i < tagList.tagCount(); i++) + { + NBTTagCompound nbttagcompound = tagList.getCompoundTagAt(i); + int[] a = nbttagcompound.getIntArray("Value"); + if (a.length != arrayLength) a = new int[arrayLength]; + list.put(nbttagcompound.getString("Slot"), a); + } + return list; + } + public static HashMap> getVectorMap(NBTTagList tagList) { HashMap> map = new HashMap>(); for(int i = 0; i < tagList.tagCount(); i++) @@ -336,6 +364,21 @@ public class NBTTags { } return nbttaglist; } + + public static NBTTagList nbtStringIntegerArrayMap(HashMap map) { + NBTTagList nbttaglist = new NBTTagList(); + if(map == null) + return nbttaglist; + for(String slot : map.keySet()) + { + NBTTagCompound nbttagcompound = new NBTTagCompound(); + nbttagcompound.setString("Slot", slot); + nbttagcompound.setIntArray("Value", map.get(slot)); + + nbttaglist.appendTag(nbttagcompound); + } + return nbttaglist; + } public static NBTBase nbtIntegerStringMap(HashMap map) { NBTTagList nbttaglist = new NBTTagList(); diff --git a/src/main/java/noppes/npcs/containers/ContainerNPCTrader.java b/src/main/java/noppes/npcs/containers/ContainerNPCTrader.java index 1165fea6f..f0390478e 100644 --- a/src/main/java/noppes/npcs/containers/ContainerNPCTrader.java +++ b/src/main/java/noppes/npcs/containers/ContainerNPCTrader.java @@ -68,12 +68,12 @@ public class ContainerNPCTrader extends ContainerNpcInterface{ NoppesUtilPlayer.consumeItem(entityplayer, role.inventoryCurrency.getStackInSlot(i + 18), role.ignoreDamage, role.ignoreNBT); ItemStack soldItem = item.copy(); givePlayer(soldItem, entityplayer); - ++role.purchases[i]; + role.addPurchase(i, entityplayer); return soldItem; } public boolean isSlotEnabled(int slot, EntityPlayer player) { - return role.disableSlot[slot] == 0; + return role.isSlotEnabled(slot, player); } public boolean canBuy(int slot, EntityPlayer player) { ItemStack currency = role.inventoryCurrency.getStackInSlot(slot); diff --git a/src/main/java/noppes/npcs/roles/RoleTrader.java b/src/main/java/noppes/npcs/roles/RoleTrader.java index b6f21eda6..3c918dc57 100644 --- a/src/main/java/noppes/npcs/roles/RoleTrader.java +++ b/src/main/java/noppes/npcs/roles/RoleTrader.java @@ -1,17 +1,19 @@ package noppes.npcs.roles; +import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; +import foxz.utils.Market; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.NBTTags; import noppes.npcs.NoppesUtilPlayer; import noppes.npcs.NoppesUtilServer; import noppes.npcs.NpcMiscInventory; import noppes.npcs.constants.EnumGuiType; import noppes.npcs.entity.EntityNPCInterface; -import foxz.utils.Market; public class RoleTrader extends RoleInterface{ @@ -26,6 +28,8 @@ public class RoleTrader extends RoleInterface{ public int[] purchases; public int[] disableSlot; + public HashMap playerPurchases; + public HashMap playerDisableSlot; public RoleTrader(EntityNPCInterface npc) { super(npc); @@ -33,6 +37,8 @@ public class RoleTrader extends RoleInterface{ inventorySold = new NpcMiscInventory(18); purchases = new int[18]; disableSlot = new int[18]; + playerPurchases = new HashMap(); + playerDisableSlot = new HashMap(); } @Override @@ -53,6 +59,8 @@ public class RoleTrader extends RoleInterface{ nbttagcompound.setBoolean("TraderIgnoreNBT", ignoreNBT); nbttagcompound.setIntArray("Purchases", purchases); nbttagcompound.setIntArray("DisableSlot", disableSlot); + nbttagcompound.setTag("PlayerPurchases", NBTTags.nbtStringIntegerArrayMap(playerPurchases)); + nbttagcompound.setTag("PlayerDisableSlot", NBTTags.nbtStringIntegerArrayMap(playerDisableSlot)); return nbttagcompound; } @@ -75,15 +83,17 @@ public class RoleTrader extends RoleInterface{ ignoreDamage = nbttagcompound.getBoolean("TraderIgnoreDamage"); ignoreNBT = nbttagcompound.getBoolean("TraderIgnoreNBT"); purchases = nbttagcompound.getIntArray("Purchases"); - if (purchases == null || purchases.length < 18) { + if (purchases == null || purchases.length != 18) { purchases = new int[18]; for (int i = 0; i < purchases.length; ++i) purchases[i] = 0; } disableSlot = nbttagcompound.getIntArray("DisableSlot"); - if (disableSlot == null || disableSlot.length < 18) { + if (disableSlot == null || disableSlot.length != 18) { disableSlot = new int[18]; for (int i = 0; i < disableSlot.length; ++i) disableSlot[i] = 0; } + playerPurchases = NBTTags.getStringIntegerArrayMap(nbttagcompound.getTagList("PlayerPurchases", 10), 18); + playerDisableSlot = NBTTags.getStringIntegerArrayMap(nbttagcompound.getTagList("PlayerDisableSlot", 10), 18); } @Override @@ -107,5 +117,24 @@ public class RoleTrader extends RoleInterface{ } return false; } + + public void addPurchase(int slot, EntityPlayer player) { + if(slot >= 18 || slot < 0) return; + ++purchases[slot]; + ++getArrayByName(player.getDisplayName(), playerPurchases)[slot]; + } + + public boolean isSlotEnabled(int slot, EntityPlayer player) { + if(slot >= 18 || slot < 0) return false; + if (disableSlot[slot] > 0) return false; + if (getArrayByName(player.getDisplayName(), playerDisableSlot)[slot] > 0) return false; + return true; + } + + public int[] getArrayByName(String name, HashMap map) { + int[] a = map.get(name); + if (a == null) a = map.put(name, new int[18]); + return a; + } } diff --git a/src/main/java/noppes/npcs/scripted/roles/ScriptRoleTrader.java b/src/main/java/noppes/npcs/scripted/roles/ScriptRoleTrader.java index 0e496569c..5ea82236f 100644 --- a/src/main/java/noppes/npcs/scripted/roles/ScriptRoleTrader.java +++ b/src/main/java/noppes/npcs/scripted/roles/ScriptRoleTrader.java @@ -1,10 +1,12 @@ package noppes.npcs.scripted.roles; import foxz.utils.Market; +import net.minecraft.entity.player.EntityPlayer; import noppes.npcs.entity.EntityNPCInterface; import noppes.npcs.roles.RoleTrader; import noppes.npcs.scripted.ScriptItemStack; import noppes.npcs.scripted.constants.RoleType; +import noppes.npcs.scripted.interfaces.IItemStack; import noppes.npcs.scripted.interfaces.IPlayer; public class ScriptRoleTrader extends ScriptRoleInterface{ @@ -20,7 +22,7 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ * @param currency2 Currency item number two * @param sold Item to be sold by this npc */ - public void setSellOption(int slot, ScriptItemStack currency, ScriptItemStack currency2, ScriptItemStack sold){ + public void setSellOption(int slot, IItemStack currency, IItemStack currency2, IItemStack sold){ if(sold == null || slot >= 18 || slot < 0) return; if(currency == null) @@ -43,7 +45,7 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ * @param currency Currency item * @param sold Item to be sold by this npc */ - public void setSellOption(int slot, ScriptItemStack currency, ScriptItemStack sold){ + public void setSellOption(int slot, IItemStack currency, IItemStack sold){ setSellOption(slot, currency, null, sold); } @@ -51,7 +53,7 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ * @param slot * @return The item being sold in this slot. */ - public ScriptItemStack getSellOption(int slot) { + public IItemStack getSellOption(int slot) { if (slot >= 18 || slot < 0) return null; if (role.inventorySold.items.get(slot) == null) return null; return new ScriptItemStack(role.inventorySold.items.get(slot)); @@ -61,7 +63,7 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ * @param slot * @return a ScriptItemStack array of size 2 which contains the currency of this trade */ - public ScriptItemStack[] getCurrency(int slot) { + public IItemStack[] getCurrency(int slot) { if (slot >= 18 || slot < 0) return null; ScriptItemStack[] currency = new ScriptItemStack[2]; if (role.inventoryCurrency.items.get(slot) != null) { @@ -77,8 +79,7 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ * @param slot Slot number 0-17 */ public void removeSellOption(int slot){ - if(slot >= 18 || slot < 0) - return; + if(slot >= 18 || slot < 0) return; role.inventoryCurrency.items.remove(slot); role.inventoryCurrency.items.remove(slot + 18); role.inventorySold.items.remove(slot); @@ -107,9 +108,14 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ return role.purchases[slot]; } + /** + * @param slot + * @param player + * @return the number of times this player has purchased from this trader + */ public int getPurchaseNum(int slot, IPlayer player) { - // TODO get purchase num by player - return -1; + if(slot >= 18 || slot < 0) return -1; + return role.getArrayByName(player.getDisplayName(), role.playerPurchases)[slot]; } /** @@ -124,9 +130,39 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ * @param slot */ public void resetPurchaseNum(int slot) { + if(slot >= 18 || slot < 0) return; role.purchases[slot] = 0; } + /** + * sets the purchase num for that slot and player to 0 + * @param slot + * @param player + */ + public void resetPurchaseNum(int slot, IPlayer player) { + if(slot >= 18 || slot < 0) return; + role.getArrayByName(player.getDisplayName(), role.playerPurchases)[slot] = 0; + } + + /** + * @param slot + * @return if this slot is enabled + */ + public boolean isSlotEnabled(int slot) { + if(slot >= 18 || slot < 0) return false; + return role.disableSlot[slot] > 0; + } + + /** + * @param slot + * @param player + * @return if this slot is enabled for this player + */ + public boolean isSlotEnabled(int slot, IPlayer player) { + if(slot >= 18 || slot < 0) return false; + return role.isSlotEnabled(slot, (EntityPlayer)player.getMCEntity()); + } + /** * prevent an item from being sold on that slot * @param slot @@ -136,6 +172,16 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ role.disableSlot[slot] = 1; } + /** + * disables the slot for this player + * @param slot + * @param player + */ + public void disableSlot(int slot, IPlayer player) { + if(slot >= 18 || slot < 0) return; + role.getArrayByName(player.getDisplayName(), role.playerDisableSlot)[slot] = 1; + } + /** * allow an item to be sold on that slot * @param slot @@ -144,6 +190,16 @@ public class ScriptRoleTrader extends ScriptRoleInterface{ if(slot >= 18 || slot < 0) return; role.disableSlot[slot] = 0; } + + /** + * enables the slot for this player + * @param slot + * @param player + */ + public void enableSlot(int slot, IPlayer player) { + if(slot >= 18 || slot < 0) return; + role.getArrayByName(player.getDisplayName(), role.playerDisableSlot)[slot] = 0; + } @Override public int getType(){