Made WorldUtil's addToInventory check if it can actually input

This commit is contained in:
Ellpeck 2015-12-09 13:35:18 +01:00
parent 77bf53a3d4
commit 071e78c275
2 changed files with 31 additions and 33 deletions

View file

@ -11,16 +11,18 @@
package ellpeck.actuallyadditions.tile; package ellpeck.actuallyadditions.tile;
import ellpeck.actuallyadditions.util.Util; import ellpeck.actuallyadditions.util.Util;
import ellpeck.actuallyadditions.util.WorldUtil;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.FishingHooks; import net.minecraftforge.common.FishingHooks;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import java.util.ArrayList;
public class TileEntityFishingNet extends TileEntityBase{ public class TileEntityFishingNet extends TileEntityBase{
public int timeUntilNextDrop; public int timeUntilNextDrop;
@ -37,7 +39,9 @@ public class TileEntityFishingNet extends TileEntityBase{
ItemStack fishable = FishingHooks.getRandomFishable(Util.RANDOM, Util.RANDOM.nextFloat()); ItemStack fishable = FishingHooks.getRandomFishable(Util.RANDOM, Util.RANDOM.nextFloat());
TileEntity tile = worldObj.getTileEntity(xCoord, yCoord+1, zCoord); TileEntity tile = worldObj.getTileEntity(xCoord, yCoord+1, zCoord);
if(tile != null && tile instanceof IInventory){ if(tile != null && tile instanceof IInventory){
this.insertIntoInventory((IInventory)tile, fishable); ArrayList<ItemStack> list = new ArrayList<ItemStack>();
list.add(fishable);
WorldUtil.addToInventory((IInventory)tile, list, ForgeDirection.DOWN, true);
} }
else{ else{
EntityItem item = new EntityItem(worldObj, xCoord+0.5, yCoord+0.5, zCoord+0.5, fishable); EntityItem item = new EntityItem(worldObj, xCoord+0.5, yCoord+0.5, zCoord+0.5, fishable);
@ -48,7 +52,7 @@ public class TileEntityFishingNet extends TileEntityBase{
} }
else{ else{
int time = 15000; int time = 15000;
this.timeUntilNextDrop = time+Util.RANDOM.nextInt(time/2); this.timeUntilNextDrop = 10;//time+Util.RANDOM.nextInt(time/2);
} }
} }
} }
@ -64,22 +68,4 @@ public class TileEntityFishingNet extends TileEntityBase{
public void readSyncableNBT(NBTTagCompound compound, boolean sync){ public void readSyncableNBT(NBTTagCompound compound, boolean sync){
this.timeUntilNextDrop = compound.getInteger("TimeUntilNextDrop"); this.timeUntilNextDrop = compound.getInteger("TimeUntilNextDrop");
} }
public void insertIntoInventory(IInventory inventory, ItemStack stack){
for(int i = 0; i < inventory.getSizeInventory(); i++){
if(inventory.isItemValidForSlot(i, stack)){
if(!(inventory instanceof ISidedInventory) || ((ISidedInventory)inventory).canInsertItem(i, stack, ForgeDirection.DOWN.flag)){
ItemStack slot = inventory.getStackInSlot(i);
if(slot == null){
inventory.setInventorySlotContents(i, stack);
return;
}
if(slot.isItemEqual(stack) && slot.stackSize <= slot.getMaxStackSize()-stack.stackSize && slot.stackSize <= inventory.getInventoryStackLimit()-stack.stackSize){
slot.stackSize += stack.stackSize;
return;
}
}
}
}
}
} }

View file

@ -21,6 +21,7 @@ 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.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
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;
@ -244,8 +245,16 @@ public class WorldUtil{
return blocks; return blocks;
} }
public static boolean addToInventory(IInventory inventory, int start, int end, ArrayList<ItemStack> stacks, boolean actuallyDo){
return addToInventory(inventory, start, end, stacks, ForgeDirection.UNKNOWN, actuallyDo);
}
public static boolean addToInventory(IInventory inventory, ArrayList<ItemStack> stacks, boolean actuallyDo){ public static boolean addToInventory(IInventory inventory, ArrayList<ItemStack> stacks, boolean actuallyDo){
return addToInventory(inventory, 0, inventory.getSizeInventory(), stacks, actuallyDo); return addToInventory(inventory, stacks, ForgeDirection.UNKNOWN, actuallyDo);
}
public static boolean addToInventory(IInventory inventory, ArrayList<ItemStack> stacks, ForgeDirection side, boolean actuallyDo){
return addToInventory(inventory, 0, inventory.getSizeInventory(), stacks, side, actuallyDo);
} }
/** /**
@ -253,10 +262,11 @@ public class WorldUtil{
* *
* @param inventory The inventory 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 side The side to input from (use UNKNOWN if it should always work)
* @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(IInventory inventory, int start, int end, ArrayList<ItemStack> stacks, boolean actuallyDo){ public static boolean addToInventory(IInventory inventory, int start, int end, ArrayList<ItemStack> stacks, ForgeDirection side, boolean actuallyDo){
//Copy the slots if just testing to later load them again //Copy the slots if just testing to later load them again
ItemStack[] backupSlots = null; ItemStack[] backupSlots = null;
if(!actuallyDo){ if(!actuallyDo){
@ -272,17 +282,19 @@ 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++){
ItemStack stackInQuestion = inventory.getStackInSlot(i); if(side == ForgeDirection.UNKNOWN || ((!(inventory instanceof ISidedInventory) || ((ISidedInventory)inventory).canInsertItem(i, stackToPutIn, side.ordinal())) && inventory.isItemValidForSlot(i, stackToPutIn))){
if(stackToPutIn != null && (stackInQuestion == null || (stackInQuestion.isItemEqual(stackToPutIn) && stackInQuestion.getMaxStackSize() >= stackInQuestion.stackSize+stackToPutIn.stackSize))){ ItemStack stackInQuestion = inventory.getStackInSlot(i);
if(stackInQuestion == null){ if(stackToPutIn != null && (stackInQuestion == null || (stackInQuestion.isItemEqual(stackToPutIn) && stackInQuestion.getMaxStackSize() >= stackInQuestion.stackSize+stackToPutIn.stackSize))){
inventory.setInventorySlotContents(i, stackToPutIn.copy()); if(stackInQuestion == null){
} inventory.setInventorySlotContents(i, stackToPutIn.copy());
else{ }
stackInQuestion.stackSize += stackToPutIn.stackSize; else{
} stackInQuestion.stackSize += stackToPutIn.stackSize;
working++; }
working++;
break; break;
}
} }
} }
} }