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

200 lines
7.9 KiB
Java
Raw Normal View History

/*
* 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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFilter;
import de.ellpeck.actuallyadditions.mod.inventory.slot.SlotFilter;
2021-11-25 21:27:45 +01:00
import de.ellpeck.actuallyadditions.mod.items.DrillItem;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-11-17 14:56:26 +01:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2019-02-06 21:27:38 +01: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 ItemStackHandlerAA filterInventory;
public boolean isWhitelist;
public boolean respectMeta;
public boolean respectNBT;
2016-11-17 14:56:26 +01:00
public boolean respectMod;
public int respectOredict;
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;
private int lastRecpectOredict;
2019-02-06 21:27:38 +01:00
public FilterSettings(int slots, boolean defaultWhitelist, boolean defaultRespectMeta, boolean defaultRespectNBT, boolean defaultRespectMod, int defaultRespectOredict, int buttonIdStart) {
this.filterInventory = new ItemStackHandlerAA(slots);
this.isWhitelist = defaultWhitelist;
this.respectMeta = defaultRespectMeta;
this.respectNBT = defaultRespectNBT;
2016-11-17 14:56:26 +01:00
this.respectMod = defaultRespectMod;
this.respectOredict = defaultRespectOredict;
this.whitelistButtonId = buttonIdStart;
2019-02-06 21:27:38 +01:00
this.metaButtonId = buttonIdStart + 1;
this.nbtButtonId = buttonIdStart + 2;
this.oredictButtonId = buttonIdStart + 3;
this.modButtonId = buttonIdStart + 4;
}
2019-02-06 21:27:38 +01:00
public static boolean check(ItemStack stack, ItemStackHandlerAA filter, boolean whitelist, boolean meta, boolean nbt, boolean mod, int oredict) {
if (StackUtil.isValid(stack)) {
for (int i = 0; i < filter.getSlots(); i++) {
ItemStack slot = filter.getStackInSlot(i);
2019-02-06 21:27:38 +01:00
if (StackUtil.isValid(slot)) {
if (SlotFilter.isFilter(slot)) {
ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerFilter.SLOT_AMOUNT);
2021-11-25 21:27:45 +01:00
DrillItem.loadSlotsFromNBT(inv, slot);
2019-02-06 21:27:38 +01:00
for (int k = 0; k < inv.getSlots(); k++) {
ItemStack filterSlot = inv.getStackInSlot(k);
2021-02-26 22:15:48 +01:00
if (StackUtil.isValid(filterSlot) && areEqualEnough(filterSlot, stack, meta, nbt, mod, oredict)) {
return whitelist;
}
}
2021-02-26 22:15:48 +01:00
} else if (areEqualEnough(slot, stack, meta, nbt, mod, oredict)) {
return whitelist;
}
}
}
}
return !whitelist;
}
2019-02-06 21:27:38 +01:00
private static boolean areEqualEnough(ItemStack first, ItemStack second, boolean meta, boolean nbt, boolean mod, int oredict) {
2016-11-17 14:56:26 +01:00
Item firstItem = first.getItem();
Item secondItem = second.getItem();
2021-02-26 22:15:48 +01:00
if (mod && firstItem.getRegistryName().getNamespace().equals(secondItem.getRegistryName().getNamespace())) {
return true;
}
2016-11-17 14:56:26 +01:00
2021-11-21 17:31:57 +01:00
// if (oredict != 0) {
// boolean firstEmpty = ArrayUtils.isEmpty(firstIds);
// boolean secondEmpty = ArrayUtils.isEmpty(secondIds);
//
// //Both empty, meaning none has OreDict entries, so they are equal
// if (firstEmpty && secondEmpty) {
// return true;
// }
// //Only one empty, meaning they are not equal
// else if (firstEmpty || secondEmpty) {
// return false;
// } else {
// 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;
// }
// }
// //Needs to match every id, so just return false when no match
// else if (oredict == 2) {
// return false;
// }
//
// }
// //If oredict mode 1, this will fail because nothing matched
// //If oredict mode 2, this will mean nothing hasn't matched
// return oredict == 2;
// }
// }
2019-02-06 21:27:38 +01:00
2021-02-26 22:15:48 +01:00
if (firstItem != secondItem) {
return false;
}
2019-02-06 21:27:38 +01:00
boolean nbtFine = !nbt || ItemStack.tagMatches(first, second);
2021-11-21 17:31:57 +01:00
if (nbtFine) {
2021-02-26 22:15:48 +01:00
return true;
}
2019-02-06 21:27:38 +01:00
return false;
}
2016-09-12 20:45:29 +02:00
2021-02-26 22:15:48 +01:00
public void writeToNBT(CompoundNBT tag, String name) {
CompoundNBT compound = new CompoundNBT();
2021-02-27 16:33:00 +01:00
compound.putBoolean("Whitelist", this.isWhitelist);
compound.putBoolean("Meta", this.respectMeta);
compound.putBoolean("NBT", this.respectNBT);
compound.putBoolean("Mod", this.respectMod);
compound.putInt("Oredict", this.respectOredict);
TileEntityInventoryBase.saveSlots(this.filterInventory, compound);
2021-11-21 17:31:57 +01:00
tag.put(name, compound);
2016-09-12 20:45:29 +02:00
}
2021-02-26 22:15:48 +01:00
public void readFromNBT(CompoundNBT tag, String name) {
2021-11-21 17:31:57 +01:00
CompoundNBT compound = tag.getCompound(name);
2016-09-12 20:45:29 +02:00
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");
2021-02-27 16:33:00 +01:00
this.respectOredict = compound.getInt("Oredict");
TileEntityInventoryBase.loadSlots(this.filterInventory, compound);
2016-09-12 20:45:29 +02:00
}
2019-02-06 21:27:38 +01:00
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
}
2019-02-06 21:27:38 +01:00
public void updateLasts() {
2016-09-12 20:45:29 +02:00
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;
}
2019-02-06 21:27:38 +01:00
public void onButtonPressed(int id) {
if (id == this.whitelistButtonId) {
2016-09-12 20:45:29 +02:00
this.isWhitelist = !this.isWhitelist;
2019-02-06 21:27:38 +01:00
} else if (id == this.metaButtonId) {
2016-09-12 20:45:29 +02:00
this.respectMeta = !this.respectMeta;
2019-02-06 21:27:38 +01:00
} else if (id == this.nbtButtonId) {
2016-09-12 20:45:29 +02:00
this.respectNBT = !this.respectNBT;
2019-02-06 21:27:38 +01:00
} else if (id == this.modButtonId) {
2016-11-17 14:56:26 +01:00
this.respectMod = !this.respectMod;
2019-02-06 21:27:38 +01:00
if (this.respectMod) {
this.respectMeta = false;
this.respectNBT = false;
this.respectOredict = 0;
}
2019-02-06 21:27:38 +01:00
} else if (id == this.oredictButtonId) {
if (this.respectOredict + 1 > 2) {
2016-09-12 20:45:29 +02:00
this.respectOredict = 0;
2019-02-06 21:27:38 +01:00
} else {
2016-09-12 20:45:29 +02:00
this.respectOredict++;
}
}
}
2019-02-06 21:27:38 +01:00
public boolean check(ItemStack stack) {
return !this.needsCheck() || check(stack, this.filterInventory, this.isWhitelist, this.respectMeta, this.respectNBT, this.respectMod, this.respectOredict);
}
2019-02-06 21:27:38 +01:00
public boolean needsCheck() {
for (int i = 0; i < this.filterInventory.getSlots(); i++) {
2021-02-26 22:15:48 +01:00
if (StackUtil.isValid(this.filterInventory.getStackInSlot(i))) {
return true;
}
}
return this.isWhitelist;
2016-09-12 20:45:29 +02:00
}
}