Skip to content

Commit d138945

Browse files
committed
fix: various issues with ?plot
1. Updated minimessage to sanitize <shadow> and <pride> 2. Allow inputting handles 3. Fix connection leaking by returning a ResultSet 4. Don't mention auto clear as it's been removed 5. Handle private nodes correctly
1 parent e224f3f commit d138945

File tree

6 files changed

+106
-57
lines changed

6 files changed

+106
-57
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ dependencies {
3030
implementation("org.geysermc.mcprotocollib:protocol:1.21.7-1")
3131
implementation("mysql:mysql-connector-java:5.1.13")
3232
implementation("org.codehaus.groovy:groovy-jsr223:3.0.8")
33-
implementation("net.kyori:adventure-api:4.16.0")
34-
implementation("net.kyori:adventure-text-minimessage:4.16.0")
33+
implementation("net.kyori:adventure-api:4.24.0")
34+
implementation("net.kyori:adventure-text-minimessage:4.24.0")
3535
implementation("dev.vankka:mcdiscordreserializer:4.3.0")
3636
}
3737

src/main/java/com/diamondfire/helpbot/bot/command/impl/stats/plot/AbstractPlotCommand.java

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,31 @@ public abstract class AbstractPlotCommand extends Command {
4040
public void run(CommandEvent event) {
4141
PresetBuilder preset = new PresetBuilder();
4242
try {
43-
ResultSet resultTablePlot = getPlot(event);
43+
Plot plot = getPlot(event);
4444
EmbedBuilder embed = preset.getEmbed();
4545

46-
if (resultTablePlot == null || !resultTablePlot.next()) {
46+
if (plot == null) {
4747
throw new IllegalStateException("Plot not found");
4848
}
49-
int plotID = resultTablePlot.getInt("id");
49+
int plotID = plot.id();
5050
preset.withPreset(
5151
new InformativeReply(InformativeReplyType.INFO, String.format("Plot Information (%s)", plotID), null)
5252
);
5353

54-
@Nullable String handle = resultTablePlot.getString("handle");
55-
@Nullable String description = resultTablePlot.getString("description");
56-
PlotSize size = PlotSize.fromID(resultTablePlot.getInt("plotsize") - 1);
54+
@Nullable String handle = plot.handle();
55+
@Nullable String description = plot.description();
56+
PlotSize size = plot.plotSize();
5757

5858
embed.setTitle(handle == null ? String.format("Plot Information (%s)", plotID) : String.format("Plot Information (%s) (%s)", handle, plotID));
5959
embed.setDescription(description);
60-
embed.addField("Name", StringUtil.fromMiniMessage(resultTablePlot.getString("name")), true);
61-
embed.addField("Owner", resultTablePlot.getString("owner_name"), true);
62-
embed.addField("Node", "Node " + resultTablePlot.getInt("node"), true);
60+
embed.addField("Name", StringUtil.fromMiniMessage(plot.name()), true);
61+
embed.addField("Owner", plot.ownerName(), true);
62+
boolean privateNode = plot.node() >= 1000;
63+
embed.addField("Node", (privateNode ? "Private " : "") + "Node " + (privateNode ? plot.node() - 1000 : plot.node()), true);
6364
embed.addField("Plot Size", StringUtil.smartCaps(size.name()), true);
6465

6566
// Creates a list of tags that the plot has
66-
String tags = resultTablePlot.getString("tags");
67+
String tags = plot.tags();
6768
if (tags.equals("none")) {
6869
embed.addField("Plot Tags", "None", true);
6970
} else {
@@ -74,22 +75,19 @@ public void run(CommandEvent event) {
7475
embed.addField("Plot Tags", StringUtil.listView("> ", true, tagList.toArray(new String[0])), true);
7576
}
7677

77-
int weeksTillClear = resultTablePlot.getInt("immunity_level");
78-
LocalDate activeTime = resultTablePlot.getDate("active_time").toLocalDate();
79-
LocalDate clearDate = activeTime.plus(weeksTillClear, ChronoUnit.WEEKS);
78+
LocalDate activeTime = plot.activeTime();
8079

81-
embed.addField("Auto Clear Date", FormatUtil.formatDate(clearDate) + String.format(" (%s weeks)", ChronoUnit.WEEKS.between(activeTime, clearDate)), true);
8280
embed.addField("Last Active Date", FormatUtil.formatDate(activeTime), true);
83-
embed.addField("Whitelisted", (resultTablePlot.getInt("whitelist") == 1) + "", true);
84-
embed.addField("Player Count", FormatUtil.formatNumber(resultTablePlot.getInt("player_count")), true);
85-
embed.addField("Current Votes", FormatUtil.formatNumber(resultTablePlot.getInt("votes")), true);
81+
embed.addField("Whitelisted", plot.whitelisted() + "", true);
82+
embed.addField("Player Count", FormatUtil.formatNumber(plot.playerCount()), true);
83+
embed.addField("Current Votes", FormatUtil.formatNumber(plot.votes()), true);
8684

87-
int x = resultTablePlot.getInt("xmin") + (size.getSize() / 2);
88-
int z = resultTablePlot.getInt("zmin") + (size.getSize() / 2);
85+
int x = plot.xMin() + (size.getSize() / 2);
86+
int z = plot.zMin() + (size.getSize() / 2);
8987
embed.addField("Plot Center", String.format("[%s, 50, %s]", x, z), true);
9088

9189
// Creates the icon for the plot
92-
String plotIcon = resultTablePlot.getString("icon");
90+
String plotIcon = plot.icon();
9391
// Plot head icons start with the character h.
9492
if (plotIcon.startsWith("h")) {
9593
embed.setThumbnail(Util.getPlayerHead(plotIcon.substring(1)));
@@ -100,35 +98,35 @@ public void run(CommandEvent event) {
10098
event.getReplyHandler().replyA(preset).addFiles(FileUpload.fromData(mcItem)).queue();
10199
}
102100

103-
} catch (SQLException | IllegalStateException e) {
101+
} catch (IllegalStateException e) {
104102
preset.withPreset(
105103
new InformativeReply(InformativeReplyType.ERROR, "Plot was not found.")
106104
);
107105
event.reply(preset);
108106
}
109107
}
110108

111-
public abstract ResultSet getPlot(CommandEvent event);
109+
public abstract @Nullable Plot getPlot(CommandEvent event);
112110

113-
114-
private enum PlotSize {
115-
BASIC(51),
116-
LARGE(101),
117-
MASSIVE(301),
118-
MEGA(1001);
119-
120-
private final int size;
121-
122-
PlotSize(int size) {
123-
this.size = size;
124-
}
125-
126-
public int getSize() {
127-
return size;
128-
}
129-
130-
public static PlotSize fromID(int id) {
131-
return PlotSize.values()[id];
132-
}
111+
protected Plot mapResultSetToPlot(final ResultSet rs) throws SQLException {
112+
return new Plot(
113+
rs.getInt("id"),
114+
rs.getString("handle"),
115+
rs.getString("description"),
116+
rs.getString("name"),
117+
rs.getString("owner_name"),
118+
rs.getInt("node"),
119+
PlotSize.fromID(rs.getInt("plotsize") - 1),
120+
rs.getString("tags"),
121+
rs.getInt("immunity_level"),
122+
rs.getDate("active_time").toLocalDate(),
123+
rs.getInt("whitelist") == 1,
124+
rs.getInt("player_count"),
125+
rs.getInt("votes"),
126+
rs.getInt("xmin"),
127+
rs.getInt("zmin"),
128+
rs.getString("icon")
129+
);
133130
}
131+
134132
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.diamondfire.helpbot.bot.command.impl.stats.plot;
2+
3+
import java.time.LocalDate;
4+
5+
public record Plot(int id, String handle, String description, String name, String ownerName, int node,
6+
PlotSize plotSize, String tags, int immunityLevel, LocalDate activeTime, boolean whitelisted,
7+
int playerCount, int votes, int xMin, int zMin, String icon) {
8+
9+
}

src/main/java/com/diamondfire/helpbot/bot/command/impl/stats/plot/PlotCommand.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.diamondfire.helpbot.bot.command.impl.stats.plot;
22

33
import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
4-
import com.diamondfire.helpbot.bot.command.argument.impl.types.IntegerArgument;
4+
import com.diamondfire.helpbot.bot.command.argument.impl.types.*;
55
import com.diamondfire.helpbot.bot.command.help.*;
66
import com.diamondfire.helpbot.bot.command.permissions.Permission;
77
import com.diamondfire.helpbot.bot.events.CommandEvent;
@@ -23,15 +23,15 @@ public HelpContext getHelpContext() {
2323
.category(CommandCategory.GENERAL_STATS)
2424
.addArgument(
2525
new HelpContextArgument()
26-
.name("plot id")
26+
.name("plot id or handle")
2727
);
2828
}
2929

3030
@Override
3131
public ArgumentSet compileArguments() {
3232
return new ArgumentSet()
33-
.addArgument("id",
34-
new IntegerArgument());
33+
.addArgument("id_or_handle",
34+
new StringArgument());
3535
}
3636

3737
@Override
@@ -40,14 +40,34 @@ public Permission getPermission() {
4040
}
4141

4242
@Override
43-
public ResultSet getPlot(CommandEvent event) {
43+
public Plot getPlot(final CommandEvent event) {
4444
try {
45-
Connection connection = ConnectionProvider.getConnection();
46-
PreparedStatement statement = connection.prepareStatement("SELECT * FROM plots WHERE id = ?");
47-
statement.setInt(1, event.getArgument("id"));
48-
49-
return statement.executeQuery();
50-
} catch (Exception e) {
45+
int id = Integer.parseInt(event.getArgument("id_or_handle"));
46+
try (Connection connection = ConnectionProvider.getConnection();
47+
PreparedStatement statement = connection.prepareStatement("SELECT * FROM plots WHERE id = ?")) {
48+
49+
statement.setInt(1, id);
50+
try (ResultSet resultSet = statement.executeQuery()) {
51+
if (resultSet.next()) {
52+
return this.mapResultSetToPlot(resultSet);
53+
}
54+
}
55+
}
56+
} catch (NumberFormatException ignored) {
57+
String handle = event.getArgument("id_or_handle");
58+
try (Connection connection = ConnectionProvider.getConnection();
59+
PreparedStatement statement = connection.prepareStatement("SELECT * FROM plots WHERE handle = ?")) {
60+
61+
statement.setString(1, handle);
62+
try (ResultSet resultSet = statement.executeQuery()) {
63+
if (resultSet.next()) {
64+
return this.mapResultSetToPlot(resultSet);
65+
}
66+
}
67+
} catch (SQLException e) {
68+
e.printStackTrace();
69+
}
70+
} catch (SQLException e) {
5171
e.printStackTrace();
5272
}
5373
return null;

src/main/java/com/diamondfire/helpbot/bot/command/impl/stats/plot/PlotLocCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Permission getPermission() {
5151

5252
// If someone who knows MYSQL enough that they can add plotsize as some sort of "local" variable. Go for it.
5353
@Override
54-
public ResultSet getPlot(CommandEvent event) {
54+
public Plot getPlot(CommandEvent event) {
5555
try {
5656
Connection connection = ConnectionProvider.getConnection();
5757
boolean nodeSpecific = event.getArgument("node") != null;
@@ -80,7 +80,7 @@ public ResultSet getPlot(CommandEvent event) {
8080

8181
statement.setInt(1, event.getArgument("x"));
8282
statement.setInt(2, event.getArgument("z"));
83-
return statement.executeQuery();
83+
return this.mapResultSetToPlot(statement.executeQuery());
8484
} catch (Exception e) {
8585
e.printStackTrace();
8686
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.diamondfire.helpbot.bot.command.impl.stats.plot;
2+
3+
enum PlotSize {
4+
BASIC(51),
5+
LARGE(101),
6+
MASSIVE(301),
7+
MEGA(1001);
8+
9+
private final int size;
10+
11+
PlotSize(int size) {
12+
this.size = size;
13+
}
14+
15+
public static PlotSize fromID(int id) {
16+
return PlotSize.values()[id];
17+
}
18+
19+
public int getSize() {
20+
return size;
21+
}
22+
}

0 commit comments

Comments
 (0)