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

140 lines
5 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;
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 {
public final ItemStackHandlerAA filterInventory;
public boolean isWhitelist;
public boolean respectNBT;
2016-11-17 14:56:26 +01:00
public boolean respectMod;
2016-09-12 20:45:29 +02:00
private boolean lastWhitelist;
private boolean lastRespectNBT;
2016-11-17 14:56:26 +01:00
private boolean lastRespectMod;
2023-03-16 00:17:57 +01:00
public enum Buttons {
WHITELIST,
NBT,
MOD
}
public FilterSettings(int slots, boolean defaultWhitelist, boolean defaultRespectNBT, boolean defaultRespectMod) {
this.filterInventory = new ItemStackHandlerAA(slots);
this.isWhitelist = defaultWhitelist;
this.respectNBT = defaultRespectNBT;
2016-11-17 14:56:26 +01:00
this.respectMod = defaultRespectMod;
}
2023-03-16 00:17:57 +01:00
public static boolean check(ItemStack stack, ItemStackHandlerAA filter, boolean whitelist, boolean nbt, boolean mod) {
if (!stack.isEmpty()) {
2019-02-06 21:27:38 +01:00
for (int i = 0; i < filter.getSlots(); i++) {
ItemStack slot = filter.getStackInSlot(i);
2023-03-16 00:17:57 +01:00
if (!slot.isEmpty()) {
2019-02-06 21:27:38 +01:00
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);
2023-03-16 00:17:57 +01:00
if (!filterSlot.isEmpty() && areEqualEnough(filterSlot, stack, nbt, mod)) {
2021-02-26 22:15:48 +01:00
return whitelist;
}
}
2023-03-16 00:17:57 +01:00
} else if (areEqualEnough(slot, stack, nbt, mod)) {
2021-02-26 22:15:48 +01:00
return whitelist;
}
}
}
}
return !whitelist;
}
2023-03-16 00:17:57 +01:00
private static boolean areEqualEnough(ItemStack first, ItemStack second, boolean nbt, boolean mod) {
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-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("NBT", this.respectNBT);
compound.putBoolean("Mod", this.respectMod);
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.respectNBT = compound.getBoolean("NBT");
2016-11-17 14:56:26 +01:00
this.respectMod = compound.getBoolean("Mod");
TileEntityInventoryBase.loadSlots(this.filterInventory, compound);
2016-09-12 20:45:29 +02:00
}
2019-02-06 21:27:38 +01:00
public boolean needsUpdateSend() {
2023-03-16 00:17:57 +01:00
return this.lastWhitelist != this.isWhitelist || this.lastRespectNBT != this.respectNBT || this.lastRespectMod != this.respectMod;
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.lastRespectNBT = this.respectNBT;
2016-11-17 14:56:26 +01:00
this.lastRespectMod = this.respectMod;
2016-09-12 20:45:29 +02:00
}
2019-02-06 21:27:38 +01:00
public void onButtonPressed(int id) {
2023-03-16 00:17:57 +01:00
if (id == Buttons.WHITELIST.ordinal()) {
2016-09-12 20:45:29 +02:00
this.isWhitelist = !this.isWhitelist;
2023-03-16 00:17:57 +01:00
} else if (id == Buttons.NBT.ordinal()) {
2016-09-12 20:45:29 +02:00
this.respectNBT = !this.respectNBT;
2023-03-16 00:17:57 +01:00
} else if (id == Buttons.MOD.ordinal()) {
2016-11-17 14:56:26 +01:00
this.respectMod = !this.respectMod;
2019-02-06 21:27:38 +01:00
if (this.respectMod) {
this.respectNBT = false;
2016-09-12 20:45:29 +02:00
}
}
}
2019-02-06 21:27:38 +01:00
public boolean check(ItemStack stack) {
2023-03-16 00:17:57 +01:00
return !this.needsCheck() || check(stack, this.filterInventory, this.isWhitelist, this.respectNBT, this.respectMod);
}
2019-02-06 21:27:38 +01:00
public boolean needsCheck() {
for (int i = 0; i < this.filterInventory.getSlots(); i++) {
2023-03-16 00:17:57 +01:00
if (!this.filterInventory.getStackInSlot(i).isEmpty()) {
2021-02-26 22:15:48 +01:00
return true;
}
}
return this.isWhitelist;
2016-09-12 20:45:29 +02:00
}
}