2016-07-23 19:20:53 +02:00
|
|
|
/*
|
|
|
|
* This file ("FilterSettings.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;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-11-17 14:56:26 +01:00
|
|
|
import net.minecraft.item.Item;
|
2016-07-23 19:20:53 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-11-20 20:13:26 +01:00
|
|
|
import net.minecraft.util.NonNullList;
|
2016-11-17 14:56:26 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2016-07-24 18:58:08 +02:00
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2016-07-23 19:20:53 +02:00
|
|
|
|
|
|
|
public class FilterSettings{
|
|
|
|
|
2016-09-12 20:45:29 +02:00
|
|
|
public final int whitelistButtonId;
|
|
|
|
public final int metaButtonId;
|
|
|
|
public final int nbtButtonId;
|
|
|
|
public final int oredictButtonId;
|
2016-11-17 14:56:26 +01:00
|
|
|
public final int modButtonId;
|
|
|
|
|
|
|
|
public final int startSlot;
|
|
|
|
public final int endSlot;
|
|
|
|
|
2016-07-23 19:20:53 +02:00
|
|
|
public boolean isWhitelist;
|
|
|
|
public boolean respectMeta;
|
|
|
|
public boolean respectNBT;
|
2016-11-17 14:56:26 +01:00
|
|
|
public boolean respectMod;
|
2016-07-24 18:58:08 +02:00
|
|
|
public int respectOredict;
|
2016-11-17 14:56:26 +01:00
|
|
|
|
2016-09-12 20:45:29 +02:00
|
|
|
private boolean lastWhitelist;
|
|
|
|
private boolean lastRespectMeta;
|
|
|
|
private boolean lastRespectNBT;
|
2016-11-17 14:56:26 +01:00
|
|
|
private boolean lastRespectMod;
|
2016-07-24 18:58:08 +02:00
|
|
|
private int lastRecpectOredict;
|
|
|
|
|
2016-11-17 14:56:26 +01:00
|
|
|
public FilterSettings(int startSlot, int endSlot, boolean defaultWhitelist, boolean defaultRespectMeta, boolean defaultRespectNBT, boolean defaultRespectMod, int defaultRespectOredict, int buttonIdStart){
|
2016-07-23 19:20:53 +02:00
|
|
|
this.startSlot = startSlot;
|
|
|
|
this.endSlot = endSlot;
|
|
|
|
|
|
|
|
this.isWhitelist = defaultWhitelist;
|
|
|
|
this.respectMeta = defaultRespectMeta;
|
|
|
|
this.respectNBT = defaultRespectNBT;
|
2016-11-17 14:56:26 +01:00
|
|
|
this.respectMod = defaultRespectMod;
|
2016-07-24 18:58:08 +02:00
|
|
|
this.respectOredict = defaultRespectOredict;
|
2016-07-23 19:20:53 +02:00
|
|
|
|
|
|
|
this.whitelistButtonId = buttonIdStart;
|
|
|
|
this.metaButtonId = buttonIdStart+1;
|
|
|
|
this.nbtButtonId = buttonIdStart+2;
|
2016-07-24 18:58:08 +02:00
|
|
|
this.oredictButtonId = buttonIdStart+3;
|
2016-11-17 14:56:26 +01:00
|
|
|
this.modButtonId = buttonIdStart+4;
|
2016-07-23 19:20:53 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 20:13:26 +01:00
|
|
|
public static boolean check(ItemStack stack, NonNullList<ItemStack> slots, int startSlot, int endSlot, boolean whitelist, boolean meta, boolean nbt, boolean mod, int oredict){
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(stack)){
|
2016-07-23 19:20:53 +02:00
|
|
|
for(int i = startSlot; i < endSlot; i++){
|
2016-11-20 20:13:26 +01:00
|
|
|
if(StackUtil.isValid(slots.get(i))){
|
|
|
|
if(areEqualEnough(slots.get(i), stack, meta, nbt, mod, oredict)){
|
2016-07-23 19:20:53 +02:00
|
|
|
return whitelist;
|
|
|
|
}
|
2016-11-20 20:13:26 +01:00
|
|
|
else if(slots.get(i).getItem() instanceof ItemFilter){
|
|
|
|
NonNullList<ItemStack> filterSlots = StackUtil.createSlots(ContainerFilter.SLOT_AMOUNT);
|
|
|
|
ItemDrill.loadSlotsFromNBT(filterSlots, slots.get(i));
|
|
|
|
if(filterSlots != null && filterSlots.size() > 0){
|
2016-07-23 19:20:53 +02:00
|
|
|
for(ItemStack filterSlot : filterSlots){
|
2016-11-17 14:56:26 +01:00
|
|
|
if(StackUtil.isValid(filterSlot) && areEqualEnough(filterSlot, stack, meta, nbt, mod, oredict)){
|
2016-07-23 19:20:53 +02:00
|
|
|
return whitelist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !whitelist;
|
|
|
|
}
|
|
|
|
|
2016-11-17 14:56:26 +01:00
|
|
|
private static boolean areEqualEnough(ItemStack first, ItemStack second, boolean meta, boolean nbt, boolean mod, int oredict){
|
|
|
|
Item firstItem = first.getItem();
|
|
|
|
Item secondItem = second.getItem();
|
|
|
|
if(mod){
|
|
|
|
ResourceLocation firstReg = firstItem.getRegistryName();
|
|
|
|
ResourceLocation secondReg = secondItem.getRegistryName();
|
|
|
|
if(firstReg != null && secondReg != null){
|
|
|
|
String firstDomain = firstReg.getResourceDomain();
|
|
|
|
String secondDomain = secondReg.getResourceDomain();
|
|
|
|
if(firstDomain != null && secondDomain != null){
|
|
|
|
if(!firstDomain.equals(secondDomain)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(firstItem != secondItem){
|
2016-07-23 19:20:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-17 14:56:26 +01:00
|
|
|
|
|
|
|
boolean metaFine = !meta || first.getItemDamage() == second.getItemDamage();
|
|
|
|
boolean nbtFine = !nbt || ItemStack.areItemStackTagsEqual(first, second);
|
|
|
|
if(metaFine && nbtFine){
|
|
|
|
if(oredict == 0){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
int[] firstIds = OreDictionary.getOreIDs(first);
|
|
|
|
int[] secondIds = OreDictionary.getOreIDs(second);
|
|
|
|
boolean firstEmpty = ArrayUtils.isEmpty(firstIds);
|
|
|
|
boolean secondEmpty = ArrayUtils.isEmpty(secondIds);
|
|
|
|
|
|
|
|
//Both empty, meaning none has OreDict entries, so they are equal
|
|
|
|
if(firstEmpty && secondEmpty){
|
2016-07-24 18:58:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-17 14:56:26 +01:00
|
|
|
//Only one empty, meaning they are not equal
|
|
|
|
else if(firstEmpty || secondEmpty){
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 18:58:08 +02:00
|
|
|
else{
|
2016-11-17 14:56:26 +01:00
|
|
|
for(int id : firstIds){
|
|
|
|
if(ArrayUtils.contains(secondIds, id)){
|
|
|
|
//Needs to match only one id, so return true on first match
|
|
|
|
if(oredict == 1){
|
|
|
|
return true;
|
2016-07-24 18:58:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 14:56:26 +01:00
|
|
|
//Needs to match every id, so just return false when no match
|
|
|
|
else if(oredict == 2){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-24 18:58:08 +02:00
|
|
|
}
|
2016-11-17 14:56:26 +01:00
|
|
|
//If oredict mode 1, this will fail because nothing matched
|
|
|
|
//If oredict mode 2, this will mean nothing hasn't matched
|
|
|
|
return oredict == 2;
|
2016-07-24 18:58:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 14:56:26 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
return false;
|
2016-07-23 19:20:53 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-12 20:45:29 +02:00
|
|
|
|
|
|
|
public void writeToNBT(NBTTagCompound tag, String name){
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
compound.setBoolean("Whitelist", this.isWhitelist);
|
|
|
|
compound.setBoolean("Meta", this.respectMeta);
|
|
|
|
compound.setBoolean("NBT", this.respectNBT);
|
2016-11-17 14:56:26 +01:00
|
|
|
compound.setBoolean("Mod", this.respectMod);
|
2016-09-12 20:45:29 +02:00
|
|
|
compound.setInteger("Oredict", this.respectOredict);
|
|
|
|
tag.setTag(name, compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound tag, String name){
|
|
|
|
NBTTagCompound compound = tag.getCompoundTag(name);
|
|
|
|
this.isWhitelist = compound.getBoolean("Whitelist");
|
|
|
|
this.respectMeta = compound.getBoolean("Meta");
|
|
|
|
this.respectNBT = compound.getBoolean("NBT");
|
2016-11-17 14:56:26 +01:00
|
|
|
this.respectMod = compound.getBoolean("Mod");
|
2016-09-12 20:45:29 +02:00
|
|
|
this.respectOredict = compound.getInteger("Oredict");
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean needsUpdateSend(){
|
2016-11-17 14:56:26 +01:00
|
|
|
return this.lastWhitelist != this.isWhitelist || this.lastRespectMeta != this.respectMeta || this.lastRespectNBT != this.respectNBT || this.lastRespectMod != this.respectMod || this.lastRecpectOredict != this.respectOredict;
|
2016-09-12 20:45:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateLasts(){
|
|
|
|
this.lastWhitelist = this.isWhitelist;
|
|
|
|
this.lastRespectMeta = this.respectMeta;
|
|
|
|
this.lastRespectNBT = this.respectNBT;
|
2016-11-17 14:56:26 +01:00
|
|
|
this.lastRespectMod = this.respectMod;
|
2016-09-12 20:45:29 +02:00
|
|
|
this.lastRecpectOredict = this.respectOredict;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onButtonPressed(int id){
|
|
|
|
if(id == this.whitelistButtonId){
|
|
|
|
this.isWhitelist = !this.isWhitelist;
|
|
|
|
}
|
|
|
|
else if(id == this.metaButtonId){
|
|
|
|
this.respectMeta = !this.respectMeta;
|
|
|
|
}
|
|
|
|
else if(id == this.nbtButtonId){
|
|
|
|
this.respectNBT = !this.respectNBT;
|
|
|
|
}
|
2016-11-17 14:56:26 +01:00
|
|
|
else if(id == this.modButtonId){
|
|
|
|
this.respectMod = !this.respectMod;
|
|
|
|
}
|
2016-09-12 20:45:29 +02:00
|
|
|
else if(id == this.oredictButtonId){
|
|
|
|
if(this.respectOredict+1 > 2){
|
|
|
|
this.respectOredict = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.respectOredict++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 20:13:26 +01:00
|
|
|
public boolean check(ItemStack stack, NonNullList<ItemStack> slots){
|
2016-11-17 14:56:26 +01:00
|
|
|
return check(stack, slots, this.startSlot, this.endSlot, this.isWhitelist, this.respectMeta, this.respectNBT, this.respectMod, this.respectOredict);
|
2016-09-12 20:45:29 +02:00
|
|
|
}
|
2016-07-23 19:20:53 +02:00
|
|
|
}
|