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

153 lines
5.6 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;
2020-05-02 18:56:54 +02:00
import de.ellpeck.prettypipes.pipe.modules.FilterModifierModule;
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.client.resources.I18n;
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;
2020-05-02 18:56:54 +02:00
import org.apache.commons.lang3.tuple.Pair;
2020-04-16 04:42:42 +02:00
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
2020-05-02 18:56:54 +02:00
import java.util.stream.Collectors;
2020-04-16 04:42:42 +02:00
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-05-07 18:30:40 +02:00
slots.add(new FilterSlot(this, i, x + i % 9 * 18, y + i / 9 * 18));
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-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
for (Direction direction : Direction.values()) {
IItemHandler handler = this.pipe.getItemHandler(direction, null);
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);
ItemHandlerHelper.insertItem(this, copy, false);
}
}
this.modified = true;
2020-04-16 15:19:13 +02:00
}
this.save();
}
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) {
ItemEqualityType[] types = this.getEqualityTypes();
2020-04-16 04:42:42 +02:00
for (int i = 0; i < this.getSlots(); i++) {
2020-05-02 18:56:54 +02:00
ItemStack filter = this.getStackInSlot(i);
2020-05-07 21:10:29 +02:00
if (filter.isEmpty())
2020-05-02 18:56:54 +02:00
continue;
if (ItemEqualityType.compareItems(stack, filter, types))
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 ItemEqualityType[] getEqualityTypes() {
return this.pipe.streamModules()
.map(Pair::getRight)
.filter(m -> m instanceof FilterModifierModule)
.map(m -> ((FilterModifierModule) m).type)
.toArray(ItemEqualityType[]::new);
}
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();
nbt.putBoolean("whitelist", this.isWhitelist);
return nbt;
}
@Override
public void deserializeNBT(CompoundNBT nbt) {
super.deserializeNBT(nbt);
this.isWhitelist = nbt.getBoolean("whitelist");
}
2020-04-16 15:19:13 +02:00
@Override
protected void onContentsChanged(int slot) {
this.modified = true;
}
2020-04-16 15:19:13 +02:00
public interface IFilteredContainer {
ItemFilter getFilter();
}
2020-04-16 04:42:42 +02:00
}