From 39b6c4b243ab78e0bf53e08cb4ecab013a4e5b3f Mon Sep 17 00:00:00 2001 From: szumielxd <43210079+szumielxd@users.noreply.github.com> Date: Mon, 22 Aug 2022 08:53:53 +0200 Subject: [PATCH] Updated to Skript 2.6 and added relational placeholders --- .../placeholderAPI/PAPIEvent.java | 68 ------------- .../placeholderAPI/PAPIListener.java | 63 ------------ .../PlaceholderSK.java | 23 +++-- .../placeholderapi/PAPIEvent.java | 78 +++++++++++++++ .../placeholderapi/PAPIListener.java | 96 +++++++++++++++++++ .../events/SKPlaceholderRequestEvent.java | 42 ++++---- .../skript/expressions/ExprIdentifier.java | 16 +++- .../skript/expressions/ExprPlaceholder.java | 20 ++-- .../skript/expressions/ExprPrefix.java | 16 +++- .../skript/expressions/ExprResult.java | 16 +++- .../skript/expressions/ExprTarget.java | 66 +++++++++++++ .../utils/ReflectionUtils.java | 4 +- 12 files changed, 327 insertions(+), 181 deletions(-) delete mode 100644 src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIEvent.java delete mode 100644 src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIListener.java rename src/main/java/me/szumielxd/{PlaceholderSK => placeholdersk}/PlaceholderSK.java (64%) create mode 100644 src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIEvent.java create mode 100644 src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIListener.java rename src/main/java/me/szumielxd/{PlaceholderSK => placeholdersk}/skript/events/SKPlaceholderRequestEvent.java (73%) rename src/main/java/me/szumielxd/{PlaceholderSK => placeholdersk}/skript/expressions/ExprIdentifier.java (80%) rename src/main/java/me/szumielxd/{PlaceholderSK => placeholdersk}/skript/expressions/ExprPlaceholder.java (82%) rename src/main/java/me/szumielxd/{PlaceholderSK => placeholdersk}/skript/expressions/ExprPrefix.java (78%) rename src/main/java/me/szumielxd/{PlaceholderSK => placeholdersk}/skript/expressions/ExprResult.java (84%) create mode 100644 src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprTarget.java rename src/main/java/me/szumielxd/{PlaceholderSK => placeholdersk}/utils/ReflectionUtils.java (98%) diff --git a/src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIEvent.java b/src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIEvent.java deleted file mode 100644 index 3fb48ed..0000000 --- a/src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIEvent.java +++ /dev/null @@ -1,68 +0,0 @@ -package me.szumielxd.PlaceholderSK.placeholderAPI; - -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class PAPIEvent extends Event { - - - private static HandlerList handlerList = new HandlerList(); - private Player player; - private String identifier; - private String result; - private String prefix; - - - @Override - public HandlerList getHandlers() { - return handlerList; - } - - - public static HandlerList getHandlerList() { - return handlerList; - } - - public PAPIEvent(Player p, String prefix, String identifier) { - this.identifier = identifier; - this.player = p; - this.prefix = prefix; - } - - public PAPIEvent(Player p, String prefix, String identifier, boolean async) { - super(async); - this.identifier = identifier; - this.player = p; - this.prefix = prefix; - } - - - public void setResult(String result) { - this.result = result; - } - - - public String getResult() { - return this.result; - } - - - public String getIdentifier() { - return this.identifier; - } - - - public Player getPlayer() { - return this.player; - } - - - public String getPrefix() { - return this.prefix; - } - - - - -} \ No newline at end of file diff --git a/src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIListener.java b/src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIListener.java deleted file mode 100644 index 09ae57a..0000000 --- a/src/main/java/me/szumielxd/PlaceholderSK/placeholderAPI/PAPIListener.java +++ /dev/null @@ -1,63 +0,0 @@ -package me.szumielxd.PlaceholderSK.placeholderAPI; - -import org.bukkit.entity.Player; -import org.bukkit.event.EventException; -import org.bukkit.event.Listener; - -import me.clip.placeholderapi.expansion.PlaceholderExpansion; -import me.szumielxd.PlaceholderSK.PlaceholderSK; -import me.szumielxd.PlaceholderSK.skript.events.SKPlaceholderRequestEvent; - -public class PAPIListener extends PlaceholderExpansion { - - - private final String identifier; - private final SKPlaceholderRequestEvent skEvent; - private final Listener listener; - - - public PAPIListener(SKPlaceholderRequestEvent skEvent, String prefix) { - this.skEvent = skEvent; - this.identifier = prefix; - this.listener = new Listener() {}; - } - - - @Override - public boolean persist() { - return true; - } - - - @Override - public String getAuthor() { - return String.join(", ", PlaceholderSK.getInstance().getDescription().getAuthors()); - } - - @Override - public String getIdentifier() { - return identifier; - } - - @Override - public String getVersion() { - return PlaceholderSK.getInstance().getDescription().getVersion(); - } - - @Override - public String getPlugin() { - return "Skript"; - } - - @Override - public String onPlaceholderRequest(Player p, String param) { - PAPIEvent event = new PAPIEvent(p, identifier, param); - try { - this.skEvent.getExecutor().execute(this.listener, event); - } catch (EventException e) { - e.printStackTrace(); - } - return event.getResult(); - } - -} \ No newline at end of file diff --git a/src/main/java/me/szumielxd/PlaceholderSK/PlaceholderSK.java b/src/main/java/me/szumielxd/placeholdersk/PlaceholderSK.java similarity index 64% rename from src/main/java/me/szumielxd/PlaceholderSK/PlaceholderSK.java rename to src/main/java/me/szumielxd/placeholdersk/PlaceholderSK.java index cf23da0..322ac6d 100644 --- a/src/main/java/me/szumielxd/PlaceholderSK/PlaceholderSK.java +++ b/src/main/java/me/szumielxd/placeholdersk/PlaceholderSK.java @@ -1,4 +1,4 @@ -package me.szumielxd.PlaceholderSK; +package me.szumielxd.placeholdersk; import java.io.IOException; @@ -6,7 +6,7 @@ import ch.njol.skript.Skript; import ch.njol.skript.SkriptAddon; -import me.szumielxd.PlaceholderSK.utils.ReflectionUtils; +import me.szumielxd.placeholdersk.utils.ReflectionUtils; public class PlaceholderSK extends JavaPlugin { @@ -16,13 +16,14 @@ public class PlaceholderSK extends JavaPlugin { private SkriptAddon addon; + @Override public void onEnable() { - instance = this; + setInstance(this); reflections = new ReflectionUtils(); try { - getSkriptInstance(); - (this.addon = getSkriptInstance()).loadClasses("me.szumielxd.PlaceholderSK.skript", "events", "expressions"); + this.addon = getSkriptInstance(); + this.addon.loadClasses("me.szumielxd.placeholdersk.skript", "events", "expressions"); } catch (IOException e) { e.printStackTrace(); } @@ -31,24 +32,22 @@ public void onEnable() { public static PlaceholderSK getInstance() { - if (instance == null) throw new IllegalStateException(); return instance; - } + private static void setInstance(PlaceholderSK instance) { + PlaceholderSK.instance = instance; + } - public SkriptAddon getSkriptInstance() { - + public SkriptAddon getSkriptInstance() { if(addon == null) addon = Skript.registerAddon(this); return addon; - } - public ReflectionUtils getReflectionUtils() { return this.reflections; } -} \ No newline at end of file +} diff --git a/src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIEvent.java b/src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIEvent.java new file mode 100644 index 0000000..5d779ab --- /dev/null +++ b/src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIEvent.java @@ -0,0 +1,78 @@ +package me.szumielxd.placeholdersk.placeholderapi; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class PAPIEvent extends Event { + + + private static final @NotNull HandlerList handlerList = new HandlerList(); + private @NotNull Player player; + private @Nullable Player target; + private @NotNull String identifier; + private @Nullable String result; + private @NotNull String prefix; + + + @Override + public @NotNull HandlerList getHandlers() { + return handlerList; + } + + + public static @NotNull HandlerList getHandlerList() { + return handlerList; + } + + public PAPIEvent(@NotNull Player player, @Nullable Player target, @NotNull String prefix, @NotNull String identifier) { + this.identifier = identifier; + this.player = player; + this.target = target; + this.prefix = prefix; + } + + public PAPIEvent(@NotNull Player player, @Nullable Player target, @NotNull String prefix, @NotNull String identifier, boolean async) { + super(async); + this.identifier = identifier; + this.player = player; + this.target = target; + this.prefix = prefix; + } + + + public void setResult(String result) { + this.result = result; + } + + + public String getResult() { + return this.result; + } + + + public String getIdentifier() { + return this.identifier; + } + + + public Player getPlayer() { + return this.player; + } + + + public Player getTarget() { + return this.target; + } + + + public String getPrefix() { + return this.prefix; + } + + + + +} diff --git a/src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIListener.java b/src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIListener.java new file mode 100644 index 0000000..23ba603 --- /dev/null +++ b/src/main/java/me/szumielxd/placeholdersk/placeholderapi/PAPIListener.java @@ -0,0 +1,96 @@ +package me.szumielxd.placeholdersk.placeholderapi; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventException; +import org.bukkit.event.Listener; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import me.clip.placeholderapi.expansion.Relational; +import me.szumielxd.placeholdersk.PlaceholderSK; +import me.szumielxd.placeholdersk.skript.events.SKPlaceholderRequestEvent; + +public class PAPIListener extends PlaceholderExpansion implements Relational { + + + private final String identifier; + private final List placeholders; + private final SKPlaceholderRequestEvent skEvent; + private final Listener listener = new Listener() {}; + + + public PAPIListener(@NotNull SKPlaceholderRequestEvent skEvent, @NotNull String prefix, @Nullable String[] placeholders) { + this.skEvent = skEvent; + this.identifier = prefix; + this.placeholders = placeholders == null ? Collections.emptyList() : Stream.of(placeholders).map(s -> "%" + prefix + "_" + s + "%").collect(Collectors.toList()); + } + + + @Override + public boolean persist() { + return true; + } + + + @Override + public List getPlaceholders() { + return this.placeholders; + } + + + @Override + public String getAuthor() { + return String.join(", ", PlaceholderSK.getInstance().getDescription().getAuthors()); + } + + @Override + public String getIdentifier() { + return identifier; + } + + @Override + public String getVersion() { + return PlaceholderSK.getInstance().getDescription().getVersion(); + } + + @Deprecated + @Override + public String getPlugin() { + return "PlaceholderSK"; + } + + @Override + public String getRequiredPlugin() { + return "Skript"; + } + + @Override + public String onPlaceholderRequest(Player player, String param) { + PAPIEvent event = new PAPIEvent(player, null, this.identifier, param); + try { + this.skEvent.getExecutor().execute(this.listener, event); + } catch (EventException e) { + e.printStackTrace(); + } + return event.getResult(); + } + + + @Override + public String onPlaceholderRequest(Player target, Player player, String param) { + PAPIEvent event = new PAPIEvent(player, target, this.identifier, param); + try { + this.skEvent.getExecutor().execute(this.listener, event); + } catch (EventException e) { + e.printStackTrace(); + } + return event.getResult(); + } + +} diff --git a/src/main/java/me/szumielxd/PlaceholderSK/skript/events/SKPlaceholderRequestEvent.java b/src/main/java/me/szumielxd/placeholdersk/skript/events/SKPlaceholderRequestEvent.java similarity index 73% rename from src/main/java/me/szumielxd/PlaceholderSK/skript/events/SKPlaceholderRequestEvent.java rename to src/main/java/me/szumielxd/placeholdersk/skript/events/SKPlaceholderRequestEvent.java index a8759c0..cc46eeb 100644 --- a/src/main/java/me/szumielxd/PlaceholderSK/skript/events/SKPlaceholderRequestEvent.java +++ b/src/main/java/me/szumielxd/placeholdersk/skript/events/SKPlaceholderRequestEvent.java @@ -1,4 +1,4 @@ -package me.szumielxd.PlaceholderSK.skript.events; +package me.szumielxd.placeholdersk.skript.events; import ch.njol.skript.Skript; import ch.njol.skript.SkriptEventHandler; @@ -13,12 +13,14 @@ import ch.njol.skript.util.Getter; import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.expansion.PlaceholderExpansion; -import me.szumielxd.PlaceholderSK.placeholderAPI.PAPIEvent; -import me.szumielxd.PlaceholderSK.placeholderAPI.PAPIListener; +import me.szumielxd.placeholdersk.placeholderapi.PAPIEvent; +import me.szumielxd.placeholdersk.placeholderapi.PAPIListener; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.HashSet; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -27,6 +29,8 @@ import org.bukkit.event.EventException; import org.bukkit.event.Listener; import org.bukkit.plugin.EventExecutor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; @Name("On Placeholder Request") @Description("Called whenever a placeholder is requested") @@ -34,7 +38,7 @@ public class SKPlaceholderRequestEvent extends SelfRegisteringSkriptEvent { static { - Skript.registerEvent("Placeholder Request", SKPlaceholderRequestEvent.class, PAPIEvent.class, "(placeholder[api]|papi) request with [the] prefix %string%"); + Skript.registerEvent("Placeholder Request", SKPlaceholderRequestEvent.class, PAPIEvent.class, "(placeholder[api]|papi) request with [the] prefix %string% [and placeholders %strings%]"); EventValues.registerEventValue(PAPIEvent.class, Player.class, new Getter() { @Override public Player get(PAPIEvent e) { @@ -52,29 +56,30 @@ public String get(PAPIEvent e) { @SuppressWarnings("unchecked") @Override public boolean init(final Literal[] args, final int matchedPattern, final SkriptParser.ParseResult parser) { - prefix = ((Literal) args[0]).getSingle(); - if ("".equals(prefix)) { - Skript.error(prefix + " is not a valid placeholder", ErrorQuality.SEMANTIC_ERROR); + this.prefix = ((Literal) args[0]).getSingle(); + if ("".equals(this.prefix)) { + Skript.error(this.prefix + " is not a valid placeholder", ErrorQuality.SEMANTIC_ERROR); return false; } - if(PlaceholderAPI.isRegistered(prefix)) { - Skript.error("Placeholder with prefix '" + prefix + "' is already registered", ErrorQuality.SEMANTIC_ERROR); + if(PlaceholderAPI.isRegistered(this.prefix)) { + Skript.error("Placeholder with prefix '" + this.prefix + "' is already registered", ErrorQuality.SEMANTIC_ERROR); return false; } + this.placeholders = args[1] == null? null : ((Literal) args[1]).getAll(); return true; } - private String prefix; - final static HashMap triggers = new HashMap<>(); - final static HashMap listeners = new HashMap<>(); + private @Nullable String prefix; + private @Nullable String[] placeholders; + static final @NotNull HashMap triggers = new HashMap<>(); + static final @NotNull HashMap listeners = new HashMap<>(); private static boolean registeredExecutor = false; - private final EventExecutor executor = new EventExecutor() { + private final @NotNull EventExecutor executor = new EventExecutor() { @Override public void execute(final Listener l, final Event e) throws EventException { - if (e == null) - return; + if (e == null) return; PAPIEvent ev = (PAPIEvent)e; if(ev.getPrefix() != null) { Trigger tr = triggers.get(ev.getPrefix()); @@ -84,7 +89,6 @@ public void execute(final Listener l, final Event e) throws EventException { SkriptEventHandler.logTriggerEnd(tr); } } - return; } }; @@ -94,13 +98,13 @@ public EventExecutor getExecutor() { @Override public String toString(Event e, boolean debug) { - return "placeholder request" + (prefix != null ? (" with prefix \"" + prefix + "\"") : ""); + return "placeholder request with prefix \"" + prefix + "\"" + (this.placeholders != null ? " and placeholders " + Stream.of(this.placeholders).map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")) : ""); } @Override public void register(Trigger tr) { triggers.put(prefix, tr); - PAPIListener l = new PAPIListener(this, prefix); + PAPIListener l = new PAPIListener(this, this.prefix, this.placeholders); listeners.put(prefix, l); l.register(); if (!registeredExecutor) { @@ -140,4 +144,6 @@ private void legacyUnregister(PlaceholderExpansion expansion) { } } } + + } \ No newline at end of file diff --git a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprIdentifier.java b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprIdentifier.java similarity index 80% rename from src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprIdentifier.java rename to src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprIdentifier.java index 3153609..d08f4d5 100644 --- a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprIdentifier.java +++ b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprIdentifier.java @@ -1,4 +1,4 @@ -package me.szumielxd.PlaceholderSK.skript.expressions; +package me.szumielxd.placeholdersk.skript.expressions; import ch.njol.skript.ScriptLoader; import ch.njol.skript.Skript; @@ -8,9 +8,10 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.parser.ParserInstance; import ch.njol.skript.log.ErrorQuality; import ch.njol.util.Kleenean; -import me.szumielxd.PlaceholderSK.placeholderAPI.PAPIEvent; +import me.szumielxd.placeholdersk.placeholderapi.PAPIEvent; import org.bukkit.event.Event; import ch.njol.skript.lang.util.SimpleExpression; @@ -26,12 +27,21 @@ public class ExprIdentifier extends SimpleExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(PAPIEvent.class)) { + if (!this.isValidEvent()) { Skript.error("The PlaceholderAPI identifier can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); return false; } return true; } + + @SuppressWarnings("deprecation") + private boolean isValidEvent() { + try { + return ParserInstance.get().isCurrentEvent(PAPIEvent.class); + } catch (NoSuchMethodError e) { + return ScriptLoader.isCurrentEvent(PAPIEvent.class); + } + } @Override protected String[] get(final Event e) { diff --git a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprPlaceholder.java b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprPlaceholder.java similarity index 82% rename from src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprPlaceholder.java rename to src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprPlaceholder.java index dbfe43b..2fabe73 100644 --- a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprPlaceholder.java +++ b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprPlaceholder.java @@ -1,4 +1,4 @@ -package me.szumielxd.PlaceholderSK.skript.expressions; +package me.szumielxd.placeholdersk.skript.expressions; import ch.njol.skript.Skript; import ch.njol.skript.doc.Description; @@ -12,6 +12,8 @@ import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.entity.Player; import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -28,7 +30,7 @@ public class ExprPlaceholder extends SimpleExpression { Skript.registerExpression(ExprPlaceholder.class, String.class, ExpressionType.SIMPLE,"[the] ([value of] placeholder[s]|placeholder [value] [of]) %strings% [from %players%]"); } - private String formatPlaceholder(String placeholder) { + private @Nullable String formatPlaceholder(@Nullable String placeholder) { if (placeholder == null) { return null; } @@ -41,7 +43,7 @@ private String formatPlaceholder(String placeholder) { return "%" + placeholder + "%"; } - private String getPlaceholder(String placeholder, Player player) { + private @Nullable String getPlaceholder(@NotNull String placeholder, @Nullable Player player) { placeholder = formatPlaceholder(placeholder); if (PlaceholderAPI.containsPlaceholders(placeholder)) { String value = PlaceholderAPI.setPlaceholders(player, placeholder); @@ -64,17 +66,17 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override protected String[] get(final Event e) { - String[] placeholders = this.placeholders.getArray(e); - Player[] players = this.players.getArray(e); + String[] placeholdersArray = this.placeholders.getArray(e); + Player[] playersArray = this.players.getArray(e); List values = new ArrayList<>(); - if (players.length != 0) { - for (String ph : placeholders) { - for (Player p : players) { + if (playersArray.length != 0) { + for (String ph : placeholdersArray) { + for (Player p : playersArray) { values.add(getPlaceholder(ph, p)); } } } else { - for (String ph : placeholders) { + for (String ph : placeholdersArray) { values.add(getPlaceholder(ph, null)); } } diff --git a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprPrefix.java b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprPrefix.java similarity index 78% rename from src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprPrefix.java rename to src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprPrefix.java index a0795da..bf839bc 100644 --- a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprPrefix.java +++ b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprPrefix.java @@ -1,4 +1,4 @@ -package me.szumielxd.PlaceholderSK.skript.expressions; +package me.szumielxd.placeholdersk.skript.expressions; import ch.njol.skript.ScriptLoader; import ch.njol.skript.Skript; @@ -8,10 +8,11 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.parser.ParserInstance; import ch.njol.skript.log.ErrorQuality; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.util.Kleenean; -import me.szumielxd.PlaceholderSK.placeholderAPI.PAPIEvent; +import me.szumielxd.placeholdersk.placeholderapi.PAPIEvent; import org.bukkit.event.Event; @@ -26,12 +27,21 @@ public class ExprPrefix extends SimpleExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(PAPIEvent.class)) { + if (!this.isValidEvent()) { Skript.error("The PlaceholderAPI prefix can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); return false; } return true; } + + @SuppressWarnings("deprecation") + private boolean isValidEvent() { + try { + return ParserInstance.get().isCurrentEvent(PAPIEvent.class); + } catch (NoSuchMethodError e) { + return ScriptLoader.isCurrentEvent(PAPIEvent.class); + } + } @Override protected String[] get(final Event e) { diff --git a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprResult.java b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprResult.java similarity index 84% rename from src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprResult.java rename to src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprResult.java index ffdf6da..8061a06 100644 --- a/src/main/java/me/szumielxd/PlaceholderSK/skript/expressions/ExprResult.java +++ b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprResult.java @@ -1,4 +1,4 @@ -package me.szumielxd.PlaceholderSK.skript.expressions; +package me.szumielxd.placeholdersk.skript.expressions; import ch.njol.skript.ScriptLoader; import ch.njol.skript.Skript; @@ -8,12 +8,13 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.parser.ParserInstance; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.skript.log.ErrorQuality; import ch.njol.util.Kleenean; import ch.njol.skript.classes.Changer.ChangeMode; import ch.njol.util.coll.CollectionUtils; -import me.szumielxd.PlaceholderSK.placeholderAPI.PAPIEvent; +import me.szumielxd.placeholdersk.placeholderapi.PAPIEvent; import org.bukkit.event.Event; @@ -28,12 +29,21 @@ public class ExprResult extends SimpleExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(PAPIEvent.class)) { + if (!this.isValidEvent()) { Skript.error("The PlaceholderAPI result can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); return false; } return true; } + + @SuppressWarnings("deprecation") + private boolean isValidEvent() { + try { + return ParserInstance.get().isCurrentEvent(PAPIEvent.class); + } catch (NoSuchMethodError e) { + return ScriptLoader.isCurrentEvent(PAPIEvent.class); + } + } @Override protected String[] get(final Event e) { diff --git a/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprTarget.java b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprTarget.java new file mode 100644 index 0000000..16a57a9 --- /dev/null +++ b/src/main/java/me/szumielxd/placeholdersk/skript/expressions/ExprTarget.java @@ -0,0 +1,66 @@ +package me.szumielxd.placeholdersk.skript.expressions; + +import ch.njol.skript.ScriptLoader; +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.parser.ParserInstance; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.util.Kleenean; +import me.szumielxd.placeholdersk.placeholderapi.PAPIEvent; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event; + +@Name("Placeholder Target") +@Description("Grabs the target of a relational placeholder request event") +@Examples("on placeholder request with prefix \"example\":\n\tif the target is \"hamster\": # example_name\n\t\tset the result to player's name\n\telse if the identifier is \"uuid\": # example_uuid\n\t\tset the result to the player's uuid\n\telse if the identifier is \"money\": # example_money\n\t\tset the result to \"$%{money::%player's uuid%}%\"") +public class ExprTarget extends SimpleExpression { + + static { + Skript.registerExpression(ExprTarget.class, Player.class, ExpressionType.SIMPLE,"[the] [(placeholder[api]|papi)] target"); + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + if (!this.isValidEvent()) { + Skript.error("The PlaceholderAPI target can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + + @SuppressWarnings("deprecation") + private boolean isValidEvent() { + try { + return ParserInstance.get().isCurrentEvent(PAPIEvent.class); + } catch (NoSuchMethodError e) { + return ScriptLoader.isCurrentEvent(PAPIEvent.class); + } + } + + @Override + protected Player[] get(final Event e) { + return new Player[] {((PAPIEvent) e).getTarget()}; + } + + @Override + public String toString(Event e, boolean debug) { + return "the relational placeholder target"; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Player.class; + } +} \ No newline at end of file diff --git a/src/main/java/me/szumielxd/PlaceholderSK/utils/ReflectionUtils.java b/src/main/java/me/szumielxd/placeholdersk/utils/ReflectionUtils.java similarity index 98% rename from src/main/java/me/szumielxd/PlaceholderSK/utils/ReflectionUtils.java rename to src/main/java/me/szumielxd/placeholdersk/utils/ReflectionUtils.java index 592deee..78c461b 100644 --- a/src/main/java/me/szumielxd/PlaceholderSK/utils/ReflectionUtils.java +++ b/src/main/java/me/szumielxd/placeholdersk/utils/ReflectionUtils.java @@ -1,4 +1,4 @@ -package me.szumielxd.PlaceholderSK.utils; +package me.szumielxd.placeholdersk.utils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -102,4 +102,4 @@ public Object invokeStaticMethod(Class clazz, String name) { -} \ No newline at end of file +}