Skip to content

Commit aa5c489

Browse files
committedJan 17, 2021
Refactor plugin to use a specific CommandExecutor class
1 parent 7bc8c53 commit aa5c489

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed
 

‎src/main/java/me/ricglz/discoords/Discoords.java

+5-46
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
import net.dv8tion.jda.api.JDABuilder;
77
import net.dv8tion.jda.api.entities.TextChannel;
88

9-
import org.bukkit.Location;
10-
import org.bukkit.command.Command;
11-
import org.bukkit.command.CommandSender;
12-
import org.bukkit.entity.Player;
139
import org.bukkit.plugin.java.JavaPlugin;
1410

1511
/**
@@ -28,54 +24,17 @@ public void onEnable() {
2824
try {
2925
jda = JDABuilder.createDefault(token).build().awaitReady();
3026
channel = jda.getTextChannelById(channelID);
31-
if (channel == null) {
32-
getLogger().warning("The channel id is incorrect");
33-
return;
34-
}
35-
channel.sendMessage(welcomeMessage).queue();
3627
} catch (LoginException | InterruptedException e) {
3728
getLogger().warning("There was a problem building the bot. It may be due to the token");
3829
if (e instanceof InterruptedException) {
3930
Thread.currentThread().interrupt();
4031
}
4132
}
42-
}
43-
44-
@Override
45-
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
46-
if (cmd.getName().equalsIgnoreCase("discoords")) {
47-
if (args.length > 1) {
48-
sender.sendMessage("The command doesn't accept as many arguments");
49-
return false;
50-
}
51-
if (jda == null || channel == null) {
52-
sender.sendMessage("Discoord is not available");
53-
return true;
54-
}
55-
if (sender instanceof Player) {
56-
Player player = (Player) sender;
57-
Location loc = player.getLocation();
58-
String locString = String.format("(%d, %d, %d)", (int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
59-
String msg = args.length == 0 ? locString : String.format("%s - %s", locString, args[0]);
60-
sender.sendMessage(msg);
61-
sendLocation(msg, player.getDisplayName());
62-
} else {
63-
sender.sendMessage("You must be a player!");
64-
}
65-
return true;
33+
if (channel == null) {
34+
getLogger().warning("The channel id is incorrect");
35+
return;
6636
}
67-
return true;
68-
}
69-
70-
/**
71-
* Sends a mesage about the current location and the player name to the
72-
* designated discord channel
73-
*
74-
* @param sentMessage Message that was sent to the player in Minecraft
75-
* @param playerName Name of the player who executed the command
76-
*/
77-
private void sendLocation(String sentMessage, String playerName) {
78-
String msg = String.format("%s - by %s", sentMessage, playerName);
79-
channel.sendMessage(msg).queue();
37+
channel.sendMessage(welcomeMessage).queue();
38+
this.getCommand("discoords").setExecutor(new DiscoordsCommandExecutor(channel));
8039
}
8140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package me.ricglz.discoords;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.command.Command;
5+
import org.bukkit.command.CommandExecutor;
6+
import org.bukkit.command.CommandSender;
7+
import org.bukkit.entity.Player;
8+
9+
import net.dv8tion.jda.api.entities.TextChannel;
10+
11+
public class DiscoordsCommandExecutor implements CommandExecutor {
12+
TextChannel channel;
13+
14+
DiscoordsCommandExecutor(TextChannel channel) {
15+
this.channel = channel;
16+
}
17+
18+
@Override
19+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
20+
if (args.length > 1) {
21+
sender.sendMessage("[Error] The command doesn't accept as many arguments");
22+
return false;
23+
}
24+
if (!(sender instanceof Player)) {
25+
sender.sendMessage("[Error] You must be a player!");
26+
return true;
27+
}
28+
if (channel == null) {
29+
sender.sendMessage("[Error] Discoord is not available");
30+
return true;
31+
}
32+
33+
Player player = (Player) sender;
34+
Location loc = player.getLocation();
35+
String locString = String.format("(%d, %d, %d)", (int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
36+
String msg = args.length == 0 ? locString : String.format("%s - %s", locString, args[0]);
37+
sender.sendMessage(msg);
38+
sendLocation(msg, player.getDisplayName());
39+
40+
return true;
41+
}
42+
43+
/**
44+
* Sends a mesage about the current location and the player name to the
45+
* designated discord channel
46+
*
47+
* @param sentMessage Message that was sent to the player in Minecraft
48+
* @param playerName Name of the player who executed the command
49+
*/
50+
private void sendLocation(String sentMessage, String playerName) {
51+
String msg = String.format("%s - by %s", sentMessage, playerName);
52+
channel.sendMessage(msg).queue();
53+
}
54+
}

0 commit comments

Comments
 (0)
Please sign in to comment.