diff --git a/libs/Skript 2.4.1.jar b/libs/Skript 2.4.1.jar new file mode 100644 index 0000000..ccaf488 Binary files /dev/null and b/libs/Skript 2.4.1.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..204791c --- /dev/null +++ b/pom.xml @@ -0,0 +1,87 @@ + + 4.0.0 + me.szumielxd + MVdWPlaceholderSK + 1.0 + MVdWPlaceholderSK + + szumielxd + me.szumielxd.MVdWPlaceholderSK.MVdWPlaceholderSK + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-shade-plugin + 1.4 + + + package + + shade + + + + + + + + src/main/resources + true + + plugin.yml + + + + src/main/resources + false + + **/*.java + plugin.yml + + + + + + + destroystokyo-repo + https://repo.destroystokyo.com/content/repositories/snapshots/ + + + mvdw-software + MVdW Public Repositories + http://repo.mvdw-software.be/content/groups/public/ + + + + + ch.njol + Skript + 2.4.1 + system + ${project.basedir}/libs/Skript 2.4.1.jar + + + be.maximvdw + MVdWPlaceholderAPI + 3.1.1-SNAPSHOT + provided + + + org.spigotmc + spigot + 1.9-R0.1-SNAPSHOT + provided + + + \ No newline at end of file diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/MVdWPlaceholderSK.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/MVdWPlaceholderSK.java new file mode 100644 index 0000000..1ee578c --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/MVdWPlaceholderSK.java @@ -0,0 +1,76 @@ +package me.szumielxd.MVdWPlaceholderSK; + +import java.io.IOException; +import java.lang.reflect.Field; + +import org.bukkit.plugin.java.JavaPlugin; + +import be.maximvdw.placeholderapi.PlaceholderAPI; +import be.maximvdw.placeholderapi.internal.PlaceholderPack; +import ch.njol.skript.Skript; +import ch.njol.skript.SkriptAddon; + +public class MVdWPlaceholderSK extends JavaPlugin { + + + private static MVdWPlaceholderSK instance; + private SkriptAddon addon; + + + public void onEnable() { + + instance = this; + try { + getSkriptInstance(); + (this.addon = getSkriptInstance()).loadClasses("me.szumielxd.MVdWPlaceholderSK.skript", "events", "expressions"); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + + public static MVdWPlaceholderSK getInstance() { + + if (instance == null) throw new IllegalStateException(); + return instance; + + } + + + public SkriptAddon getSkriptInstance() { + + if(addon == null) addon = Skript.registerAddon(this); + return addon; + + } + + + public static boolean isPlaceholderRegistered(String prefix) { + try { + Field f = PlaceholderAPI.class.getDeclaredField("customPlaceholders"); + f.setAccessible(true); + PlaceholderPack pack = (PlaceholderPack) f.get(null); + f.setAccessible(false); + return pack.getPlaceholderReplacer("{"+prefix+"}") != null; + } catch (NoSuchFieldException|SecurityException | IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); + } + return false; + } + + + public static void unregisterPlaceholder(String prefix) { + try { + Field f = PlaceholderAPI.class.getDeclaredField("customPlaceholders"); + f.setAccessible(true); + PlaceholderPack pack = (PlaceholderPack) f.get(null); + pack.removePlaceholder("{"+prefix+"}"); + f.setAccessible(false); + } catch (NoSuchFieldException|SecurityException | IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); + } + } + + +} diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/placeholderAPI/MVdWPAPIEvent.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/placeholderAPI/MVdWPAPIEvent.java new file mode 100644 index 0000000..2ec703f --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/placeholderAPI/MVdWPAPIEvent.java @@ -0,0 +1,68 @@ +package me.szumielxd.MVdWPlaceholderSK.placeholderAPI; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class MVdWPAPIEvent 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 MVdWPAPIEvent(Player p, String prefix, String identifier) { + this.identifier = identifier; + this.player = p; + this.prefix = prefix; + } + + public MVdWPAPIEvent(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; + } + + + + +} diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/placeholderAPI/MVdWPAPIListener.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/placeholderAPI/MVdWPAPIListener.java new file mode 100644 index 0000000..60319d6 --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/placeholderAPI/MVdWPAPIListener.java @@ -0,0 +1,30 @@ +package me.szumielxd.MVdWPlaceholderSK.placeholderAPI; + +import org.bukkit.Bukkit; +import be.maximvdw.placeholderapi.PlaceholderReplaceEvent; +import be.maximvdw.placeholderapi.PlaceholderReplacer; + +public class MVdWPAPIListener implements PlaceholderReplacer { + + + private String identifier; + + + public MVdWPAPIListener(String prefix) { + this.identifier = prefix; + } + + + @Override + public String onPlaceholderReplace(PlaceholderReplaceEvent e) { + MVdWPAPIEvent event; + if(Bukkit.isPrimaryThread()) { + event = new MVdWPAPIEvent(e.getPlayer(), identifier, e.getPlaceholder()); + }else { + event = new MVdWPAPIEvent(e.getPlayer(), identifier, e.getPlaceholder(), true); + } + Bukkit.getPluginManager().callEvent(event); + return event.getResult(); + } + +} diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/events/SKPlaceholderRequestEvent.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/events/SKPlaceholderRequestEvent.java new file mode 100644 index 0000000..95b7f68 --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/events/SKPlaceholderRequestEvent.java @@ -0,0 +1,143 @@ +package me.szumielxd.MVdWPlaceholderSK.skript.events; + +import ch.njol.skript.Skript; +import ch.njol.skript.SkriptConfig; +import ch.njol.skript.SkriptEventHandler; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.SelfRegisteringSkriptEvent; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.Trigger; +import ch.njol.skript.log.ErrorQuality; +import ch.njol.skript.util.Getter; +import ch.njol.skript.util.Task; +import me.szumielxd.MVdWPlaceholderSK.MVdWPlaceholderSK; +import me.szumielxd.MVdWPlaceholderSK.placeholderAPI.MVdWPAPIEvent; +import me.szumielxd.MVdWPlaceholderSK.placeholderAPI.MVdWPAPIListener; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.concurrent.Callable; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import ch.njol.skript.registrations.EventValues; +import org.bukkit.event.Event; +import org.bukkit.event.EventException; +import org.bukkit.event.Listener; +import org.bukkit.plugin.EventExecutor; + +import be.maximvdw.placeholderapi.PlaceholderAPI; + +@Name("On Placeholder Request") +@Description("Called whenever a placeholder is requested") +@Examples("on placeholder request with prefix \"example\":\n\tif the identifier is \"name\": # 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 SKPlaceholderRequestEvent extends SelfRegisteringSkriptEvent { + + static { + Skript.registerEvent("Placeholder Request", SKPlaceholderRequestEvent.class, MVdWPAPIEvent.class, "(mvdw[-]placeholder[api]|mvdw) request with [the] prefix %string%"); + EventValues.registerEventValue(MVdWPAPIEvent.class, Player.class, new Getter() { + @Override + public Player get(MVdWPAPIEvent e) { + return e.getPlayer(); + } + }, 0); + EventValues.registerEventValue(MVdWPAPIEvent.class, String.class, new Getter() { + @Override + public String get(MVdWPAPIEvent e) { + return e.getIdentifier(); + } + }, 0); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(final Literal[] args, final int matchedPattern, final SkriptParser.ParseResult parser) { + prefix = ((Literal) args[0]).getSingle(); + if ("".equals(prefix) || prefix.equals("*")) { + Skript.error(prefix + " is not a valid placeholder", ErrorQuality.SEMANTIC_ERROR); + return false; + } + if(MVdWPlaceholderSK.isPlaceholderRegistered(prefix)) { + Skript.error("Placeholder with prefix '" + prefix + "' is already registered in MVdWPlaceholderAPI", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + + private String prefix; + final static HashMap triggers = new HashMap<>(); + final static HashMap listeners = new HashMap<>(); + + private static boolean registeredExecutor = false; + private final EventExecutor executor = new EventExecutor() { + + @Override + public void execute(final Listener l, final Event e) throws EventException { + if (e == null) + return; + MVdWPAPIEvent ev = (MVdWPAPIEvent)e; + if(ev.getPrefix() != null) { + final Trigger tr = triggers.get(ev.getPrefix()); + if(tr != null) { + + if(!ev.isAsynchronous()) { + SkriptEventHandler.logTriggerStart(tr); + tr.execute(e); + SkriptEventHandler.logTriggerEnd(tr); + } else { + Task.callSync(new Callable() { + @Override + public Void call() throws Exception { + SkriptEventHandler.logTriggerStart(tr); + tr.execute(e); + SkriptEventHandler.logTriggerEnd(tr); + return null; + } + }); + } + } + } + return; + } + }; + + @Override + public String toString(Event e, boolean debug) { + return "placeholder request" + (prefix != null ? (" with prefix \"" + prefix + "\"") : ""); + } + + @Override + public void register(Trigger tr) { + triggers.put(prefix, tr); + MVdWPAPIListener l = new MVdWPAPIListener(prefix); + listeners.put(prefix, l); + PlaceholderAPI.registerPlaceholder(MVdWPlaceholderSK.getInstance(), prefix, l); + if (!registeredExecutor) { + Bukkit.getPluginManager().registerEvent(MVdWPAPIEvent.class, new Listener() {}, SkriptConfig.defaultEventPriority.value(), executor, MVdWPlaceholderSK.getInstance(), true); + registeredExecutor = true; + } + } + + @Override + public void unregister(Trigger tr) { + for(String key : new HashSet<>(triggers.keySet())) { + if(triggers.get(key) != tr) continue; + triggers.remove(key); + listeners.remove(key); + MVdWPlaceholderSK.unregisterPlaceholder(key); + } + } + + @Override + public void unregisterAll() { + for(String key : new HashSet<>(triggers.keySet())) { + triggers.remove(key); + listeners.remove(key); + MVdWPlaceholderSK.unregisterPlaceholder(key); + } + } + +} \ No newline at end of file diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprIdentifier.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprIdentifier.java new file mode 100644 index 0000000..da6dd6b --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprIdentifier.java @@ -0,0 +1,54 @@ +package me.szumielxd.MVdWPlaceholderSK.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.log.ErrorQuality; +import ch.njol.util.Kleenean; +import me.szumielxd.MVdWPlaceholderSK.placeholderAPI.MVdWPAPIEvent; +import org.bukkit.event.Event; +import ch.njol.skript.lang.util.SimpleExpression; + +@Name("Placeholder Identifier") +@Description("Grabs the identifier from a placeholder request event") +@Examples("on placeholder request with prefix \"example\":\n\tif the identifier is \"name\": # 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 ExprIdentifier extends SimpleExpression { + + static { + Skript.registerExpression(ExprIdentifier.class, String.class, ExpressionType.SIMPLE,"[the] [(mvdw-placeholder[api]|mvdw)] identifier"); + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(MVdWPAPIEvent.class)) { + Skript.error("The PlaceholderAPI identifier can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + + @Override + protected String[] get(final Event e) { + return new String[] {((MVdWPAPIEvent) e).getIdentifier()}; + } + + @Override + public String toString(Event e, boolean debug) { + return "the placeholder identifier"; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return String.class; + } +} \ No newline at end of file diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprPlaceholder.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprPlaceholder.java new file mode 100644 index 0000000..14a11d4 --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprPlaceholder.java @@ -0,0 +1,95 @@ +package me.szumielxd.MVdWPlaceholderSK.skript.expressions; + +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.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; + +import be.maximvdw.placeholderapi.PlaceholderAPI; + +import java.util.ArrayList; +import java.util.List; + +@Name("Value of Placeholder") +@Description("Grabs the value of a MVdWPlaceholderAPI prefix") +@Examples("on first join:\n\tset {_uniqueJoins} to the value of placeholder \"server_unique_joins\"\n\tbroadcast \"%{_uniqueJoins}% unique players have joined our server!\"") +public class ExprPlaceholder extends SimpleExpression { + + private Expression placeholders; + private Expression players; + + static { + Skript.registerExpression(ExprPlaceholder.class, String.class, ExpressionType.SIMPLE,"[the] ([value of] mvdw[-]placeholder[s]|mvdw[-]placeholder [value] [of]) %strings% [from %players%]"); + } + + 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, Player player) { + placeholder = formatPlaceholder(placeholder); + String value = PlaceholderAPI.replacePlaceholders(player, placeholder); + if (value.equals(placeholder) || "".equals(value)) { + return null; + } + return value; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + placeholders = (Expression) exprs[0]; + players = (Expression) exprs[1]; + return true; + } + + @Override + protected String[] get(final Event e) { + String[] placeholders = this.placeholders.getArray(e); + Player[] players = this.players.getArray(e); + List values = new ArrayList<>(); + if (players.length != 0) { + for (String ph : placeholders) { + for (Player p : players) { + values.add(getPlaceholder(ph, p)); + } + } + } else { + for (String ph : placeholders) { + values.add(getPlaceholder(ph, null)); + } + } + return values.toArray(new String[values.size()]); + } + + @Override + public String toString(Event e, boolean debug) { + return "the value of placeholder " + placeholders.toString(e, debug) + " from " + players.toString(e, debug); + } + + @Override + public boolean isSingle() { + return placeholders.isSingle() && players.isSingle(); + } + + @Override + public Class getReturnType() { + return String.class; + } +} \ No newline at end of file diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprPrefix.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprPrefix.java new file mode 100644 index 0000000..c257c79 --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprPrefix.java @@ -0,0 +1,54 @@ +package me.szumielxd.MVdWPlaceholderSK.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.log.ErrorQuality; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import me.szumielxd.MVdWPlaceholderSK.placeholderAPI.MVdWPAPIEvent; +import org.bukkit.event.Event; + +@Name("MVdW Placeholder Prefix") +@Description("Represents the prefix in a placeholder request event") +@Examples("on mvdw-placeholder request with the prefix \"example\":\n\tbroadcast the prefix # \"example\" will be broadcasted") +public class ExprPrefix extends SimpleExpression { + + static { + Skript.registerExpression(ExprPrefix.class, String.class, ExpressionType.SIMPLE,"[the] [(mvdw[-]placeholder[api]|mvdw)] (prefix|placeholder)"); + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(MVdWPAPIEvent.class)) { + Skript.error("The PlaceholderAPI prefix can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + + @Override + protected String[] get(final Event e) { + return new String[] {((MVdWPAPIEvent) e).getPrefix()}; + } + + @Override + public String toString(Event e, boolean debug) { + return "the placeholder prefix"; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return String.class; + } +} \ No newline at end of file diff --git a/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprResult.java b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprResult.java new file mode 100644 index 0000000..5221e8c --- /dev/null +++ b/src/main/java/me/szumielxd/MVdWPlaceholderSK/skript/expressions/ExprResult.java @@ -0,0 +1,79 @@ +package me.szumielxd.MVdWPlaceholderSK.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.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.MVdWPlaceholderSK.placeholderAPI.MVdWPAPIEvent; +import org.bukkit.event.Event; + +@Name("Placeholder Result") +@Description("Grabs the result from a placeholder request event") +@Examples("on placeholder request with prefix \"example\":\n\tif the identifier is \"name\": # 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 ExprResult extends SimpleExpression { + + static { + Skript.registerExpression(ExprResult.class, String.class, ExpressionType.SIMPLE,"[the] [(mvdw[-]placeholder[api]|mvdw)] result"); + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + if (!ScriptLoader.isCurrentEvent(MVdWPAPIEvent.class)) { + Skript.error("The PlaceholderAPI result can only be used in a placeholder request event", ErrorQuality.SEMANTIC_ERROR); + return false; + } + return true; + } + + @Override + protected String[] get(final Event e) { + return new String[] {((MVdWPAPIEvent) e).getResult()}; + } + + @Override + public String toString(Event e, boolean debug) { + return "the placeholder result"; + } + + @Override + public boolean isSingle() { + return true; + } + + @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: + ((MVdWPAPIEvent) e).setResult((String) delta[0]); + break; + case RESET: + case DELETE: + ((MVdWPAPIEvent) e).setResult(null); + break; + default: + break; + } + } + + @Override + public Class getReturnType() { + return String.class; + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..f9c5dee --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ +name: ${name} +version: ${version} +author: ${author} +main: ${mainClass} +depend: [Skript, MVdWPlaceholderAPI] +api-version: 1.13 \ No newline at end of file