PrettyPipes/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java

160 lines
6.1 KiB
Java
Raw Normal View History

2020-04-16 04:42:42 +02:00
package de.ellpeck.prettypipes.misc;
2020-09-22 19:14:07 +02:00
import com.mojang.blaze3d.matrix.MatrixStack;
2020-04-16 04:42:42 +02:00
import de.ellpeck.prettypipes.PrettyPipes;
2020-04-16 15:19:13 +02:00
import de.ellpeck.prettypipes.packets.PacketButton;
2020-04-16 22:25:58 +02:00
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
2021-03-03 01:56:19 +01:00
import de.ellpeck.prettypipes.pipe.modules.modifier.FilterModifierModuleItem;
2020-04-16 04:42:42 +02:00
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.Widget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
2020-04-16 22:25:58 +02:00
import net.minecraft.util.Direction;
import net.minecraft.util.text.TextFormatting;
2020-09-22 19:14:07 +02:00
import net.minecraft.util.text.TranslationTextComponent;
2020-04-16 04:42:42 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class ItemFilter extends ItemStackHandler {
private final ItemStack stack;
2020-04-16 22:25:58 +02:00
private final PipeTileEntity pipe;
2020-04-17 20:03:54 +02:00
public boolean isWhitelist;
2020-04-16 22:25:58 +02:00
public boolean canPopulateFromInventories;
2020-04-17 20:03:54 +02:00
public boolean canModifyWhitelist = true;
private boolean modified;
2020-04-16 04:42:42 +02:00
2020-04-16 22:25:58 +02:00
public ItemFilter(int size, ItemStack stack, PipeTileEntity pipe) {
2020-04-16 04:42:42 +02:00
super(size);
this.stack = stack;
2020-04-16 22:25:58 +02:00
this.pipe = pipe;
if (stack.hasTag())
this.deserializeNBT(stack.getTag().getCompound("filter"));
2020-04-16 04:42:42 +02:00
}
public List<Slot> getSlots(int x, int y) {
List<Slot> slots = new ArrayList<>();
2020-04-16 21:19:40 +02:00
for (int i = 0; i < this.getSlots(); i++)
2020-10-14 22:04:52 +02:00
slots.add(new FilterSlot(this, i, x + i % 9 * 18, y + i / 9 * 18, true));
2020-04-16 04:42:42 +02:00
return slots;
}
@OnlyIn(Dist.CLIENT)
2020-04-16 22:25:58 +02:00
public List<Widget> getButtons(Screen gui, int x, int y) {
List<Widget> buttons = new ArrayList<>();
2020-04-17 20:03:54 +02:00
if (this.canModifyWhitelist) {
2020-09-22 19:14:07 +02:00
Supplier<TranslationTextComponent> whitelistText = () -> new TranslationTextComponent("info." + PrettyPipes.ID + "." + (this.isWhitelist ? "whitelist" : "blacklist"));
2020-04-17 20:03:54 +02:00
buttons.add(new Button(x, y, 70, 20, whitelistText.get(), button -> {
2020-04-18 22:30:14 +02:00
PacketButton.sendAndExecute(this.pipe.getPos(), PacketButton.ButtonResult.FILTER_CHANGE, 0);
2020-04-17 20:03:54 +02:00
button.setMessage(whitelistText.get());
}));
}
2020-04-16 22:25:58 +02:00
if (this.canPopulateFromInventories) {
2020-09-22 19:14:07 +02:00
buttons.add(new Button(x + 72, y, 70, 20, new TranslationTextComponent("info." + PrettyPipes.ID + ".populate"), button -> PacketButton.sendAndExecute(this.pipe.getPos(), PacketButton.ButtonResult.FILTER_CHANGE, 1)) {
2020-04-16 22:25:58 +02:00
@Override
2020-09-22 19:14:07 +02:00
public void renderToolTip(MatrixStack matrix, int x, int y) {
gui.renderTooltip(matrix, new TranslationTextComponent("info." + PrettyPipes.ID + ".populate.description").mergeStyle(TextFormatting.GRAY), x, y);
2020-04-16 22:25:58 +02:00
}
});
}
return buttons;
2020-04-16 04:42:42 +02:00
}
2020-04-16 15:19:13 +02:00
public void onButtonPacket(int id) {
2020-04-17 20:03:54 +02:00
if (id == 0 && this.canModifyWhitelist) {
2020-04-16 15:19:13 +02:00
this.isWhitelist = !this.isWhitelist;
this.modified = true;
2020-10-14 04:28:58 +02:00
this.save();
2020-04-17 20:03:54 +02:00
} else if (id == 1 && this.canPopulateFromInventories) {
2020-04-16 22:25:58 +02:00
// populate filter from inventories
List<ItemFilter> filters = this.pipe.getFilters();
2020-04-16 22:25:58 +02:00
for (Direction direction : Direction.values()) {
2020-10-15 01:19:22 +02:00
IItemHandler handler = this.pipe.getItemHandler(direction);
2020-04-16 22:25:58 +02:00
if (handler == null)
continue;
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i);
if (stack.isEmpty() || this.isFiltered(stack))
continue;
ItemStack copy = stack.copy();
copy.setCount(1);
2020-10-14 04:28:58 +02:00
// try inserting into ourselves and any filter increase modifiers
for (ItemFilter filter : filters) {
if (ItemHandlerHelper.insertItem(filter, copy, false).isEmpty()) {
filter.save();
2020-10-14 04:28:58 +02:00
break;
}
2020-10-14 04:28:58 +02:00
}
2020-04-16 22:25:58 +02:00
}
}
2020-04-16 15:19:13 +02:00
}
}
2020-04-16 04:42:42 +02:00
public boolean isAllowed(ItemStack stack) {
2020-04-16 22:25:58 +02:00
return this.isFiltered(stack) == this.isWhitelist;
}
private boolean isFiltered(ItemStack stack) {
2021-03-03 01:56:19 +01:00
ItemEquality[] types = getEqualityTypes(this.pipe);
2020-10-14 04:28:58 +02:00
// also check if any filter increase modules have the item we need
for (ItemStackHandler handler : this.pipe.getFilters()) {
2020-10-14 04:28:58 +02:00
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack filter = handler.getStackInSlot(i);
if (filter.isEmpty())
continue;
2021-03-03 01:56:19 +01:00
if (ItemEquality.compareItems(stack, filter, types))
2020-10-14 04:28:58 +02:00
return true;
}
2020-04-16 04:42:42 +02:00
}
2020-04-16 22:25:58 +02:00
return false;
2020-04-16 04:42:42 +02:00
}
public void save() {
if (this.modified) {
this.stack.getOrCreateTag().put("filter", this.serializeNBT());
this.modified = false;
}
2020-04-16 04:42:42 +02:00
}
@Override
public CompoundNBT serializeNBT() {
CompoundNBT nbt = super.serializeNBT();
2020-10-14 04:28:58 +02:00
if (this.canModifyWhitelist)
nbt.putBoolean("whitelist", this.isWhitelist);
2020-04-16 04:42:42 +02:00
return nbt;
}
@Override
public void deserializeNBT(CompoundNBT nbt) {
super.deserializeNBT(nbt);
2020-10-14 04:28:58 +02:00
if (this.canModifyWhitelist)
this.isWhitelist = nbt.getBoolean("whitelist");
2020-04-16 04:42:42 +02:00
}
2020-04-16 15:19:13 +02:00
@Override
protected void onContentsChanged(int slot) {
this.modified = true;
}
2021-03-03 01:56:19 +01:00
public static ItemEquality[] getEqualityTypes(PipeTileEntity pipe) {
2020-10-14 23:39:11 +02:00
return pipe.streamModules()
2021-03-03 01:56:19 +01:00
.filter(m -> m.getRight() instanceof FilterModifierModuleItem)
.map(m -> ((FilterModifierModuleItem) m.getRight()).getEqualityType(m.getLeft()))
.toArray(ItemEquality[]::new);
2020-10-14 23:39:11 +02:00
}
2020-04-16 15:19:13 +02:00
public interface IFilteredContainer {
ItemFilter getFilter();
}
2020-04-16 04:42:42 +02:00
}