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()); int meta = worldObj.getBlockMetadata(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ());
drops.addAll(blockToBreak.getDrops(worldObj, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), meta, 0)); 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)); worldObj.playAuxSFX(2001, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), Block.getIdFromBlock(blockToBreak)+(meta << 12));
WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord); WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord);
WorldUtil.addToInventory(this.slots, drops, true); WorldUtil.addToInventory(this, drops, true);
this.markDirty(); this.markDirty();
} }
} }

View file

@ -57,10 +57,10 @@ public class TileEntityDirectionalBreaker extends TileEntityInventoryBase implem
int meta = worldObj.getBlockMetadata(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ()); int meta = worldObj.getBlockMetadata(coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ());
drops.addAll(blockToBreak.getDrops(worldObj, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), meta, 0)); 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)); worldObj.playAuxSFX(2001, coordsBlock.getX(), coordsBlock.getY(), coordsBlock.getZ(), Block.getIdFromBlock(blockToBreak)+(meta << 12));
WorldUtil.breakBlockAtSide(sideToManipulate, worldObj, xCoord, yCoord, zCoord, i); 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.storage.extractEnergy(ENERGY_USE, false);
this.markDirty(); this.markDirty();
} }

View file

@ -12,6 +12,7 @@ package ellpeck.actuallyadditions.tile;
import cofh.api.energy.EnergyStorage; import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver; import cofh.api.energy.IEnergyReceiver;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
@ -20,11 +21,30 @@ public class TileEntityMiner extends TileEntityBase implements IEnergyReceiver{
public EnergyStorage storage = new EnergyStorage(800000); public EnergyStorage storage = new EnergyStorage(800000);
@Override @Override
@SuppressWarnings("unchecked")
public void updateEntity(){ public void updateEntity(){
super.updateEntity(); super.updateEntity();
if(!this.worldObj.isRemote){ 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()); 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)); 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)); 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.breakBlockAtSide(ForgeDirection.UNKNOWN, boundPosition.getWorld(), boundPosition.getX(), boundPosition.getY(), boundPosition.getZ());
WorldUtil.addToInventory(this.slots, drops, true); WorldUtil.addToInventory(this, drops, true);
this.markDirty(); this.markDirty();
} }
} }

View file

@ -45,8 +45,8 @@ public class TileEntityRangedCollector extends TileEntityInventoryBase implement
if(this.checkFilter(toAdd)){ if(this.checkFilter(toAdd)){
ArrayList<ItemStack> checkList = new ArrayList<ItemStack>(); ArrayList<ItemStack> checkList = new ArrayList<ItemStack>();
checkList.add(toAdd); checkList.add(toAdd);
if(WorldUtil.addToInventory(this.slots, 0, WHITELIST_START, checkList, false)){ if(WorldUtil.addToInventory(this, 0, WHITELIST_START, checkList, false)){
WorldUtil.addToInventory(this.slots, 0, WHITELIST_START, checkList, true); WorldUtil.addToInventory(this, 0, WHITELIST_START, checkList, true);
item.setDead(); 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.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.network.play.client.C07PacketPlayerDigging; import net.minecraft.network.play.client.C07PacketPlayerDigging;
import net.minecraft.network.play.server.S23PacketBlockChange; import net.minecraft.network.play.server.S23PacketBlockChange;
@ -243,30 +244,27 @@ public class WorldUtil{
return blocks; return blocks;
} }
public static boolean addToInventory(ItemStack[] slots, ArrayList<ItemStack> stacks, boolean actuallyDo){ public static boolean addToInventory(IInventory inventory, ArrayList<ItemStack> stacks, boolean actuallyDo){
return addToInventory(slots, 0, slots.length, stacks, actuallyDo); return addToInventory(inventory, 0, inventory.getSizeInventory(), stacks, actuallyDo);
} }
/** /**
* Add an ArrayList of ItemStacks to an Array of slots * 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 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? * @param actuallyDo Do it or just test if it works?
* @return Does it work? * @return Does it work?
*/ */
public static boolean addToInventory(ItemStack[] slots, int start, int end, ArrayList<ItemStack> stacks, boolean actuallyDo){ public static boolean addToInventory(IInventory inventory, int start, int end, ArrayList<ItemStack> stacks, boolean actuallyDo){
ItemStack[] theSlots; //Copy the slots if just testing to later load them again
if(actuallyDo){ ItemStack[] backupSlots = null;
theSlots = slots; if(!actuallyDo){
} backupSlots = new ItemStack[inventory.getSizeInventory()];
else{ for(int i = 0; i < backupSlots.length; i++){
//Create "Test Slots" to put the items into to try if it works out in the end ItemStack stack = inventory.getStackInSlot(i);
theSlots = new ItemStack[slots.length]; if(stack != null){
backupSlots[i] = stack.copy();
for(int i = 0; i < theSlots.length; i++){
if(slots[i] != null){
theSlots[i] = slots[i].copy();
} }
} }
} }
@ -274,12 +272,13 @@ public class WorldUtil{
int working = 0; int working = 0;
for(ItemStack stackToPutIn : stacks){ for(ItemStack stackToPutIn : stacks){
for(int i = start; i < end; i++){ 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))){ ItemStack stackInQuestion = inventory.getStackInSlot(i);
if(theSlots[i] == null){ if(stackToPutIn != null && (stackInQuestion == null || (stackInQuestion.isItemEqual(stackToPutIn) && stackInQuestion.getMaxStackSize() >= stackInQuestion.stackSize+stackToPutIn.stackSize))){
theSlots[i] = stackToPutIn.copy(); if(stackInQuestion == null){
inventory.setInventorySlotContents(i, stackToPutIn.copy());
} }
else{ else{
theSlots[i].stackSize += stackToPutIn.stackSize; stackInQuestion.stackSize += stackToPutIn.stackSize;
} }
working++; 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(); return working >= stacks.size();
} }