mirror of
https://github.com/Ellpeck/PrettyPipes.git
synced 2024-11-25 21:18:34 +01:00
generify the filter system for the future(tm)
This commit is contained in:
parent
608de4ed24
commit
4c0862c9ed
12 changed files with 53 additions and 30 deletions
|
@ -1,5 +1,6 @@
|
|||
package de.ellpeck.prettypipes.items;
|
||||
|
||||
import de.ellpeck.prettypipes.misc.ItemFilter;
|
||||
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
|
||||
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
||||
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);
|
||||
|
||||
Integer getCustomNextNode(ItemStack module, PipeTileEntity tile, List<BlockPos> nodes, int index);
|
||||
|
||||
ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package de.ellpeck.prettypipes.items;
|
|||
|
||||
import de.ellpeck.prettypipes.Registry;
|
||||
import de.ellpeck.prettypipes.Utility;
|
||||
import de.ellpeck.prettypipes.misc.ItemFilter;
|
||||
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
|
||||
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import de.ellpeck.prettypipes.PrettyPipes;
|
|||
import de.ellpeck.prettypipes.packets.PacketButton;
|
||||
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
|
||||
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.widget.Widget;
|
||||
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.List;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ItemFilter extends ItemStackHandler {
|
||||
|
||||
|
@ -80,7 +78,7 @@ public class ItemFilter extends ItemStackHandler {
|
|||
this.save();
|
||||
} else if (id == 1 && this.canPopulateFromInventories) {
|
||||
// populate filter from inventories
|
||||
List<ItemFilter> filters = this.getAllFilters();
|
||||
List<ItemFilter> filters = this.pipe.getFilters();
|
||||
for (Direction direction : Direction.values()) {
|
||||
IItemHandler handler = this.pipe.getItemHandler(direction);
|
||||
if (handler == null)
|
||||
|
@ -110,7 +108,7 @@ public class ItemFilter extends ItemStackHandler {
|
|||
private boolean isFiltered(ItemStack stack) {
|
||||
ItemEqualityType[] types = getEqualityTypes(this.pipe);
|
||||
// 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++) {
|
||||
ItemStack filter = handler.getStackInSlot(i);
|
||||
if (filter.isEmpty())
|
||||
|
@ -122,16 +120,6 @@ public class ItemFilter extends ItemStackHandler {
|
|||
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() {
|
||||
if (this.modified) {
|
||||
this.stack.getOrCreateTag().put("filter", this.serializeNBT());
|
||||
|
|
|
@ -5,6 +5,7 @@ import de.ellpeck.prettypipes.Registry;
|
|||
import de.ellpeck.prettypipes.Utility;
|
||||
import de.ellpeck.prettypipes.items.IModule;
|
||||
import de.ellpeck.prettypipes.misc.ItemEqualityType;
|
||||
import de.ellpeck.prettypipes.misc.ItemFilter;
|
||||
import de.ellpeck.prettypipes.network.NetworkLock;
|
||||
import de.ellpeck.prettypipes.network.PipeNetwork;
|
||||
import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer;
|
||||
|
@ -406,6 +407,12 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
|
|||
.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
|
||||
public void remove() {
|
||||
super.remove();
|
||||
|
|
|
@ -20,7 +20,7 @@ public class ExtractionModuleContainer extends AbstractPipeContainer<ExtractionM
|
|||
|
||||
@Override
|
||||
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))
|
||||
this.addSlot(slot);
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ public class ExtractionModuleItem extends ModuleItem {
|
|||
public void tick(ItemStack module, PipeTileEntity tile) {
|
||||
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
|
||||
return;
|
||||
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
|
||||
ItemFilter filter = this.getItemFilter(module, tile);
|
||||
|
||||
PipeNetwork network = PipeNetwork.get(tile.getWorld());
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
package de.ellpeck.prettypipes.pipe.modules.filter;
|
||||
|
||||
import de.ellpeck.prettypipes.misc.FilterSlot;
|
||||
import de.ellpeck.prettypipes.misc.ItemFilter;
|
||||
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.container.ContainerType;
|
||||
import net.minecraft.inventory.container.Slot;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
|
||||
public class FilterIncreaseModuleContainer extends AbstractPipeContainer<FilterIncreaseModuleItem> implements ItemFilter.IFilteredContainer {
|
||||
|
||||
|
@ -19,8 +17,7 @@ public class FilterIncreaseModuleContainer extends AbstractPipeContainer<FilterI
|
|||
|
||||
@Override
|
||||
protected void addSlots() {
|
||||
this.filter = new ItemFilter(18, this.moduleStack, this.tile);
|
||||
this.filter.canModifyWhitelist = false;
|
||||
this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
|
||||
for (Slot slot : this.filter.getSlots(8, 49))
|
||||
this.addSlot(slot);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
|||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,8 +20,7 @@ public class FilterModuleContainer extends AbstractPipeContainer<FilterModuleIte
|
|||
|
||||
@Override
|
||||
protected void addSlots() {
|
||||
this.filter = new ItemFilter(this.module.filterSlots, this.moduleStack, this.tile);
|
||||
this.filter.canPopulateFromInventories = this.module.canPopulateFromInventories;
|
||||
this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
|
||||
for (Slot slot : this.filter.getSlots((176 - Math.min(this.module.filterSlots, 9) * 18) / 2 + 1, 17 + 32))
|
||||
this.addSlot(slot);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import net.minecraft.item.ItemStack;
|
|||
public class FilterModuleItem extends ModuleItem {
|
||||
|
||||
public final int filterSlots;
|
||||
public final boolean canPopulateFromInventories;
|
||||
private final boolean canPopulateFromInventories;
|
||||
|
||||
public FilterModuleItem(String name, ModuleTier tier) {
|
||||
super(name);
|
||||
|
@ -24,7 +24,7 @@ public class FilterModuleItem extends ModuleItem {
|
|||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -42,4 +42,11 @@ public class FilterModuleItem extends ModuleItem {
|
|||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemFilter getItemFilter(ItemStack module, PipeTileEntity tile) {
|
||||
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
|
||||
filter.canPopulateFromInventories = this.canPopulateFromInventories;
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,8 +20,7 @@ public class RetrievalModuleContainer extends AbstractPipeContainer<RetrievalMod
|
|||
|
||||
@Override
|
||||
protected void addSlots() {
|
||||
this.filter = new ItemFilter(this.module.filterSlots, this.moduleStack, this.tile);
|
||||
this.filter.canModifyWhitelist = false;
|
||||
this.filter = this.module.getItemFilter(this.moduleStack, this.tile);
|
||||
for (Slot slot : this.filter.getSlots((176 - this.module.filterSlots * 18) / 2 + 1, 17 + 32))
|
||||
this.addSlot(slot);
|
||||
}
|
||||
|
|
|
@ -35,11 +35,9 @@ public class RetrievalModuleItem extends ModuleItem {
|
|||
return;
|
||||
PipeNetwork network = PipeNetwork.get(tile.getWorld());
|
||||
|
||||
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
|
||||
ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile);
|
||||
filter.isWhitelist = true;
|
||||
// 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++) {
|
||||
ItemStack filtered = subFilter.getStackInSlot(f);
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue