From 93b42be9d8a79f52448e1c092eb0d950addf05ea Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 19 Oct 2016 16:28:09 +0200 Subject: [PATCH] Made fishing net accept chests from different sies Closes #273 --- .../mod/tile/TileEntityFishingNet.java | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFishingNet.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFishingNet.java index d04b0fe74..c221169bc 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFishingNet.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFishingNet.java @@ -11,10 +11,8 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.util.Util; -import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.material.Material; import net.minecraft.entity.item.EntityItem; -import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -22,8 +20,9 @@ import net.minecraft.util.EnumFacing; import net.minecraft.world.WorldServer; import net.minecraft.world.storage.loot.LootContext; import net.minecraft.world.storage.loot.LootTableList; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; -import java.util.ArrayList; import java.util.List; public class TileEntityFishingNet extends TileEntityBase{ @@ -62,14 +61,9 @@ public class TileEntityFishingNet extends TileEntityBase{ LootContext.Builder builder = new LootContext.Builder((WorldServer)this.worldObj); List fishables = this.worldObj.getLootTableManager().getLootTableFromLocation(LootTableList.GAMEPLAY_FISHING).generateLootForPools(Util.RANDOM, builder.build()); for(ItemStack fishable : fishables){ - TileEntity tile = this.worldObj.getTileEntity(this.pos.up()); - if(tile != null && tile instanceof IInventory){ - ArrayList list = new ArrayList(); - list.add(fishable); - WorldUtil.addToInventory((IInventory)tile, list, EnumFacing.DOWN, true, false); - } - else{ - EntityItem item = new EntityItem(this.worldObj, this.pos.getX()+0.5, this.pos.getY()+0.5, this.pos.getZ()+0.5, fishable); + ItemStack leftover = this.storeInContainer(fishable); + if(leftover != null){ + EntityItem item = new EntityItem(this.worldObj, this.pos.getX()+0.5, this.pos.getY()+0.5, this.pos.getZ()+0.5, leftover.copy()); item.lifespan = 2000; this.worldObj.spawnEntityInWorld(item); } @@ -77,11 +71,37 @@ public class TileEntityFishingNet extends TileEntityBase{ } } else{ - int time = 15000; + int time = 150; this.timeUntilNextDrop = time+Util.RANDOM.nextInt(time/2); } } } } } + + private ItemStack storeInContainer(ItemStack stack){ + for(EnumFacing side : EnumFacing.values()){ + TileEntity tile = this.tilesAround[side.ordinal()]; + if(tile != null){ + if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite())){ + IItemHandler cap = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite()); + if(cap != null){ + for(int i = 0; i < cap.getSlots(); i++){ + stack = cap.insertItem(i, stack, false); + + if(stack == null || stack.stackSize <= 0){ + return null; + } + } + } + } + } + } + return stack; + } + + @Override + public boolean shouldSaveHandlersAround(){ + return true; + } }