diff --git a/src/main/java/makeo/gadomancy/common/blocks/BlockStoneMachine.java b/src/main/java/makeo/gadomancy/common/blocks/BlockStoneMachine.java index e4cad68..9ffca31 100644 --- a/src/main/java/makeo/gadomancy/common/blocks/BlockStoneMachine.java +++ b/src/main/java/makeo/gadomancy/common/blocks/BlockStoneMachine.java @@ -53,6 +53,7 @@ public class BlockStoneMachine extends Block { public IIcon pedestalSideIcon; public IIcon pedestalTopIcon; public IIcon protectorIcon; + public IIcon packagerIcon; @Override public void registerBlockIcons(IIconRegister ir) { @@ -61,6 +62,7 @@ public class BlockStoneMachine extends Block { pedestalTopIcon = ir.registerIcon(Gadomancy.MODID + ":manipulator_bot"); pedestalSideIcon = ir.registerIcon(Gadomancy.MODID + ":ancient_pedestal_side"); protectorIcon = ir.registerIcon("thaumcraft:pipe_1"); + packagerIcon = ir.registerIcon("thaumcraft:planks_greatwood"); } @Override @@ -77,6 +79,8 @@ public class BlockStoneMachine extends Block { } } else if (metadata == 2) { return protectorIcon; + } else if(metadata == 4) { + return packagerIcon; } return super.getIcon(side, metadata); diff --git a/src/main/java/makeo/gadomancy/common/blocks/tiles/TileArcanePackager.java b/src/main/java/makeo/gadomancy/common/blocks/tiles/TileArcanePackager.java index 2c454f2..8db7c7f 100644 --- a/src/main/java/makeo/gadomancy/common/blocks/tiles/TileArcanePackager.java +++ b/src/main/java/makeo/gadomancy/common/blocks/tiles/TileArcanePackager.java @@ -1,12 +1,15 @@ package makeo.gadomancy.common.blocks.tiles; import makeo.gadomancy.common.registration.RegisteredItems; +import makeo.gadomancy.common.utils.ItemUtils; +import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.ForgeDirection; import thaumcraft.api.aspects.Aspect; import thaumcraft.common.tiles.TileJarFillable; @@ -39,25 +42,58 @@ public class TileArcanePackager extends TileJarFillable implements IInventory, I aspectFilter = ASPECT; } + @Override + public void readFromNBT(NBTTagCompound compound) { + super.readFromNBT(compound); + + contents = new ItemStack[getSizeInventory()]; + NBTTagList list = compound.getTagList("Items", 10); + + for (int i = 0; i < list.tagCount(); ++i) { + NBTTagCompound slot = list.getCompoundTagAt(i); + int j = slot.getByte("Slot") & 255; + + if (j >= 0 && j < contents.length) { + contents[j] = ItemStack.loadItemStackFromNBT(slot); + } + } + } + + @Override + public void writeToNBT(NBTTagCompound compound) { + super.writeToNBT(compound); + + NBTTagList list = new NBTTagList(); + for (int i = 0; i < contents.length; ++i) { + if (contents[i] != null) { + NBTTagCompound slot = new NBTTagCompound(); + slot.setByte("Slot", (byte) i); + contents[i].writeToNBT(slot); + list.appendTag(slot); + } + } + compound.setTag("Items", list); + } + @Override public void updateEntity() { super.updateEntity(); - if(worldObj.isRemote) { - if(progress >= 0 && progress < 46) { + if (worldObj.isRemote) { + if (progress >= 0 && progress < 46) { progress++; } } else { - if(progress >= 47) { + if (progress >= 47) { doPack(); progress = -1; markForUpdate(); count = 1; } - if(progress >= 0) { + if (progress >= 0) { progress++; - } else if(count++ % 5 == 0) { - if(autoStart && canPack()) { + } else if (count++ % 5 == 0) { + if (autoStart && canPack()) { progress = 0; markForUpdate(); } @@ -66,34 +102,34 @@ public class TileArcanePackager extends TileJarFillable implements IInventory, I } private boolean canPack() { - if(getStackInSlot(11) != null) { + if (getStackInSlot(11) != null) { return false; } boolean check = false; - for(int i = 0; i < 9; i++) { - if(getStackInSlot(i) != null) { + for (int i = 0; i < 9; i++) { + if (getStackInSlot(i) != null) { check = true; break; } } - if(!check) { - return false; + if (!check) { + return false; } - if(useEssentia) { - if(amount < 4) { + if (useEssentia) { + if (amount < 4) { return false; } } else { ItemStack leather = getStackInSlot(9); - if(leather == null || leather.stackSize < 1 || leather.getItem() != Items.leather) { + if (leather == null || leather.stackSize < 1 || leather.getItem() != Items.leather) { return false; } ItemStack string = getStackInSlot(10); - if(string == null || string.stackSize < 1 || string.getItem() != Items.string) { + if (string == null || string.stackSize < 1 || string.getItem() != Items.string) { return false; } } @@ -102,18 +138,18 @@ public class TileArcanePackager extends TileJarFillable implements IInventory, I } private boolean startPack() { - if(canPack()) { + if (canPack()) { } return false; } private void doPack() { - if(canPack()) { + if (canPack()) { List contents = new ArrayList(); - for(int i = 0; i < 9; i++) { + for (int i = 0; i < 9; i++) { ItemStack stack = getStackInSlot(i); - if(stack != null) { + if (stack != null) { contents.add(stack); } } @@ -124,8 +160,8 @@ public class TileArcanePackager extends TileJarFillable implements IInventory, I boolean success = RegisteredItems.itemPackage.setContents(pack, contents); - if(success) { - if(useEssentia) { + if (success) { + if (useEssentia) { amount -= 4; } else { decrStackSize(9, 1); @@ -133,12 +169,24 @@ public class TileArcanePackager extends TileJarFillable implements IInventory, I } setInventorySlotContents(11, pack); - for(int i = 0; i < 9; i++) { + for (int i = 0; i < 9; i++) { setInventorySlotContents(i, null); } } else { - worldObj.newExplosion(null, xCoord, yCoord, zCoord, 4, false, true); - worldObj.setBlockToAir(xCoord, yCoord, zCoord); + worldObj.newExplosion(null, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5, 1, false, false); + + for(int i = 0; i < 9; i++) { + ItemStack stack = this.contents[i]; + if(stack != null) { + EntityItem entityItem = new EntityItem(worldObj, xCoord + 0.5, yCoord + (13/16f), zCoord + 0.5, stack); + ItemUtils.applyRandomDropOffset(entityItem, worldObj.rand); + worldObj.spawnEntityInWorld(entityItem); + this.contents[i] = null; + } + } + + progress = -1; + markForUpdate(); } } } @@ -263,11 +311,11 @@ public class TileArcanePackager extends TileJarFillable implements IInventory, I @Override public boolean isItemValidForSlot(int slot, ItemStack stack) { - if(slot == 9) { + if (slot == 9) { return stack.getItem() == Items.leather; - } else if(slot == 10) { + } else if (slot == 10) { return stack.getItem() == Items.string; - } else if(slot == 11) { + } else if (slot == 11) { return false; } return true; @@ -277,11 +325,11 @@ public class TileArcanePackager extends TileJarFillable implements IInventory, I @Override public int[] getAccessibleSlotsFromSide(int side) { - if(side == 0) { + if (side == 0) { return new int[]{11}; } - if(ORIENTATION_MAPPING[side] == super.facing) { + if (ORIENTATION_MAPPING[side] == super.facing) { return new int[]{9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8}; } diff --git a/src/main/java/makeo/gadomancy/common/containers/ContainerArcanePackager.java b/src/main/java/makeo/gadomancy/common/containers/ContainerArcanePackager.java index 94494aa..7714fbe 100644 --- a/src/main/java/makeo/gadomancy/common/containers/ContainerArcanePackager.java +++ b/src/main/java/makeo/gadomancy/common/containers/ContainerArcanePackager.java @@ -3,9 +3,11 @@ package makeo.gadomancy.common.containers; import makeo.gadomancy.common.blocks.tiles.TileArcanePackager; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.init.Items; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; /** * This class is part of the Gadomancy Mod @@ -47,17 +49,68 @@ public class ContainerArcanePackager extends Container { } } + @Override + public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { + ItemStack itemstack = null; + Slot slot = (Slot) this.inventorySlots.get(slotIndex); + + if (slot != null && slot.getHasStack()) { + ItemStack itemstack1 = slot.getStack(); + itemstack = itemstack1.copy(); + + int size = packagerInv.getSizeInventory(); + if (slotIndex < size) { + if (!this.mergeItemStack(itemstack1, size, this.inventorySlots.size(), true)) { + return null; + } + } else { + int specialSlot = itemstack1.getItem() == Items.leather ? 9 : itemstack1.getItem() == Items.string ? 10 : -1; + if(specialSlot != -1 && (packagerInv.getStackInSlot(specialSlot) == null || packagerInv.getStackInSlot(specialSlot).stackSize < 64)) { + this.mergeItemStack(itemstack1, specialSlot, specialSlot + 1, false); + + if(itemstack1.stackSize == 0) { + slot.putStack(null); + } + + return null; + } else if (!this.mergeItemStack(itemstack1, 0, size - 3, false)) { + return null; + } + } + + if (itemstack1.stackSize == 0) { + slot.putStack(null); + } else { + slot.onSlotChanged(); + } + } + + return itemstack; + } + @Override public boolean enchantItem(EntityPlayer player, int id) { TileArcanePackager tile = (TileArcanePackager) packagerInv; switch (id) { - case 0: tile.useEssentia = true; break; - case 1: tile.useEssentia = false; break; - case 2: tile.autoStart = true; break; - case 3: tile.autoStart = false; break; - case 4: tile.disguise = true; break; - case 5: tile.disguise = false; break; + case 0: + tile.useEssentia = true; + break; + case 1: + tile.useEssentia = false; + break; + case 2: + tile.autoStart = true; + break; + case 3: + tile.autoStart = false; + break; + case 4: + tile.disguise = true; + break; + case 5: + tile.disguise = false; + break; } tile.markForUpdate();