Skip to content

Commit 634e48e

Browse files
authored
Merge pull request #1 from ricglz/manage-coords
Upgrade to version 0.2.0 of the plugin
2 parents b4c57c2 + a0371c3 commit 634e48e

18 files changed

+622
-62
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This is explained in the following [wiki page](https://github.com/ricglz/discoor
99
# Features
1010

1111
- Sends message to Discord that the server is up
12-
- `/discoords [label]` command. Allows to send your current coordinates to an specific message channel. If a label is passed as argument it will also sent the label to the discord text channel.
12+
- `/discoords [message]` command. Allows to send your current coordinates to an specific message channel. If a label is passed as argument it will also sent the label to the discord text channel.
1313

1414
Ex.
1515

@@ -21,11 +21,17 @@ Ex.
2121
/discoords diamonds
2222
2323
-> Sent to discord: "(0, 0, 0) - diamonds - by player"
24+
25+
/discoords a very big fortress
26+
-> Sent to discord: "(0, 0, 0) - a very big fortress - by player"
2427
```
28+
- `/distance` command. Calculates the distance between 2 locations, optionally being one of those your current location
29+
- `/save-coords <label>`. Saves your current location to later on search it locally, but will not be sent through discord
30+
- `/search-coords <label>`. Searches for a coordinate based on the given label
2531

2632
# Possible Future Features
2733

28-
- [ ] Stores all the locations also in Minecraft to be able to manage those other locations
29-
- [ ] Add command to save the current location without sending it to discord
30-
- [ ] Add command to send those locations that still haven't been sent but are stored
31-
- [ ] Don't send message if the coordinate or location desired probably have already been sent
34+
- [X] Stores all the locations also in Minecraft to be able to manage those other locations
35+
- [ ] Add command to save the current location without sending it to discord
36+
- [ ] Add command to send those locations that still haven't been sent but are stored
37+
- [ ] Don't send message if the coordinate or location desired probably have already been sent

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<groupId>me.ricglz</groupId>
44
<artifactId>Discoords</artifactId>
55
<packaging>jar</packaging>
6-
<version>0.1.2</version>
6+
<version>0.2.0</version>
77
<name>Discoords</name>
88
<url>http://maven.apache.org</url>
99

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package me.ricglz.discoords;
2+
3+
import me.ricglz.discoords.exceptions.CoordinatesExistException;
4+
import me.ricglz.discoords.exceptions.CoordinatesNotFoundException;
5+
import me.ricglz.discoords.exceptions.InvalidWorldException;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.Collection;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Locale;
14+
import java.util.Map;
15+
import java.util.regex.Pattern;
16+
17+
import org.bukkit.Location;
18+
import org.bukkit.Server;
19+
20+
public class Coordinates {
21+
private final Map<StringIgnoreCase, DiscoordsConf> coordMap = new HashMap<>();
22+
private final File coordsFolder;
23+
private final Server server;
24+
25+
public Coordinates(final Server server, final File dataFolder) {
26+
this.server = server;
27+
coordsFolder = new File(dataFolder, "coords");
28+
if (!coordsFolder.exists()) {
29+
coordsFolder.mkdirs();
30+
}
31+
reloadConfig();
32+
}
33+
34+
public boolean isEmpty() {
35+
return coordMap.isEmpty();
36+
}
37+
38+
public Collection<String> getList() {
39+
final List<String> keys = new ArrayList<>();
40+
for (final StringIgnoreCase filename : coordMap.keySet()) {
41+
keys.add(filename.getString());
42+
}
43+
keys.sort(String.CASE_INSENSITIVE_ORDER);
44+
return keys;
45+
}
46+
47+
public Location getCoordinates(final String coords) throws CoordinatesNotFoundException, InvalidWorldException {
48+
final DiscoordsConf conf = coordMap.get(new StringIgnoreCase(coords));
49+
if (conf == null) {
50+
throw new CoordinatesNotFoundException();
51+
}
52+
return conf.getLocation(null, server);
53+
}
54+
55+
private String sanitizeFileName(final String name) {
56+
final Pattern invalidFileChars = Pattern.compile("[^a-z0-9-]");
57+
return invalidFileChars.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_");
58+
}
59+
60+
public void setCoordinates(final String name, final Location loc) throws CoordinatesExistException, IOException {
61+
final String filename = sanitizeFileName(name);
62+
final StringIgnoreCase ignoreCaseName = new StringIgnoreCase(name);
63+
DiscoordsConf conf = coordMap.get(ignoreCaseName);
64+
if (conf == null) {
65+
final File confFile = new File(coordsFolder, filename + ".yml");
66+
if (confFile.exists()) {
67+
throw new CoordinatesExistException();
68+
}
69+
conf = new DiscoordsConf(confFile);
70+
coordMap.put(ignoreCaseName, conf);
71+
}
72+
conf.setProperty(null, loc);
73+
conf.setProperty("name", name);
74+
try {
75+
conf.saveWithError();
76+
} catch (final IOException ex) {
77+
throw new IOException("That was an invalid coordinates label");
78+
}
79+
}
80+
81+
public final void reloadConfig() {
82+
coordMap.clear();
83+
final File[] listOfFiles = coordsFolder.listFiles();
84+
if (listOfFiles.length >= 1) {
85+
for (final File listOfFile : listOfFiles) {
86+
final String filename = listOfFile.getName();
87+
if (listOfFile.isFile() && filename.endsWith(".yml")) {
88+
try {
89+
final DiscoordsConf conf = new DiscoordsConf(listOfFile);
90+
conf.load();
91+
final String name = conf.getString("name");
92+
if (name != null) {
93+
coordMap.put(new StringIgnoreCase(name), conf);
94+
}
95+
} catch (final Exception ex) {
96+
continue;
97+
}
98+
}
99+
}
100+
}
101+
}
102+
103+
public int getCount() {
104+
return getList().size();
105+
}
106+
107+
private static class StringIgnoreCase {
108+
private final String string;
109+
110+
StringIgnoreCase(final String string) {
111+
this.string = string;
112+
}
113+
114+
@Override
115+
public int hashCode() {
116+
return getString().toLowerCase(Locale.ENGLISH).hashCode();
117+
}
118+
119+
@Override
120+
public boolean equals(final Object o) {
121+
if (o instanceof StringIgnoreCase) {
122+
return getString().equalsIgnoreCase(((StringIgnoreCase) o).getString());
123+
}
124+
return false;
125+
}
126+
127+
public String getString() {
128+
return string;
129+
}
130+
}
131+
}

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

+68-1
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,36 @@
66
import net.dv8tion.jda.api.JDABuilder;
77
import net.dv8tion.jda.api.entities.TextChannel;
88

9+
import org.apache.commons.lang.WordUtils;
10+
import org.bukkit.ChatColor;
11+
import org.bukkit.command.Command;
12+
import org.bukkit.command.CommandSender;
13+
import org.bukkit.entity.Player;
914
import org.bukkit.plugin.java.JavaPlugin;
1015

16+
import me.ricglz.discoords.commands.GeneralCommand;
17+
import me.ricglz.discoords.exceptions.InvalidAmountOfArgumentsException;
18+
import me.ricglz.discoords.exceptions.NotAPlayerError;
19+
1120
/**
1221
* Main class for the Discoords plugin where the listeners are declared
1322
*/
1423
public final class Discoords extends JavaPlugin {
1524
JDA jda;
1625
TextChannel channel;
26+
Coordinates coordinates;
27+
28+
static final String COMMAND_PATH = "me.ricglz.discoords.commands.Command";
1729

1830
@Override
1931
public void onEnable() {
2032
this.saveDefaultConfig();
33+
coordinates = new Coordinates(getServer(), getDataFolder());
34+
coordinates.reloadConfig();
35+
enableDiscordAPI();
36+
}
37+
38+
private void enableDiscordAPI() {
2139
String token = (String) getConfig().get("token");
2240
String channelID = (String) getConfig().get("channel-id");
2341
String welcomeMessage = (String) getConfig().get("welcome-message");
@@ -35,6 +53,55 @@ public void onEnable() {
3553
return;
3654
}
3755
channel.sendMessage(welcomeMessage).queue();
38-
this.getCommand("discoords").setExecutor(new DiscoordsCommandExecutor(channel));
56+
}
57+
58+
public Coordinates getCoordinates() {
59+
return coordinates;
60+
}
61+
62+
public TextChannel getTextChannel() {
63+
return channel;
64+
}
65+
66+
private void sendError(CommandSender sender, String error) {
67+
sender.sendMessage(ChatColor.RED + String.format("[Error] %s", error));
68+
}
69+
70+
@Override
71+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
72+
GeneralCommand cmd = null;
73+
String commandClassName = getCommandClassName(label);
74+
try {
75+
cmd = (GeneralCommand) this.getClassLoader().loadClass(COMMAND_PATH + commandClassName).newInstance();
76+
cmd.setDiscoords(this);
77+
} catch (final Exception e) {
78+
sendError(sender, "Command was not loaded");
79+
getLogger().severe("Command was not loaded");
80+
}
81+
if (cmd != null) {
82+
try {
83+
if (sender instanceof Player) {
84+
cmd.run(((Player) sender), command, label, args);
85+
} else {
86+
throw new NotAPlayerError();
87+
}
88+
} catch (final InvalidAmountOfArgumentsException ex) {
89+
sendError(sender, ex.getMessage());
90+
sender.sendMessage(command.getDescription());
91+
sender.sendMessage(command.getUsage().replace("<command>", label));
92+
} catch (final Exception ex) {
93+
sendError(sender, ex.getMessage());
94+
ex.printStackTrace();
95+
}
96+
}
97+
return true;
98+
}
99+
100+
private String getCommandClassName(String label) {
101+
StringBuilder builder = new StringBuilder();
102+
for (String word : label.split("-")) {
103+
builder.append(WordUtils.capitalize(word));
104+
}
105+
return builder.toString();
39106
}
40107
}

src/main/java/me/ricglz/discoords/DiscoordsCommandExecutor.java

-52
This file was deleted.

0 commit comments

Comments
 (0)