generify the filter system for the future(tm)

This commit is contained in:
Ell 2021-03-03 00:10:24 +01:00
parent 608de4ed24
commit 4c0862c9ed
12 changed files with 53 additions and 30 deletions

View file

@ -1,5 +1,6 @@
package de.ellpeck.prettypipes.items; package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
@ -41,4 +42,6 @@ public interface IModule {
ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain); ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain);
Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index); Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index);
ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile);
} }

View file

@ -2,6 +2,7 @@ package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.client.util.ITooltipFlag; import net.minecraft.client.util.ITooltipFlag;
@ -98,4 +99,9 @@ public abstract class ModuleItem extends Item implements IModule {
public Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index) { public Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index) {
return null; return null;
} }
@Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) {
return null;
}
} }

View file

@ -5,7 +5,6 @@ import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.packets.PacketButton; import de.ellpeck.prettypipes.packets.PacketButton;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.modules.FilterModifierModuleItem; import de.ellpeck.prettypipes.pipe.modules.FilterModifierModuleItem;
import de.ellpeck.prettypipes.pipe.modules.filter.FilterIncreaseModuleItem;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.Widget; import net.minecraft.client.gui.widget.Widget;
import net.minecraft.client.gui.widget.button.Button; import net.minecraft.client.gui.widget.button.Button;
@ -25,7 +24,6 @@ import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
public class ItemFilter extends ItemStackHandler { public class ItemFilter extends ItemStackHandler {
@ -80,7 +78,7 @@ public class ItemFilter extends ItemStackHandler {
this.save(); this.save();
} else if (id == 1 && this.canPopulateFromInventories) { } else if (id == 1 && this.canPopulateFromInventories) {
// populate filter from inventories // populate filter from inventories
List<ItemFilter> filters = this.getAllFilters(); List<ItemFilter> filters = this.pipe.getFilters();
for (Direction direction : Direction.values()) { for (Direction direction : Direction.values()) {
IItemHandler handler = this.pipe.getItemHandler(direction); IItemHandler handler = this.pipe.getItemHandler(direction);
if (handler == null) if (handler == null)
@ -110,7 +108,7 @@ public class ItemFilter extends ItemStackHandler {
private boolean isFiltered(ItemStack stack) { private boolean isFiltered(ItemStack stack) {
ItemEqualityType[] types = getEqualityTypes(this.pipe); ItemEqualityType[] types = getEqualityTypes(this.pipe);
// also check if any filter increase modules have the item we need // also check if any filter increase modules have the item we need
for (ItemStackHandler handler : this.getAllFilters()) { for (ItemStackHandler handler : this.pipe.getFilters()) {
for (int i = 0; i < handler.getSlots(); i++) { for (int i = 0; i < handler.getSlots(); i++) {
ItemStack filter = handler.getStackInSlot(i); ItemStack filter = handler.getStackInSlot(i);
if (filter.isEmpty()) if (filter.isEmpty())
@ -122,16 +120,6 @@ public class ItemFilter extends ItemStackHandler {
return false; return false;
} }
public List<ItemFilter> getAllFilters() {
List<ItemFilter> filters = this.pipe.streamModules()
.filter(p -> p.getRight() instanceof FilterIncreaseModuleItem)
.map(p -> new ItemFilter(18, p.getLeft(), this.pipe))
.collect(Collectors.toList());
// add ourselves to the front
filters.add(0, this);
return filters;
}
public void save() { public void save() {
if (this.modified) { if (this.modified) {
this.stack.getOrCreateTag().put("filter", this.serializeNBT()); this.stack.getOrCreateTag().put("filter", this.serializeNBT());

View file

@ -5,6 +5,7 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.misc.ItemEqualityType;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.NetworkLock; import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer; import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer;
@ -406,6 +407,12 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
.filter(Objects::nonNull).findFirst().orElse(index); .filter(Objects::nonNull).findFirst().orElse(index);
} }
public List<ItemFilter> getFilters() {
return this.streamModules()
.map(p -> p.getRight().getItemFilter(p.getLeft(), this))
.filter(Objects::nonNull).collect(Collectors.toList());
}
@Override @Override
public void remove() { public void remove() {
super.remove(); super.remove();

View file

@ -20,7 +20,7 @@ public class ExtractionModuleContainer extends AbstractPipeContainer<ExtractionM
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = new ItemFilter(this.module.filterSlots, this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32)) for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32))
this.addSlot(slot); this.addSlot(slot);
} }

View file

@ -33,7 +33,7 @@ public class ExtractionModuleItem extends ModuleItem {
public void tick(ItemStack module, PipeTileEntity tile) { public void tick(ItemStack module, PipeTileEntity tile) {
if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return; return;
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile); ItemFilter filter = this.getItemFilter(module, tile);
PipeNetwork network = PipeNetwork.get(tile.getWorld()); PipeNetwork network = PipeNetwork.get(tile.getWorld());
for (Direction dir : Direction.values()) { for (Direction dir : Direction.values()) {
@ -79,4 +79,9 @@ public class ExtractionModuleItem extends ModuleItem {
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) {
return new ExtractionModuleContainer(Registry.extractionModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new ExtractionModuleContainer(Registry.extractionModuleContainer, windowId, player, tile.getPos(), moduleIndex);
} }
@Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) {
return new ItemFilter(this.filterSlots, module, tile);
}
} }

View file

@ -1,13 +1,11 @@
package de.ellpeck.prettypipes.pipe.modules.filter; package de.ellpeck.prettypipes.pipe.modules.filter;
import de.ellpeck.prettypipes.misc.FilterSlot;
import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.ContainerType; import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot; import net.minecraft.inventory.container.Slot;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.ItemStackHandler;
public class FilterIncreaseModuleContainer extends AbstractPipeContainer<FilterIncreaseModuleItem> implements ItemFilter.IFilteredContainer { public class FilterIncreaseModuleContainer extends AbstractPipeContainer<FilterIncreaseModuleItem> implements ItemFilter.IFilteredContainer {
@ -19,8 +17,7 @@ public class FilterIncreaseModuleContainer extends AbstractPipeContainer<FilterI
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = new ItemFilter(18, this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
this.filter.canModifyWhitelist = false;
for (Slot slot : this.filter.getSlots(8, 49)) for (Slot slot : this.filter.getSlots(8, 49))
this.addSlot(slot); this.addSlot(slot);
} }

View file

@ -9,7 +9,6 @@ import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemStackHandler;
public class FilterIncreaseModuleItem extends ModuleItem { public class FilterIncreaseModuleItem extends ModuleItem {
@ -32,4 +31,11 @@ public class FilterIncreaseModuleItem extends ModuleItem {
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) {
return new FilterIncreaseModuleContainer(Registry.filterIncreaseModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new FilterIncreaseModuleContainer(Registry.filterIncreaseModuleContainer, windowId, player, tile.getPos(), moduleIndex);
} }
@Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) {
ItemFilter filter = new ItemFilter(18, module, tile);
filter.canModifyWhitelist = false;
return filter;
}
} }

View file

@ -20,8 +20,7 @@ public class FilterModuleContainer extends AbstractPipeContainer<FilterModuleIte
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = new ItemFilter(this.module.filterSlots, this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
this.filter.canPopulateFromInventories = this.module.canPopulateFromInventories;
for (Slot slot : this.filter.getSlots((176 - Math.min(this.module.filterSlots, 9) * 18) / 2 + 1, 17 + 32)) for (Slot slot : this.filter.getSlots((176 - Math.min(this.module.filterSlots, 9) * 18) / 2 + 1, 17 + 32))
this.addSlot(slot); this.addSlot(slot);
} }

View file

@ -14,7 +14,7 @@ import net.minecraft.item.ItemStack;
public class FilterModuleItem extends ModuleItem { public class FilterModuleItem extends ModuleItem {
public final int filterSlots; public final int filterSlots;
public final boolean canPopulateFromInventories; private final boolean canPopulateFromInventories;
public FilterModuleItem(String name, ModuleTier tier) { public FilterModuleItem(String name, ModuleTier tier) {
super(name); super(name);
@ -24,7 +24,7 @@ public class FilterModuleItem extends ModuleItem {
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) { public boolean canAcceptItem(ItemStack module, PipeTileEntity tile, ItemStack stack) {
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile); ItemFilter filter = this.getItemFilter(module, tile);
return filter.isAllowed(stack); return filter.isAllowed(stack);
} }
@ -42,4 +42,11 @@ public class FilterModuleItem extends ModuleItem {
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) {
return new FilterModuleContainer(Registry.filterModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new FilterModuleContainer(Registry.filterModuleContainer, windowId, player, tile.getPos(), moduleIndex);
} }
@Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) {
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
filter.canPopulateFromInventories = this.canPopulateFromInventories;
return filter;
}
} }

View file

@ -20,8 +20,7 @@ public class RetrievalModuleContainer extends AbstractPipeContainer<RetrievalMod
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = new ItemFilter(this.module.filterSlots, this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
this.filter.canModifyWhitelist = false;
for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32)) for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32))
this.addSlot(slot); this.addSlot(slot);
} }

View file

@ -35,11 +35,9 @@ public class RetrievalModuleItem extends ModuleItem {
return; return;
PipeNetwork network = PipeNetwork.get(tile.getWorld()); PipeNetwork network = PipeNetwork.get(tile.getWorld());
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile); ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile);
filter.isWhitelist = true;
// loop through filters to see which items to pull // loop through filters to see which items to pull
for (ItemFilter subFilter : filter.getAllFilters()) { for (ItemFilter subFilter : tile.getFilters()) {
for (int f = 0; f < subFilter.getSlots(); f++) { for (int f = 0; f < subFilter.getSlots(); f++) {
ItemStack filtered = subFilter.getStackInSlot(f); ItemStack filtered = subFilter.getStackInSlot(f);
if (filtered.isEmpty()) if (filtered.isEmpty())
@ -82,4 +80,12 @@ public class RetrievalModuleItem extends ModuleItem {
public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) { public AbstractPipeContainer<?> getContainer(ItemStack module, PipeTileEntity tile, int windowId, PlayerInventory inv, PlayerEntity player, int moduleIndex) {
return new RetrievalModuleContainer(Registry.retrievalModuleContainer, windowId, player, tile.getPos(), moduleIndex); return new RetrievalModuleContainer(Registry.retrievalModuleContainer, windowId, player, tile.getPos(), moduleIndex);
} }
@Override
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) {
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
filter.canModifyWhitelist = false;
filter.isWhitelist = true;
return filter;
}
} }