forked from Redolith/ReActions
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
4 changed files
with
131 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
76 changes: 76 additions & 0 deletions
76
reactions/src/main/java/fun/reactions/module/basic/placeholders/EscapePlaceholder.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,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 | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
reactions/src/test/java/fun/reactions/module/basic/placeholders/EscapePlaceholderTest.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,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 | ||
); | ||
} | ||
} |