2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.containers;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
2023-01-12 21:33:06 -05:00
|
|
|
import net.minecraft.inventory.*;
|
2018-09-13 12:03:28 -04:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.network.play.server.S2FPacketSetSlot;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import noppes.npcs.CustomItems;
|
2025-07-09 15:40:31 -04:00
|
|
|
import noppes.npcs.EventHooks;
|
2025-07-09 18:17:32 -04:00
|
|
|
import noppes.npcs.scripted.event.RecipeScriptEvent;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.controllers.RecipeController;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.controllers.data.RecipeCarpentry;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public class ContainerCarpentryBench extends Container {
|
2018-09-13 12:03:28 -04:00
|
|
|
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 4, 4);
|
|
|
|
|
public IInventory craftResult = new InventoryCraftResult();
|
|
|
|
|
private EntityPlayer player;
|
|
|
|
|
private World worldObj;
|
|
|
|
|
private int posX;
|
|
|
|
|
private int posY;
|
|
|
|
|
private int posZ;
|
|
|
|
|
|
2025-07-09 15:40:31 -04:00
|
|
|
private RecipeCarpentry currentRecipe;
|
2025-07-09 18:17:32 -04:00
|
|
|
private SlotCarpentryResult resultSlot;
|
2025-07-09 18:34:38 -04:00
|
|
|
private boolean resultCanPickup = true;
|
2025-07-09 15:40:31 -04:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public ContainerCarpentryBench(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.worldObj = par2World;
|
|
|
|
|
this.posX = par3;
|
|
|
|
|
this.posY = par4;
|
|
|
|
|
this.posZ = par5;
|
|
|
|
|
this.player = par1InventoryPlayer.player;
|
2025-07-09 18:17:32 -04:00
|
|
|
this.resultSlot = new SlotCarpentryResult(this, par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 133, 41);
|
|
|
|
|
this.addSlotToContainer(this.resultSlot);
|
2018-09-13 12:03:28 -04:00
|
|
|
int var6;
|
|
|
|
|
int var7;
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (var6 = 0; var6 < 4; ++var6) {
|
|
|
|
|
for (var7 = 0; var7 < 4; ++var7) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.addSlotToContainer(new Slot(this.craftMatrix, var7 + var6 * 4, 17 + var7 * 18, 14 + var6 * 18));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (var6 = 0; var6 < 3; ++var6) {
|
|
|
|
|
for (var7 = 0; var7 < 9; ++var7) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.addSlotToContainer(new Slot(par1InventoryPlayer, var7 + var6 * 9 + 9, 8 + var7 * 18, 98 + var6 * 18));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (var6 = 0; var6 < 9; ++var6) {
|
2018-09-13 12:03:28 -04:00
|
|
|
this.addSlotToContainer(new Slot(par1InventoryPlayer, var6, 8 + var6 * 18, 156));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.onCraftMatrixChanged(this.craftMatrix);
|
|
|
|
|
}
|
2024-03-16 14:49:32 -04:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
public int getMetadata() {
|
|
|
|
|
return worldObj.getBlockMetadata(posX, posY, posZ);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback for when the crafting matrix is changed.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2025-03-13 17:14:45 -04:00
|
|
|
public void onCraftMatrixChanged(IInventory par1IInventory) {
|
|
|
|
|
if (!this.worldObj.isRemote) {
|
|
|
|
|
RecipeCarpentry recipe = RecipeController.Instance.findMatchingRecipe(this.craftMatrix);
|
2025-07-09 15:40:31 -04:00
|
|
|
this.currentRecipe = recipe;
|
2025-03-13 17:14:45 -04:00
|
|
|
|
|
|
|
|
ItemStack item = null;
|
2025-07-09 18:17:32 -04:00
|
|
|
boolean canPickup = true;
|
2025-03-13 17:14:45 -04:00
|
|
|
if (recipe != null && recipe.availability.isAvailable(player)) {
|
|
|
|
|
item = recipe.getCraftingResult(this.craftMatrix);
|
2025-07-09 18:17:32 -04:00
|
|
|
|
|
|
|
|
ItemStack[] items = new ItemStack[this.craftMatrix.getSizeInventory()];
|
|
|
|
|
for (int i = 0; i < items.length; i++) {
|
|
|
|
|
items[i] = this.craftMatrix.getStackInSlot(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RecipeScriptEvent.Pre pre = EventHooks.onRecipeScriptPre(player, recipe.getScriptHandler(), recipe, items);
|
|
|
|
|
canPickup = !pre.isCanceled();
|
|
|
|
|
item = EventHooks.onRecipeScriptPost(player, recipe.getScriptHandler(), recipe, items, item);
|
2025-03-13 17:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.craftResult.setInventorySlotContents(0, item);
|
2025-07-09 18:34:38 -04:00
|
|
|
this.resultCanPickup = canPickup;
|
2025-07-09 18:17:32 -04:00
|
|
|
if (this.resultSlot != null) {
|
|
|
|
|
this.resultSlot.setCanPickup(canPickup);
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
EntityPlayerMP plmp = (EntityPlayerMP) player;
|
|
|
|
|
plmp.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(this.windowId, 0, item));
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback for when the crafting gui is closed.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2025-03-13 17:14:45 -04:00
|
|
|
public void onContainerClosed(EntityPlayer par1EntityPlayer) {
|
2018-09-13 12:03:28 -04:00
|
|
|
super.onContainerClosed(par1EntityPlayer);
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (!this.worldObj.isRemote) {
|
|
|
|
|
for (int var2 = 0; var2 < 16; ++var2) {
|
2018-09-13 12:03:28 -04:00
|
|
|
ItemStack var3 = this.craftMatrix.getStackInSlotOnClosing(var2);
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (var3 != null) {
|
2018-09-13 12:03:28 -04:00
|
|
|
par1EntityPlayer.dropPlayerItemWithRandomChoice(var3, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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 boolean canInteractWith(EntityPlayer par1EntityPlayer) {
|
|
|
|
|
return this.worldObj.getBlock(this.posX, this.posY, this.posZ) != CustomItems.carpentyBench ? false : par1EntityPlayer.getDistanceSq((double) this.posX + 0.5D, (double) this.posY + 0.5D, (double) this.posZ + 0.5D) <= 64.0D;
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 21:35:27 -04:00
|
|
|
public boolean canPickupResult() {
|
|
|
|
|
return this.resultCanPickup;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
/**
|
|
|
|
|
* Called to transfer a stack from one inventory to the other eg. when shift clicking.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2025-03-13 17:14:45 -04:00
|
|
|
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par1) {
|
2018-09-13 12:03:28 -04:00
|
|
|
ItemStack var2 = null;
|
2025-03-13 17:14:45 -04:00
|
|
|
Slot var3 = (Slot) this.inventorySlots.get(par1);
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (var3 != null && var3.getHasStack()) {
|
2018-09-13 12:03:28 -04:00
|
|
|
ItemStack var4 = var3.getStack();
|
|
|
|
|
var2 = var4.copy();
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (par1 == 0) {
|
2025-07-09 16:58:57 -04:00
|
|
|
SlotCarpentryResult resultSlot = (SlotCarpentryResult) var3;
|
2025-07-09 18:17:32 -04:00
|
|
|
if (!resultSlot.canPickup()) {
|
2025-07-09 16:58:57 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
if (!this.mergeItemStack(var4, 17, 53, true)) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var3.onSlotChange(var4, var2);
|
2025-03-13 17:14:45 -04:00
|
|
|
} else if (par1 >= 17 && par1 < 44) {
|
|
|
|
|
if (!this.mergeItemStack(var4, 44, 53, false)) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} else if (par1 >= 44 && par1 < 53) {
|
|
|
|
|
if (!this.mergeItemStack(var4, 17, 44, false)) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2025-03-13 17:14:45 -04:00
|
|
|
} else if (!this.mergeItemStack(var4, 17, 53, false)) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (var4.stackSize == 0) {
|
|
|
|
|
var3.putStack((ItemStack) null);
|
|
|
|
|
} else {
|
2018-09-13 12:03:28 -04:00
|
|
|
var3.onSlotChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
if (var4.stackSize == var2.stackSize) {
|
2018-09-13 12:03:28 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var3.onPickupFromSlot(par1EntityPlayer, var4);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-16 14:49:32 -04:00
|
|
|
// Update client-side inventory
|
2025-07-09 16:58:57 -04:00
|
|
|
if (!this.worldObj.isRemote && par1EntityPlayer instanceof EntityPlayerMP) {
|
|
|
|
|
((EntityPlayerMP) par1EntityPlayer).sendContainerToPlayer(this);
|
2024-03-16 14:49:32 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
return var2;
|
|
|
|
|
}
|
2024-03-16 14:49:32 -04:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean func_94530_a(ItemStack stack, Slot slotIn) {
|
|
|
|
|
return slotIn.inventory != this.craftResult && super.func_94530_a(stack, slotIn);
|
|
|
|
|
}
|
2025-07-09 15:40:31 -04:00
|
|
|
|
2025-07-09 18:34:38 -04:00
|
|
|
// --- Sync Methods ---
|
|
|
|
|
@Override
|
|
|
|
|
public void addCraftingToCrafters(ICrafting listener) {
|
|
|
|
|
super.addCraftingToCrafters(listener);
|
|
|
|
|
listener.sendProgressBarUpdate(this, 0, this.resultCanPickup ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void detectAndSendChanges() {
|
|
|
|
|
super.detectAndSendChanges();
|
|
|
|
|
for (Object crafterObj : this.crafters) {
|
|
|
|
|
((ICrafting) crafterObj).sendProgressBarUpdate(this, 0, this.resultCanPickup ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateProgressBar(int id, int data) {
|
|
|
|
|
if (id == 0 && this.resultSlot != null) {
|
|
|
|
|
this.resultSlot.setCanPickup(data != 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// --- End Sync Methods ---
|
|
|
|
|
|
2025-07-09 15:40:31 -04:00
|
|
|
// Custom result slot to handle recipe scripts
|
|
|
|
|
private class SlotCarpentryResult extends SlotCrafting {
|
|
|
|
|
private final ContainerCarpentryBench container;
|
2025-07-09 16:58:57 -04:00
|
|
|
private boolean canPickup = true;
|
2025-07-09 15:40:31 -04:00
|
|
|
|
|
|
|
|
public SlotCarpentryResult(ContainerCarpentryBench container, EntityPlayer player, InventoryCrafting matrix, IInventory result, int index, int x, int y) {
|
|
|
|
|
super(player, matrix, result, index, x, y);
|
|
|
|
|
this.container = container;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 18:17:32 -04:00
|
|
|
public void setCanPickup(boolean value) {
|
|
|
|
|
this.canPickup = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean canPickup() {
|
|
|
|
|
return this.canPickup;
|
2025-07-09 16:58:57 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 15:40:31 -04:00
|
|
|
@Override
|
2025-07-09 16:58:57 -04:00
|
|
|
public boolean canTakeStack(EntityPlayer player) {
|
|
|
|
|
return canPickup && super.canTakeStack(player);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onPickupFromSlot(EntityPlayer player, ItemStack stack) {
|
|
|
|
|
if (!canPickup)
|
|
|
|
|
return;
|
2025-07-09 15:40:31 -04:00
|
|
|
super.onPickupFromSlot(player, stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|