2016-05-16 22:52:27 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2016-05-16 22:52:27 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-06-07 23:17:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFilter;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemDrill;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemFilter;
|
2016-05-14 17:50:10 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-05-14 17:50:10 +02:00
|
|
|
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;
|
2016-05-27 18:54:17 +02:00
|
|
|
import net.minecraft.util.text.TextComponentTranslation;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
public class TileEntityLaserRelayItemWhitelist extends TileEntityLaserRelayItem implements IButtonReactor{
|
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
public final IInventory filterInventory;
|
2016-07-24 18:58:08 +02:00
|
|
|
public FilterSettings leftFilter = new FilterSettings(0, 12, true, true, false, 0, -1000);
|
|
|
|
public FilterSettings rightFilter = new FilterSettings(12, 24, true, true, false, 0, -2000);
|
2016-09-12 20:45:29 +02:00
|
|
|
private ItemStack[] slots = new ItemStack[24];
|
2016-05-14 17:50:10 +02:00
|
|
|
|
|
|
|
public TileEntityLaserRelayItemWhitelist(){
|
|
|
|
super("laserRelayItemWhitelist");
|
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
Arrays.fill(this.slots, StackUtil.getNull());
|
2016-05-14 17:50:10 +02:00
|
|
|
this.filterInventory = new IInventory(){
|
|
|
|
|
2016-05-27 18:54:17 +02:00
|
|
|
private TileEntityLaserRelayItemWhitelist tile;
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-05-27 18:54:17 +02:00
|
|
|
private IInventory setTile(TileEntityLaserRelayItemWhitelist tile){
|
2016-05-14 17:50:10 +02:00
|
|
|
this.tile = tile;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
@Override
|
|
|
|
public String getName(){
|
|
|
|
return this.tile.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit(){
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void markDirty(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isUseableByPlayer(EntityPlayer player){
|
2016-05-14 17:50:10 +02:00
|
|
|
return this.tile.canPlayerUse(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public void openInventory(EntityPlayer player){
|
2016-05-14 17:50:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public void closeInventory(EntityPlayer player){
|
2016-05-14 17:50:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@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];
|
2016-11-16 16:59:00 +01:00
|
|
|
Arrays.fill(this.tile.slots, StackUtil.getNull());
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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 ItemStack getStackInSlot(int i){
|
|
|
|
if(i < this.getSizeInventory()){
|
|
|
|
return this.tile.slots[i];
|
|
|
|
}
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int i, int j){
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(this.tile.slots[i])){
|
2016-05-14 17:50:10 +02:00
|
|
|
ItemStack stackAt;
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.getStackSize(this.tile.slots[i]) <= j){
|
2016-05-14 17:50:10 +02:00
|
|
|
stackAt = this.tile.slots[i];
|
2016-11-16 16:59:00 +01:00
|
|
|
this.tile.slots[i] = StackUtil.getNull();
|
2016-05-14 17:50:10 +02:00
|
|
|
this.markDirty();
|
|
|
|
return stackAt;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
stackAt = this.tile.slots[i].splitStack(j);
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.getStackSize(this.tile.slots[i]) <= 0){
|
|
|
|
this.tile.slots[i] = StackUtil.getNull();
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
this.markDirty();
|
|
|
|
return stackAt;
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack removeStackFromSlot(int index){
|
|
|
|
ItemStack stack = this.tile.slots[index];
|
2016-11-16 16:59:00 +01:00
|
|
|
this.tile.slots[index] = StackUtil.getNull();
|
2016-05-14 17:50:10 +02:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCustomName(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
@Override
|
|
|
|
public ITextComponent getDisplayName(){
|
2016-05-27 18:54:17 +02:00
|
|
|
return new TextComponentTranslation(this.getName());
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack){
|
2016-05-14 17:50:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}.setTile(this);
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:50:38 +02:00
|
|
|
@Override
|
|
|
|
public boolean isWhitelisted(ItemStack stack, boolean output){
|
2016-07-23 19:20:53 +02:00
|
|
|
return output ? this.rightFilter.check(stack, this.slots) : this.leftFilter.check(stack, this.slots);
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
if(type == NBTType.SAVE_TILE){
|
2016-06-17 23:50:38 +02:00
|
|
|
TileEntityInventoryBase.saveSlots(this.slots, compound);
|
|
|
|
}
|
2016-07-23 19:20:53 +02:00
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
this.leftFilter.writeToNBT(compound, "LeftFilter");
|
|
|
|
this.rightFilter.writeToNBT(compound, "RightFilter");
|
|
|
|
}
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
if(type == NBTType.SAVE_TILE){
|
2016-05-14 17:50:10 +02:00
|
|
|
TileEntityInventoryBase.loadSlots(this.slots, compound);
|
|
|
|
}
|
2016-07-23 19:20:53 +02:00
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
this.leftFilter.readFromNBT(compound, "LeftFilter");
|
|
|
|
this.rightFilter.readFromNBT(compound, "RightFilter");
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onButtonPressed(int buttonID, EntityPlayer player){
|
2016-07-23 19:20:53 +02:00
|
|
|
this.leftFilter.onButtonPressed(buttonID);
|
|
|
|
this.rightFilter.onButtonPressed(buttonID);
|
|
|
|
if(buttonID == 2){
|
2016-06-06 17:59:01 +02:00
|
|
|
this.addWhitelistSmart(false);
|
|
|
|
}
|
|
|
|
else if(buttonID == 3){
|
|
|
|
this.addWhitelistSmart(true);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 17:59:01 +02:00
|
|
|
private void addWhitelistSmart(boolean output){
|
2016-07-23 19:20:53 +02:00
|
|
|
FilterSettings usedSettings = output ? this.rightFilter : this.leftFilter;
|
2016-08-06 02:17:11 +02:00
|
|
|
for(IItemHandler handler : this.handlersAround.values()){
|
2016-05-14 17:50:10 +02:00
|
|
|
for(int i = 0; i < handler.getSlots(); i++){
|
|
|
|
ItemStack stack = handler.getStackInSlot(i);
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
2016-06-07 23:17:06 +02:00
|
|
|
ItemStack copy = stack.copy();
|
2016-11-16 16:59:00 +01:00
|
|
|
copy = StackUtil.setStackSize(copy, 1);
|
2016-06-07 23:17:06 +02:00
|
|
|
|
2016-07-24 18:58:08 +02:00
|
|
|
if(!FilterSettings.check(copy, this.slots, usedSettings.startSlot, usedSettings.endSlot, true, usedSettings.respectMeta, usedSettings.respectNBT, usedSettings.respectOredict)){
|
2016-07-23 19:20:53 +02:00
|
|
|
for(int k = usedSettings.startSlot; k < usedSettings.endSlot; k++){
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(this.slots[k])){
|
2016-06-10 17:53:11 +02:00
|
|
|
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++){
|
2016-11-16 16:59:00 +01:00
|
|
|
if(!StackUtil.isValid(filterSlots[j])){
|
2016-06-10 17:53:11 +02:00
|
|
|
filterSlots[j] = copy;
|
|
|
|
did = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-07 23:17:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:53:11 +02:00
|
|
|
if(did){
|
|
|
|
ItemDrill.writeSlotsToNBT(filterSlots, this.slots[k]);
|
|
|
|
break;
|
|
|
|
}
|
2016-06-07 23:17:06 +02:00
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
2016-06-10 17:53:11 +02:00
|
|
|
else{
|
|
|
|
this.slots[k] = copy;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-07 23:17:06 +02:00
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
super.updateEntity();
|
|
|
|
|
|
|
|
if(!this.worldObj.isRemote){
|
2016-07-23 19:20:53 +02:00
|
|
|
if((this.leftFilter.needsUpdateSend() || this.rightFilter.needsUpdateSend()) && this.sendUpdateWithInterval()){
|
|
|
|
this.leftFilter.updateLasts();
|
|
|
|
this.rightFilter.updateLasts();
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|