From 77bf53a3d41da533ce8cc10a53d1c9424303ae04 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 8 Dec 2015 18:10:37 +0100 Subject: [PATCH] Made WorldUtil's addToInventory work for IInventories and not just for slots --- .../tile/TileEntityBreaker.java | 4 +- .../tile/TileEntityDirectionalBreaker.java | 4 +- .../tile/TileEntityMiner.java | 22 ++++++++- .../tile/TileEntityPhantomPlacer.java | 4 +- .../tile/TileEntityRangedCollector.java | 4 +- .../actuallyadditions/util/WorldUtil.java | 45 +++++++++++-------- 6 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBreaker.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBreaker.java index d25d8d122..aedc31812 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBreaker.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBreaker.java @@ -65,10 +65,10 @@ public class TileEntityBreaker extends TileEntityInventoryBase{ int meta = worldObj.getBlockMetadata(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()); drops.addAll(blockToBreak.getDrops(worldObj, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), meta, 0)); - if(WorldUtil.addToInventory(this.slots, drops, false)){ + if(WorldUtil.addToInventory(this, drops, false)){ worldObj.playAuxSFX(2001, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), Block.getIdFromBlock(blockToBreak)+(meta << 12)); WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord); - WorldUtil.addToInventory(this.slots, drops, true); + WorldUtil.addToInventory(this, drops, true); this.markDirty(); } } diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityDirectionalBreaker.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityDirectionalBreaker.java index 609f02a99..5cd6bb5d1 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityDirectionalBreaker.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityDirectionalBreaker.java @@ -57,10 +57,10 @@ public class TileEntityDirectionalBreaker extends TileEntityInventoryBase implem int meta = worldObj.getBlockMetadata(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()); drops.addAll(blockToBreak.getDrops(worldObj, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), meta, 0)); - if(WorldUtil.addToInventory(this.slots, drops, false)){ + if(WorldUtil.addToInventory(this, drops, false)){ worldObj.playAuxSFX(2001, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), Block.getIdFromBlock(blockToBreak)+(meta << 12)); WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord, i); - WorldUtil.addToInventory(this.slots, drops, true); + WorldUtil.addToInventory(this, drops, true); this.storage.extractEnergy(ENERGY_USE, false); this.markDirty(); } diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityMiner.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityMiner.java index 9269bdab3..00101a238 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityMiner.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityMiner.java @@ -12,6 +12,7 @@ package ellpeck.actuallyadditions.tile; import cofh.api.energy.EnergyStorage; import cofh.api.energy.IEnergyReceiver; +import net.minecraft.block.Block; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.ForgeDirection; @@ -20,11 +21,30 @@ public class TileEntityMiner extends TileEntityBase implements IEnergyReceiver{ public EnergyStorage storage = new EnergyStorage(800000); @Override - @SuppressWarnings("unchecked") public void updateEntity(){ super.updateEntity(); if(!this.worldObj.isRemote){ + this.mine(2); + } + } + private void mine(int range){ + for(int anX = -range; anX <= range; anX++){ + for(int aZ = -range; aZ <= range; aZ++){ + for(int y = this.yCoord-1; y > 0; y--){ + int x = this.xCoord+anX; + int z = this.zCoord+aZ; + + Block block = this.worldObj.getBlock(x, y, z); + int meta = this.worldObj.getBlockMetadata(x, y, z); + if(block != null && !block.isAir(this.worldObj, x, y, z)){ + if(block.getHarvestLevel(meta) <= 3){ + + return; + } + } + } + } } } diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomPlacer.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomPlacer.java index 1674ee130..584c2a055 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomPlacer.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomPlacer.java @@ -62,10 +62,10 @@ public class TileEntityPhantomPlacer extends TileEntityInventoryBase implements int meta = boundPosition.getWorld().getBlockMetadata(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); drops.addAll(blockToBreak.getDrops(boundPosition.getWorld(), boundPosition.getX(), boundPosition.getY(), boundPosition.getZ(), meta, 0)); - if(WorldUtil.addToInventory(this.slots, drops, false)){ + if(WorldUtil.addToInventory(this, drops, false)){ boundPosition.getWorld().playAuxSFX(2001, boundPosition.getX(), boundPosition.getY(), boundPosition.getZ(), Block.getIdFromBlock(blockToBreak)+(meta << 12)); WorldUtil.breakBlockAtSide(ForgeDirection.UNKNOWN, boundPosition.getWorld(), boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); - WorldUtil.addToInventory(this.slots, drops, true); + WorldUtil.addToInventory(this, drops, true); this.markDirty(); } } diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityRangedCollector.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityRangedCollector.java index 16e6487bd..bd1865eab 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityRangedCollector.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityRangedCollector.java @@ -45,8 +45,8 @@ public class TileEntityRangedCollector extends TileEntityInventoryBase implement if(this.checkFilter(toAdd)){ ArrayList checkList = new ArrayList(); checkList.add(toAdd); - if(WorldUtil.addToInventory(this.slots, 0, WHITELIST_START, checkList, false)){ - WorldUtil.addToInventory(this.slots, 0, WHITELIST_START, checkList, true); + if(WorldUtil.addToInventory(this, 0, WHITELIST_START, checkList, false)){ + WorldUtil.addToInventory(this, 0, WHITELIST_START, checkList, true); item.setDead(); } } diff --git a/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java b/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java index 7bdecde0c..a065d4485 100644 --- a/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java +++ b/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java @@ -20,6 +20,7 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.Blocks; +import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.network.play.client.C07PacketPlayerDigging; import net.minecraft.network.play.server.S23PacketBlockChange; @@ -243,30 +244,27 @@ public class WorldUtil{ return blocks; } - public static boolean addToInventory(ItemStack[] slots, ArrayList stacks, boolean actuallyDo){ - return addToInventory(slots, 0, slots.length, stacks, actuallyDo); + public static boolean addToInventory(IInventory inventory, ArrayList stacks, boolean actuallyDo){ + return addToInventory(inventory, 0, inventory.getSizeInventory(), stacks, actuallyDo); } /** * Add an ArrayList of ItemStacks to an Array of slots * - * @param slots The slots to try to put the items into + * @param inventory The inventory to try to put the items into * @param stacks The stacks to be put into the slots (Items don't actually get removed from there!) * @param actuallyDo Do it or just test if it works? * @return Does it work? */ - public static boolean addToInventory(ItemStack[] slots, int start, int end, ArrayList stacks, boolean actuallyDo){ - ItemStack[] theSlots; - if(actuallyDo){ - theSlots = slots; - } - else{ - //Create "Test Slots" to put the items into to try if it works out in the end - theSlots = new ItemStack[slots.length]; - - for(int i = 0; i < theSlots.length; i++){ - if(slots[i] != null){ - theSlots[i] = slots[i].copy(); + public static boolean addToInventory(IInventory inventory, int start, int end, ArrayList stacks, boolean actuallyDo){ + //Copy the slots if just testing to later load them again + ItemStack[] backupSlots = null; + if(!actuallyDo){ + backupSlots = new ItemStack[inventory.getSizeInventory()]; + for(int i = 0; i < backupSlots.length; i++){ + ItemStack stack = inventory.getStackInSlot(i); + if(stack != null){ + backupSlots[i] = stack.copy(); } } } @@ -274,12 +272,13 @@ public class WorldUtil{ int working = 0; for(ItemStack stackToPutIn : stacks){ for(int i = start; i < end; i++){ - if(stackToPutIn != null && (theSlots[i] == null || (theSlots[i].isItemEqual(stackToPutIn) && theSlots[i].getMaxStackSize() >= theSlots[i].stackSize+stackToPutIn.stackSize))){ - if(theSlots[i] == null){ - theSlots[i] = stackToPutIn.copy(); + ItemStack stackInQuestion = inventory.getStackInSlot(i); + if(stackToPutIn != null && (stackInQuestion == null || (stackInQuestion.isItemEqual(stackToPutIn) && stackInQuestion.getMaxStackSize() >= stackInQuestion.stackSize+stackToPutIn.stackSize))){ + if(stackInQuestion == null){ + inventory.setInventorySlotContents(i, stackToPutIn.copy()); } else{ - theSlots[i].stackSize += stackToPutIn.stackSize; + stackInQuestion.stackSize += stackToPutIn.stackSize; } working++; @@ -287,6 +286,14 @@ public class WorldUtil{ } } } + + //Load the slots again + if(!actuallyDo && backupSlots != null){ + for(int i = 0; i < backupSlots.length; i++){ + inventory.setInventorySlotContents(i, backupSlots[i]); + } + } + return working >= stacks.size(); }