Skip to content

Commit 331dbbf

Browse files
committed
Allow to use all ReA placeholders in PAPI expansion
1 parent 5db69f8 commit 331dbbf

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

reactions/src/main/java/fun/reactions/module/ModulesRegistry.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.jetbrains.annotations.NotNull;
1010

1111
import java.io.File;
12-
import java.io.IOException;
1312
import java.net.URI;
1413
import java.net.URL;
1514
import java.net.URLClassLoader;
@@ -98,9 +97,9 @@ private <T extends Named> void register(String what, Collection<T> values, Consu
9897
try {
9998
register.accept(type);
10099
names.add(type.getName().toUpperCase(Locale.ROOT));
101-
} catch (IllegalStateException e) {
100+
} catch (Exception ex) {
102101
if (failed == null) failed = new ArrayList<>();
103-
failed.add(e.getMessage());
102+
failed.add(ex.getMessage());
104103
}
105104
}
106105
if (!names.isEmpty()) {
@@ -131,7 +130,7 @@ public void loadFolderModules() {
131130
toRegister.add(clazz);
132131
}
133132
}
134-
} catch (IOException | ClassNotFoundException ex) {
133+
} catch (Exception ex) {
135134
platform.logger().error("Something went wrong during module parsing", ex);
136135
}
137136
}

reactions/src/main/java/fun/reactions/module/papi/PapiModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class PapiModule implements Module { // TODO To own jar
2222

2323
@Override
2424
public void preRegister(@NotNull ReActions.Platform platform) {
25-
new RaPapiExpansion().register();
25+
new RaPapiExpansion(platform).register();
2626
this.papiPlugin = (PlaceholderAPIPlugin) Objects.requireNonNull(
2727
platform.getServer().getPluginManager().getPlugin("PlaceholderAPI")
2828
);
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package fun.reactions.module.papi.external;
22

3-
import fun.reactions.PersistentVariablesManager;
43
import fun.reactions.ReActions;
4+
import fun.reactions.model.environment.Environment;
5+
import fun.reactions.model.environment.Variables;
6+
import fun.reactions.placeholders.PlaceholdersManager;
57
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
68
import org.bukkit.OfflinePlayer;
7-
import org.bukkit.util.StringUtil;
9+
import org.bukkit.entity.Player;
810
import org.jetbrains.annotations.NotNull;
911

1012
public class RaPapiExpansion extends PlaceholderExpansion {
13+
private final ReActions.Platform platform;
14+
private final PlaceholdersManager raPlaceholders;
15+
16+
public RaPapiExpansion(@NotNull ReActions.Platform platform) {
17+
this.platform = platform;
18+
this.raPlaceholders = platform.getPlaceholders();
19+
}
20+
1121
@Override
1222
public boolean persist() {
1323
return true;
@@ -25,29 +35,22 @@ public boolean canRegister() {
2535

2636
@Override
2737
public @NotNull String getAuthor() {
28-
return "fromgate";
38+
return "imDaniX";
2939
}
3040

3141
@Override
3242
public @NotNull String getVersion() {
33-
return "0.0.3";
43+
return "1.0";
3444
}
3545

3646
@Override
3747
public String onRequest(OfflinePlayer player, @NotNull String param) {
38-
PersistentVariablesManager variables = ReActions.getPersistentVariables();
39-
40-
if (StringUtil.startsWithIgnoreCase(param, "varp:")) {
41-
return player == null || player.getName() == null
42-
? null
43-
: variables.getVariable(player.getName(), param.substring(5));
44-
} else if (StringUtil.startsWithIgnoreCase(param, "var:")) {
45-
String[] split = param.substring(4).split("\\.", 2);
46-
return split.length > 1
47-
? variables.getVariable(split[0], split[1])
48-
: variables.getVariable(null, split[0]);
49-
}
50-
51-
return null;
48+
return raPlaceholders.resolvePlaceholder(new Environment(
49+
platform,
50+
"",
51+
new Variables(),
52+
player instanceof Player onlinePlayer ? onlinePlayer : null,
53+
true // We don't know if we're in async, so let's consider we are
54+
), param);
5255
}
5356
}

reactions/src/main/java/fun/reactions/placeholders/Placeholder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import fun.reactions.model.environment.Environment;
44
import fun.reactions.util.naming.Named;
5-
import org.intellij.lang.annotations.Pattern;
65
import org.jetbrains.annotations.NotNull;
76
import org.jetbrains.annotations.Nullable;
87

@@ -14,7 +13,7 @@ public interface Placeholder extends Named {
1413
* @param params Text of placeholder(e.g. %var:test% - test)
1514
* @return Processed placeholder
1615
*/
17-
@Nullable String resolve(@NotNull Environment env, @Pattern("[a-z\\d_-]+") @NotNull String key, @NotNull String params);
16+
@Nullable String resolve(@NotNull Environment env, @NotNull String key, @NotNull String params);
1817

1918
interface Dynamic extends Placeholder {
2019
@Override

reactions/src/main/java/fun/reactions/placeholders/PlaceholdersManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ public static void setCountLimit(int countLimit) {
2222

2323
public final void registerPlaceholder(@NotNull Placeholder ph) {
2424
if (!resolver.add(ph)) {
25-
throw new IllegalArgumentException("Cannot register '" + ph.getName() + "' placeholder - it doesn't implement any specific Placeholder interfaces");
25+
throw new IllegalStateException("Cannot register " + ph.getClass() + " placeholder - " +
26+
"the name '" + ph.getName() + "' is already registered");
2627
}
2728
}
2829

2930
public abstract @NotNull String parse(@NotNull Environment env, @NotNull String text);
3031

31-
protected final @Nullable String resolvePlaceholder(@NotNull Environment env, @NotNull String phText) {
32+
public final @Nullable String resolvePlaceholder(@NotNull Environment env, @NotNull String phText) {
3233
return resolver.resolve(env, phText);
3334
}
3435
}

0 commit comments

Comments
 (0)