ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java

272 lines
9.3 KiB
Java

/*
* 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.NonNullList;
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 NonNullList<ItemStack> slots = StackUtil.createSlots(24);
public TileEntityLaserRelayItemWhitelist(){
super("laserRelayItemWhitelist");
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(){
this.tile.slots.clear();
}
@Override
public void setInventorySlotContents(int i, ItemStack stack){
this.tile.slots.set(i, stack);
this.markDirty();
}
@Override
public int getSizeInventory(){
return this.tile.slots.size();
}
@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.get(i);
}
return StackUtil.getNull();
}
@Override
public ItemStack decrStackSize(int i, int j){
if(StackUtil.isValid(this.tile.slots.get(i))){
ItemStack stackAt;
if(StackUtil.getStackSize(this.tile.slots.get(i)) <= j){
stackAt = this.tile.slots.get(i);
this.tile.slots.set(i, StackUtil.getNull());
this.markDirty();
return stackAt;
}
else{
stackAt = this.tile.slots.get(i).splitStack(j);
if(StackUtil.getStackSize(this.tile.slots.get(i)) <= 0){
this.tile.slots.set(i, StackUtil.getNull());
}
this.markDirty();
return stackAt;
}
}
return StackUtil.getNull();
}
@Override
public ItemStack removeStackFromSlot(int index){
ItemStack stack = this.tile.slots.get(index);
this.tile.slots.set(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.get(k))){
if(this.slots.get(k).getItem() instanceof ItemFilter){
NonNullList<ItemStack> filterSlots = StackUtil.createSlots(ContainerFilter.SLOT_AMOUNT);
ItemDrill.loadSlotsFromNBT(filterSlots, this.slots.get(k));
boolean did = false;
if(filterSlots != null && filterSlots.size() > 0){
for(int j = 0; j < filterSlots.size(); j++){
if(!StackUtil.isValid(filterSlots.get(j))){
filterSlots.set(j, copy);
did = true;
break;
}
}
}
if(did){
ItemDrill.writeSlotsToNBT(filterSlots, this.slots.get(k));
break;
}
}
}
else{
this.slots.set(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();
}
}
}
}