Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jam.icc.tileentity;
- import java.util.List;
- import com.jam.icc.block.BlockItemCollectorChest;
- import net.minecraft.entity.item.EntityItem;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.Slot;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EntitySelectors;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.math.AxisAlignedBB;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.items.CapabilityItemHandler;
- import net.minecraftforge.items.IItemHandler;
- import net.minecraftforge.items.ItemHandlerHelper;
- import net.minecraftforge.items.ItemStackHandler;
- public class TileEntityICSB extends TileEntity implements ITickable{
- public static final int SIZE = 27;
- int currentTickRate = 20;
- int counter = 0;
- // This item handler will hold the inventory slots
- public ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
- @Override
- protected void onContentsChanged(int slot) {
- // We need to tell the tile entity that something has changed so
- // that the chest contents is persisted
- TileEntityICSB.this.markDirty();
- }
- };
- // This item handler will hold the filter slot
- public ItemStackHandler filterStackHandler = new ItemStackHandler(1) {
- @Override
- protected void onContentsChanged(int slot) {
- // We need to tell the tile entity that something has changed so
- // that the chest
- //contents is persisted
- TileEntityICSB.this.markDirty();
- }
- };
- private boolean hasBeenCleared;
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- if (compound.hasKey("items")) {
- itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
- }
- if(compound.hasKey("filterItem"))
- {
- filterStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("filterItem"));
- }
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- compound.setTag("items", itemStackHandler.serializeNBT());
- compound.setTag("filterItem", filterStackHandler.serializeNBT());
- return compound;
- }
- @Override
- public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
- if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- return true;
- }
- return super.hasCapability(capability, facing);
- }
- @Override
- public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
- if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- return (T) itemStackHandler;
- }
- return super.getCapability(capability, facing);
- }
- public ItemStackHandler filterstack()
- {
- return filterStackHandler;
- }
- public ItemStackHandler temstack()
- {
- return itemStackHandler;
- }
- @Override
- public void update() {
- TileEntityICSB tee = (TileEntityICSB) world.getTileEntity(pos);
- if(!this.world.isRemote)
- {
- counter++;
- if (counter >= currentTickRate)
- {
- counter = 0;
- List<EntityItem> entityItemList = this.world.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(this.pos.add(-5,-5,-5),this.pos.add(6,6,6)),EntitySelectors.IS_ALIVE);
- boolean didSomething = false;
- if(!entityItemList.isEmpty())
- {
- TileEntityICSB te = (TileEntityICSB)world.getTileEntity(pos);
- if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
- {
- IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
- IItemHandler filterHandler = te.filterstack();
- for(EntityItem ei : entityItemList)
- {
- ItemStack eitem = ei.getItem();
- if(!ei.isDead && filterHandler.getStackInSlot(0).getItem() == eitem.getItem() && !filterHandler.getStackInSlot(0).isEmpty())
- {
- ItemStack original = ei.getItem().copy();
- ItemStack left = ItemHandlerHelperIndex.insertItemStacked(itemHandler, original, false, 27);
- if(left.getCount() < original.getCount())
- {
- didSomething=true;
- }
- if(left.isEmpty() || left.getCount() == 0)
- {
- ei.setDead();
- }else
- {
- ei.setItem(left);
- }
- }
- if(!ei.isDead && filterHandler.getStackInSlot(0).isEmpty())
- {
- ItemStack original = ei.getItem().copy();
- ItemStack left = ItemHandlerHelperIndex.insertItemStacked(itemHandler, original, false);
- if(left.getCount() < original.getCount())
- {
- didSomething=true;
- }
- if(left.isEmpty() || left.getCount() == 0)
- {
- ei.setDead();
- }else
- {
- ei.setItem(left);
- }
- }
- }
- }
- }
- if(!didSomething)
- {
- if(currentTickRate < 20)
- {
- currentTickRate++;
- }
- }
- else
- {
- if(currentTickRate > 1)
- {
- currentTickRate--;
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment