Made WorldUtil's addToInventory work for IInventories and not just for slots

This commit is contained in:
Ellpeck 2015-12-08 18:10:37 +01:00
parent 5912380d66
commit 77bf53a3d4
6 changed files with 55 additions and 28 deletions

View file

@ -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();
}
}

View file

@ -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();
}

View file

@ -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;
}
}
}
}
}
}

View file

@ -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();
}
}

View file

@ -45,8 +45,8 @@ public class TileEntityRangedCollector extends TileEntityInventoryBase implement
if(this.checkFilter(toAdd)){
ArrayList<ItemStack> checkList = new ArrayList<ItemStack>();
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();
}
}

View file

@ -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<ItemStack> stacks, boolean actuallyDo){
return addToInventory(slots, 0, slots.length, stacks, actuallyDo);
public static boolean addToInventory(IInventory inventory, ArrayList<ItemStack> 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<ItemStack> 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<ItemStack> 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();
}