Skip to content

Commit

Permalink
Use item predicates for filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed Jun 29, 2024
1 parent 9e64a6e commit 7e0fe70
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 144 deletions.
3 changes: 2 additions & 1 deletion changelog/4.0.0+1.21.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Updated to 1.21
* Updated to 1.21
* Make filter nodes use proper component filtering
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import io.github.mattidragon.nodeflow.graph.node.Node;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.CyclingButtonWidget;
import net.minecraft.client.gui.widget.PressableTextWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Util;

public class ResourceFilterConfigScreen<N extends Node> extends NodeConfigScreen<N> {
private final ResourceFilter<?, ?> filter;
Expand All @@ -21,11 +24,11 @@ public ResourceFilterConfigScreen(N owner, EditorScreen parent, ResourceFilter<?

@Override
protected void init() {
var x = ((width - 200) / 2) - 50;
var x = ((width - 200) / 2) - 100;

var regexButton = CyclingButtonWidget.onOffBuilder()
.initially(filter.shouldUseRegex())
.build(x, 45, 100, 20, Text.translatable("node.advanced_networking.filter.use_regex"), (button1, value) -> filter.setUseRegex(value));
.build(x + 50, 45, 100, 20, Text.translatable("node.advanced_networking.filter.use_regex"), (button1, value) -> filter.setUseRegex(value));
if (AdvancedNetworking.CONFIG.get().disableRegexFilter()) {
regexButton.active = false;
regexButton.setTooltip(Tooltip.of(Text.translatable("node.advanced_networking.filter.use_regex.disabled")));
Expand All @@ -35,27 +38,31 @@ protected void init() {
var whitelistButton = CyclingButtonWidget.onOffBuilder(Text.translatable("node.advanced_networking.filter.mode.whitelist"), Text.translatable("node.advanced_networking.filter.mode.blacklist"))
.initially(filter.isWhitelist())
.omitKeyText()
.build(x, 70, 100, 20, Text.empty(), (button1, value) -> filter.setWhitelist(value));
.build(x + 50, 70, 100, 20, Text.empty(), (button1, value) -> filter.setWhitelist(value));
addDrawableChild(whitelistButton);

var infoText1 = Text.translatable("node.advanced_networking.filter.syntax_info.line1").formatted(Formatting.YELLOW);
var infoText2 = Text.translatable("node.advanced_networking.filter.syntax_info.line2");
var textWidth = Math.max(textRenderer.getWidth(infoText1), textRenderer.getWidth(infoText2));
addDrawableChild(new TextWidget(((width - 200 - textWidth) / 2),
95,
textWidth,
10,
infoText1,
textRenderer));
addDrawableChild(new PressableTextWidget(((width - 200 - textWidth) / 2),
105,
textWidth,
10,
infoText2,
button -> Util.getOperatingSystem().open("https://minecraft.wiki/w/Argument_types#item_predicate"),
textRenderer));

var button = CyclingButtonWidget.<ResourceFilter.Mode>builder(mode -> mode == ResourceFilter.Mode.RESOURCE ? Text.translatable("node.advanced_networking.filter.mode.resource") : Text.translatable("node.advanced_networking.filter.mode.tag"))
.values(ResourceFilter.Mode.values())
.initially(filter.getMode())
.build(x, 95, 100, 20, Text.translatable("node.advanced_networking.filter.mode"), (button1, value) -> filter.setMode(value));
addDrawableChild(button);

var idField = new TextFieldWidget(textRenderer, x, 120, 100, 20, Text.empty());
var idField = new TextFieldWidget(textRenderer, x, 120, 200, 20, Text.empty());
idField.setMaxLength(100);
idField.setPlaceholder(Text.literal("id").formatted(Formatting.GRAY));
idField.setText(filter.getIdFilter());
idField.setChangedListener(filter::setIdFilter);
idField.setPlaceholder(Text.literal("filter").formatted(Formatting.GRAY));
idField.setText(filter.getFilter());
idField.setChangedListener(filter::setFilter);
addDrawableChild(idField);

var nbtField = new TextFieldWidget(textRenderer, x, 145, 100, 20, Text.empty());
nbtField.setMaxLength(200);
nbtField.setPlaceholder(Text.literal("nbt").formatted(Formatting.GRAY));
nbtField.setText(filter.getNbtFilter());
nbtField.setChangedListener(filter::setNbtFilter);
addDrawableChild(nbtField);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.datafixers.util.Either;
import io.github.mattidragon.advancednetworking.graph.NetworkControllerContext;
import io.github.mattidragon.advancednetworking.misc.FilterPredicateParsing;
import io.github.mattidragon.advancednetworking.misc.ResourceFilter;
import io.github.mattidragon.nodeflow.graph.Connector;
import io.github.mattidragon.nodeflow.graph.Graph;
Expand All @@ -28,9 +29,9 @@ public abstract class CountNode<R, V extends TransferVariant<R>> extends Interfa
private final ResourceFilter<R, V> filter;
private final BlockApiLookup<Storage<V>, @Nullable Direction> lookup;

public CountNode(NodeType<?> type, Graph graph, Registry<R> registry, BlockApiLookup<Storage<V>, @Nullable Direction> lookup) {
public CountNode(NodeType<?> type, Graph graph, Registry<R> registry, BlockApiLookup<Storage<V>, @Nullable Direction> lookup, FilterPredicateParsing.PredicateParser<R> predicateParser) {
super(type, List.of(NetworkControllerContext.TYPE, ContextType.SERVER_WORLD), graph);
filter = new ResourceFilter<>(registry);
filter = new ResourceFilter<>(registry, predicateParser);
this.lookup = lookup;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.datafixers.util.Either;
import io.github.mattidragon.advancednetworking.graph.path.PathBundle;
import io.github.mattidragon.advancednetworking.misc.FilterPredicateParsing;
import io.github.mattidragon.advancednetworking.misc.ResourceFilter;
import io.github.mattidragon.nodeflow.graph.Connector;
import io.github.mattidragon.nodeflow.graph.Graph;
Expand All @@ -22,9 +23,9 @@
public abstract class FilterNode<R, V extends TransferVariant<R>, T> extends Node {
private final ResourceFilter<R, V> filter;

public FilterNode(NodeType<? extends FilterNode<R, V, T>> type, Graph graph, Registry<R> registry) {
public FilterNode(NodeType<? extends FilterNode<R, V, T>> type, Graph graph, Registry<R> registry, FilterPredicateParsing.PredicateParser<R> predicateParser) {
super(type, List.of(), graph);
this.filter = new ResourceFilter<>(registry);
this.filter = new ResourceFilter<>(registry, predicateParser);
}

protected abstract DataType<PathBundle<Storage<V>, T>> getDataType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.mattidragon.advancednetworking.graph.node.base.FilterNode;
import io.github.mattidragon.advancednetworking.graph.node.fluid.FluidTransformer;
import io.github.mattidragon.advancednetworking.graph.path.PathBundle;
import io.github.mattidragon.advancednetworking.misc.FilterPredicateParsing;
import io.github.mattidragon.nodeflow.graph.Graph;
import io.github.mattidragon.nodeflow.graph.data.DataType;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
Expand All @@ -16,7 +17,7 @@

public class FilterFluidNode extends FilterNode<Fluid, FluidVariant, FluidTransformer> {
public FilterFluidNode(Graph graph) {
super(ModNodeTypes.FILTER_FLUID, graph, Registries.FLUID);
super(ModNodeTypes.FILTER_FLUID, graph, Registries.FLUID, FilterPredicateParsing.FLUID_PREDICATE_PARSER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.mattidragon.advancednetworking.graph.ModNodeTypes;
import io.github.mattidragon.advancednetworking.graph.node.base.CountNode;
import io.github.mattidragon.advancednetworking.misc.FilterPredicateParsing;
import io.github.mattidragon.nodeflow.graph.Graph;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
Expand All @@ -10,6 +11,6 @@

public class FluidCountNode extends CountNode<Fluid, FluidVariant> {
public FluidCountNode(Graph graph) {
super(ModNodeTypes.FLUID_COUNT, graph, Registries.FLUID, FluidStorage.SIDED);
super(ModNodeTypes.FLUID_COUNT, graph, Registries.FLUID, FluidStorage.SIDED, FilterPredicateParsing.FLUID_PREDICATE_PARSER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.mattidragon.advancednetworking.graph.node.base.FilterNode;
import io.github.mattidragon.advancednetworking.graph.node.item.ItemTransformer;
import io.github.mattidragon.advancednetworking.graph.path.PathBundle;
import io.github.mattidragon.advancednetworking.misc.FilterPredicateParsing;
import io.github.mattidragon.nodeflow.graph.Graph;
import io.github.mattidragon.nodeflow.graph.data.DataType;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
Expand All @@ -16,7 +17,7 @@

public class FilterItemsNode extends FilterNode<Item, ItemVariant, ItemTransformer> {
public FilterItemsNode(Graph graph) {
super(ModNodeTypes.FILTER_ITEMS, graph, Registries.ITEM);
super(ModNodeTypes.FILTER_ITEMS, graph, Registries.ITEM, FilterPredicateParsing.ITEM_PREDICATE_PARSER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.mattidragon.advancednetworking.graph.ModNodeTypes;
import io.github.mattidragon.advancednetworking.graph.node.base.CountNode;
import io.github.mattidragon.advancednetworking.misc.FilterPredicateParsing;
import io.github.mattidragon.nodeflow.graph.Graph;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
Expand All @@ -10,6 +11,6 @@

public class ItemCountNode extends CountNode<Item, ItemVariant> {
public ItemCountNode(Graph graph) {
super(ModNodeTypes.ITEM_COUNT, graph, Registries.ITEM, ItemStorage.SIDED);
super(ModNodeTypes.ITEM_COUNT, graph, Registries.ITEM, ItemStorage.SIDED, FilterPredicateParsing.ITEM_PREDICATE_PARSER);
}
}
Loading

0 comments on commit 7e0fe70

Please sign in to comment.