Skip to content

Commit b4b87bf

Browse files
authored
Code fixes (#56)
2 parents a63d974 + a888e44 commit b4b87bf

23 files changed

+220
-276
lines changed

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- uses: actions/setup-java@v3
5353
with:
5454
distribution: 'temurin'
55-
java-version: 17
55+
java-version: 25
5656

5757
# Initializes the CodeQL tools for scanning.
5858
- name: Initialize CodeQL

src/main/java/pro/cloudnode/smp/smpcore/CitizenRequest.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.jetbrains.annotations.Nullable;
77
import pro.cloudnode.smp.smpcore.command.Command;
88

9-
import java.sql.Connection;
109
import java.sql.PreparedStatement;
1110
import java.sql.ResultSet;
1211
import java.sql.SQLException;
@@ -118,13 +117,6 @@ public void accept() {
118117
delete();
119118
}
120119

121-
/**
122-
* Rejects the request
123-
*/
124-
public void reject() {
125-
delete();
126-
}
127-
128120
public CitizenRequest(final @NotNull ResultSet rs) throws SQLException {
129121
this(
130122
UUID.fromString(rs.getString("member")),
@@ -137,10 +129,10 @@ public CitizenRequest(final @NotNull ResultSet rs) throws SQLException {
137129

138130
public void save() {
139131
try (
140-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
141-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
142-
"INSERT INTO `citizen_requests` (`member`, `nation`, `mode`, `created`, `expires`) VALUES (?,"
143-
+ " ?, ?, ?, ?)")
132+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
133+
"INSERT INTO `citizen_requests` (`member`, `nation`, `mode`, `created`, `expires`) VALUES (?,"
134+
+ " ?, ?, ?, ?)"
135+
)
144136
) {
145137
stmt.setString(1, uuid.toString());
146138
stmt.setString(2, nationID);
@@ -162,8 +154,7 @@ public void save() {
162154

163155
public void delete() {
164156
try (
165-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
166-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
157+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
167158
"DELETE FROM `citizen_requests` WHERE `member` = ? AND `nation` = ?")
168159
) {
169160
stmt.setString(1, uuid.toString());
@@ -192,8 +183,7 @@ public void delete() {
192183
final @NotNull Nation nation
193184
) {
194185
try (
195-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
196-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
186+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
197187
"SELECT * FROM `citizen_requests` WHERE `member` = ? AND `nation` = ? LIMIT 1")
198188
) {
199189
stmt.setString(1, member.uuid.toString());
@@ -221,8 +211,7 @@ public void delete() {
221211
*/
222212
public static @NotNull List<@NotNull CitizenRequest> get(final @NotNull Nation nation, final boolean mode) {
223213
try (
224-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
225-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
214+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
226215
"SELECT * FROM `citizen_requests` WHERE `nation` = ? AND `mode` = ? ORDER BY `created`")
227216
) {
228217
stmt.setString(1, nation.id);
@@ -249,8 +238,7 @@ public void delete() {
249238
*/
250239
public static @NotNull List<@NotNull CitizenRequest> get(final @NotNull Member member, final boolean mode) {
251240
try (
252-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
253-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
241+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
254242
"SELECT * FROM `citizen_requests` WHERE `member` = ? AND `mode` = ? ORDER BY `created`")
255243
) {
256244
stmt.setString(1, member.uuid.toString());
@@ -280,19 +268,19 @@ public void delete() {
280268
*/
281269
public static void delete(final @NotNull List<@NotNull CitizenRequest> requests) {
282270
try (
283-
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
284-
final @NotNull PreparedStatement stmt = conn.prepareStatement(
271+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
285272
"DELETE FROM `citizen_requests` WHERE `member` = ? AND `nation` = ?")
286273
) {
287-
conn.setAutoCommit(false);
274+
SMPCore.getInstance().conn.setAutoCommit(false);
288275
for (final @NotNull CitizenRequest request : requests) {
289276
stmt.setString(1, request.uuid.toString());
290277
stmt.setString(2, request.nationID);
291278
stmt.addBatch();
292279
}
293280

294281
stmt.executeBatch();
295-
conn.commit();
282+
SMPCore.getInstance().conn.commit();
283+
SMPCore.getInstance().conn.setAutoCommit(true);
296284
}
297285
catch (final @NotNull SQLException e) {
298286
SMPCore.getInstance().getLogger().log(

src/main/java/pro/cloudnode/smp/smpcore/Configuration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ public boolean deathBanEnabled() {
7070
case DAYS -> "days";
7171
case MONTHS -> "months";
7272
case YEARS -> "years";
73-
default -> {
74-
throw new IllegalStateException("No relative time format for ChronoUnit " + unit);
75-
}
73+
default -> throw new IllegalStateException("No relative time format for ChronoUnit " + unit);
7674
}));
7775
return MiniMessage.miniMessage().deserialize(formatString,
7876
Formatter.number("t", t),

src/main/java/pro/cloudnode/smp/smpcore/Member.java

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
import io.papermc.paper.ban.BanListType;
44
import org.bukkit.OfflinePlayer;
5-
import org.bukkit.entity.Player;
6-
import org.bukkit.metadata.MetadataValue;
75
import org.jetbrains.annotations.NotNull;
86
import org.jetbrains.annotations.Nullable;
97

10-
import java.sql.Connection;
118
import java.sql.PreparedStatement;
129
import java.sql.ResultSet;
1310
import java.sql.SQLException;
@@ -23,6 +20,7 @@
2320
public final class Member {
2421
public final @NotNull UUID uuid;
2522
public @Nullable String nationID;
23+
@SuppressWarnings("CanBeFinal")
2624
public boolean staff;
2725
public final @Nullable UUID altOwnerUUID;
2826
public final @NotNull Date added;
@@ -39,25 +37,14 @@ public Member(final @NotNull OfflinePlayer player, final @Nullable Member altOwn
3937
this(player.getUniqueId(), null, false, altOwner == null ? null : altOwner.uuid, new Date());
4038
}
4139

42-
private Member(final @NotNull ResultSet rs) throws @NotNull SQLException {
40+
private Member(final @NotNull ResultSet rs) throws SQLException {
4341
this(UUID.fromString(rs.getString("uuid")), rs.getString("nation"), rs.getBoolean("staff"), rs.getString("alt_owner") == null ? null : UUID.fromString(rs.getString("alt_owner")), rs.getTimestamp("added"));
4442
}
4543

4644
public @NotNull OfflinePlayer player() {
4745
return SMPCore.getInstance().getServer().getOfflinePlayer(uuid);
4846
}
4947

50-
/**
51-
* Check if player is online and not vanished
52-
*/
53-
public boolean onlineNotVanished() {
54-
final @NotNull Optional<@NotNull Player> player = Optional.ofNullable(player().getPlayer());
55-
if (player.isEmpty()) return false;
56-
for (final @NotNull MetadataValue meta : player.get().getMetadata("vanished"))
57-
if (meta.asBoolean()) return false;
58-
return player.get().isOnline();
59-
}
60-
6148
public boolean isActive() {
6249
return new Date().getTime() - player().getLastSeen() < (long) SMPCore.config().membersInactiveDays() * 24 * 60 * 60 * 1000;
6350
}
@@ -74,19 +61,14 @@ public boolean isAlt() {
7461
return nationID == null ? Optional.empty() : Nation.get(nationID);
7562
}
7663

77-
public @NotNull Token createToken() throws @NotNull SQLException {
78-
return Token.create(this);
79-
}
80-
81-
public @NotNull HashSet<@NotNull Token> tokens() {
64+
public @NotNull Set<@NotNull Token> tokens() {
8265
return Token.get(this);
8366
}
8467

85-
public @NotNull HashSet<@NotNull Member> getAlts() {
86-
final @NotNull HashSet<@NotNull Member> alts = new HashSet<>();
68+
public @NotNull Set<@NotNull Member> getAlts() {
69+
final @NotNull Set<@NotNull Member> alts = new HashSet<>();
8770
try (
88-
final @NotNull Connection conn = SMPCore.getInstance().db()
89-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `alt_owner` = ?")
71+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `alt_owner` = ?")
9072
) {
9173
stmt.setString(1, uuid.toString());
9274
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -105,8 +87,7 @@ public void unban() {
10587

10688
public void save() {
10789
try (
108-
final @NotNull Connection conn = SMPCore.getInstance().db()
109-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)")
90+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)")
11091
) {
11192
stmt.setString(1, uuid.toString());
11293
stmt.setString(2, nationID == null ? null : nationID);
@@ -125,8 +106,7 @@ public void save() {
125106
*/
126107
private void remove() {
127108
try (
128-
final @NotNull Connection conn = SMPCore.getInstance().db()
129-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `members` WHERE `uuid` = ?")
109+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("DELETE FROM `members` WHERE `uuid` = ?")
130110
) {
131111
stmt.setString(1, uuid.toString());
132112
stmt.executeUpdate();
@@ -148,6 +128,7 @@ private void remove() {
148128
*
149129
* @return whether the member was deleted
150130
*/
131+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
151132
public boolean delete() {
152133
if (!getAlts().isEmpty()) return false;
153134
final @NotNull OfflinePlayer player = player();
@@ -166,21 +147,13 @@ public boolean delete() {
166147
return true;
167148
}
168149

169-
public static @NotNull Member create(final @NotNull OfflinePlayer player, final @Nullable Member altOwner) {
170-
final @NotNull Member member = new Member(player.getUniqueId(), null, false, altOwner == null ? null : altOwner.uuid, new Date());
171-
member.save();
172-
member.player().setWhitelisted(true);
173-
return member;
174-
}
175-
176150
public static @NotNull Optional<@NotNull Member> get(final @NotNull OfflinePlayer player) {
177151
return get(player.getUniqueId());
178152
}
179153

180154
public static @NotNull Optional<@NotNull Member> get(final @NotNull UUID uuid) {
181155
try (
182-
final @NotNull Connection conn = SMPCore.getInstance().db()
183-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `uuid` = ? LIMIT 1")
156+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `uuid` = ? LIMIT 1")
184157
) {
185158
stmt.setString(1, uuid.toString());
186159
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -193,11 +166,10 @@ public boolean delete() {
193166
}
194167
}
195168

196-
public static @NotNull HashSet<@NotNull Member> get() {
197-
final @NotNull HashSet<@NotNull Member> members = new HashSet<>();
169+
public static @NotNull Set<@NotNull Member> get() {
170+
final @NotNull Set<@NotNull Member> members = new HashSet<>();
198171
try (
199-
final @NotNull Connection conn = SMPCore.getInstance().db()
200-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members`")
172+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members`")
201173
) {
202174
final @NotNull ResultSet rs = stmt.executeQuery();
203175
while (rs.next()) members.add(new Member(rs));
@@ -208,12 +180,11 @@ public boolean delete() {
208180
return members;
209181
}
210182

211-
public static @NotNull HashSet<@NotNull Member> get(int limit, int page) {
183+
public static @NotNull Set<@NotNull Member> get(int limit, int page) {
212184
final int offset = (page - 1) * limit;
213-
final @NotNull HashSet<@NotNull Member> members = new HashSet<>();
185+
final @NotNull Set<@NotNull Member> members = new HashSet<>();
214186
try (
215-
final @NotNull Connection conn = SMPCore.getInstance().db()
216-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` LIMIT ? OFFSET ?")
187+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` LIMIT ? OFFSET ?")
217188
) {
218189
stmt.setInt(1, limit);
219190
stmt.setInt(2, offset);
@@ -226,11 +197,10 @@ public boolean delete() {
226197
return members;
227198
}
228199

229-
public static @NotNull HashSet<@NotNull Member> get(final @NotNull Nation nation) {
230-
final @NotNull HashSet<@NotNull Member> members = new HashSet<>();
200+
public static @NotNull Set<@NotNull Member> get(final @NotNull Nation nation) {
201+
final @NotNull Set<@NotNull Member> members = new HashSet<>();
231202
try (
232-
final @NotNull Connection conn = SMPCore.getInstance().db()
233-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `nation` = ?")
203+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `nation` = ?")
234204
) {
235205
stmt.setString(1, nation.id);
236206
final @NotNull ResultSet rs = stmt.executeQuery();
@@ -244,8 +214,7 @@ public boolean delete() {
244214

245215
public static int count() {
246216
try (
247-
final @NotNull Connection conn = SMPCore.getInstance().db()
248-
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) as `n` FROM `members`")
217+
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT COUNT(*) as `n` FROM `members`")
249218
) {
250219
final @NotNull ResultSet rs = stmt.executeQuery();
251220
rs.next();
@@ -257,10 +226,12 @@ public static int count() {
257226
}
258227
}
259228

229+
@SuppressWarnings("NullableProblems")
260230
public static @NotNull Set<@NotNull String> getNames() {
261231
return get().stream().map(m -> m.player().getName()).filter(Objects::nonNull).collect(Collectors.toSet());
262232
}
263233

234+
@SuppressWarnings("NullableProblems")
264235
public static @NotNull Set<@NotNull String> getAltNames() {
265236
return get().stream().filter(Member::isAlt).map(m -> m.player().getName()).filter(Objects::nonNull).collect(Collectors.toSet());
266237
}

src/main/java/pro/cloudnode/smp/smpcore/Messages.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import java.util.Arrays;
2323
import java.util.Calendar;
2424
import java.util.Date;
25-
import java.util.HashSet;
2625
import java.util.List;
2726
import java.util.Objects;
2827
import java.util.Optional;
28+
import java.util.Set;
2929
import java.util.TimeZone;
3030
import java.util.stream.Collectors;
3131

@@ -241,7 +241,7 @@ public Messages() {
241241
}
242242

243243
public @NotNull Component nationCitizensList(final @NotNull Nation nation, final @NotNull Permissible sender, final boolean other) {
244-
final @NotNull HashSet<@NotNull Member> members = nation.citizens();
244+
final @NotNull Set<@NotNull Member> members = nation.citizens();
245245
final @NotNull Component header = MiniMessage.miniMessage()
246246
.deserialize(Objects.requireNonNull(config.getString("nation.citizens.list.header"))
247247
.replaceAll("<color>", "<#" + nation.color + ">")
@@ -513,7 +513,7 @@ public Messages() {
513513
.ofNullable(player.player().getName()).orElse(player.player().getUniqueId().toString())));
514514
}
515515

516-
public @NotNull Component errorDisallowedCharacters(final @NotNull HashSet<@NotNull Character> chars) {
516+
public @NotNull Component errorDisallowedCharacters(final @NotNull Set<@NotNull Character> chars) {
517517
return MiniMessage.miniMessage()
518518
.deserialize(Objects.requireNonNull(config.getString("error.disallowed-characters")), Placeholder.unparsed("chars", chars.stream().map(String::valueOf).collect(Collectors.joining())));
519519
}
@@ -584,10 +584,6 @@ public Messages() {
584584
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("error.other-citizen")), Placeholder.unparsed("player", Optional.ofNullable(member.player().getName()).orElse(member.player().getUniqueId().toString())));
585585
}
586586

587-
public @NotNull Component errorNotPlayer() {
588-
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("error.not-player")));
589-
}
590-
591587
public @NotNull Component errorKickLeadership() {
592588
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("error.kick-leadership")));
593589
}

0 commit comments

Comments
 (0)