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

128 lines
4.6 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;
2024-03-04 20:21:48 +01:00
import net.minecraft.core.registries.BuiltInRegistries;
2024-03-02 21:23:08 +01:00
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
2019-02-06 21:27:38 +01:00
public class FilterSettings {
public final ItemStackHandlerAA filterInventory;
2024-03-13 02:05:05 +01:00
public boolean isAllowFilter;
2016-11-17 14:56:26 +01:00
public boolean respectMod;
2016-09-12 20:45:29 +02:00
private boolean lastWhitelist;
2016-11-17 14:56:26 +01:00
private boolean lastRespectMod;
2024-03-06 00:02:32 +01:00
public enum Buttons {
WHITELIST,
MOD
}
2024-03-05 23:23:49 +01:00
public FilterSettings(int slots, boolean defaultWhitelist, boolean defaultRespectMod) {
2024-03-04 20:36:26 +01:00
this.filterInventory = new ItemStackHandlerAA(slots) {
@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
FilterSettings.this.onContentsChanged();
}
};
2024-03-13 02:05:05 +01:00
this.isAllowFilter = defaultWhitelist;
2016-11-17 14:56:26 +01:00
this.respectMod = defaultRespectMod;
}
2024-03-04 20:36:26 +01:00
public void onContentsChanged() {}
2024-03-05 23:23:49 +01:00
public static boolean check(ItemStack stack, ItemStackHandlerAA filter, boolean whitelist, boolean mod) {
2023-03-16 00:17:57 +01:00
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);
2024-03-05 23:23:49 +01:00
if (!filterSlot.isEmpty() && areEqualEnough(filterSlot, stack, mod)) {
2021-02-26 22:15:48 +01:00
return whitelist;
}
}
2024-03-05 23:23:49 +01:00
} else if (areEqualEnough(slot, stack, mod)) {
2021-02-26 22:15:48 +01:00
return whitelist;
}
}
}
}
return !whitelist;
}
2024-03-05 23:23:49 +01:00
private static boolean areEqualEnough(ItemStack first, ItemStack second, boolean mod) {
2016-11-17 14:56:26 +01:00
Item firstItem = first.getItem();
Item secondItem = second.getItem();
2024-03-04 20:21:48 +01:00
if (mod && BuiltInRegistries.ITEM.getKey(firstItem).getNamespace().equals(BuiltInRegistries.ITEM.getKey(secondItem).getNamespace())) {
2021-02-26 22:15:48 +01:00
return true;
}
2016-11-17 14:56:26 +01:00
2024-03-05 23:23:49 +01:00
return firstItem == secondItem;
}
2016-09-12 20:45:29 +02:00
2024-03-02 21:23:08 +01:00
public void writeToNBT(CompoundTag tag, String name) {
CompoundTag compound = new CompoundTag();
2024-03-13 02:05:05 +01:00
compound.putBoolean("Whitelist", this.isAllowFilter);
2021-02-27 16:33:00 +01:00
compound.putBoolean("Mod", this.respectMod);
2024-03-04 20:36:26 +01:00
compound.put("Items", filterInventory.serializeNBT());
2021-11-21 17:31:57 +01:00
tag.put(name, compound);
2016-09-12 20:45:29 +02:00
}
2024-03-02 21:23:08 +01:00
public void readFromNBT(CompoundTag tag, String name) {
CompoundTag compound = tag.getCompound(name);
2024-03-13 02:05:05 +01:00
this.isAllowFilter = compound.getBoolean("Whitelist");
2016-11-17 14:56:26 +01:00
this.respectMod = compound.getBoolean("Mod");
2024-03-04 20:36:26 +01:00
this.filterInventory.deserializeNBT(compound.getCompound("Items"));
2016-09-12 20:45:29 +02:00
}
2019-02-06 21:27:38 +01:00
public boolean needsUpdateSend() {
2024-03-13 02:05:05 +01:00
return this.lastWhitelist != this.isAllowFilter || this.lastRespectMod != this.respectMod;
2016-09-12 20:45:29 +02:00
}
2019-02-06 21:27:38 +01:00
public void updateLasts() {
2024-03-13 02:05:05 +01:00
this.lastWhitelist = this.isAllowFilter;
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) {
2024-03-06 00:02:32 +01:00
if (id == Buttons.WHITELIST.ordinal()) {
2024-03-13 02:05:05 +01:00
this.isAllowFilter = !this.isAllowFilter;
2024-03-06 00:02:32 +01:00
} else if (id == Buttons.MOD.ordinal()) {
2016-11-17 14:56:26 +01:00
this.respectMod = !this.respectMod;
2016-09-12 20:45:29 +02:00
}
}
2019-02-06 21:27:38 +01:00
public boolean check(ItemStack stack) {
2024-03-13 02:05:05 +01:00
return !this.needsCheck() || check(stack, this.filterInventory, this.isAllowFilter, 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;
}
}
2024-03-13 02:05:05 +01:00
return this.isAllowFilter;
2016-09-12 20:45:29 +02:00
}
}