From aa5c489a59027d4142fe169c111e6dbeb1733420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20J=20Gonz=C3=A1lez=20C?= Date: Sun, 17 Jan 2021 15:33:27 -0600 Subject: [PATCH] Refactor plugin to use a specific CommandExecutor class --- .../java/me/ricglz/discoords/Discoords.java | 51 ++---------------- .../discoords/DiscoordsCommandExecutor.java | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 src/main/java/me/ricglz/discoords/DiscoordsCommandExecutor.java diff --git a/src/main/java/me/ricglz/discoords/Discoords.java b/src/main/java/me/ricglz/discoords/Discoords.java index 7f8d011..7a7ebb7 100644 --- a/src/main/java/me/ricglz/discoords/Discoords.java +++ b/src/main/java/me/ricglz/discoords/Discoords.java @@ -6,10 +6,6 @@ import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.TextChannel; -import org.bukkit.Location; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; /** @@ -28,54 +24,17 @@ public void onEnable() { try { jda = JDABuilder.createDefault(token).build().awaitReady(); channel = jda.getTextChannelById(channelID); - if (channel == null) { - getLogger().warning("The channel id is incorrect"); - return; - } - channel.sendMessage(welcomeMessage).queue(); } catch (LoginException | InterruptedException e) { getLogger().warning("There was a problem building the bot. It may be due to the token"); if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); } } - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (cmd.getName().equalsIgnoreCase("discoords")) { - if (args.length > 1) { - sender.sendMessage("The command doesn't accept as many arguments"); - return false; - } - if (jda == null || channel == null) { - sender.sendMessage("Discoord is not available"); - return true; - } - if (sender instanceof Player) { - Player player = (Player) sender; - Location loc = player.getLocation(); - String locString = String.format("(%d, %d, %d)", (int) loc.getX(), (int) loc.getY(), (int) loc.getZ()); - String msg = args.length == 0 ? locString : String.format("%s - %s", locString, args[0]); - sender.sendMessage(msg); - sendLocation(msg, player.getDisplayName()); - } else { - sender.sendMessage("You must be a player!"); - } - return true; + if (channel == null) { + getLogger().warning("The channel id is incorrect"); + return; } - return true; - } - - /** - * Sends a mesage about the current location and the player name to the - * designated discord channel - * - * @param sentMessage Message that was sent to the player in Minecraft - * @param playerName Name of the player who executed the command - */ - private void sendLocation(String sentMessage, String playerName) { - String msg = String.format("%s - by %s", sentMessage, playerName); - channel.sendMessage(msg).queue(); + channel.sendMessage(welcomeMessage).queue(); + this.getCommand("discoords").setExecutor(new DiscoordsCommandExecutor(channel)); } } diff --git a/src/main/java/me/ricglz/discoords/DiscoordsCommandExecutor.java b/src/main/java/me/ricglz/discoords/DiscoordsCommandExecutor.java new file mode 100644 index 0000000..841b134 --- /dev/null +++ b/src/main/java/me/ricglz/discoords/DiscoordsCommandExecutor.java @@ -0,0 +1,54 @@ +package me.ricglz.discoords; + +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import net.dv8tion.jda.api.entities.TextChannel; + +public class DiscoordsCommandExecutor implements CommandExecutor { + TextChannel channel; + + DiscoordsCommandExecutor(TextChannel channel) { + this.channel = channel; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length > 1) { + sender.sendMessage("[Error] The command doesn't accept as many arguments"); + return false; + } + if (!(sender instanceof Player)) { + sender.sendMessage("[Error] You must be a player!"); + return true; + } + if (channel == null) { + sender.sendMessage("[Error] Discoord is not available"); + return true; + } + + Player player = (Player) sender; + Location loc = player.getLocation(); + String locString = String.format("(%d, %d, %d)", (int) loc.getX(), (int) loc.getY(), (int) loc.getZ()); + String msg = args.length == 0 ? locString : String.format("%s - %s", locString, args[0]); + sender.sendMessage(msg); + sendLocation(msg, player.getDisplayName()); + + return true; + } + + /** + * Sends a mesage about the current location and the player name to the + * designated discord channel + * + * @param sentMessage Message that was sent to the player in Minecraft + * @param playerName Name of the player who executed the command + */ + private void sendLocation(String sentMessage, String playerName) { + String msg = String.format("%s - by %s", sentMessage, playerName); + channel.sendMessage(msg).queue(); + } +}