/* * This file ("TileEntityLaserRelayItemWhitelist.java") is part of the Actually Additions mod for Minecraft. * It is created and owned by Ellpeck and distributed * under the Actually Additions License to be found at * http://ellpeck.de/actaddlicense * View the source code at https://github.com/Ellpeck/ActuallyAdditions * * © 2015-2016 Ellpeck */ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.inventory.ContainerFilter; import de.ellpeck.actuallyadditions.mod.items.ItemDrill; import de.ellpeck.actuallyadditions.mod.items.ItemFilter; import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor; import de.ellpeck.actuallyadditions.mod.util.StackUtil; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentTranslation; import net.minecraftforge.items.IItemHandler; import java.util.Arrays; public class TileEntityLaserRelayItemWhitelist extends TileEntityLaserRelayItem implements IButtonReactor{ public final IInventory filterInventory; public FilterSettings leftFilter = new FilterSettings(0, 12, true, true, false, false, 0, -1000); public FilterSettings rightFilter = new FilterSettings(12, 24, true, true, false, false, 0, -2000); private ItemStack[] slots = new ItemStack[24]; public TileEntityLaserRelayItemWhitelist(){ super("laserRelayItemWhitelist"); Arrays.fill(this.slots, StackUtil.getNull()); this.filterInventory = new IInventory(){ private TileEntityLaserRelayItemWhitelist tile; private IInventory setTile(TileEntityLaserRelayItemWhitelist tile){ this.tile = tile; return this; } @Override public String getName(){ return this.tile.name; } @Override public int getInventoryStackLimit(){ return 64; } @Override public void markDirty(){ } @Override public boolean isUseableByPlayer(EntityPlayer player){ return this.tile.canPlayerUse(player); } @Override public void openInventory(EntityPlayer player){ } @Override public void closeInventory(EntityPlayer player){ } @Override public int getField(int id){ return 0; } @Override public void setField(int id, int value){ } @Override public int getFieldCount(){ return 0; } @Override public void clear(){ int length = this.tile.slots.length; this.tile.slots = new ItemStack[length]; Arrays.fill(this.tile.slots, StackUtil.getNull()); } @Override public void setInventorySlotContents(int i, ItemStack stack){ this.tile.slots[i] = stack; this.markDirty(); } @Override public int getSizeInventory(){ return this.tile.slots.length; } @Override public boolean func_191420_l(){ return StackUtil.isIInvEmpty(this.tile.slots); } @Override public ItemStack getStackInSlot(int i){ if(i < this.getSizeInventory()){ return this.tile.slots[i]; } return StackUtil.getNull(); } @Override public ItemStack decrStackSize(int i, int j){ if(StackUtil.isValid(this.tile.slots[i])){ ItemStack stackAt; if(StackUtil.getStackSize(this.tile.slots[i]) <= j){ stackAt = this.tile.slots[i]; this.tile.slots[i] = StackUtil.getNull(); this.markDirty(); return stackAt; } else{ stackAt = this.tile.slots[i].splitStack(j); if(StackUtil.getStackSize(this.tile.slots[i]) <= 0){ this.tile.slots[i] = StackUtil.getNull(); } this.markDirty(); return stackAt; } } return StackUtil.getNull(); } @Override public ItemStack removeStackFromSlot(int index){ ItemStack stack = this.tile.slots[index]; this.tile.slots[index] = StackUtil.getNull(); return stack; } @Override public boolean hasCustomName(){ return false; } @Override public ITextComponent getDisplayName(){ return new TextComponentTranslation(this.getName()); } @Override public boolean isItemValidForSlot(int index, ItemStack stack){ return false; } }.setTile(this); } @Override public int getPriority(){ return super.getPriority()+10; } @Override public boolean isWhitelisted(ItemStack stack, boolean output){ return output ? this.rightFilter.check(stack, this.slots) : this.leftFilter.check(stack, this.slots); } @Override public void writeSyncableNBT(NBTTagCompound compound, NBTType type){ super.writeSyncableNBT(compound, type); if(type == NBTType.SAVE_TILE){ TileEntityInventoryBase.saveSlots(this.slots, compound); } if(type != NBTType.SAVE_BLOCK){ this.leftFilter.writeToNBT(compound, "LeftFilter"); this.rightFilter.writeToNBT(compound, "RightFilter"); } } @Override public void readSyncableNBT(NBTTagCompound compound, NBTType type){ super.readSyncableNBT(compound, type); if(type == NBTType.SAVE_TILE){ TileEntityInventoryBase.loadSlots(this.slots, compound); } if(type != NBTType.SAVE_BLOCK){ this.leftFilter.readFromNBT(compound, "LeftFilter"); this.rightFilter.readFromNBT(compound, "RightFilter"); } } @Override public void onButtonPressed(int buttonID, EntityPlayer player){ this.leftFilter.onButtonPressed(buttonID); this.rightFilter.onButtonPressed(buttonID); if(buttonID == 2){ this.addWhitelistSmart(false); } else if(buttonID == 3){ this.addWhitelistSmart(true); } } private void addWhitelistSmart(boolean output){ FilterSettings usedSettings = output ? this.rightFilter : this.leftFilter; for(IItemHandler handler : this.handlersAround.values()){ for(int i = 0; i < handler.getSlots(); i++){ ItemStack stack = handler.getStackInSlot(i); if(StackUtil.isValid(stack)){ ItemStack copy = stack.copy(); copy = StackUtil.setStackSize(copy, 1); if(!FilterSettings.check(copy, this.slots, usedSettings.startSlot, usedSettings.endSlot, true, usedSettings.respectMeta, usedSettings.respectNBT, usedSettings.respectMod, usedSettings.respectOredict)){ for(int k = usedSettings.startSlot; k < usedSettings.endSlot; k++){ if(StackUtil.isValid(this.slots[k])){ if(this.slots[k].getItem() instanceof ItemFilter){ ItemStack[] filterSlots = new ItemStack[ContainerFilter.SLOT_AMOUNT]; ItemDrill.loadSlotsFromNBT(filterSlots, this.slots[k]); boolean did = false; if(filterSlots != null && filterSlots.length > 0){ for(int j = 0; j < filterSlots.length; j++){ if(!StackUtil.isValid(filterSlots[j])){ filterSlots[j] = copy; did = true; break; } } } if(did){ ItemDrill.writeSlotsToNBT(filterSlots, this.slots[k]); break; } } } else{ this.slots[k] = copy; break; } } } } } } } @Override public void updateEntity(){ super.updateEntity(); if(!this.worldObj.isRemote){ if((this.leftFilter.needsUpdateSend() || this.rightFilter.needsUpdateSend()) && this.sendUpdateWithInterval()){ this.leftFilter.updateLasts(); this.rightFilter.updateLasts(); } } } }