2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.roles;
|
|
|
|
|
|
2022-05-26 15:57:24 -05:00
|
|
|
import foxz.utils.Market;
|
2018-09-13 12:03:28 -04:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2022-05-26 15:57:24 -05:00
|
|
|
import noppes.npcs.NBTTags;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.NoppesUtilPlayer;
|
|
|
|
|
import noppes.npcs.NoppesUtilServer;
|
|
|
|
|
import noppes.npcs.NpcMiscInventory;
|
|
|
|
|
import noppes.npcs.constants.EnumGuiType;
|
|
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
|
|
|
|
|
2023-01-12 21:33:06 -05:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public class RoleTrader extends RoleInterface{
|
|
|
|
|
|
|
|
|
|
public String marketName = "";
|
|
|
|
|
public NpcMiscInventory inventoryCurrency;
|
|
|
|
|
public NpcMiscInventory inventorySold;
|
|
|
|
|
|
|
|
|
|
public boolean ignoreDamage = false;
|
|
|
|
|
public boolean ignoreNBT = false;
|
|
|
|
|
|
|
|
|
|
public boolean toSave = false;
|
|
|
|
|
|
2022-05-24 03:06:16 -05:00
|
|
|
public int[] purchases;
|
|
|
|
|
public int[] disableSlot;
|
2022-05-26 15:57:24 -05:00
|
|
|
public HashMap<String, int[]> playerPurchases;
|
|
|
|
|
public HashMap<String, int[]> playerDisableSlot;
|
2022-05-24 03:06:16 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public RoleTrader(EntityNPCInterface npc) {
|
|
|
|
|
super(npc);
|
|
|
|
|
inventoryCurrency = new NpcMiscInventory(36);
|
|
|
|
|
inventorySold = new NpcMiscInventory(18);
|
2022-05-24 03:06:16 -05:00
|
|
|
purchases = new int[18];
|
|
|
|
|
disableSlot = new int[18];
|
2022-05-26 15:57:24 -05:00
|
|
|
playerPurchases = new HashMap<String, int[]>();
|
|
|
|
|
playerDisableSlot = new HashMap<String, int[]>();
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) {
|
|
|
|
|
nbttagcompound.setString("TraderMarket", marketName);
|
|
|
|
|
writeNBT(nbttagcompound);
|
|
|
|
|
if (toSave && !npc.isRemote()) {
|
|
|
|
|
Market.save(this, marketName);
|
|
|
|
|
}
|
|
|
|
|
toSave = false;
|
|
|
|
|
return nbttagcompound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound writeNBT(NBTTagCompound nbttagcompound) {
|
|
|
|
|
nbttagcompound.setTag("TraderCurrency", inventoryCurrency.getToNBT());
|
|
|
|
|
nbttagcompound.setTag("TraderSold", inventorySold.getToNBT());
|
|
|
|
|
nbttagcompound.setBoolean("TraderIgnoreDamage", ignoreDamage);
|
|
|
|
|
nbttagcompound.setBoolean("TraderIgnoreNBT", ignoreNBT);
|
2022-05-24 03:06:16 -05:00
|
|
|
nbttagcompound.setIntArray("Purchases", purchases);
|
|
|
|
|
nbttagcompound.setIntArray("DisableSlot", disableSlot);
|
2022-05-26 15:57:24 -05:00
|
|
|
nbttagcompound.setTag("PlayerPurchases", NBTTags.nbtStringIntegerArrayMap(playerPurchases));
|
|
|
|
|
nbttagcompound.setTag("PlayerDisableSlot", NBTTags.nbtStringIntegerArrayMap(playerDisableSlot));
|
2018-09-13 12:03:28 -04:00
|
|
|
return nbttagcompound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void readFromNBT(NBTTagCompound nbttagcompound) {
|
|
|
|
|
marketName = nbttagcompound.getString("TraderMarket");
|
|
|
|
|
readNBT(nbttagcompound);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Market.load(this, marketName);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readNBT(NBTTagCompound nbttagcompound) {
|
|
|
|
|
inventoryCurrency.setFromNBT(nbttagcompound.getCompoundTag("TraderCurrency"));
|
|
|
|
|
inventorySold.setFromNBT(nbttagcompound.getCompoundTag("TraderSold"));
|
|
|
|
|
ignoreDamage = nbttagcompound.getBoolean("TraderIgnoreDamage");
|
|
|
|
|
ignoreNBT = nbttagcompound.getBoolean("TraderIgnoreNBT");
|
2022-05-24 03:06:16 -05:00
|
|
|
purchases = nbttagcompound.getIntArray("Purchases");
|
2022-05-26 15:57:24 -05:00
|
|
|
if (purchases == null || purchases.length != 18) {
|
2022-05-24 03:06:16 -05:00
|
|
|
purchases = new int[18];
|
|
|
|
|
for (int i = 0; i < purchases.length; ++i) purchases[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
disableSlot = nbttagcompound.getIntArray("DisableSlot");
|
2022-05-26 15:57:24 -05:00
|
|
|
if (disableSlot == null || disableSlot.length != 18) {
|
2022-05-24 03:06:16 -05:00
|
|
|
disableSlot = new int[18];
|
|
|
|
|
for (int i = 0; i < disableSlot.length; ++i) disableSlot[i] = 0;
|
|
|
|
|
}
|
2022-05-26 15:57:24 -05:00
|
|
|
playerPurchases = NBTTags.getStringIntegerArrayMap(nbttagcompound.getTagList("PlayerPurchases", 10), 18);
|
|
|
|
|
playerDisableSlot = NBTTags.getStringIntegerArrayMap(nbttagcompound.getTagList("PlayerDisableSlot", 10), 18);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void interact(EntityPlayer player) {
|
|
|
|
|
npc.say(player, npc.advanced.getInteractLine());
|
|
|
|
|
try {
|
|
|
|
|
Market.load(this, marketName);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
|
|
|
|
|
}
|
|
|
|
|
NoppesUtilServer.sendOpenGui(player, EnumGuiType.PlayerTrader, npc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasCurrency(ItemStack itemstack) {
|
|
|
|
|
if(itemstack == null)
|
|
|
|
|
return false;
|
|
|
|
|
for(ItemStack item : inventoryCurrency.items.values()){
|
|
|
|
|
if(item != null && NoppesUtilPlayer.compareItems(item, itemstack, ignoreDamage, ignoreNBT)){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-05-26 15:57:24 -05:00
|
|
|
|
2022-06-06 14:38:33 -05:00
|
|
|
public void addPurchase(int slot, String playerName) {
|
2022-05-26 15:57:24 -05:00
|
|
|
if(slot >= 18 || slot < 0) return;
|
|
|
|
|
++purchases[slot];
|
2022-06-06 14:38:33 -05:00
|
|
|
++getArrayByName(playerName, playerPurchases)[slot];
|
2022-05-26 15:57:24 -05:00
|
|
|
}
|
|
|
|
|
|
2022-06-06 14:38:33 -05:00
|
|
|
public boolean isSlotEnabled(int slot, String playerName) {
|
2022-05-26 15:57:24 -05:00
|
|
|
if(slot >= 18 || slot < 0) return false;
|
|
|
|
|
if (disableSlot[slot] > 0) return false;
|
2022-06-12 04:23:05 -05:00
|
|
|
return getArrayByName(playerName, playerDisableSlot)[slot] <= 0;
|
2022-05-26 15:57:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int[] getArrayByName(String name, HashMap<String, int[]> map) {
|
2022-06-12 04:23:05 -05:00
|
|
|
map.computeIfAbsent(name, k -> new int[18]);
|
2022-06-06 14:38:33 -05:00
|
|
|
return map.get(name);
|
2022-05-26 15:57:24 -05:00
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
}
|