From afb86636f3a73a03e8a228a623af572c6df11830 Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Sun, 29 Dec 2024 16:02:32 +1100 Subject: [PATCH] Distance Filtering --- .../nomiceu/nomilabs/event/ClientProxy.java | 5 ++ .../integration/betterp2p/LabsFilters.java | 50 ++++++++++++++++ .../betterp2p/GuiAdvancedMemoryCardMixin.java | 30 +++++++++- .../mixin/betterp2p/InfoFilterMixin.java | 60 +++++++++++++++++++ .../nomiceu/nomilabs/util/LabsTranslate.java | 14 ++++- .../resources/assets/nomilabs/lang/en_us.lang | 7 +++ .../resources/mixins.nomilabs.betterp2p.json | 1 + 7 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsFilters.java create mode 100644 src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoFilterMixin.java diff --git a/src/main/java/com/nomiceu/nomilabs/event/ClientProxy.java b/src/main/java/com/nomiceu/nomilabs/event/ClientProxy.java index eaff83bc..d5dbbc7c 100644 --- a/src/main/java/com/nomiceu/nomilabs/event/ClientProxy.java +++ b/src/main/java/com/nomiceu/nomilabs/event/ClientProxy.java @@ -20,6 +20,7 @@ import com.nomiceu.nomilabs.NomiLabs; import com.nomiceu.nomilabs.fluid.registry.LabsFluids; import com.nomiceu.nomilabs.gregtech.block.registry.LabsMetaBlocks; +import com.nomiceu.nomilabs.integration.betterp2p.LabsFilters; import com.nomiceu.nomilabs.integration.betterp2p.ModeDescriptionsHandler; import com.nomiceu.nomilabs.integration.betterquesting.LabsTierHelper; import com.nomiceu.nomilabs.integration.findme.FindMeKeybindRegister; @@ -62,6 +63,10 @@ public static void postInit() { // Register Find Me's Fluid Keyubind if (Loader.isModLoaded(LabsValues.FIND_ME_MODID)) FindMeKeybindRegister.register(); + + // Register Better P2P Custom Filters + if (Loader.isModLoaded(LabsValues.BETTER_P2P_MODID)) + LabsFilters.postInit(); } @SubscribeEvent diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsFilters.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsFilters.java new file mode 100644 index 00000000..cdb0772d --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsFilters.java @@ -0,0 +1,50 @@ +package com.nomiceu.nomilabs.integration.betterp2p; + +import java.util.List; + +import net.minecraftforge.common.util.EnumHelper; + +import com.projecturanus.betterp2p.client.gui.Filter; +import com.projecturanus.betterp2p.client.gui.InfoWrapper; + +import kotlin.jvm.functions.Function2; +import kotlin.text.Regex; + +public class LabsFilters { + + public static final Filter DISTANCE_LESS = addFilter("DISTANCE_LESS", + new Regex("\\A@distless=((\\d+(\\.\\d*)?)|(\\.\\d+))m?\\z"), (info, args) -> { + double distance = Double.MAX_VALUE; + + for (String arg : args) { + distance = Math.min(Double.parseDouble(arg), distance); + } + + return ((AccessibleInfoWrapper) (Object) info).labs$getDistance() <= distance; + }); + + public static final Filter DISTANCE_MORE = addFilter("DISTANCE_MORE", + new Regex("\\A@distmore=((\\d+(\\.\\d*)?)|(\\.\\d+))m?\\z"), (info, args) -> { + double distance = 0; + + for (String arg : args) { + distance = Math.max(Double.parseDouble(arg), distance); + } + + return ((AccessibleInfoWrapper) (Object) info).labs$getDistance() >= distance; + }); + + /** + * Essentially, this loads the class, allowing the above values to be added. + *

+ * If for some reason, the values are needed before this, they will still be loaded, and calling init will have no + * affect on that. + */ + public static void postInit() {} + + private static Filter addFilter(String name, Regex regex, Function2, Boolean> filter) { + return EnumHelper.addEnum(Filter.class, name, + new Class[] { Regex.class, Function2.class }, + regex, filter); + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java index 63f5ea60..e81415b7 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java @@ -1,5 +1,10 @@ package com.nomiceu.nomilabs.mixin.betterp2p; +import static com.nomiceu.nomilabs.util.LabsTranslate.*; +import static net.minecraft.util.text.TextFormatting.*; + +import java.util.List; + import net.minecraft.client.gui.GuiScreen; import org.lwjgl.input.Keyboard; @@ -11,7 +16,9 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import com.google.common.collect.ImmutableList; import com.nomiceu.nomilabs.integration.betterp2p.*; import com.projecturanus.betterp2p.client.gui.GuiAdvancedMemoryCard; import com.projecturanus.betterp2p.client.gui.InfoList; @@ -25,9 +32,10 @@ import kotlin.Pair; /** - * Allows accessing needed functions and fields, and initializes playerPos field in InfoList. Also fills up - * LabsClientCache, cancels existing checks for invalid setups (moved to infoList, right before sorting), - * calls proper resetting of scrollbar in custom places, and allows arrows to scroll. + * Allows accessing needed functions and fields, initializes playerPos field in InfoList, fills up + * LabsClientCache, cancels existing checks for invalid setups (moved to InfoList, right before sorting), + * calls proper resetting of scrollbar in custom places, allows for custom filtering hover text, + * and allow using arrow keys to scroll. */ @Mixin(value = GuiAdvancedMemoryCard.class, remap = false) public abstract class GuiAdvancedMemoryCardMixin extends GuiScreen implements AccessibleGuiAdvancedMemoryCard { @@ -108,6 +116,22 @@ public abstract class GuiAdvancedMemoryCardMixin extends GuiScreen implements Ac return labs$getAccessibleInfo().labs$getSortMode(); } + @Inject(method = "getSortRules", at = @At("HEAD"), cancellable = true) + private void getCustomSortRules(CallbackInfoReturnable> cir) { + cir.setReturnValue(ImmutableList.of( + format(translate("nomilabs.gui.advanced_memory_card.filter.title"), BOLD, UNDERLINE), + "§7 - " + translate("nomilabs.gui.advanced_memory_card.filter.name"), + "§9@in§7 - " + translate("gui.advanced_memory_card.sortinfo2"), + "§6@out§7 - " + translate("gui.advanced_memory_card.sortinfo3"), + "§a@b§7 - " + translate("gui.advanced_memory_card.sortinfo4"), + "§c@u§7 - " + translate("gui.advanced_memory_card.sortinfo5"), + "§b@distless=§7 - " + translate("nomilabs.gui.advanced_memory_card.filter.distless"), + "§d@distmore=§7 - " + translate("nomilabs.gui.advanced_memory_card.filter.distmore"), + "§e@type=;;...§7 - " + translate("nomilabs.gui.advanced_memory_card.filter.type"), + "", + translateFormat("nomilabs.gui.advanced_memory_card.filter.end", GRAY))); + } + @Inject(method = "initGui", at = @At("HEAD"), remap = true) private void setup(CallbackInfo ci) { labs$getAccessibleInfo().labs$setPlayerPos(mc.player.getPositionVector()); diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoFilterMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoFilterMixin.java new file mode 100644 index 00000000..f2c68334 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoFilterMixin.java @@ -0,0 +1,60 @@ +package com.nomiceu.nomilabs.mixin.betterp2p; + +import java.util.ArrayList; +import java.util.Iterator; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import com.llamalad7.mixinextras.sugar.Local; +import com.nomiceu.nomilabs.integration.betterp2p.LabsFilters; +import com.projecturanus.betterp2p.client.gui.InfoFilter; + +import kotlin.sequences.Sequence; +import kotlin.text.MatchResult; + +@Mixin(value = InfoFilter.class, remap = false) +public class InfoFilterMixin { + + @Inject(method = "updateFilter", + at = @At(value = "INVOKE", target = "Ljava/util/Map;clear()V", shift = At.Shift.AFTER), + locals = LocalCapture.CAPTURE_FAILEXCEPTION, + require = 1) + private void handleLabsFilter(String query, CallbackInfo ci, @Local Sequence tokens) { + Iterator iter = tokens.iterator(); + + while (iter.hasNext()) { + String token = iter.next().getValue(); + + // It would be nice if we could remove these values + // So they don't have to be regex matched + // But sequence is read only + if (token.contains("@")) { + if (LabsFilters.DISTANCE_LESS.getPattern().matches(token)) { + MatchResult result = LabsFilters.DISTANCE_LESS.getPattern().find(token, 0); + labs$getThis().getActiveFilters().computeIfAbsent(LabsFilters.DISTANCE_LESS, + (k) -> new ArrayList<>()); + + labs$getThis().getActiveFilters().get(LabsFilters.DISTANCE_LESS) + .add(result.getGroupValues().get(1)); + } else if (LabsFilters.DISTANCE_MORE.getPattern().matches(token)) { + MatchResult result = LabsFilters.DISTANCE_MORE.getPattern().find(token, 0); + labs$getThis().getActiveFilters().computeIfAbsent(LabsFilters.DISTANCE_MORE, + (k) -> new ArrayList<>()); + + labs$getThis().getActiveFilters().get(LabsFilters.DISTANCE_MORE) + .add(result.getGroupValues().get(1)); + } + } + } + } + + @Unique + private InfoFilter labs$getThis() { + return (InfoFilter) (Object) this; + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/util/LabsTranslate.java b/src/main/java/com/nomiceu/nomilabs/util/LabsTranslate.java index 7ebe62c6..19301c62 100644 --- a/src/main/java/com/nomiceu/nomilabs/util/LabsTranslate.java +++ b/src/main/java/com/nomiceu/nomilabs/util/LabsTranslate.java @@ -51,13 +51,23 @@ private static String translateServerSide(String key, Object... params) { } /** - * Only GT Format Code version is available.
- * If need Text Formatting format, place it in lang. + * Generally, if need Text Formatting format, place it in lang. */ + public static String translateFormat(String key, TextFormatting format, Object... params) { + return format(translate(key, params), format); + } + public static String translateFormat(String key, TooltipHelper.GTFormatCode format, Object... params) { return format(translate(key, params), format); } + /** + * Generally, if need Text Formatting format, place it in lang. + */ + public static String translateFormat(String key, Format format, Object... params) { + return format(translate(key, params), format); + } + public static String format(String str, TextFormatting... formats) { return String.join("", Arrays.stream(formats).map(TextFormatting::toString).toArray(String[]::new)) + str + TextFormatting.RESET; diff --git a/src/main/resources/assets/nomilabs/lang/en_us.lang b/src/main/resources/assets/nomilabs/lang/en_us.lang index eca6176b..a6ff3f72 100644 --- a/src/main/resources/assets/nomilabs/lang/en_us.lang +++ b/src/main/resources/assets/nomilabs/lang/en_us.lang @@ -175,6 +175,13 @@ nomilabs.gui.advanced_memory_card.hover_info.connections.output.none=§cNo Outpu nomilabs.gui.advanced_memory_card.hover_info.connections.output.one=§a%s Output nomilabs.gui.advanced_memory_card.hover_info.connections.output.multi=§b%s Outputs +nomilabs.gui.advanced_memory_card.filter.title=Filtering Tags: +nomilabs.gui.advanced_memory_card.filter.distless=Set a Maximum Distance +nomilabs.gui.advanced_memory_card.filter.distmore=Set a Minimum Distance +nomilabs.gui.advanced_memory_card.filter.type=Filter by Type +nomilabs.gui.advanced_memory_card.filter.name=Filter by Name +nomilabs.gui.advanced_memory_card.filter.end=Tags are Separated by Spaces. + nomilabs.gui.advanced_memory_card.sort.default=Sorting Mode: §bDefault nomilabs.gui.advanced_memory_card.sort.distance=Sorting Mode: §bDistance nomilabs.gui.advanced_memory_card.sort.name=Sorting Mode: §bName diff --git a/src/main/resources/mixins.nomilabs.betterp2p.json b/src/main/resources/mixins.nomilabs.betterp2p.json index 725b4627..781ac8d6 100644 --- a/src/main/resources/mixins.nomilabs.betterp2p.json +++ b/src/main/resources/mixins.nomilabs.betterp2p.json @@ -17,6 +17,7 @@ "ClientProxyMixin", "GuiAdvancedMemoryCardKtAccessor", "GuiAdvancedMemoryCardMixin", + "InfoFilterMixin", "InfoListMixin", "InfoWrapperMixin", "ModeWidgetButtonMixin",