2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.containers;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.inventory.Slot;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import noppes.npcs.NoppesUtilPlayer;
|
2026-01-04 08:29:03 +00:00
|
|
|
import noppes.npcs.controllers.CurrencyController;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
|
|
|
|
import noppes.npcs.roles.RoleTrader;
|
|
|
|
|
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public class ContainerNPCTrader extends ContainerNpcInterface {
|
|
|
|
|
public RoleTrader role;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public ContainerNPCTrader(EntityNPCInterface npc, EntityPlayer player) {
|
|
|
|
|
super(player);
|
2018-09-13 12:03:28 -04:00
|
|
|
role = (RoleTrader) npc.roleInterface;
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (int i = 0; i < 18; i++) {
|
|
|
|
|
int x = 53;
|
|
|
|
|
x += i % 3 * 72;
|
|
|
|
|
int y = 7;
|
|
|
|
|
y += i / 3 * 21;
|
|
|
|
|
|
|
|
|
|
ItemStack item = role.inventoryCurrency.items.get(i);
|
|
|
|
|
ItemStack item2 = role.inventoryCurrency.items.get(i + 18);
|
|
|
|
|
if (item == null) {
|
|
|
|
|
item = item2;
|
|
|
|
|
item2 = null;
|
|
|
|
|
}
|
|
|
|
|
addSlotToContainer(new Slot(role.inventorySold, i, x, y));
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (int i1 = 0; i1 < 3; i1++) {
|
|
|
|
|
for (int l1 = 0; l1 < 9; l1++) {
|
|
|
|
|
addSlotToContainer(new Slot(player.inventory, l1 + i1 * 9 + 9, 32 + l1 * 18, 140 + i1 * 18));
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (int j1 = 0; j1 < 9; j1++) {
|
|
|
|
|
addSlotToContainer(new Slot(player.inventory, j1, 32 + j1 * 18, 198));
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-03-13 17:14:45 -04:00
|
|
|
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int i) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
@Override
|
2025-03-13 17:14:45 -04:00
|
|
|
public ItemStack slotClick(int i, int j, int par3, EntityPlayer entityplayer) {
|
|
|
|
|
if (par3 == 6)
|
|
|
|
|
par3 = 0;
|
|
|
|
|
if (i < 0 || i >= 18)
|
|
|
|
|
return super.slotClick(i, j, par3, entityplayer);
|
|
|
|
|
if (j == 1)
|
|
|
|
|
return null;
|
|
|
|
|
Slot slot = (Slot) inventorySlots.get(i);
|
|
|
|
|
if (slot == null || slot.getStack() == null)
|
|
|
|
|
return null;
|
2018-09-13 12:03:28 -04:00
|
|
|
ItemStack item = slot.getStack();
|
2025-03-13 17:14:45 -04:00
|
|
|
if (!canGivePlayer(item, entityplayer))
|
|
|
|
|
return null;
|
|
|
|
|
if (!isSlotEnabled(i, entityplayer))
|
|
|
|
|
return null;
|
2026-01-04 08:29:03 +00:00
|
|
|
|
|
|
|
|
// Check stock availability
|
|
|
|
|
String playerName = entityplayer.getCommandSenderName();
|
|
|
|
|
if (!role.hasStock(i, playerName, 1)) {
|
|
|
|
|
return null; // Out of stock
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle currency-based purchase
|
|
|
|
|
if (role.hasSlotPrice(i)) {
|
|
|
|
|
long price = role.getSlotPrice(i);
|
|
|
|
|
if (CurrencyController.Instance == null) {
|
|
|
|
|
return null; // Currency system not available
|
|
|
|
|
}
|
|
|
|
|
if (!CurrencyController.Instance.canAfford(entityplayer, price)) {
|
|
|
|
|
return null; // Can't afford
|
|
|
|
|
}
|
|
|
|
|
// Withdraw currency
|
|
|
|
|
if (!CurrencyController.Instance.withdraw(entityplayer, price)) {
|
|
|
|
|
return null; // Withdrawal failed
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Item-based currency - existing logic
|
|
|
|
|
if (!canBuy(i, entityplayer))
|
|
|
|
|
return null;
|
|
|
|
|
NoppesUtilPlayer.consumeItem(entityplayer, role.inventoryCurrency.getStackInSlot(i), role.ignoreDamage, role.ignoreNBT);
|
|
|
|
|
NoppesUtilPlayer.consumeItem(entityplayer, role.inventoryCurrency.getStackInSlot(i + 18), role.ignoreDamage, role.ignoreNBT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Consume stock
|
|
|
|
|
role.consumeStock(i, playerName, 1);
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
ItemStack soldItem = item.copy();
|
|
|
|
|
givePlayer(soldItem, entityplayer);
|
2022-06-06 14:38:33 -05:00
|
|
|
role.addPurchase(i, entityplayer.getDisplayName());
|
2018-09-13 12:03:28 -04:00
|
|
|
return soldItem;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2022-05-26 01:43:17 -05:00
|
|
|
public boolean isSlotEnabled(int slot, EntityPlayer player) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return role.isSlotEnabled(slot, player.getDisplayName());
|
2022-05-26 01:43:17 -05:00
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public boolean canBuy(int slot, EntityPlayer player) {
|
2025-03-13 17:14:45 -04:00
|
|
|
ItemStack currency = role.inventoryCurrency.getStackInSlot(slot);
|
|
|
|
|
ItemStack currency2 = role.inventoryCurrency.getStackInSlot(slot + 18);
|
|
|
|
|
if (currency == null && currency2 == null)
|
|
|
|
|
return true;
|
|
|
|
|
if (currency == null) {
|
|
|
|
|
currency = currency2;
|
|
|
|
|
currency2 = null;
|
|
|
|
|
}
|
|
|
|
|
if (NoppesUtilPlayer.compareItems(currency, currency2, role.ignoreDamage, role.ignoreNBT)) {
|
|
|
|
|
currency = currency.copy();
|
|
|
|
|
currency.stackSize += currency2.stackSize;
|
|
|
|
|
currency2 = null;
|
|
|
|
|
}
|
|
|
|
|
if (currency2 == null)
|
|
|
|
|
return NoppesUtilPlayer.compareItems(player, currency, role.ignoreDamage, role.ignoreNBT);
|
|
|
|
|
return NoppesUtilPlayer.compareItems(player, currency, role.ignoreDamage, role.ignoreNBT) && NoppesUtilPlayer.compareItems(player, currency2, role.ignoreDamage, role.ignoreNBT);
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
private boolean canGivePlayer(ItemStack item, EntityPlayer entityplayer) {//check Item being held with the mouse
|
2018-09-13 12:03:28 -04:00
|
|
|
ItemStack itemstack3 = entityplayer.inventory.getItemStack();
|
2025-03-13 17:14:45 -04:00
|
|
|
if (itemstack3 == null) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (NoppesUtilPlayer.compareItems(itemstack3, item, false, false)) {
|
2018-09-13 12:03:28 -04:00
|
|
|
int k1 = item.stackSize;
|
2025-03-13 17:14:45 -04:00
|
|
|
if (k1 > 0 && k1 + itemstack3.stackSize <= itemstack3.getMaxStackSize()) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
|
|
|
|
|
private void givePlayer(ItemStack item, EntityPlayer entityplayer) {//set item bought to the held mouse item
|
2018-09-13 12:03:28 -04:00
|
|
|
ItemStack itemstack3 = entityplayer.inventory.getItemStack();
|
2025-03-13 17:14:45 -04:00
|
|
|
if (itemstack3 == null) {
|
|
|
|
|
entityplayer.inventory.setItemStack(item);
|
|
|
|
|
} else if (NoppesUtilPlayer.compareItems(itemstack3, item, false, false)) {
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
int k1 = item.stackSize;
|
2025-03-13 17:14:45 -04:00
|
|
|
if (k1 > 0 && k1 + itemstack3.stackSize <= itemstack3.getMaxStackSize()) {
|
2018-09-13 12:03:28 -04:00
|
|
|
itemstack3.stackSize += k1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|