mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-22 06:12:19 +00:00
Trader System + Stocks Implemented
- Removed Auction System Temporarily for Full Rebase
This commit is contained in:
1 parent
692b985237
commit
d0dc8a4578
61 files changed
+2595
-6897
No files matched your search
@@ -1,45 +1,71 @@
|
||||
package noppes.npcs.containers;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import noppes.npcs.NoppesUtilPlayer;
|
||||
import noppes.npcs.controllers.CurrencyController;
|
||||
import noppes.npcs.controllers.data.PlayerData;
|
||||
import noppes.npcs.entity.EntityNPCInterface;
|
||||
import noppes.npcs.roles.RoleTrader;
|
||||
|
||||
|
||||
public class ContainerNPCTrader extends ContainerNpcInterface {
|
||||
public RoleTrader role;
|
||||
private final EntityNPCInterface npc;
|
||||
|
||||
// Layout constants for 256px wide GUI
|
||||
public static final int COLUMN_WIDTH = 80;
|
||||
public static final int COLUMN_START_X = 8;
|
||||
public static final int ROW_HEIGHT = 22;
|
||||
public static final int ROW_START_Y = 6;
|
||||
|
||||
// Slot offsets within each column (for 18x18 slots, items render centered)
|
||||
public static final int CURRENCY1_OFFSET = 2; // Primary currency slot
|
||||
public static final int CURRENCY2_OFFSET = 20; // Secondary currency slot
|
||||
public static final int OUTPUT_OFFSET = 50; // Output slot
|
||||
|
||||
public ContainerNPCTrader(EntityNPCInterface npc, EntityPlayer player) {
|
||||
super(player);
|
||||
this.npc = npc;
|
||||
role = (RoleTrader) npc.roleInterface;
|
||||
|
||||
for (int i = 0; i < 18; i++) {
|
||||
int x = 53;
|
||||
x += i % 3 * 72;
|
||||
int y = 7;
|
||||
y += i / 3 * 21;
|
||||
// Register this player as viewing the trader (server-side only)
|
||||
// Needed for shared stock (not per-player) to sync updates to other viewers
|
||||
// Works for both linked markets (MarketRegistry) and normal traders (local viewers)
|
||||
if (player instanceof EntityPlayerMP && !role.stock.perPlayer) {
|
||||
role.registerViewer((EntityPlayerMP) player);
|
||||
}
|
||||
|
||||
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));
|
||||
// Trade slots: 3 columns x 6 rows, 80px column width
|
||||
// Layout per column: [Slot1][Slot2] = [Output]
|
||||
for (int i = 0; i < 18; i++) {
|
||||
int col = i % 3;
|
||||
int row = i / 3;
|
||||
int x = COLUMN_START_X + col * COLUMN_WIDTH;
|
||||
int y = ROW_START_Y + row * ROW_HEIGHT;
|
||||
// Output slot position (slot texture is 18x18, item renders centered at +1)
|
||||
addSlotToContainer(new Slot(role.inventorySold, i, x + OUTPUT_OFFSET + 1, y + 1));
|
||||
}
|
||||
|
||||
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));
|
||||
addSlotToContainer(new Slot(player.inventory, l1 + i1 * 9 + 9, 48 + l1 * 18, 137 + i1 * 18));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Player hotbar
|
||||
for (int j1 = 0; j1 < 9; j1++) {
|
||||
addSlotToContainer(new Slot(player.inventory, j1, 32 + j1 * 18, 198));
|
||||
addSlotToContainer(new Slot(player.inventory, j1, 48 + j1 * 18, 195));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContainerClosed(EntityPlayer player) {
|
||||
super.onContainerClosed(player);
|
||||
// Unregister this player from viewer tracking (only if registered for shared stock)
|
||||
if (player instanceof EntityPlayerMP && !role.stock.perPlayer) {
|
||||
role.unregisterViewer((EntityPlayerMP) player);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,25 +97,27 @@ public class ContainerNPCTrader extends ContainerNpcInterface {
|
||||
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
|
||||
// Item-based currency
|
||||
if (!canBuy(i, entityplayer))
|
||||
return null;
|
||||
|
||||
// Currency cost check (additive to item costs)
|
||||
long currencyCost = role.getCurrencyCost(i);
|
||||
if (currencyCost > 0) {
|
||||
PlayerData data = PlayerData.get(entityplayer);
|
||||
if (data.currencyData.getBalance() < currencyCost) {
|
||||
return null; // Can't afford currency cost
|
||||
}
|
||||
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 item currency
|
||||
NoppesUtilPlayer.consumeItem(entityplayer, role.inventoryCurrency.getStackInSlot(i), role.ignoreDamage, role.ignoreNBT);
|
||||
NoppesUtilPlayer.consumeItem(entityplayer, role.inventoryCurrency.getStackInSlot(i + 18), role.ignoreDamage, role.ignoreNBT);
|
||||
|
||||
// Withdraw currency cost (after item consumption to maintain order)
|
||||
if (currencyCost > 0) {
|
||||
PlayerData data = PlayerData.get(entityplayer);
|
||||
data.currencyData.withdraw(currencyCost);
|
||||
}
|
||||
|
||||
// Consume stock
|
||||
@@ -98,6 +126,18 @@ public class ContainerNPCTrader extends ContainerNpcInterface {
|
||||
ItemStack soldItem = item.copy();
|
||||
givePlayer(soldItem, entityplayer);
|
||||
role.addPurchase(i, entityplayer.getDisplayName());
|
||||
|
||||
// Sync updated data after purchase
|
||||
// For per-player stocks: sync only to the purchasing player
|
||||
// For shared stocks: consumeStock() handles sync (market-wide or local)
|
||||
if (entityplayer instanceof EntityPlayerMP) {
|
||||
if (role.stock.perPlayer) {
|
||||
// Per-player: only sync to purchasing player (their own stock/balance)
|
||||
role.syncToPlayer((EntityPlayerMP) entityplayer);
|
||||
}
|
||||
// Shared stock sync is already handled in consumeStock()
|
||||
}
|
||||
|
||||
return soldItem;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user