Files
CustomNPC-Plus/src/main/java/noppes/npcs/containers/ContainerCarpentryBench.java
T

153 lines
5.3 KiB
Java
Raw Normal View History

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;
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-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;
this.addSlotToContainer(new SlotCrafting(par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 133, 41));
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);
}
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);
ItemStack item = null;
if (recipe != null && recipe.availability.isAvailable(player)) {
item = recipe.getCraftingResult(this.craftMatrix);
}
this.craftResult.setInventorySlotContents(0, item);
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
}
/**
* 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) {
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);
}
// Update client-side inventory
if (!this.worldObj.isRemote && player instanceof EntityPlayerMP) {
((EntityPlayerMP) player).sendContainerToPlayer(this);
}
2018-09-13 12:03:28 -04:00
return var2;
}
@Override
public boolean func_94530_a(ItemStack stack, Slot slotIn) {
return slotIn.inventory != this.craftResult && super.func_94530_a(stack, slotIn);
}
2018-09-13 12:03:28 -04:00
}