add get reset disable purchase num by slot number and IPlayer

needed to add get/setStringIntegerArrayMap to NBTTags so save the player purchases and disabled slots with a String int[] hashmap
This commit is contained in:
1whohears committed 2022-05-26 15:57:24 -05:00
1 parent e7250158ac
commit 3cf780d9a2
4 files changed
+141 -13

No files matched your search

+43
View File
@@ -131,6 +131,34 @@ public class NBTTags {
}
return list;
}
public static HashMap<String, int[]> getStringIntegerArrayMap(NBTTagList tagList) {
HashMap<String, int[]> list = new HashMap<String, int[]>();
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<String, int[]> getStringIntegerArrayMap(NBTTagList tagList, int arrayLength) {
HashMap<String, int[]> list = new HashMap<String, int[]>();
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<String, Vector<String>> getVectorMap(NBTTagList tagList) {
HashMap<String, Vector<String>> map = new HashMap<String, Vector<String>>();
for(int i = 0; i < tagList.tagCount(); i++)
@@ -336,6 +364,21 @@ public class NBTTags {
}
return nbttaglist;
}
public static NBTTagList nbtStringIntegerArrayMap(HashMap<String, int[]> 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<Integer, String> map) {
NBTTagList nbttaglist = new NBTTagList();
@@ -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);
@@ -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<String, int[]> playerPurchases;
public HashMap<String, int[]> 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<String, int[]>();
playerDisableSlot = new HashMap<String, int[]>();
}
@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<String, int[]> map) {
int[] a = map.get(name);
if (a == null) a = map.put(name, new int[18]);
return a;
}
}
@@ -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(){