diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 33f4c7c..0aef3d6 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -15,3 +15,9 @@ jobs: java-version: 1.8 - name: Build with Gradle run: ./gradlew build + - name: Upload artifact + uses: actions/upload-artifact@v2 + if: success() + with: + name: skript-placeholders-dev + path: build/libs/skript-placeholders.jar diff --git a/build.gradle b/build.gradle index 690019a..6b2858f 100644 --- a/build.gradle +++ b/build.gradle @@ -14,9 +14,10 @@ repositories { } dependencies { + implementation 'org.eclipse.jdt:org.eclipse.jdt.annotation:1.1.0' implementation 'org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT' implementation 'com.github.SkriptLang:Skript:2.3' - implementation 'me.clip:placeholderapi:2.10.6' + implementation 'me.clip:placeholderapi:2.10.8' implementation('be.maximvdw:MVdWPlaceholderAPI:3.0.1-SNAPSHOT') { exclude group: 'org.spigotmc' } diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/mvdwapi/MvdwAPIEvent.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/mvdwapi/MvdwAPIEvent.java deleted file mode 100644 index fa47bb7..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/mvdwapi/MvdwAPIEvent.java +++ /dev/null @@ -1,50 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.mvdwapi; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class MvdwAPIEvent extends Event { - - private static final HandlerList handlerList = new HandlerList(); - - private Player player; - private String placeholder; - private String result; - - public MvdwAPIEvent(Player player, String placeholder) { - - // Declare the event as sync or async. - super(!Bukkit.getServer().isPrimaryThread()); - - this.placeholder = placeholder; - this.player = player; - } - - public static HandlerList getHandlerList() { - return handlerList; - } - - @Override - public HandlerList getHandlers() { - return handlerList; - } - - public Player getPlayer() { - return player; - } - - public String getPlaceholder() { - return placeholder; - } - - public String getResult() { - return result; - } - - public void setResult(String result) { - this.result = result; - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/mvdwapi/MvdwAPIListener.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/mvdwapi/MvdwAPIListener.java deleted file mode 100644 index 1db836f..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/mvdwapi/MvdwAPIListener.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.mvdwapi; - -import org.bukkit.Bukkit; -import org.bukkit.plugin.Plugin; - -import be.maximvdw.placeholderapi.PlaceholderAPI; -import be.maximvdw.placeholderapi.PlaceholderReplaceEvent; -import be.maximvdw.placeholderapi.PlaceholderReplacer; -import io.github.apickledwalrus.placeholderaddon.mvdwapi.MvdwAPIEvent; - -public class MvdwAPIListener { - - public static void registerPlaceholder(Plugin plugin, String placeholder) { - PlaceholderAPI.registerPlaceholder(plugin, placeholder, - new PlaceholderReplacer() { - - @Override - public String onPlaceholderReplace(PlaceholderReplaceEvent event) { - MvdwAPIEvent e = new MvdwAPIEvent(event.getPlayer(), event.getPlaceholder()); - Bukkit.getPluginManager().callEvent(e); - return e.getResult(); - } - - }); - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/placeholderapi/PlaceholderAPIEvent.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/placeholderapi/PlaceholderAPIEvent.java deleted file mode 100644 index 6929e34..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/placeholderapi/PlaceholderAPIEvent.java +++ /dev/null @@ -1,56 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.placeholderapi; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class PlaceholderAPIEvent extends Event { - - private static final HandlerList handlerList = new HandlerList(); - - private String identifier; - private Player player; - private String prefix; - private String result; - - public PlaceholderAPIEvent(String identifier, Player player, String prefix) { - - // Declare the event as sync or async. - super(!Bukkit.getServer().isPrimaryThread()); - - this.prefix = prefix; - this.identifier = identifier; - this.player = player; - } - - public static HandlerList getHandlerList() { - return handlerList; - } - - @Override - public HandlerList getHandlers() { - return handlerList; - } - - public Player getPlayer() { - return player; - } - - public String getPrefix() { - return prefix; - } - - public String getIdentifier() { - return identifier; - } - - public String getResult() { - return result; - } - - public void setResult(String result) { - this.result = result; - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/events/EvtMvdwPlaceholderRequest.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/events/EvtMvdwPlaceholderRequest.java deleted file mode 100644 index 06d11a0..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/events/EvtMvdwPlaceholderRequest.java +++ /dev/null @@ -1,71 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.skript.events; - -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.doc.Since; -import ch.njol.skript.lang.Literal; -import ch.njol.skript.lang.SkriptEvent; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.skript.registrations.EventValues; -import ch.njol.skript.util.Getter; -import io.github.apickledwalrus.placeholderaddon.Main; -import io.github.apickledwalrus.placeholderaddon.mvdwapi.MvdwAPIListener; -import io.github.apickledwalrus.placeholderaddon.mvdwapi.MvdwAPIEvent; - -import org.apache.commons.lang.StringUtils; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; - -@Name("MVdWPlaceholderAPI Placeholder Request Event") -@Description({"Called whenever a placeholder is requested by MVdWPlaceholderAPI.", - "Use this event to create MVdWPlaceholderAPI placeholders." -}) -@Examples({"# Creates a new MVdWPlaceholderAPI placeholder: {doublehealth}", - "on mvdw placeholder request for placeholder \"doublehealth\":", - "\tset the result to the player's health * 2"}) -@Since("1.3") -public class EvtMvdwPlaceholderRequest extends SkriptEvent { - - static { - if (Main.hasMVdW()) { - Skript.registerEvent("Placeholder Request", EvtMvdwPlaceholderRequest.class, MvdwAPIEvent.class, "mvdw[ ](placeholderapi [placeholder]|placeholder) request (for|with) [the] placeholder %string%"); - EventValues.registerEventValue(MvdwAPIEvent.class, Player.class, new Getter() { - @Override - public Player get(MvdwAPIEvent e) { - return e.getPlayer(); - } - }, 0); - } - } - - private String placeholder; - - @SuppressWarnings("unchecked") - @Override - public boolean init(final Literal[] args, final int matchedPattern, final ParseResult parser) { - Literal l = (Literal) args[0]; - if (l == null) - return false; - placeholder = l.getSingle(); - if (StringUtils.isBlank(placeholder)) { - Skript.error(placeholder + " is not a valid placeholder", ErrorQuality.SEMANTIC_ERROR); - return false; - } - MvdwAPIListener.registerPlaceholder(Main.getInstance(), placeholder); - return true; - } - - @Override - public boolean check(final Event e) { - return ((MvdwAPIEvent) e).getPlaceholder().equalsIgnoreCase(placeholder); - } - - @Override - public String toString(Event e, boolean debug) { - return "mvdwplaceholderapi placeholder request" + (placeholder != null ? ("for placeholder '" + placeholder + "'") : ""); - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/events/EvtPapiPlaceholderRequest.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/events/EvtPapiPlaceholderRequest.java deleted file mode 100644 index 5a635af..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/events/EvtPapiPlaceholderRequest.java +++ /dev/null @@ -1,71 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.skript.events; - -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.doc.Since; -import ch.njol.skript.lang.Literal; -import ch.njol.skript.lang.SkriptEvent; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.skript.registrations.EventValues; -import ch.njol.skript.util.Getter; -import io.github.apickledwalrus.placeholderaddon.Main; -import io.github.apickledwalrus.placeholderaddon.placeholderapi.PlaceholderAPIEvent; -import io.github.apickledwalrus.placeholderaddon.placeholderapi.PlaceholderAPIListener; - -import org.apache.commons.lang.StringUtils; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; - -@Name("PlaceholderAPI Placeholder Request Event") -@Description({"Called whenever a placeholder is requested by PlaceholderAPI.", - "Use this event to create PlaceholderAPI placeholders." -}) -@Examples({"on placeholderapi request with prefix \"double\":", - "\tif the identifier is \"health\": # The placeholder is double_health", - "\t\tset the result to player's health * 2 "}) -@Since("1.0") -public class EvtPapiPlaceholderRequest extends SkriptEvent { - - static { - if (Main.hasPapi()) { - Skript.registerEvent("Placeholder Request", EvtPapiPlaceholderRequest.class, PlaceholderAPIEvent.class, "(placeholderapi|papi) [placeholder] request (for|with) [the] prefix %string%"); - EventValues.registerEventValue(PlaceholderAPIEvent.class, Player.class, new Getter() { - @Override - public Player get(PlaceholderAPIEvent e) { - return e.getPlayer(); - } - }, 0); - } - } - - private String prefix; - - @SuppressWarnings("unchecked") - @Override - public boolean init(final Literal[] args, final int matchedPattern, final ParseResult parser) { - Literal l = (Literal) args[0]; - if (l == null) - return false; - prefix = l.getSingle(); - if (StringUtils.isBlank(prefix)) { - Skript.error(prefix + " is not a valid placeholder", ErrorQuality.SEMANTIC_ERROR); - return false; - } - new PlaceholderAPIListener(Main.getInstance(), prefix).register(); - return true; - } - - @Override - public boolean check(final Event e) { - return ((PlaceholderAPIEvent) e).getPrefix().equalsIgnoreCase(prefix); - } - - @Override - public String toString(Event e, boolean debug) { - return "placeholderapi placeholder request" + (prefix != null ? ("for prefix \"" + prefix + "\"") : ""); - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprMvdwPlaceholder.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprMvdwPlaceholder.java deleted file mode 100644 index 77df56f..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprMvdwPlaceholder.java +++ /dev/null @@ -1,62 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.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.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.util.Kleenean; -import io.github.apickledwalrus.placeholderaddon.Main; -import io.github.apickledwalrus.placeholderaddon.mvdwapi.MvdwAPIEvent; -import org.bukkit.event.Event; - -@Name("MVdWPlaceholderAPI Placeholder") -@Description("Returns the placeholder in a MVdWPlaceholderAPI request event.") -@Examples({"on mvdw placeholder request for placeholder \"doublehealth\":", - "\tsend \"MVdWPlaceholderAPI requested the placeholder %placeholder%!\" to console"}) -@Since("1.3") -public class ExprMvdwPlaceholder extends SimpleExpression { - - static { - if (Main.hasMVdW()) { - Skript.registerExpression(ExprMvdwPlaceholder.class, String.class, ExpressionType.SIMPLE, - "[the] [mvdw[ ][placeholder[api]]] placeholder"); - } - } - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(MvdwAPIEvent.class)) { - Skript.error("The MVdWPlaceholderAPI placeholder can only be used in a MVdWPlaceholderAPI request event", ErrorQuality.SEMANTIC_ERROR); - return false; - } - return true; - } - - @Override - protected String[] get(final Event e) { - return new String[]{((MvdwAPIEvent) e).getPlaceholder()}; - } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return String.class; - } - - @Override - public String toString(Event e, boolean debug) { - return "the mvdwplaceholderapi prefix"; - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprMvdwResult.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprMvdwResult.java deleted file mode 100644 index 8d2e209..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprMvdwResult.java +++ /dev/null @@ -1,92 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.skript.expressions; - -import ch.njol.skript.ScriptLoader; -import ch.njol.skript.Skript; -import ch.njol.skript.classes.Changer.ChangeMode; -import ch.njol.skript.doc.Description; -import ch.njol.skript.doc.Examples; -import ch.njol.skript.doc.Name; -import ch.njol.skript.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.util.Kleenean; -import ch.njol.util.coll.CollectionUtils; -import io.github.apickledwalrus.placeholderaddon.Main; -import io.github.apickledwalrus.placeholderaddon.mvdwapi.MvdwAPIEvent; -import org.bukkit.event.Event; - -@Name("MVdWPlaceholderAPI Result") -@Description("The result (placeholder value) in a MVdWPlaceholderAPI request event. It can be set or reset/deleted.") -@Examples({"on mvdw placeholder request for placeholder \"isAdmin\":", - "\tif player has permission \"is.admin\":", - "\t\tset the result to \"true\"", - "\telse:", - "\t\tset the result to \"false\""}) -@Since("1.3") -public class ExprMvdwResult extends SimpleExpression { - - static { - if (Main.hasMVdW()) { - Skript.registerExpression(ExprMvdwResult.class, String.class, ExpressionType.SIMPLE, - "[the] [mvdw[ ][placeholder[api]]] result"); - } - } - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(MvdwAPIEvent.class)) { - Skript.error("The MVdWPlaceholderAPI result can only be used in a MVdWPlaceholderAPI placeholder request event", ErrorQuality.SEMANTIC_ERROR); - return false; - } - return true; - } - - @Override - protected String[] get(final Event e) { - return new String[]{((MvdwAPIEvent) e).getResult()}; - } - - @Override - public Class[] acceptChange(final ChangeMode mode) { - if (mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) { - return CollectionUtils.array(String.class); - } - return null; - } - - @Override - public void change(Event e, Object[] delta, ChangeMode mode) { - switch (mode) { - case SET: - ((MvdwAPIEvent) e).setResult(String.valueOf(delta[0])); - break; - case RESET: - case DELETE: - ((MvdwAPIEvent) e).setResult(null); - break; - case ADD: - case REMOVE: - case REMOVE_ALL: - assert false; - } - } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return String.class; - } - - @Override - public String toString(Event e, boolean debug) { - return "the mvdwplaceholderapi result"; - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiIdentifier.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiIdentifier.java deleted file mode 100644 index 9b0831b..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiIdentifier.java +++ /dev/null @@ -1,63 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.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.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.util.Kleenean; -import io.github.apickledwalrus.placeholderaddon.Main; -import io.github.apickledwalrus.placeholderaddon.placeholderapi.PlaceholderAPIEvent; -import org.bukkit.event.Event; - -@Name("PlaceholderAPI Identifier") -@Description("Returns the identifier of the placeholder in a PlaceholderAPI request event.") -@Examples({"on placeholderapi request with prefix \"hello\":", - "\tif the identifier is \"world\": # Placeholder is hello_world", - "# The identifier is the part after the first underscore."}) -@Since("1.0") -public class ExprPapiIdentifier extends SimpleExpression { - - static { - if (Main.hasPapi()) { - Skript.registerExpression(ExprPapiIdentifier.class, String.class, ExpressionType.SIMPLE, - "[the] [(placeholder[api]|papi)] identifier"); - } - } - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(PlaceholderAPIEvent.class)) { - Skript.error("The PlaceholderAPI identifier can only be used in a PlaceholderAPI request event", ErrorQuality.SEMANTIC_ERROR); - return false; - } - return true; - } - - @Override - protected String[] get(final Event e) { - return new String[]{((PlaceholderAPIEvent) e).getIdentifier()}; - } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return String.class; - } - - @Override - public String toString(Event e, boolean debug) { - return "the placeholderapi identifier"; - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiPrefix.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiPrefix.java deleted file mode 100644 index 29e7c62..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiPrefix.java +++ /dev/null @@ -1,63 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.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.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.util.Kleenean; -import io.github.apickledwalrus.placeholderaddon.Main; -import io.github.apickledwalrus.placeholderaddon.placeholderapi.PlaceholderAPIEvent; -import org.bukkit.event.Event; - -@Name("PlaceholderAPI Prefix") -@Description("Returns the prefix of the placeholder in a PlaceholderAPI request event.") -@Examples({"on placeholderapi request with prefix \"hello\":", - "\tif the identifier is \"world\": # Placeholder is hello_world", - "# The prefix is the part before the first underscore."}) -@Since("1.0") -public class ExprPapiPrefix extends SimpleExpression { - - static { - if (Main.hasPapi()) { - Skript.registerExpression(ExprPapiPrefix.class, String.class, ExpressionType.SIMPLE, - "[the] [(placeholder[api]|papi)] (prefix|placeholder)"); - } - } - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(PlaceholderAPIEvent.class)) { - Skript.error("The PlaceholderAPI prefix can only be used in a PlaceholderAPI request event", ErrorQuality.SEMANTIC_ERROR); - return false; - } - return true; - } - - @Override - protected String[] get(final Event e) { - return new String[]{((PlaceholderAPIEvent) e).getPrefix()}; - } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return String.class; - } - - @Override - public String toString(Event e, boolean debug) { - return "the placeholderapi prefix"; - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiResult.java b/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiResult.java deleted file mode 100644 index c517697..0000000 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprPapiResult.java +++ /dev/null @@ -1,93 +0,0 @@ -package io.github.apickledwalrus.placeholderaddon.skript.expressions; - -import ch.njol.skript.ScriptLoader; -import ch.njol.skript.Skript; -import ch.njol.skript.classes.Changer.ChangeMode; -import ch.njol.skript.doc.Description; -import ch.njol.skript.doc.Examples; -import ch.njol.skript.doc.Name; -import ch.njol.skript.doc.Since; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.log.ErrorQuality; -import ch.njol.util.Kleenean; -import ch.njol.util.coll.CollectionUtils; -import io.github.apickledwalrus.placeholderaddon.Main; -import io.github.apickledwalrus.placeholderaddon.placeholderapi.PlaceholderAPIEvent; -import org.bukkit.event.Event; - -@Name("PlaceholderAPI Result") -@Description("The result (placeholder value) in a PlaceholderAPI request event. It can be set or reset/deleted.") -@Examples({"on mvdw placeholder request for prefix \"user\":", - "\tif the identifier is \"is_admin\": # The placeholder is 'user_is_admin'", - "\t\tif player has permission \"is.admin\":", - "\t\t\tset the result to \"true\"", - "\t\telse:", - "\t\t\tset the result to \"false\""}) -@Since("1.0") -public class ExprPapiResult extends SimpleExpression { - - static { - if (Main.hasPapi()) { - Skript.registerExpression(ExprPapiResult.class, String.class, ExpressionType.SIMPLE, - "[the] [(placeholder[api]|papi)] result"); - } - } - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { - if (!ScriptLoader.isCurrentEvent(PlaceholderAPIEvent.class)) { - Skript.error("The PlaceholderAPI result can only be used in a PlaceholderAPI request event", ErrorQuality.SEMANTIC_ERROR); - return false; - } - return true; - } - - @Override - protected String[] get(final Event e) { - return new String[]{((PlaceholderAPIEvent) e).getResult()}; - } - - @Override - public Class[] acceptChange(final ChangeMode mode) { - if (mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) { - return CollectionUtils.array(String.class); - } - return null; - } - - @Override - public void change(Event e, Object[] delta, ChangeMode mode) { - switch (mode) { - case SET: - ((PlaceholderAPIEvent) e).setResult(String.valueOf(delta[0])); - break; - case RESET: - case DELETE: - ((PlaceholderAPIEvent) e).setResult(null); - break; - case ADD: - case REMOVE: - case REMOVE_ALL: - assert false; - } - } - - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return String.class; - } - - @Override - public String toString(Event e, boolean debug) { - return "the placeholderapi result"; - } - -} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/Main.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/SkriptPlaceholders.java similarity index 72% rename from src/main/java/io/github/apickledwalrus/placeholderaddon/Main.java rename to src/main/java/io/github/apickledwalrus/skriptplaceholders/SkriptPlaceholders.java index 95b7065..9590093 100644 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/Main.java +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/SkriptPlaceholders.java @@ -1,4 +1,4 @@ -package io.github.apickledwalrus.placeholderaddon; +package io.github.apickledwalrus.skriptplaceholders; import ch.njol.skript.Skript; import ch.njol.skript.SkriptAddon; @@ -7,14 +7,22 @@ import java.io.IOException; -public class Main extends JavaPlugin { +public class SkriptPlaceholders extends JavaPlugin { - private static Main instance; + private static SkriptPlaceholders instance; private static SkriptAddon addonInstance; private static final boolean hasMVdW = Skript.classExists("be.maximvdw.placeholderapi.PlaceholderAPI"); private static final boolean hasPapi = Skript.classExists("me.clip.placeholderapi.expansion.PlaceholderExpansion"); + public static boolean hasMVdW() { + return hasMVdW; + } + + public static boolean hasPapi() { + return hasPapi; + } + @Override public void onEnable() { instance = this; @@ -25,16 +33,13 @@ public void onEnable() { } try { addonInstance = Skript.registerAddon(this); - addonInstance.loadClasses("io.github.apickledwalrus.placeholderaddon.skript", "events", "expressions"); + addonInstance.loadClasses("io.github.apickledwalrus.skriptplaceholders.skript.elements"); } catch (IOException e) { e.printStackTrace(); } } - public static Main getInstance() { - if (instance == null) { - throw new IllegalStateException("The plugin's instance should not be null."); - } + public static SkriptPlaceholders getInstance() { return instance; } @@ -42,12 +47,4 @@ public static SkriptAddon getAddonInstance() { return addonInstance; } - public static boolean hasMVdW() { - return hasMVdW; - } - - public static boolean hasPapi() { - return hasPapi; - } - } diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderEvent.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderEvent.java new file mode 100644 index 0000000..8ccfdfb --- /dev/null +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/PlaceholderEvent.java @@ -0,0 +1,72 @@ +package io.github.apickledwalrus.skriptplaceholders.placeholder; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.eclipse.jdt.annotation.Nullable; + +public class PlaceholderEvent extends Event { + + private static final HandlerList handlerList = new HandlerList(); + + private final String placeholder; + private final String prefix; + private final String identifier; + private String result; + + private final OfflinePlayer player; + + public PlaceholderEvent(String placeholder, OfflinePlayer player) { + // Declare the event as sync or async. + super(!Bukkit.getServer().isPrimaryThread()); + + this.placeholder = placeholder; + int underscorePos = placeholder.indexOf("_"); + if (underscorePos != -1) { // It exists + prefix = placeholder.substring(0, underscorePos); + identifier = placeholder.substring(underscorePos + 1); + } else { + prefix = null; + identifier = null; + } + + this.player = player; + } + + public static HandlerList getHandlerList() { + return handlerList; + } + + @Override + public HandlerList getHandlers() { + return handlerList; + } + + public String getPlaceholder() { + return this.placeholder; + } + + @Nullable + public String getPrefix() { + return prefix; + } + + @Nullable + public String getIdentifier() { + return identifier; + } + + public OfflinePlayer getPlayer() { + return this.player; + } + + public void setResult(String result) { + this.result = result; + } + + public String getResult() { + return result; + } + +} diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/mvdwplaceholderapi/MVdWPlaceholderAPIListener.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/mvdwplaceholderapi/MVdWPlaceholderAPIListener.java new file mode 100644 index 0000000..8b7241d --- /dev/null +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/mvdwplaceholderapi/MVdWPlaceholderAPIListener.java @@ -0,0 +1,35 @@ +package io.github.apickledwalrus.skriptplaceholders.placeholder.mvdwplaceholderapi; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +import be.maximvdw.placeholderapi.PlaceholderAPI; +import be.maximvdw.placeholderapi.PlaceholderReplaceEvent; +import be.maximvdw.placeholderapi.PlaceholderReplacer; + +import io.github.apickledwalrus.skriptplaceholders.placeholder.PlaceholderEvent; + +public class MVdWPlaceholderAPIListener { + + private Plugin plugin; + private String placeholder; + + public MVdWPlaceholderAPIListener(Plugin plugin, String placeholder) { + this.plugin = plugin; + this.placeholder = placeholder; + } + + public void register() { + PlaceholderAPI.registerPlaceholder(plugin, placeholder, + new PlaceholderReplacer() { + @Override + public String onPlaceholderReplace(PlaceholderReplaceEvent e) { + PlaceholderEvent event = new PlaceholderEvent(placeholder, e.getPlayer()); + Bukkit.getPluginManager().callEvent(event); + return event.getResult(); + } + } + ); + } + +} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/placeholderapi/PlaceholderAPIListener.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/placeholderapi/PlaceholderAPIListener.java similarity index 52% rename from src/main/java/io/github/apickledwalrus/placeholderaddon/placeholderapi/PlaceholderAPIListener.java rename to src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/placeholderapi/PlaceholderAPIListener.java index cd03fcf..9318136 100644 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/placeholderapi/PlaceholderAPIListener.java +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/placeholder/placeholderapi/PlaceholderAPIListener.java @@ -1,16 +1,19 @@ -package io.github.apickledwalrus.placeholderaddon.placeholderapi; +package io.github.apickledwalrus.skriptplaceholders.placeholder.placeholderapi; -import io.github.apickledwalrus.placeholderaddon.Main; import me.clip.placeholderapi.expansion.PlaceholderExpansion; + import org.bukkit.Bukkit; -import org.bukkit.entity.Player; +import org.bukkit.OfflinePlayer; +import org.bukkit.plugin.Plugin; + +import io.github.apickledwalrus.skriptplaceholders.placeholder.PlaceholderEvent; public class PlaceholderAPIListener extends PlaceholderExpansion { - private Main plugin; + private Plugin plugin; private String prefix; - public PlaceholderAPIListener(Main plugin, String prefix) { + public PlaceholderAPIListener(Plugin plugin, String prefix) { this.plugin = plugin; this.prefix = prefix; } @@ -31,8 +34,13 @@ public String getVersion() { } @Override - public String onPlaceholderRequest(Player player, String identifier) { - PlaceholderAPIEvent event = new PlaceholderAPIEvent(identifier, player, prefix); + public boolean persist() { + return true; + } + + @Override + public String onRequest(OfflinePlayer player, String identifier) { + PlaceholderEvent event = new PlaceholderEvent(this.prefix + "_" + identifier, player); Bukkit.getPluginManager().callEvent(event); return event.getResult(); } diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/events/EvtPlaceholderRequest.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/events/EvtPlaceholderRequest.java new file mode 100644 index 0000000..c601273 --- /dev/null +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/events/EvtPlaceholderRequest.java @@ -0,0 +1,144 @@ +package io.github.apickledwalrus.skriptplaceholders.skript.elements.events; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +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.doc.Since; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.SkriptEvent; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.skript.registrations.EventValues; +import ch.njol.skript.util.Getter; + +import io.github.apickledwalrus.skriptplaceholders.SkriptPlaceholders; +import io.github.apickledwalrus.skriptplaceholders.placeholder.PlaceholderEvent; +import io.github.apickledwalrus.skriptplaceholders.placeholder.mvdwplaceholderapi.MVdWPlaceholderAPIListener; +import io.github.apickledwalrus.skriptplaceholders.placeholder.placeholderapi.PlaceholderAPIListener; + +@Name("Placeholder Request Event") +@Description("Called whenever a placeholder is requested by a supported placeholder plugin.") +@Examples({"on placeholderapi placeholder request for the prefix \"custom\":", + "\tif the identifier is \"hello\": # Placeholder is \"%custom_hey%\"", + "\t\tset the result to \"Hey there %player%!\"", + "on mvdw placeholder request for the placeholder \"custom_hey\":", + "\t# Placeholder is \"{custom_hey}\"", + "\tset the result to \"Hey there %player%!\"" +}) +@Since("1.0 - PlaceholderAPI | 1.3 - MVdWPlaceholderAPI | 2.0 - New Event Features") +public class EvtPlaceholderRequest extends SkriptEvent { + + static { + Skript.registerEvent("Placeholder Request", EvtPlaceholderRequest.class, PlaceholderEvent.class, + "(placeholder[ ]api|papi) [placeholder] request (for|with) [the] prefix[es] %strings%", + "mvdw[ ](placeholder[ ]api [placeholder]|placeholder) request (for|with) [the] placeholder[es] %strings%" + ); + EventValues.registerEventValue(PlaceholderEvent.class, Player.class, new Getter() { + @Override + public Player get(PlaceholderEvent e) { + if (e.getPlayer().isOnline()) + return (Player) e.getPlayer(); + return null; + } + }, 0); + EventValues.registerEventValue(PlaceholderEvent.class, OfflinePlayer.class, new Getter() { + @Override + public OfflinePlayer get(PlaceholderEvent e) { + return e.getPlayer(); + } + }, 0); + } + + private String[] placeholders; + + private int pattern; + + @Override + public boolean init(Literal[] args, int matchedPattern, ParseResult parseResult) { + switch (matchedPattern) { // Installation Check + case 0: // PlaceholderAPI + if (!SkriptPlaceholders.hasPapi()) { + Skript.error("PlaceholderAPI is required to register PlaceholderAPI placeholders.", ErrorQuality.SEMANTIC_ERROR); + return false; + } + break; + case 1: // MVdWPlaceholderAPI + if (!SkriptPlaceholders.hasPapi()) { + Skript.error("MVdWPlaceholderAPI is required to register MVdWPlaceholderAPI placeholders.", ErrorQuality.SEMANTIC_ERROR); + return false; + } + break; + } + List placeholders = new ArrayList<>(); + for (Literal literal : args) { + String placeholder = (String) literal.getSingle(); + if (StringUtils.isBlank(placeholder)) { + Skript.error(placeholder + " is not a valid placeholder", ErrorQuality.SEMANTIC_ERROR); + return false; + } + placeholders.add(placeholder); + } + if (placeholders.isEmpty()) + return false; + switch (matchedPattern) { + case 0: // PlaceholderAPI + for (String placeholder : placeholders) + new PlaceholderAPIListener(SkriptPlaceholders.getInstance(), placeholder).register(); + break; + case 1: // MVdWPlaceholderAPI + for (String placeholder : placeholders) + new MVdWPlaceholderAPIListener(SkriptPlaceholders.getInstance(), placeholder).register(); + break; + } + this.placeholders = placeholders.toArray(new String[0]); + this.pattern = matchedPattern; + return true; + } + + @Override + public boolean check(Event e) { + String eventPlaceholder = ""; + switch (pattern) { + case 0: // PlaceholderAPI + eventPlaceholder = ((PlaceholderEvent) e).getPrefix(); + break; + case 1: // MVdWPlaceholderAPI + eventPlaceholder = ((PlaceholderEvent) e).getPlaceholder(); + break; + } + if (eventPlaceholder.equals("")) + return false; + for (String placeholder : this.placeholders) { + if (eventPlaceholder.equalsIgnoreCase(placeholder)) + return true; + } + return false; + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + String placeholders = Arrays.toString(this.placeholders); + // Trim off the ends + placeholders = placeholders.substring(1, placeholders.length() - 1); + switch (pattern) { + case 0: // PlaceholderAPI + return "placeholderapi placeholder request for the prefixes " + placeholders; + case 1: // MVdWPlaceholderAPI + return "mvdwplaceholderapi placeholder request for the placeholders " + placeholders; + default: + return "placeholder request event"; + } + } + +} diff --git a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprParsePlaceholder.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprParsePlaceholder.java similarity index 53% rename from src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprParsePlaceholder.java rename to src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprParsePlaceholder.java index 5cf8f7d..3e76592 100644 --- a/src/main/java/io/github/apickledwalrus/placeholderaddon/skript/expressions/ExprParsePlaceholder.java +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprParsePlaceholder.java @@ -1,4 +1,4 @@ -package io.github.apickledwalrus.placeholderaddon.skript.expressions; +package io.github.apickledwalrus.skriptplaceholders.skript.elements.expressions; import ch.njol.skript.Skript; import ch.njol.skript.doc.Description; @@ -10,11 +10,10 @@ import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.util.Kleenean; -import io.github.apickledwalrus.placeholderaddon.Main; -import me.clip.placeholderapi.PlaceholderAPI; + +import io.github.apickledwalrus.skriptplaceholders.skript.util.PlaceholderUtils; import org.bukkit.OfflinePlayer; -import org.bukkit.entity.Player; import org.bukkit.event.Event; import java.util.ArrayList; @@ -28,12 +27,12 @@ "\t\tset {_ping} to placeholder \"player_ping\" from arg-1 # PlaceholderAPI", "\t\tset {_ping} to placeholder \"{ping}\" from arg-1 # MVdWPlaceholderAPI", "\t\tsend \"Ping of %arg-1%: %{_ping}%\" to player"}) -@Since("1.0 - PAPI Placeholders, 1.2 - MVdW Placeholders, 1.3 - Updated Syntax, 1.4 - Colorize Option") +@Since("1.0 - PAPI Placeholders | 1.2 - MVdW Placeholders | 1.3 - Updated Syntax | 1.4 - Colorize Option") public class ExprParsePlaceholder extends SimpleExpression { static { Skript.registerExpression(ExprParsePlaceholder.class, String.class, ExpressionType.SIMPLE, - "[the] ([value of] placeholder[s]|placeholder [value] [of]) %strings% [from %-players/offlineplayers%] [(1¦without color)]", + "[the] ([value of] placeholder[s]|placeholder [value] [of]) %strings% [(from|of) %-players/offlineplayers%] [(1¦without color)]", "parse placeholder[s] %strings% [(for|as) %-players/offlineplayers%] [(1¦without color)]"); } @@ -42,53 +41,12 @@ public class ExprParsePlaceholder extends SimpleExpression { private boolean colorize; - private String formatPlaceholder(String placeholder) { - if (placeholder == null) { - return null; - } - if (placeholder.charAt(0) == '%') { - placeholder = placeholder.substring(1); - } - if (placeholder.charAt(placeholder.length() - 1) == '%') { - placeholder = placeholder.substring(0, placeholder.length() - 1); - } - return "%" + placeholder + "%"; - } - - private String getPlaceholder(String placeholder, OfflinePlayer player) { - String value; - - if (Main.hasMVdW()) { - if (placeholder.charAt(0) == '{' && placeholder.charAt(placeholder.length() - 1) == '}') { - value = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(player, placeholder); - return value.equals(placeholder) ? null : value; - } - } - - if (Main.hasPapi()) { - placeholder = formatPlaceholder(placeholder); - if (PlaceholderAPI.containsPlaceholders(placeholder)) { - - if (player != null && player.isOnline()) { - value = PlaceholderAPI.setPlaceholders((Player) player, placeholder, !colorize); - } else { - value = PlaceholderAPI.setPlaceholders(player, placeholder, !colorize); - } - if (value == null || value.isEmpty() || value.equalsIgnoreCase(placeholder)) - return null; - return value; - } - } - - return null; - } - @SuppressWarnings("unchecked") @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { placeholders = (Expression) exprs[0]; players = (Expression) exprs[1]; - colorize = parseResult.mark == 1; + colorize = parseResult.mark != 1; return true; } @@ -96,17 +54,17 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye protected String[] get(final Event e) { List values = new ArrayList<>(); String[] placeholders = this.placeholders.getArray(e); - if (this.players == null) { - for (String pl : placeholders) { - values.add(getPlaceholder(pl, null)); - } - } else { - OfflinePlayer[] players = this.players.getArray(e); - for (String pl : placeholders) { - for (OfflinePlayer p : players) { - values.add(getPlaceholder(pl, p)); + if (this.players != null) { + Object[] players = this.players.getArray(e); + for (String placeholder : placeholders) { + for (Object player : players) { + if (player instanceof OfflinePlayer) + values.add(PlaceholderUtils.getPlaceholder(placeholder, (OfflinePlayer) player, colorize)); } } + } else { + for (String placeholder : placeholders) + values.add(PlaceholderUtils.getPlaceholder(placeholder, null, colorize)); } return values.toArray(new String[0]); } @@ -123,7 +81,9 @@ public Class getReturnType() { @Override public String toString(Event e, boolean debug) { - return "the value of placeholder(s) " + placeholders.toString(e, debug) + " from " + players.toString(e, debug); + if (players != null) + return "the value of placeholder(s) " + placeholders.toString(e, debug) + " from " + players.toString(e, debug); + return "the value of placeholder(s) " + placeholders.toString(e, debug); } } diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprPlaceholder.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprPlaceholder.java new file mode 100644 index 0000000..4165d28 --- /dev/null +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprPlaceholder.java @@ -0,0 +1,95 @@ +package io.github.apickledwalrus.skriptplaceholders.skript.elements.expressions; + +import org.bukkit.event.Event; + +import ch.njol.skript.ScriptLoader; +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Events; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.util.Kleenean; + +import io.github.apickledwalrus.skriptplaceholders.placeholder.PlaceholderEvent; + +@Name("Placeholder") +@Description("Returns the placeholder in a placeholder request event.") +@Examples({"on placeholderapi placeholder request for the prefix \"custom\":", + "\tbroadcast \"Placeholder: %the placeholder%\"", + "\tbroadcast \"Prefix: %the placeholder prefix%\"", + "\tbroadcast \"Identifier: %the placeholder identifier%\"", + "on mvdw placeholder request for the placeholder \"custom_hey\":", + "\tbroadcast \"Placeholder: %the placeholder%\"", +}) +@Since("1.0 - PlaceholderAPI | 1.3 - MVdWPlaceholderAPI") +@Events("Placeholder Request") +public class ExprPlaceholder extends SimpleExpression { + + static { + Skript.registerExpression(ExprPlaceholder.class, String.class, ExpressionType.SIMPLE, + "[the] [event(-| )]placeholder", + "[the] [[event(-| )]placeholder] (1¦prefix|2¦identifier)" + ); + } + + /** + * 0 = full placeholder + * 1 = part BEFORE first underscore + * 2 = part AFTER first underscore + */ + private int placeholderPart; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(PlaceholderEvent.class)) { + Skript.error("The placeholder can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + this.placeholderPart = parseResult.mark; + return true; + } + + @Override + protected String[] get(Event e) { + switch (placeholderPart) { + case 0: // Full placeholder + return new String[]{((PlaceholderEvent) e).getPlaceholder()}; + case 1: // Part BEFORE first underscore + return new String[]{((PlaceholderEvent) e).getPrefix()}; + case 2: // Part AFTER first underscore + return new String[]{((PlaceholderEvent) e).getIdentifier()}; + } + return new String[]{}; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return String.class; + } + + @Override + public String toString(Event e, boolean debug) { + switch (placeholderPart) { + case 0: // Full placeholder + return "the placeholder"; + case 1: // Part BEFORE first underscore + return "the placeholder prefix"; + case 2: // Part AFTER first underscore + return "the placeholder identifier"; + default: + return "placeholder"; + } + } + +} diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprPlaceholderResult.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprPlaceholderResult.java new file mode 100644 index 0000000..6bea2c5 --- /dev/null +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/elements/expressions/ExprPlaceholderResult.java @@ -0,0 +1,97 @@ +package io.github.apickledwalrus.skriptplaceholders.skript.elements.expressions; + +import org.bukkit.event.Event; + +import ch.njol.skript.ScriptLoader; +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer.ChangeMode; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Events; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; +import io.github.apickledwalrus.skriptplaceholders.placeholder.PlaceholderEvent; + +@Name("Placeholder Result") +@Description("The value of a placeholder in a placeholder event. Can be set, reset, or deleted.") +@Examples({"on placeholderapi placeholder request for the prefix \"custom\":", + "\tif the identifier is \"hello\": # Placeholder is \"%custom_hey%\"", + "\t\tset the result to \"Hey there %player%!\"", + "on mvdw placeholder request for the placeholder \"custom_hey\":", + "\t# Placeholder is \"{custom_hey}\"", + "\tset the result to \"Hey there %player%!\"" +}) +@Since("1.0 - PlaceholderAPI | 1.3 - MVdWPlaceholderAPI") +@Events("Placeholder Request") +public class ExprPlaceholderResult extends SimpleExpression { + + static { + Skript.registerExpression(ExprPlaceholderResult.class, String.class, ExpressionType.SIMPLE, + "[the] [[event(-| )]placeholder] result" + ); + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(PlaceholderEvent.class)) { + Skript.error("The placeholder result can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + + @Override + protected String[] get(Event e) { + return new String[]{((PlaceholderEvent) e).getResult()}; + } + + @Override + public Class[] acceptChange(final ChangeMode mode) { + if (mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET) { + return CollectionUtils.array(String.class); + } + return null; + } + + @Override + public void change(Event e, Object[] delta, ChangeMode mode) { + if (delta == null && mode == ChangeMode.SET) + return; + switch (mode) { + case SET: + ((PlaceholderEvent) e).setResult((String) delta[0]); + break; + case RESET: + case DELETE: + ((PlaceholderEvent) e).setResult(null); + break; + case ADD: + case REMOVE: + case REMOVE_ALL: + assert false; + } + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return String.class; + } + + @Override + public String toString(Event e, boolean debug) { + return "the placeholder result"; + } + +} diff --git a/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/util/PlaceholderUtils.java b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/util/PlaceholderUtils.java new file mode 100644 index 0000000..d9151e6 --- /dev/null +++ b/src/main/java/io/github/apickledwalrus/skriptplaceholders/skript/util/PlaceholderUtils.java @@ -0,0 +1,38 @@ +package io.github.apickledwalrus.skriptplaceholders.skript.util; + +import org.bukkit.OfflinePlayer; + +import io.github.apickledwalrus.skriptplaceholders.SkriptPlaceholders; +import me.clip.placeholderapi.PlaceholderAPI; +import net.md_5.bungee.api.ChatColor; + +public class PlaceholderUtils { + + /** + * @param placeholder The placeholder to get the value of + * @param player The player to get the placeholder from. Can be null in some cases (e.g. PlaceholderAPI) + * @return The value of the placeholder for the given player (if one is given) + */ + public static String getPlaceholder(String placeholder, OfflinePlayer player, boolean colorize) { + String value = ""; + + if (SkriptPlaceholders.hasMVdW()) { + if (placeholder.charAt(0) == '{' && placeholder.charAt(placeholder.length() - 1) == '}') { + value = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(player, placeholder); + return value.equals(placeholder) ? null : (colorize ? ChatColor.translateAlternateColorCodes('&', value) : value); + } + } + + if (SkriptPlaceholders.hasPapi()) { + if (placeholder.indexOf('%') == -1) // Try to add percentage signs manually + placeholder = "%" + placeholder + "%"; + value = PlaceholderAPI.setPlaceholders(player, placeholder); + if (value.isEmpty() || value.equalsIgnoreCase(placeholder)) + return null; + return colorize ? ChatColor.translateAlternateColorCodes('&', value) : value; + } + + return null; + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a109eb3..b0a2307 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: skript-placeholders -main: io.github.apickledwalrus.placeholderaddon.Main -version: 1.4 -authors: [Pikachu, APickledWalrus] +main: io.github.apickledwalrus.skriptplaceholders.SkriptPlaceholders +version: 1.5 +authors: [APickledWalrus, Pikachu920] description: Allows using and registering placeholders with PlaceholderAPI and MVdWPlaceholderAPI in Skript. website: https://github.com/APickledWalrus/skript-placeholders depend: [Skript]