Skip to content

Commit

Permalink
Merge pull request #7 from FTBTeam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
desht authored Jun 5, 2024
2 parents 6135b75 + 379d6b2 commit a4ba3ae
Show file tree
Hide file tree
Showing 68 changed files with 425 additions and 238 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
with:
curse-publish-task: ""
maven-snapshots: true
java-version: 21
secrets:
ftb-maven-token: ${{ secrets.FTB_MAVEN_TOKEN }}
saps-token: ${{ secrets.SAPS_TOKEN }}
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import me.modmuss50.mpp.ReleaseType

plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.5-SNAPSHOT" apply false
id "dev.architectury.loom" version "1.6-SNAPSHOT" apply false
id "me.modmuss50.mod-publish-plugin" version "0.4.5"
}

Expand Down Expand Up @@ -38,7 +38,7 @@ allprojects {

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.release = 17
options.release = 21
}

java {
Expand Down Expand Up @@ -111,12 +111,12 @@ publishMods {
}
}

curseforge("forge") {
from curseOptions
modLoaders.add("forge")
file = project(":forge").tasks.remapJar.archiveFile
displayName = "[FORGE] FTB Filter System ${mod_version} MC ${minecraft_version}"
}
// curseforge("forge") {
// from curseOptions
// modLoaders.add("forge")
// file = project(":forge").tasks.remapJar.archiveFile
// displayName = "[FORGE] FTB Filter System ${mod_version} MC ${minecraft_version}"
// }

curseforge("neoforge") {
from curseOptions
Expand Down
3 changes: 2 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ dependencies {
// Remove the next line if you don't want to depend on the API
modApi "dev.architectury:architectury:${rootProject.architectury_version}"

modCompileOnly("mezz.jei:jei-${rootProject.minecraft_version}-common-api:${rootProject.jei_version}")
// modCompileOnly("mezz.jei:jei-${rootProject.minecraft_version}-common-api:${rootProject.jei_version}")
modCompileOnly("mezz.jei:jei-1.20.4-common-api:${rootProject.jei_version}")
}

tasks.register("buildApi", Jar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import dev.ftb.mods.ftbfiltersystem.filter.compound.OrFilter;
import dev.ftb.mods.ftbfiltersystem.network.FTBFilterSystemNet;
import dev.ftb.mods.ftbfiltersystem.registry.FilterRegistry;
import dev.ftb.mods.ftbfiltersystem.registry.ModDataComponents;
import dev.ftb.mods.ftbfiltersystem.registry.ModItems;
import net.minecraft.server.MinecraftServer;
import org.apache.logging.log4j.LogManager;
Expand All @@ -38,6 +39,7 @@ public FTBFilterSystem() {
FilterRegistrationEvent.REGISTER.register(this::registerBuiltinFilters);
ModItems.TABS.register();
ModItems.ITEMS.register();
ModDataComponents.COMPONENT_TYPES.register();

EnvExecutor.runInEnv(Env.CLIENT, () -> FTBFilterSystemClient.INSTANCE::init);

Expand All @@ -62,7 +64,7 @@ private void registerBuiltinFilters(FTBFilterSystemRegistry reg) {
reg.register(ItemTagFilter.ID, ItemTagFilter::fromString, ItemTagFilter::new);
reg.register(MaxStackSizeFilter.ID, MaxStackSizeFilter::fromString, MaxStackSizeFilter::new);
reg.register(ModFilter.ID, ModFilter::fromString, ModFilter::new);
reg.register(NbtFilter.ID, NbtFilter::fromString, NbtFilter::new);
reg.register(ComponentFilter.ID, ComponentFilter::fromString, ComponentFilter::new);
reg.register(StackSizeFilter.ID, StackSizeFilter::fromString, StackSizeFilter::new);

reg.register(AndFilter.ID, AndFilter::fromString, AndFilter::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public enum FilterSystemAPIImpl implements FTBFilterSystemAPI.API {
Expand All @@ -32,6 +33,11 @@ public boolean isFilterItem(ItemStack stack) {
return stack.getItem() instanceof SmartFilterItem;
}

@Override
public Item filterItem() {
return Objects.requireNonNull(ModItems.SMART_FILTER.get());
}

@Override
public boolean doesFilterMatch(ItemStack filterStack, ItemStack toMatch) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import dev.ftb.mods.ftbfiltersystem.api.filter.DumpedFilter;
import dev.ftb.mods.ftbfiltersystem.api.FTBFilterSystemAPI;
import dev.ftb.mods.ftbfiltersystem.api.FilterException;
import dev.ftb.mods.ftbfiltersystem.api.filter.DumpedFilter;
import dev.ftb.mods.ftbfiltersystem.api.filter.SmartFilter;
import dev.ftb.mods.ftbfiltersystem.registry.item.SmartFilterItem;
import dev.ftb.mods.ftbfiltersystem.util.FilterCache;
import dev.ftb.mods.ftbfiltersystem.util.FilterParser;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.component.TypedDataComponent;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.item.ItemStack;
Expand All @@ -27,6 +30,8 @@ public class FilterSystemCommands {
= new DynamicCommandExceptionType(object -> Component.translatable("ftbfiltersystem.message.parse_failed", object));
public static final SimpleCommandExceptionType NOT_A_FILTER
= new SimpleCommandExceptionType(Component.translatable("ftbfiltersystem.message.not_a_filter"));
public static final SimpleCommandExceptionType NOT_CONFIGURED
= new SimpleCommandExceptionType(Component.translatable("ftbfiltersystem.message.not_configured"));
public static final SimpleCommandExceptionType NO_OFFHAND_ITEM
= new SimpleCommandExceptionType(Component.translatable("ftbfiltersystem.message.no_offhand_item"));

Expand Down Expand Up @@ -54,16 +59,29 @@ public static void registerCommands(CommandDispatcher<CommandSourceStack> dispat
.then(literal("try_match")
.executes(ctx -> tryMatch(ctx.getSource()))
)
.then(literal("dump_nbt")
.then(literal("dump_components")
.requires(ctx -> ctx.hasPermission(2))
.executes(ctx -> dumpComponents(ctx.getSource(), false))
.then(literal("all")
.executes(ctx -> dumpComponents(ctx.getSource(), true))
)
)
.then(literal("cache")
.requires(ctx -> ctx.hasPermission(2))
.executes(ctx -> dumpNbt(ctx.getSource()))
.then(literal("clear")
.executes(ctx -> clearCache(ctx.getSource()))
)
)
);
}

private static int tryParseCommand(CommandSourceStack source, String string) throws CommandSyntaxException {
try {
for (DumpedFilter entry : FTBFilterSystemAPI.api().dump(FilterParser.parse(string))) {
SmartFilter parsed = FilterParser.parse(string);
if (parsed == null) {
throw new FilterException("can't parse: " + string);
}
for (DumpedFilter entry : FTBFilterSystemAPI.api().dump(parsed)) {
source.sendSuccess(() -> {
MutableComponent txt = entry.filter().getDisplayName().copy().withStyle(ChatFormatting.AQUA);
if (!(entry.filter() instanceof SmartFilter.Compound)) {
Expand All @@ -81,6 +99,9 @@ private static int tryParseCommand(CommandSourceStack source, String string) thr

private static int tryShowFilter(CommandSourceStack source, boolean prettyPrint) throws CommandSyntaxException {
String filterString = SmartFilterItem.getFilterString(getHeldFilter(source));
if (filterString.isEmpty()) {
throw NOT_CONFIGURED.create();
}
if (prettyPrint) {
return tryParseCommand(source, filterString);
} else {
Expand All @@ -91,7 +112,8 @@ private static int tryShowFilter(CommandSourceStack source, boolean prettyPrint)

private static int trySetFilter(CommandSourceStack source, String string) throws CommandSyntaxException {
try {
SmartFilterItem.setFilter(getHeldFilter(source), FilterParser.parse(string).toString());
SmartFilter parsed = FilterParser.parse(string);
SmartFilterItem.setFilter(getHeldFilter(source), parsed.toString());
return 1;
} catch (FilterException e) {
source.sendFailure(Component.literal(e.getMessage()).withStyle(ChatFormatting.RED));
Expand All @@ -105,29 +127,61 @@ 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());
}
}

private static int dumpNbt(CommandSourceStack source) throws CommandSyntaxException {
private static int dumpComponents(CommandSourceStack source, boolean all) throws CommandSyntaxException {
ItemStack stack = source.getPlayerOrException().getMainHandItem();

if (stack.hasTag()) {
source.sendSuccess(() -> Component.literal("NBT dump:").withStyle(ChatFormatting.YELLOW), false);
source.sendSuccess(() -> Component.literal(stack.getTag().toString()), false);
if (!stack.getComponents().isEmpty()) {
int amount = stack.getComponents().size();
source.sendSuccess(() -> Component.translatable("ftbfiltersystem.message.components_header", amount, stack.getItem().getDescription())
.withStyle(ChatFormatting.YELLOW), false);
if (!all) {
source.sendSuccess(() -> Component.translatable("ftbfiltersystem.message.non_default_components")
.withStyle(ChatFormatting.YELLOW, ChatFormatting.ITALIC), false);
}
stack.getComponents().forEach(tc -> {
if (all || !tc.value().equals(stack.getItem().components().get(tc.type()))) {
source.sendSuccess(() -> Component.empty()
.append(Component.literal(tc.type().toString()).withStyle(ChatFormatting.AQUA))
.append(": ")
.append(encodeComponent(tc)),
false);
}
});
return 1;
} else {
source.sendFailure(Component.literal("No NBT").withStyle(ChatFormatting.RED));
source.sendFailure(Component.translatable("ftbfiltersystem.message.components_header_none").withStyle(ChatFormatting.GOLD));
return 0;
}
}

private static Component encodeComponent(TypedDataComponent<?> tc) {
try {
return Component.literal(tc.encodeValue(NbtOps.INSTANCE).getOrThrow().toString());
} catch (IllegalStateException ignored) {
return Component.literal("(encoding failed)").withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC);
}
}

private static int clearCache(CommandSourceStack source) {
FilterCache.INSTANCE.clear();
source.sendSuccess(() -> Component.translatable("ftbfiltersystem.message.cache_cleared"), false);
return 1;
}

// ---------------------------------------------------------------------------------------------
// supporting methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public interface API {
*/
boolean isFilterItem(ItemStack stack);

/**
* {@return the Smart Filter item} May be useful for mods which wish to add capabilities and/or components
* to the Smart Filter. Do not call this before registration has completed.
*/
Item filterItem();

/**
* Check if the given filter stack matches the given item stack
* .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@
import java.util.Optional;
import java.util.function.IntPredicate;

public class NumericComparison implements IntPredicate {
private final ComparisonOp op;
private final int value;
private final boolean percentage;

public NumericComparison(ComparisonOp op, int value, boolean percentage) {
this.op = op;
this.value = value;
this.percentage = percentage;
}

/**
* A comparison object, which is retrieved from a
* {@link dev.ftb.mods.ftbfiltersystem.api.filter.AbstractComparisonFilter comparison filter}. Effectively just an
* integer predicate.
*
* @param op the comparison operator
* @param value the value to compare
* @param percentage true if this is a percentage comparison
*/
public record NumericComparison(ComparisonOp op, int value, boolean percentage) implements IntPredicate {
/**
* Create a new comparison from the given string. The expected format is {@code {OP}{VAL}[%]}, where OP is a
* comparison operator (see {@link ComparisonOp}, and VAL is an integer quantity. See the {@link #toString()}
* for an illustration of the reverse process.
*
* @param str the str to parse
* @param allowPercentages true if this comparison should allow percentage comparisons
* @return a new NumericComparison object
* @throws FilterException if the input is any way invalid
*/
public static NumericComparison fromString(String str, boolean allowPercentages) throws FilterException {
boolean pct = false;
if (str.endsWith("%")) {
Expand Down Expand Up @@ -64,18 +73,6 @@ public boolean test(int toCheck) {
return op.test(toCheck, value);
}

public boolean isPercentage() {
return percentage;
}

public int getValue() {
return value;
}

public ComparisonOp getOp() {
return op;
}

public enum ComparisonOp {
EQ("=", (v1, v2) -> v1 == v2),
NE("!=", (v1, v2) -> v1 != v2),
Expand All @@ -87,7 +84,7 @@ public enum ComparisonOp {
private final String str;
private final ValuePredicate predicate;

private static final Map<String,ComparisonOp> map = Util.make(new HashMap<>(),
private static final Map<String, ComparisonOp> map = Util.make(new HashMap<>(),
map -> Arrays.stream(values()).forEach(op -> map.put(op.str, op))
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.ftb.mods.ftbfiltersystem.api.client.gui.AbstractFilterScreen;
import dev.ftb.mods.ftbfiltersystem.api.filter.SmartFilter;

@FunctionalInterface
public interface FilterScreenFactory<T extends SmartFilter> {
AbstractFilterConfigScreen<T> createScreen(T filter, AbstractFilterScreen parentScreen);
}
Loading

0 comments on commit a4ba3ae

Please sign in to comment.