Skip to content

Commit

Permalink
Add escape placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
imDaniX committed Feb 21, 2024
1 parent e5d2c26 commit bb285f6
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public void onDisable(@NotNull ReActions.Platform platform) {
new PersistentVarPlaceholders(),
new LocalVarPlaceholder(),
new EnderChestPlaceholder(),
new FunctionPlaceholder()
new FunctionPlaceholder(),
new EscapePlaceholder()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package fun.reactions.module.basic.placeholders;

import fun.reactions.model.environment.Environment;
import fun.reactions.placeholders.ModernPlaceholdersManager;
import fun.reactions.placeholders.Placeholder;
import fun.reactions.util.naming.Aliased;
import fun.reactions.util.parameter.Parameters;
import ink.glowing.text.InkyMessage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;

@Aliased.Names({"escape-inky", "escape-params", "escape-params-full", "escape-phs"})
public class EscapePlaceholder implements Placeholder {
@Override
public @Nullable String resolve(@NotNull Environment env, @NotNull String key, @NotNull String params) {
Set<Type> escapeTypes;
String phText = params;
switch (key) {
case "escape-inky" -> escapeTypes = Set.of(Type.INKY);
case "escape-params" -> escapeTypes = Set.of(Type.PARAMETERS);
case "escape-params-full" -> escapeTypes = Set.of(Type.PARAMETERS_FULL);
case "escape-phs" -> escapeTypes = Set.of(Type.PLACEHOLDERS);
default -> {
String[] split = params.split("\\|", 2);
if (split.length == 1) {
escapeTypes = new TreeSet<>();
escapeTypes.add(Type.PLACEHOLDERS);
escapeTypes.add(Type.PARAMETERS);
} else {
String[] typesStr = split[0].split(",");
escapeTypes = new TreeSet<>();
for (String typeStr : typesStr) switch (typeStr.toLowerCase(Locale.ROOT)) {
case "inky", "inkymessage" -> escapeTypes.add(Type.INKY);
case "params", "parameters" -> escapeTypes.add(Type.PARAMETERS);
case "params-full", "parameters-full" -> escapeTypes.add(Type.PARAMETERS_FULL);
case "phs", "placeholders" -> escapeTypes.add(Type.PLACEHOLDERS);
}
phText = split[1];
}
}
}
String resolved = env.getPlatform().getPlaceholders().resolvePlaceholder(
new Environment(
env.getPlatform(),
"",
env.getVariables(),
env.getPlayer(),
env.getDepth() + 1,
env.isAsync()
), phText
);
if (resolved == null) return null;
for (Type escapeType : escapeTypes) {
resolved = switch (escapeType) {
case INKY -> InkyMessage.escape(resolved);
case PARAMETERS -> Parameters.escapeValue(resolved);
case PARAMETERS_FULL -> Parameters.escape(resolved);
case PLACEHOLDERS -> ModernPlaceholdersManager.escape(resolved);
};
}
return resolved;
}

@Override
public @NotNull String getName() {
return "escape";
}

private enum Type {
PARAMETERS_FULL, PARAMETERS, INKY, PLACEHOLDERS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ModernPlaceholdersManager extends PlaceholdersManager {
oldText = text;
text = parseGradually(env, text);
} while (!oldText.equals(text) && --limit > 0);
return unescapeSpecial(text);
return unescape(text);
}

private String parseGradually(@NotNull Environment env, @NotNull String text) {
Expand Down Expand Up @@ -70,7 +70,7 @@ private String parseGradually(@NotNull Environment env, @NotNull String text) {
if (options != null) {
index += options.length() + 2;
if (options.contains("prms")) processed = Parameters.escapeValue(processed);
if (options.contains("phs")) processed = escapeSpecial(processed);
if (options.contains("phs")) processed = escape(processed);
}
}
builder.append(processed);
Expand Down Expand Up @@ -116,14 +116,14 @@ private String parseGradually(@NotNull Environment env, @NotNull String text) {
return matcher.appendTail(builder).toString();
}

private static @NotNull String escapeSpecial(@NotNull String text) {
public static @NotNull String escape(@NotNull String text) {
return text
.replace("\\", "\\\\")
.replace("%[", "\\%[")
.replace("]", "\\]");
}

private static @NotNull String unescapeSpecial(@NotNull String text) {
public static @NotNull String unescape(@NotNull String text) {
return text
.replace("\\\\", "\\")
.replace("\\%[", "%[")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package fun.reactions.module.basic.placeholders;

import fun.reactions.ReActions;
import fun.reactions.model.environment.Environment;
import fun.reactions.model.environment.Variables;
import fun.reactions.placeholders.ModernPlaceholdersManager;
import fun.reactions.placeholders.PlaceholdersManager;
import org.mockito.Mockito;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

public class EscapePlaceholderTest {
@DataProvider
public static Object[][] resolveData() {
return new Object[][]{
{"esc-ph", "%[esc-ph]", "placeholders:esc-ph", "\\%[esc-ph\\]"},
{"esc-prm", "\\ {test}\\", "params:esc-prm", "\\ {test}\\\\"},
{"esc-prm-f", "\\ {test}\\", "params-full:esc-prm-f", "\\\\ \\{test\\}\\\\"},
{"esc-inky", "&aHello", "inky:esc-inky", "\\&aHello"},
{"esc-default", "some %[test]}", "esc-default", "some \\%[test\\]\\\\}"}
};
}


@Test(dataProvider = "resolveData")
public void resolveTest(String varKey, String varValue, String arg, String expected) {
PlaceholdersManager placeholdersManager = new ModernPlaceholdersManager();
ReActions.Platform platform = Mockito.mock(ReActions.Platform.class);
when(platform.getPlaceholders()).thenReturn(placeholdersManager);

EscapePlaceholder escapePlaceholder = new EscapePlaceholder();
LocalVarPlaceholder varPlaceholder = new LocalVarPlaceholder();
placeholdersManager.registerPlaceholder(escapePlaceholder);
placeholdersManager.registerPlaceholder(varPlaceholder);

Variables vars = new Variables();

Environment env = new Environment(platform, "id", vars, null, 0, false);

vars.set(varKey, varValue);
assertEquals(
escapePlaceholder.resolve(env, "escape", arg),
expected
);
}
}

0 comments on commit bb285f6

Please sign in to comment.