package slimeknights.tconstruct.gadgets.block;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.IHopper;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
public class BlockWoodRailDropper extends BlockWoodRail {
@Override
public void onMinecartPass(World world, EntityMinecart cart, BlockPos pos) {
if(!cart.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN) || !(cart instanceof IHopper)) {
return;
}
TileEntity tileEntity = world.getTileEntity(pos.down());
if(tileEntity == null || !tileEntity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN)) {
return;
}
IItemHandler itemHandlerCart = cart.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
IItemHandler itemHandlerTE = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
for(int i = 0; i < itemHandlerCart.getSlots(); i++) {
ItemStack itemStack = itemHandlerCart.extractItem(i, 1, true);
if(ItemHandlerHelper.insertItem(itemHandlerTE, itemStack, true) == null) {
itemStack = itemHandlerCart.extractItem(i, 1, false);
ItemHandlerHelper.insertItem(itemHandlerTE, itemStack, false);
break;
}
}
}
}