Skip to content

Commit

Permalink
PR Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemmy committed Oct 23, 2024
1 parent c02e181 commit 61f30d7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.gregtechceu.gtceu.api.gui.GuiTextures;
import com.gregtechceu.gtceu.data.lang.LangHandler;
import com.gregtechceu.gtceu.utils.OreDictExprFilter;
import com.gregtechceu.gtceu.utils.TagExprFilter;

import com.lowdragmc.lowdraglib.gui.widget.ImageWidget;
import com.lowdragmc.lowdraglib.gui.widget.TextFieldWidget;
Expand Down Expand Up @@ -36,7 +36,7 @@ public abstract class TagFilter<T, S extends Filter<T, S>> implements Filter<T,
protected Consumer<S> itemWriter = filter -> {};
protected Consumer<S> onUpdated = filter -> itemWriter.accept(filter);

protected OreDictExprFilter.OreDictExprParser.MatchExpr matchExpr = null;
protected TagExprFilter.TagExprParser.MatchExpr matchExpr = null;

protected TagFilter() {}

Expand All @@ -48,7 +48,7 @@ public CompoundTag saveFilter() {

public void setOreDict(String oreDict) {
this.oreDictFilterExpression = oreDict;
matchExpr = OreDictExprFilter.parseExpression(oreDictFilterExpression);
matchExpr = TagExprFilter.parseExpression(oreDictFilterExpression);
onUpdated.accept((S) this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gregtechceu.gtceu.api.cover.filter;

import com.gregtechceu.gtceu.utils.OreDictExprFilter;
import com.gregtechceu.gtceu.utils.TagExprFilter;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -33,7 +33,7 @@ private static TagFluidFilter loadFilter(CompoundTag tag, Consumer<FluidFilter>
handler.oreDictFilterExpression = tag.getString("oreDict");
handler.matchExpr = null;
handler.cache.clear();
handler.matchExpr = OreDictExprFilter.parseExpression(handler.oreDictFilterExpression);
handler.matchExpr = TagExprFilter.parseExpression(handler.oreDictFilterExpression);
return handler;
}

Expand All @@ -46,7 +46,7 @@ public void setOreDict(String oreDict) {
public boolean test(FluidStack fluidStack) {
if (oreDictFilterExpression.isEmpty()) return true;
if (cache.containsKey(fluidStack.getFluid())) return cache.getOrDefault(fluidStack.getFluid(), false);
if (OreDictExprFilter.matchesOreDict(matchExpr, fluidStack)) {
if (TagExprFilter.tagsMatch(matchExpr, fluidStack)) {
cache.put(fluidStack.getFluid(), true);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gregtechceu.gtceu.api.cover.filter;

import com.gregtechceu.gtceu.utils.OreDictExprFilter;
import com.gregtechceu.gtceu.utils.TagExprFilter;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -32,7 +32,7 @@ private static TagItemFilter loadFilter(CompoundTag tag, Consumer<ItemFilter> it
handler.oreDictFilterExpression = tag.getString("oreDict");
handler.matchExpr = null;
handler.cache.clear();
handler.matchExpr = OreDictExprFilter.parseExpression(handler.oreDictFilterExpression);
handler.matchExpr = TagExprFilter.parseExpression(handler.oreDictFilterExpression);
return handler;
}

Expand All @@ -45,7 +45,7 @@ public void setOreDict(String oreDict) {
public boolean test(ItemStack itemStack) {
if (oreDictFilterExpression.isEmpty()) return true;
if (cache.containsKey(itemStack.getItem())) return cache.getOrDefault(itemStack.getItem(), false);
if (OreDictExprFilter.matchesOreDict(matchExpr, itemStack)) {
if (TagExprFilter.tagsMatch(matchExpr, itemStack)) {
cache.put(itemStack.getItem(), true);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gregtechceu.gtceu.utils;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -13,9 +14,9 @@
/**
* @author brachy84
*/
public class OreDictExprFilter {
public class TagExprFilter {

public static class OreDictExprParser {
public static class TagExprParser {

public enum TokenType {
LParen,
Expand All @@ -42,12 +43,12 @@ public Token(TokenType type, String lexeme) {
}
}

abstract static public class MatchExpr {
public static abstract class MatchExpr {

public abstract boolean matches(Set<String> input);
}

static class BinExpr extends MatchExpr {
private static class BinExpr extends MatchExpr {

MatchExpr left, right;
Token op;
Expand All @@ -70,7 +71,7 @@ public boolean matches(Set<String> input) {
}
}

static class UnaryExpr extends MatchExpr {
private static class UnaryExpr extends MatchExpr {

Token token;
MatchExpr expr;
Expand All @@ -90,7 +91,7 @@ public boolean matches(Set<String> input) {
}
}

static class StringExpr extends MatchExpr {
private static class StringExpr extends MatchExpr {

String value;

Expand Down Expand Up @@ -122,7 +123,7 @@ private String quote(String str) {
}
}

static class GroupingExpr extends MatchExpr {
private static class GroupingExpr extends MatchExpr {

MatchExpr inner;

Expand Down Expand Up @@ -262,8 +263,8 @@ private List<Token> tokenize(String expr) {
* @param expression expr to parse
* @return The parsed expression tree
*/
public static OreDictExprParser.MatchExpr parseExpression(String expression) {
return new OreDictExprParser().parse(expression);
public static TagExprParser.MatchExpr parseExpression(String expression) {
return new TagExprParser().parse(expression);
}

/**
Expand All @@ -273,9 +274,9 @@ public static OreDictExprParser.MatchExpr parseExpression(String expression) {
* @param stack item to check
* @return if any of the items oreDicts matches the rules
*/
public static boolean matchesOreDict(OreDictExprParser.MatchExpr expr, ItemStack stack) {
public static boolean tagsMatch(TagExprParser.MatchExpr expr, ItemStack stack) {
Set<String> oreDicts = stack.getTags().map(TagKey::location)
.map(rl -> rl.getNamespace() + ":" + rl.getPath()).collect(Collectors.toSet());
.map(ResourceLocation::toString).collect(Collectors.toSet());

if (oreDicts.isEmpty() || expr == null) {
return false;
Expand All @@ -284,9 +285,9 @@ public static boolean matchesOreDict(OreDictExprParser.MatchExpr expr, ItemStack
return expr.matches(oreDicts);
}

public static boolean matchesOreDict(OreDictExprParser.MatchExpr expr, FluidStack stack) {
public static boolean tagsMatch(TagExprParser.MatchExpr expr, FluidStack stack) {
Set<String> oreDicts = stack.getFluid().defaultFluidState().getTags().map(TagKey::location)
.map(rl -> rl.getNamespace() + ":" + rl.getPath()).collect(Collectors.toSet());
.map(ResourceLocation::toString).collect(Collectors.toSet());

if (oreDicts.isEmpty() || expr == null) {
return false;
Expand Down

0 comments on commit 61f30d7

Please sign in to comment.