From fd403803ca8e59e9543883af0734d098817681f5 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sat, 1 Nov 2014 13:00:59 +0100 Subject: [PATCH] Added BarAPI support. --- ChestCommands/plugin.yml | 2 +- .../chestcommands/ChestCommands.java | 5 +++ .../chestcommands/bridge/BarAPIBridge.java | 29 ++++++++++++++- .../icon/command/DragonBarIconCommand.java | 36 +++++++++++++++++++ .../serializer/CommandSerializer.java | 4 ++- .../filoghost/chestcommands/util/Utils.java | 8 +++++ 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/DragonBarIconCommand.java diff --git a/ChestCommands/plugin.yml b/ChestCommands/plugin.yml index 03c0752..fc0ab7b 100644 --- a/ChestCommands/plugin.yml +++ b/ChestCommands/plugin.yml @@ -1,7 +1,7 @@ name: ChestCommands main: com.gmail.filoghost.chestcommands.ChestCommands version: 3.0 -softdepend: [Vault] +softdepend: [Vault, BarAPI] commands: chestcommands: diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/ChestCommands.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/ChestCommands.java index 9895232..980d06d 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/ChestCommands.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/ChestCommands.java @@ -14,6 +14,7 @@ import org.bukkit.plugin.java.JavaPlugin; import org.mcstats.MetricsLite; import com.gmail.filoghost.chestcommands.SimpleUpdater.ResponseHandler; +import com.gmail.filoghost.chestcommands.bridge.BarAPIBridge; import com.gmail.filoghost.chestcommands.bridge.EconomyBridge; import com.gmail.filoghost.chestcommands.command.CommandFramework; import com.gmail.filoghost.chestcommands.command.CommandHandler; @@ -75,6 +76,10 @@ public class ChestCommands extends JavaPlugin { getLogger().info("Vault with a compatible economy plugin was not found! Icons with a PRICE or commands that give money will not work."); } + if (BarAPIBridge.setupPlugin()) { + getLogger().info("Hooked BarAPI"); + } + String version = Utils.getBukkitVersion(); try { Class clazz = Class.forName("com.gmail.filoghost.chestcommands.nms." + version); diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/bridge/BarAPIBridge.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/bridge/BarAPIBridge.java index 3440b91..268a0ed 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/bridge/BarAPIBridge.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/bridge/BarAPIBridge.java @@ -1,7 +1,34 @@ package com.gmail.filoghost.chestcommands.bridge; +import me.confuser.barapi.BarAPI; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + public class BarAPIBridge { - // A big TODO + private static BarAPI barAPI; + + public static boolean setupPlugin() { + Plugin barPlugin = Bukkit.getPluginManager().getPlugin("BarAPI"); + + if (barPlugin == null) { + return false; + } + + barAPI = (BarAPI) barPlugin; + return true; + } + + public static boolean hasValidPlugin() { + return barAPI != null; + } + + public static void setMessage(Player player, String message, int seconds) { + if (!hasValidPlugin()) throw new IllegalStateException("BarAPI plugin was not found!"); + + BarAPI.setMessage(player, message, seconds); + } } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/DragonBarIconCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/DragonBarIconCommand.java new file mode 100644 index 0000000..fb24d9b --- /dev/null +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/DragonBarIconCommand.java @@ -0,0 +1,36 @@ +package com.gmail.filoghost.chestcommands.internal.icon.command; + +import org.bukkit.entity.Player; + +import com.gmail.filoghost.chestcommands.bridge.BarAPIBridge; +import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; +import com.gmail.filoghost.chestcommands.util.Utils; + +public class DragonBarIconCommand extends IconCommand { + + String message; + int seconds; + + public DragonBarIconCommand(String command) { + super(command); + + seconds = 1; + message = command; + + String[] split = command.split("\\|", 2); // Max of 2 pieces. + if (split.length > 1 && Utils.isValidPositiveInteger(split[0].trim())) { + seconds = Integer.parseInt(split[0].trim()); + message = split[1].trim(); + } + + message = Utils.addColors(message); + } + + @Override + public void execute(Player player) { + if (BarAPIBridge.hasValidPlugin()) { + BarAPIBridge.setMessage(player, message, seconds); + } + } + +} diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java index 933a94c..36e2048 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java @@ -11,6 +11,7 @@ import java.util.regex.Pattern; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; import com.gmail.filoghost.chestcommands.internal.icon.command.BroadcastIconCommand; import com.gmail.filoghost.chestcommands.internal.icon.command.ConsoleIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.DragonBarIconCommand; import com.gmail.filoghost.chestcommands.internal.icon.command.GiveIconCommand; import com.gmail.filoghost.chestcommands.internal.icon.command.GiveMoneyIconCommand; import com.gmail.filoghost.chestcommands.internal.icon.command.OpIconCommand; @@ -35,6 +36,7 @@ public class CommandSerializer { commandTypesMap.put(commandPattern("give:"), GiveIconCommand.class); commandTypesMap.put(commandPattern("give-?money:"), GiveMoneyIconCommand.class); commandTypesMap.put(commandPattern("sound:"), SoundIconCommand.class); + commandTypesMap.put(commandPattern("dragon-?bar:"), DragonBarIconCommand.class); } private static Pattern commandPattern(String regex) { @@ -81,7 +83,7 @@ public class CommandSerializer { for (Entry> entry : commandTypesMap.entrySet()) { Matcher matcher = entry.getKey().matcher(input); if (matcher.find()) { - + // Remove the command prefix. String cleanedCommand = matcher.replaceFirst(""); try { diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/util/Utils.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/util/Utils.java index b0fe6fa..5802741 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/util/Utils.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/util/Utils.java @@ -187,6 +187,14 @@ public class Utils { } } + public static boolean isValidPositiveInteger(String input) { + try { + return Integer.parseInt(input) > 0; + } catch (NumberFormatException ex) { + return false; + } + } + public static boolean isValidShort(String input) { try { Short.parseShort(input);