mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-28 10:06:00 +00:00
Auction System Implementation
This commit is contained in:
1 parent
9cbd9fd3eb
commit
5afa1c8959
60 files changed
+6187
-589
No files matched your search
@@ -0,0 +1,125 @@
|
||||
package noppes.npcs.containers;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.InventoryBasic;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import noppes.npcs.NoppesUtilPlayer;
|
||||
import noppes.npcs.entity.EntityNPCInterface;
|
||||
|
||||
/**
|
||||
* Container for creating auction listings with a single sell slot.
|
||||
* Items are cloned from inventory, not moved. Validation happens on submit.
|
||||
*/
|
||||
public class ContainerAuctionSell extends ContainerAuction {
|
||||
public static final int SELL_SLOT_X = 101;
|
||||
public static final int SELL_SLOT_Y = 106;
|
||||
public static final int SELL_SLOT_INDEX = PLAYER_INV_SLOT_COUNT;
|
||||
private static final int MAX_STACK = 64;
|
||||
|
||||
private final IInventory sellInventory;
|
||||
|
||||
public ContainerAuctionSell(EntityNPCInterface npc, EntityPlayer player) {
|
||||
super(npc, player);
|
||||
sellInventory = new InventoryBasic("Sell", false, 1);
|
||||
|
||||
// Sell slot blocks direct interaction
|
||||
addSlotToContainer(new Slot(sellInventory, 0, SELL_SLOT_X, SELL_SLOT_Y) {
|
||||
@Override
|
||||
public boolean isItemValid(ItemStack stack) { return false; }
|
||||
@Override
|
||||
public boolean canTakeStack(EntityPlayer player) { return false; }
|
||||
});
|
||||
}
|
||||
|
||||
public ItemStack getItemToSell() {
|
||||
return sellInventory.getStackInSlot(0);
|
||||
}
|
||||
|
||||
public void clearSellSlot() {
|
||||
sellInventory.setInventorySlotContents(0, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add items from inventory to sell slot.
|
||||
* Not limited by inventory - just stages items for sale. Validation on submit.
|
||||
* @param sourceSlot Inventory slot index
|
||||
* @param fullStack True=full stack, False=1 item
|
||||
*/
|
||||
public void addToSellSlot(int sourceSlot, boolean fullStack) {
|
||||
if (!isPlayerInventorySlot(sourceSlot)) return;
|
||||
|
||||
ItemStack source = getPlayerInventoryStack(sourceSlot);
|
||||
if (source == null) return;
|
||||
|
||||
ItemStack sellStack = sellInventory.getStackInSlot(0);
|
||||
int toAdd = fullStack ? source.stackSize : 1;
|
||||
|
||||
if (sellStack == null) {
|
||||
// Empty slot - create new stack
|
||||
ItemStack newStack = source.copy();
|
||||
newStack.stackSize = Math.min(toAdd, MAX_STACK);
|
||||
sellInventory.setInventorySlotContents(0, newStack);
|
||||
} else if (itemsMatch(sellStack, source)) {
|
||||
// Same item - add to existing (capped at 64)
|
||||
int space = MAX_STACK - sellStack.stackSize;
|
||||
int add = Math.min(toAdd, space);
|
||||
if (add > 0) {
|
||||
sellStack.stackSize += add;
|
||||
}
|
||||
} else {
|
||||
// Different item - replace
|
||||
ItemStack newStack = source.copy();
|
||||
newStack.stackSize = Math.min(toAdd, MAX_STACK);
|
||||
sellInventory.setInventorySlotContents(0, newStack);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove items from sell slot.
|
||||
* @param removeAll True=clear slot, False=remove 1 item
|
||||
*/
|
||||
public void removeFromSellSlot(boolean removeAll) {
|
||||
ItemStack sellStack = sellInventory.getStackInSlot(0);
|
||||
if (sellStack == null) return;
|
||||
|
||||
if (removeAll || sellStack.stackSize <= 1) {
|
||||
sellInventory.setInventorySlotContents(0, null);
|
||||
} else {
|
||||
sellStack.stackSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/** Check if sell slot is this container slot index */
|
||||
public boolean isSellSlot(int slotIndex) {
|
||||
return slotIndex == SELL_SLOT_INDEX;
|
||||
}
|
||||
|
||||
/** Check if two stacks match using NoppesUtilPlayer.compareItems (same item, damage, and NBT) */
|
||||
private boolean itemsMatch(ItemStack a, ItemStack b) {
|
||||
return NoppesUtilPlayer.compareItems(a, b, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count total items of a specific type across player inventory.
|
||||
* Used for submit validation and UI indicator.
|
||||
*/
|
||||
public int countItemInInventory(ItemStack target) {
|
||||
if (target == null) return 0;
|
||||
int total = 0;
|
||||
for (int i = 0; i < PLAYER_INV_SLOT_COUNT; i++) {
|
||||
ItemStack stack = getPlayerInventoryStack(i);
|
||||
if (stack != null && itemsMatch(stack, target)) {
|
||||
total += stack.stackSize;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed(EntityPlayer player) {
|
||||
super.onContainerClosed(player);
|
||||
clearSellSlot();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user