Skip to content

Commit

Permalink
Refactor plugin to use a specific CommandExecutor class
Browse files Browse the repository at this point in the history
  • Loading branch information
ricglz committed Jan 17, 2021
1 parent 7bc8c53 commit aa5c489
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
51 changes: 5 additions & 46 deletions src/main/java/me/ricglz/discoords/Discoords.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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));
}
}
54 changes: 54 additions & 0 deletions src/main/java/me/ricglz/discoords/DiscoordsCommandExecutor.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit aa5c489

Please # to comment.