Skip to content

Commit

Permalink
fix: more robust validation for filter text
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed May 13, 2024
1 parent ee1c846 commit a3d75bf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,17 @@ public static int tryMatch(CommandSourceStack source) throws CommandSyntaxExcept
throw NO_OFFHAND_ITEM.create();
}

SmartFilter filter = FilterParser.parse(SmartFilterItem.getFilterString(getHeldFilter(source)));
if (filter.test(offhandItem)) {
source.sendSuccess(() -> TICK_MARK.copy().append(Component.translatable("ftbfiltersystem.message.matched", offhandItem.getDisplayName())), false);
return 1;
} else {
source.sendSuccess(() -> X_MARK.copy().append(Component.translatable("ftbfiltersystem.message.not_matched", offhandItem.getDisplayName())), false);
return 0;
try {
SmartFilter filter = FilterParser.parse(SmartFilterItem.getFilterString(getHeldFilter(source)));
if (filter.test(offhandItem)) {
source.sendSuccess(() -> TICK_MARK.copy().append(Component.translatable("ftbfiltersystem.message.matched", offhandItem.getDisplayName())), false);
return 1;
} else {
source.sendSuccess(() -> X_MARK.copy().append(Component.translatable("ftbfiltersystem.message.not_matched", offhandItem.getDisplayName())), false);
return 0;
}
} catch (FilterException e) {
throw PARSE_FAILED.create(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public AbstractItemEditorConfigScreen(T filter, AbstractFilterScreen parentScree
private CustomStringWidget statusLine;
private final List<SearchItemWidget> itemWidgets = new ArrayList<>();
protected Component customHoverName = null;
// protected Component statusMsg = Component.empty();

@Override
protected void init() {
Expand Down Expand Up @@ -74,8 +73,6 @@ public void render(GuiGraphics guiGraphics, int pMouseX, int pMouseY, float pPar
super.render(guiGraphics, pMouseX, pMouseY, pPartialTick);

guiGraphics.renderOutline(leftPos + 7, topPos + 109, 164, 74, 0xFFA0A0A0);
// int w = font.width(statusMsg);
// guiGraphics.drawString(font, statusMsg, leftPos + guiWidth - (w + 8), topPos + 98, 0x404040, false);
}

protected abstract Predicate<ItemStack> inventoryChecker();
Expand All @@ -89,6 +86,7 @@ protected void setStatus(boolean ok, Component message, String detail) {
}

protected abstract String serialize(ItemStack stack);

private class SearchItemWidget extends ItemWidget {

public SearchItemWidget(int row, int col) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.ftb.mods.ftbfiltersystem.filter;

import dev.ftb.mods.ftbfiltersystem.api.FTBFilterSystemAPI;
import dev.ftb.mods.ftbfiltersystem.api.FilterException;
import dev.ftb.mods.ftbfiltersystem.api.filter.AbstractSmartFilter;
import dev.ftb.mods.ftbfiltersystem.api.filter.SmartFilter;
import dev.ftb.mods.ftbfiltersystem.util.FilterParser;
Expand All @@ -15,11 +16,11 @@ public class ExpressionFilter extends AbstractSmartFilter {
private final String customName;
private final SmartFilter parsedExpression;

public ExpressionFilter(SmartFilter.Compound parent) {
public ExpressionFilter(SmartFilter.Compound parent) throws FilterException {
this(parent, "");
}

public ExpressionFilter(SmartFilter.Compound parent, String expression) {
public ExpressionFilter(SmartFilter.Compound parent, String expression) throws FilterException {
super(parent);

String[] parts = expression.split("/", 2);
Expand Down Expand Up @@ -52,7 +53,7 @@ public Component getDisplayArg() {
return customName.isEmpty() ? super.getDisplayArg() : Component.literal(customName);
}

public static ExpressionFilter fromString(SmartFilter.Compound parent, String str) {
public static ExpressionFilter fromString(SmartFilter.Compound parent, String str) throws FilterException {
return new ExpressionFilter(parent, str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -31,6 +32,7 @@ public static String getFilterString(ItemStack filterStack) {
return filterStack.hasTag() ? filterStack.getTag().getString(FILTER_TAG_NAME) : "";
}

@NotNull
public static SmartFilter getFilter(ItemStack filterStack) throws FilterException {
return FilterParser.parse(getFilterString(filterStack));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
import dev.ftb.mods.ftbfiltersystem.filter.compound.RootFilter;
import dev.ftb.mods.ftbfiltersystem.registry.FilterRegistry;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class FilterParser {
@NotNull
public static SmartFilter parse(String str) throws FilterException {
return FilterCache.INSTANCE.getOrCreateFilter(str);
SmartFilter filter = FilterCache.INSTANCE.getOrCreateFilter(str);
if (filter == null) {
throw new FilterException("invalid filter: " + str);
}
return filter;
}

@NotNull
static SmartFilter parseRaw(String str) throws FilterException {
RootFilter root = new RootFilter();
root.getChildren().addAll(parseFilterList(root, str));
Expand Down

0 comments on commit a3d75bf

Please sign in to comment.