Skip to content

Commit

Permalink
Distance Filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Dec 29, 2024
1 parent 2fcbe42 commit afb8663
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/event/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<InfoWrapper, List<String>, Boolean> filter) {
return EnumHelper.addEnum(Filter.class, name,
new Class<?>[] { Regex.class, Function2.class },
regex, filter);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<List<String>> cir) {
cir.setReturnValue(ImmutableList.of(
format(translate("nomilabs.gui.advanced_memory_card.filter.title"), BOLD, UNDERLINE),
"<name>§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=<distance>§7 - " + translate("nomilabs.gui.advanced_memory_card.filter.distless"),
"§d@distmore=<distance>§7 - " + translate("nomilabs.gui.advanced_memory_card.filter.distmore"),
"§e@type=<type1>;<type2>;...§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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MatchResult> tokens) {
Iterator<MatchResult> 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;
}
}
14 changes: 12 additions & 2 deletions src/main/java/com/nomiceu/nomilabs/util/LabsTranslate.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,23 @@ private static String translateServerSide(String key, Object... params) {
}

/**
* Only GT Format Code version is available.<br>
* 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;
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/assets/nomilabs/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.nomilabs.betterp2p.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ClientProxyMixin",
"GuiAdvancedMemoryCardKtAccessor",
"GuiAdvancedMemoryCardMixin",
"InfoFilterMixin",
"InfoListMixin",
"InfoWrapperMixin",
"ModeWidgetButtonMixin",
Expand Down

0 comments on commit afb8663

Please sign in to comment.