Update to 1.20 and use String2ObjectMap
This commit is contained in:
parent
857774fa2c
commit
8c5c2e168e
|
@ -14,5 +14,5 @@ dependencies {
|
|||
// Dev env
|
||||
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-config-ui-tiny-v1:${prop("libjf_version")}")
|
||||
modLocalRuntime("io.gitlab.jfronny.libjf:libjf-devutil:${prop("libjf_version")}")
|
||||
modLocalRuntime("com.terraformersmc:modmenu:6.1.0-rc.4")
|
||||
modLocalRuntime("com.terraformersmc:modmenu:7.0.1")
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# https://fabricmc.net/develop/
|
||||
minecraft_version=1.19.4
|
||||
minecraft_version=1.20
|
||||
yarn_mappings=build.1
|
||||
loader_version=0.14.17
|
||||
loader_version=0.14.21
|
||||
|
||||
maven_group=io.gitlab.jfronny
|
||||
archives_base_name=chat-transform
|
||||
|
@ -13,5 +13,5 @@ curseforge_id=839175
|
|||
curseforge_required_dependencies=libjf
|
||||
curseforge_optional_dependencies=modmenu
|
||||
|
||||
libjf_version=3.7.1
|
||||
fabric_version=0.75.3+1.19.4
|
||||
libjf_version=3.8.0
|
||||
fabric_version=0.83.0+1.20
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
package io.gitlab.jfronny.chattransform.client.mixin;
|
||||
|
||||
import io.gitlab.jfronny.chattransform.*;
|
||||
import io.gitlab.jfronny.chattransform.Cfg;
|
||||
import io.gitlab.jfronny.chattransform.ChatTransform;
|
||||
import io.gitlab.jfronny.chattransform.client.*;
|
||||
import net.minecraft.client.font.TextRenderer;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.ClickableWidget;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.text.Text;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.spongepowered.asm.mixin.*;
|
||||
import org.spongepowered.asm.mixin.injection.*;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mixin(TextFieldWidget.class)
|
||||
public abstract class TextFieldWidgetMixin extends ClickableWidget implements ITextFieldWidget {
|
||||
|
@ -75,29 +75,32 @@ public abstract class TextFieldWidgetMixin extends ClickableWidget implements IT
|
|||
if (!transformStart.isAvailable()) transformStart.set(selectionStart - 1);
|
||||
if (transformStart.get() >= selectionStart) return;
|
||||
String currentString = getText().substring(transformStart.get(), selectionStart);
|
||||
Set<Map.Entry<String, String>> complete = getStartingWith(currentString);
|
||||
// Exact match
|
||||
if (Cfg.substitutions.containsKey(currentString) && complete.size() == 1
|
||||
&& substitute(transformStart.get(), selectionStart, Cfg.substitutions.get(currentString))) {
|
||||
transformStart.set(selectionStart);
|
||||
Iterator<? extends Map.Entry<String, String>> complete = Cfg.substitutions.getKeyValuePairsForKeysStartingWith(currentString).iterator();
|
||||
if (complete.hasNext()) {
|
||||
Map.Entry<String, String> substitution = complete.next();
|
||||
if (substitution.getKey().length() == currentString.length() // Match without "overshoot"
|
||||
&& !complete.hasNext() // No other options
|
||||
&& substitute(transformStart.get(), selectionStart, substitution.getValue())) {
|
||||
transformStart.set(selectionStart);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (complete.isEmpty()) {
|
||||
if (transformStart.get() == selectionStart - 1) {
|
||||
// Nothing starts with this char
|
||||
transformStart.increment();
|
||||
// No matches
|
||||
if (transformStart.get() == selectionStart - 1) {
|
||||
// Nothing starts with this char
|
||||
transformStart.increment();
|
||||
} else {
|
||||
// Something previously started with this...
|
||||
String previousString = getText().substring(transformStart.get(), selectionStart - 1);
|
||||
String substitution = Cfg.substitutions.get(previousString);
|
||||
if (substitution != null
|
||||
// ...and matched -> replace
|
||||
&& substitute(transformStart.get(), selectionStart - 1, substitution)) {
|
||||
setCursor(selectionStart + 1);
|
||||
} else {
|
||||
// Something previously started with this...
|
||||
String previousString = getText().substring(transformStart.get(), selectionStart - 1);
|
||||
if (Cfg.substitutions.containsKey(previousString)
|
||||
// ...and matched -> replace
|
||||
&& substitute(transformStart.get(), selectionStart - 1, Cfg.substitutions.get(previousString))) {
|
||||
setCursor(selectionStart + 1);
|
||||
} else {
|
||||
// ...and didn't match -> move transform start and call transform again (substring might have matched)
|
||||
transformStart.increment();
|
||||
transform();
|
||||
}
|
||||
// ...and didn't match -> move transform start and call transform again (substring might have matched)
|
||||
transformStart.increment();
|
||||
transform();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -107,8 +110,8 @@ public abstract class TextFieldWidgetMixin extends ClickableWidget implements IT
|
|||
String str = getText();
|
||||
if (!transformStart.isAvailable() || transformStart.get() >= selectionStart) return str;
|
||||
String currentString = str.substring(transformStart.get(), selectionStart);
|
||||
if (!Cfg.substitutions.containsKey(currentString)
|
||||
|| !substitute(transformStart.get(), selectionStart, Cfg.substitutions.get(currentString))) {
|
||||
String substitution = Cfg.substitutions.get(currentString);
|
||||
if (substitution == null || !substitute(transformStart.get(), selectionStart, substitution)) {
|
||||
transformStart.increment();
|
||||
return chattransform$finalize();
|
||||
}
|
||||
|
@ -130,32 +133,28 @@ public abstract class TextFieldWidgetMixin extends ClickableWidget implements IT
|
|||
} else return false;
|
||||
}
|
||||
|
||||
Set<Map.Entry<String, String>> getStartingWith(String start) {
|
||||
return Cfg.substitutions.entrySet().stream().filter(s -> s.getKey().startsWith(start)).collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
@Inject(method = "renderButton(Lnet/minecraft/client/util/math/MatrixStack;IIF)V", at = @At(value = "TAIL"))
|
||||
void renderTransformStart(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) {
|
||||
@Inject(method = "renderButton(Lnet/minecraft/client/gui/DrawContext;IIF)V", at = @At(value = "TAIL"))
|
||||
void renderTransformStart(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
|
||||
if (isVisible() && Cfg.Client.visualize) {
|
||||
int y = this.drawsBackground ? this.getY() + (this.height - 8) / 2 : this.getY();
|
||||
if (transformStart.isAvailable()) {
|
||||
int x = getCharacterX(transformStart.get());
|
||||
chattransform$fill(matrices, x, y - 1, x + 1, y + 1 + 9, 0x7f0000ff);
|
||||
chattransform$fill(context, x, y - 1, x + 1, y + 1 + 9, 0x7f0000ff);
|
||||
}
|
||||
if (transformStart.showPrevious()) {
|
||||
int x = getCharacterX(transformStart.getPrevious());
|
||||
chattransform$fill(matrices, x, y - 1, x + 1, y + 1 + 9, 0x7fff0000);
|
||||
chattransform$fill(context, x, y - 1, x + 1, y + 1 + 9, 0x7fff0000);
|
||||
}
|
||||
if (lastSubstitution != null && lastSubstitution.shouldShow()) {
|
||||
int start = getCharacterX(lastSubstitution.start());
|
||||
int end = getCharacterX(Math.min(lastSubstitution.end() + 1, text.length()));
|
||||
chattransform$fill(matrices, start, y - 1, end, y + 1 + 9, 0x7fffff00);
|
||||
chattransform$fill(context, start, y - 1, end, y + 1 + 9, 0x7fffff00);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Unique
|
||||
private void chattransform$fill(MatrixStack matrices, int x1, int y1, int x2, int y2, int color) {
|
||||
private void chattransform$fill(DrawContext context, int x1, int y1, int x2, int y2, int color) {
|
||||
if (x1 < 0) x1 = 0;
|
||||
if (x2 < 0) x2 = 0;
|
||||
int maxX = getX() + getWidth();
|
||||
|
@ -174,7 +173,7 @@ public abstract class TextFieldWidgetMixin extends ClickableWidget implements IT
|
|||
y2 = i;
|
||||
}
|
||||
|
||||
textRenderer.draw(matrices, "X={" + x1 + ";" + x2 + "} Y={" + y1 + ";" + y2 + "}", 0, 0, 0xFF000000);
|
||||
fill(matrices, x1, y1, x2, y2, color);
|
||||
context.drawText(textRenderer, "X={" + x1 + ";" + x2 + "} Y={" + y1 + ";" + y2 + "}", 0, 0, 0xFF000000, false);
|
||||
context.fill(x1, y1, x2, y2, color);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
"chat-transform.jfconfig.client.title": "Client Settings",
|
||||
"chat-transform.jfconfig.client.mode": "Mode",
|
||||
"chat-transform.jfconfig.client.mode.tooltip": "When to perform replacements",
|
||||
"chat-transform.jfconfig.enum.Mode.Live": "Live",
|
||||
"chat-transform.jfconfig.enum.Mode.OnSend": "On Send",
|
||||
"chat-transform.jfconfig.client.enum.Mode.Live": "Live",
|
||||
"chat-transform.jfconfig.client.enum.Mode.OnSend": "On Send",
|
||||
"chat-transform.jfconfig.client.visualize": "Visualize",
|
||||
"chat-transform.jfconfig.client.visualize.tooltip": "Visualize transformations as you type in live mode",
|
||||
"chat-transform.jfconfig.server.title": "Server Settings",
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package io.gitlab.jfronny.chattransform;
|
||||
|
||||
import io.gitlab.jfronny.commons.data.String2ObjectMap;
|
||||
import io.gitlab.jfronny.commons.serialize.gson.api.v1.Ignore;
|
||||
import io.gitlab.jfronny.libjf.config.api.v1.*;
|
||||
|
||||
|
@ -7,7 +8,7 @@ import java.util.*;
|
|||
|
||||
@JfConfig
|
||||
public class Cfg {
|
||||
@Entry public static Map<String, String> substitutions = Map.of();
|
||||
@Entry public static String2ObjectMap<String> substitutions = new String2ObjectMap<>();
|
||||
|
||||
@Category
|
||||
public static class Client {
|
||||
|
@ -29,7 +30,7 @@ public class Cfg {
|
|||
|
||||
@Preset
|
||||
public static void owo() {
|
||||
substitutions = new LinkedHashMap<>();
|
||||
substitutions = new String2ObjectMap<>();
|
||||
substitutions.put("r", "w");
|
||||
substitutions.put("l", "w");
|
||||
substitutions.put("R", "W");
|
||||
|
@ -46,7 +47,7 @@ public class Cfg {
|
|||
|
||||
@Preset
|
||||
public static void katakana() {
|
||||
substitutions = new LinkedHashMap<>();
|
||||
substitutions = new String2ObjectMap<>();
|
||||
substitutions.put("-", "ー");
|
||||
for (char c : consonants) {
|
||||
substitutions.put("" + c + c, "ッ" + c);
|
||||
|
@ -58,7 +59,7 @@ public class Cfg {
|
|||
|
||||
@Preset
|
||||
public static void hiragana() {
|
||||
substitutions = new LinkedHashMap<>();
|
||||
substitutions = new String2ObjectMap<>();
|
||||
substitutions.put("-", "ー");
|
||||
for (char c : consonants) {
|
||||
substitutions.put("" + c + c, "っ" + c);
|
||||
|
|
|
@ -2,27 +2,11 @@ package io.gitlab.jfronny.chattransform;
|
|||
|
||||
import io.gitlab.jfronny.commons.log.Logger;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ChatTransform {
|
||||
public static final String MOD_ID = "chat-transform";
|
||||
public static final Logger LOG = Logger.forName(MOD_ID);
|
||||
|
||||
public static String transform(String text) {
|
||||
for (Map.Entry<String, String> e : Cfg.substitutions
|
||||
.entrySet()
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(s -> s.getKey().length()))
|
||||
.entrySet()
|
||||
.stream()
|
||||
.sorted(Map.Entry.<Integer, List<Map.Entry<String, String>>>comparingByKey().reversed())
|
||||
.map(Map.Entry::getValue)
|
||||
.flatMap(Collection::stream)
|
||||
.toList()) {
|
||||
text = text.replaceAll(Pattern.quote(e.getKey()), e.getValue());
|
||||
}
|
||||
return text;
|
||||
return Cfg.substitutions.asSubstitution().apply(text);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,6 @@
|
|||
],
|
||||
"depends": {
|
||||
"fabricloader": ">=0.14.17",
|
||||
"minecraft": "1.19.4"
|
||||
"minecraft": "*"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue