Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/java/noppes/npcs/blocks/tiles/TileBlockAnvil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package noppes.npcs.blocks.tiles;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.EnumSkyBlock;

public class TileBlockAnvil extends TileEntity {

public boolean firstTick = true;
public ItemStack[] items = new ItemStack[16];

public TileBlockAnvil() {
}
Expand All @@ -22,4 +26,41 @@ public void updateEntity() {
getWorldObj().markBlockForUpdate(xCoord, yCoord, zCoord);
}
}

@Override
public void writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);

NBTTagList list = new NBTTagList();

for (int i = 0; i < items.length; i++) {
if (items[i] != null) {
NBTTagCompound tag = new NBTTagCompound();
tag.setByte("Slot", (byte) i);
items[i].writeToNBT(tag);
list.appendTag(tag);
}
}

compound.setTag("Items", list);


}

@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);

NBTTagList list = compound.getTagList("Items", 10); // 10 = NBTTagCompound
this.items = new ItemStack[16];

for (int i = 0; i < list.tagCount(); i++) {
NBTTagCompound tag = list.getCompoundTagAt(i);
int slot = tag.getByte("Slot") & 255;

if (slot >= 0 && slot < items.length) {
items[slot] = ItemStack.loadItemStackFromNBT(tag);
}
}
}
}
20 changes: 16 additions & 4 deletions src/main/java/noppes/npcs/containers/ContainerCarpentryBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.server.S2FPacketSetSlot;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import noppes.npcs.CustomItems;
import noppes.npcs.EventHooks;
import noppes.npcs.blocks.tiles.TileBlockAnvil;
import noppes.npcs.controllers.RecipeController;
import noppes.npcs.controllers.data.RecipeCarpentry;
import noppes.npcs.scripted.event.RecipeScriptEvent;
Expand Down Expand Up @@ -58,14 +60,23 @@ public ContainerCarpentryBench(InventoryPlayer par1InventoryPlayer, World par2Wo
for (var6 = 0; var6 < 9; ++var6) {
this.addSlotToContainer(new Slot(par1InventoryPlayer, var6, 8 + var6 * 18, 156));
}

restoreCraftMatrix();
this.onCraftMatrixChanged(this.craftMatrix);
}

public int getMetadata() {
return worldObj.getBlockMetadata(posX, posY, posZ);
}

private void restoreCraftMatrix() {
TileEntity tileEntity = worldObj.getTileEntity(posX, posY, posZ);
if (tileEntity instanceof TileBlockAnvil) {
for (int i = 0; i < ((TileBlockAnvil) tileEntity).items.length; i++) {
craftMatrix.setInventorySlotContents(i, ((TileBlockAnvil) tileEntity).items[i]);
}
}
}

/**
* Callback for when the crafting matrix is changed.
*/
Expand Down Expand Up @@ -108,13 +119,14 @@ public void onContainerClosed(EntityPlayer par1EntityPlayer) {
super.onContainerClosed(par1EntityPlayer);

if (!this.worldObj.isRemote) {
TileEntity tileEntity = worldObj.getTileEntity(posX, posY, posZ);
for (int var2 = 0; var2 < 16; ++var2) {
ItemStack var3 = this.craftMatrix.getStackInSlotOnClosing(var2);

if (var3 != null) {
par1EntityPlayer.dropPlayerItemWithRandomChoice(var3, false);
if (tileEntity instanceof TileBlockAnvil) {
((TileBlockAnvil) tileEntity).items[var2] = var3;
}
}
tileEntity.markDirty();
}
}

Expand Down
Loading