-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fcbe42
commit afb8663
Showing
7 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsFilters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoFilterMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters