Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fd4301a
make all public static permissions final
zefir-git Dec 28, 2025
0a7528e
use Set instead of HashSet for type
zefir-git Dec 28, 2025
1c5a2ec
use Map instead of HashMap for type
zefir-git Dec 28, 2025
a8ebcef
set CodeQL to Java 25
zefir-git Dec 28, 2025
8a9fcb6
replace deprecated vanish detection
zefir-git Dec 28, 2025
589c814
remove unused @SuppressWarnings
zefir-git Dec 28, 2025
decadda
replace deprecated PlayerLoginEvent
zefir-git Dec 28, 2025
d4da75d
unwrap redundant code block
zefir-git Dec 28, 2025
cdc9ffe
fix `throws` statements
zefir-git Dec 28, 2025
13aa370
suppress some warnings
zefir-git Dec 28, 2025
c563e2c
remove unused methods
zefir-git Dec 28, 2025
7335251
remove redundant casts
zefir-git Dec 28, 2025
e4fbb86
ignore unused parameter
zefir-git Dec 28, 2025
688533f
remove unused method
zefir-git Dec 28, 2025
a99f466
use Java 21 `getFirst()`
zefir-git Dec 28, 2025
67e9e22
remove @NotNull from throws
zefir-git Dec 28, 2025
55fc03c
suppress unused token method
zefir-git Dec 28, 2025
a3c02ab
require non-null config
zefir-git Dec 28, 2025
32193bb
fix grammar
zefir-git Dec 28, 2025
9ee725a
simplify lambda
zefir-git Dec 28, 2025
56412c1
remove unused method
zefir-git Dec 28, 2025
1de68eb
suppress warnings
zefir-git Dec 28, 2025
9e8dc9a
rename listeners and their methods
zefir-git Dec 28, 2025
53387d0
remove redundant nullability attributes in main class
zefir-git Dec 28, 2025
f59d2da
fix REST class name capitalisation
zefir-git Dec 28, 2025
a888e44
reuse db connection
zefir-git Dec 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
java-version: 25

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
36 changes: 12 additions & 24 deletions src/main/java/pro/cloudnode/smp/smpcore/CitizenRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.jetbrains.annotations.Nullable;
import pro.cloudnode.smp.smpcore.command.Command;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -118,13 +117,6 @@ public void accept() {
delete();
}

/**
* Rejects the request
*/
public void reject() {
delete();
}

public CitizenRequest(final @NotNull ResultSet rs) throws SQLException {
this(
UUID.fromString(rs.getString("member")),
Expand All @@ -137,10 +129,10 @@ public CitizenRequest(final @NotNull ResultSet rs) throws SQLException {

public void save() {
try (
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO `citizen_requests` (`member`, `nation`, `mode`, `created`, `expires`) VALUES (?,"
+ " ?, ?, ?, ?)")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
"INSERT INTO `citizen_requests` (`member`, `nation`, `mode`, `created`, `expires`) VALUES (?,"
+ " ?, ?, ?, ?)"
)
) {
stmt.setString(1, uuid.toString());
stmt.setString(2, nationID);
Expand All @@ -162,8 +154,7 @@ public void save() {

public void delete() {
try (
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
"DELETE FROM `citizen_requests` WHERE `member` = ? AND `nation` = ?")
) {
stmt.setString(1, uuid.toString());
Expand Down Expand Up @@ -192,8 +183,7 @@ public void delete() {
final @NotNull Nation nation
) {
try (
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
"SELECT * FROM `citizen_requests` WHERE `member` = ? AND `nation` = ? LIMIT 1")
) {
stmt.setString(1, member.uuid.toString());
Expand Down Expand Up @@ -221,8 +211,7 @@ public void delete() {
*/
public static @NotNull List<@NotNull CitizenRequest> get(final @NotNull Nation nation, final boolean mode) {
try (
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
"SELECT * FROM `citizen_requests` WHERE `nation` = ? AND `mode` = ? ORDER BY `created`")
) {
stmt.setString(1, nation.id);
Expand All @@ -249,8 +238,7 @@ public void delete() {
*/
public static @NotNull List<@NotNull CitizenRequest> get(final @NotNull Member member, final boolean mode) {
try (
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
"SELECT * FROM `citizen_requests` WHERE `member` = ? AND `mode` = ? ORDER BY `created`")
) {
stmt.setString(1, member.uuid.toString());
Expand Down Expand Up @@ -280,19 +268,19 @@ public void delete() {
*/
public static void delete(final @NotNull List<@NotNull CitizenRequest> requests) {
try (
final @NotNull Connection conn = SMPCore.getInstance().db().getConnection();
final @NotNull PreparedStatement stmt = conn.prepareStatement(
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement(
"DELETE FROM `citizen_requests` WHERE `member` = ? AND `nation` = ?")
) {
conn.setAutoCommit(false);
SMPCore.getInstance().conn.setAutoCommit(false);
for (final @NotNull CitizenRequest request : requests) {
stmt.setString(1, request.uuid.toString());
stmt.setString(2, request.nationID);
stmt.addBatch();
}

stmt.executeBatch();
conn.commit();
SMPCore.getInstance().conn.commit();
SMPCore.getInstance().conn.setAutoCommit(true);
}
catch (final @NotNull SQLException e) {
SMPCore.getInstance().getLogger().log(
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/pro/cloudnode/smp/smpcore/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public boolean deathBanEnabled() {
case DAYS -> "days";
case MONTHS -> "months";
case YEARS -> "years";
default -> {
throw new IllegalStateException("No relative time format for ChronoUnit " + unit);
}
default -> throw new IllegalStateException("No relative time format for ChronoUnit " + unit);
}));
return MiniMessage.miniMessage().deserialize(formatString,
Formatter.number("t", t),
Expand Down
73 changes: 22 additions & 51 deletions src/main/java/pro/cloudnode/smp/smpcore/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import io.papermc.paper.ban.BanListType;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -23,6 +20,7 @@
public final class Member {
public final @NotNull UUID uuid;
public @Nullable String nationID;
@SuppressWarnings("CanBeFinal")
public boolean staff;
public final @Nullable UUID altOwnerUUID;
public final @NotNull Date added;
Expand All @@ -39,25 +37,14 @@ public Member(final @NotNull OfflinePlayer player, final @Nullable Member altOwn
this(player.getUniqueId(), null, false, altOwner == null ? null : altOwner.uuid, new Date());
}

private Member(final @NotNull ResultSet rs) throws @NotNull SQLException {
private Member(final @NotNull ResultSet rs) throws SQLException {
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"));
}

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

/**
* Check if player is online and not vanished
*/
public boolean onlineNotVanished() {
final @NotNull Optional<@NotNull Player> player = Optional.ofNullable(player().getPlayer());
if (player.isEmpty()) return false;
for (final @NotNull MetadataValue meta : player.get().getMetadata("vanished"))
if (meta.asBoolean()) return false;
return player.get().isOnline();
}

public boolean isActive() {
return new Date().getTime() - player().getLastSeen() < (long) SMPCore.config().membersInactiveDays() * 24 * 60 * 60 * 1000;
}
Expand All @@ -74,19 +61,14 @@ public boolean isAlt() {
return nationID == null ? Optional.empty() : Nation.get(nationID);
}

public @NotNull Token createToken() throws @NotNull SQLException {
return Token.create(this);
}

public @NotNull HashSet<@NotNull Token> tokens() {
public @NotNull Set<@NotNull Token> tokens() {
return Token.get(this);
}

public @NotNull HashSet<@NotNull Member> getAlts() {
final @NotNull HashSet<@NotNull Member> alts = new HashSet<>();
public @NotNull Set<@NotNull Member> getAlts() {
final @NotNull Set<@NotNull Member> alts = new HashSet<>();
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `alt_owner` = ?")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `alt_owner` = ?")
) {
stmt.setString(1, uuid.toString());
final @NotNull ResultSet rs = stmt.executeQuery();
Expand All @@ -105,8 +87,7 @@ public void unban() {

public void save() {
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)")
) {
stmt.setString(1, uuid.toString());
stmt.setString(2, nationID == null ? null : nationID);
Expand All @@ -125,8 +106,7 @@ public void save() {
*/
private void remove() {
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("DELETE FROM `members` WHERE `uuid` = ?")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("DELETE FROM `members` WHERE `uuid` = ?")
) {
stmt.setString(1, uuid.toString());
stmt.executeUpdate();
Expand All @@ -148,6 +128,7 @@ private void remove() {
*
* @return whether the member was deleted
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean delete() {
if (!getAlts().isEmpty()) return false;
final @NotNull OfflinePlayer player = player();
Expand All @@ -166,21 +147,13 @@ public boolean delete() {
return true;
}

public static @NotNull Member create(final @NotNull OfflinePlayer player, final @Nullable Member altOwner) {
final @NotNull Member member = new Member(player.getUniqueId(), null, false, altOwner == null ? null : altOwner.uuid, new Date());
member.save();
member.player().setWhitelisted(true);
return member;
}

public static @NotNull Optional<@NotNull Member> get(final @NotNull OfflinePlayer player) {
return get(player.getUniqueId());
}

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

public static @NotNull HashSet<@NotNull Member> get() {
final @NotNull HashSet<@NotNull Member> members = new HashSet<>();
public static @NotNull Set<@NotNull Member> get() {
final @NotNull Set<@NotNull Member> members = new HashSet<>();
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members`")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members`")
) {
final @NotNull ResultSet rs = stmt.executeQuery();
while (rs.next()) members.add(new Member(rs));
Expand All @@ -208,12 +180,11 @@ public boolean delete() {
return members;
}

public static @NotNull HashSet<@NotNull Member> get(int limit, int page) {
public static @NotNull Set<@NotNull Member> get(int limit, int page) {
final int offset = (page - 1) * limit;
final @NotNull HashSet<@NotNull Member> members = new HashSet<>();
final @NotNull Set<@NotNull Member> members = new HashSet<>();
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` LIMIT ? OFFSET ?")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` LIMIT ? OFFSET ?")
) {
stmt.setInt(1, limit);
stmt.setInt(2, offset);
Expand All @@ -226,11 +197,10 @@ public boolean delete() {
return members;
}

public static @NotNull HashSet<@NotNull Member> get(final @NotNull Nation nation) {
final @NotNull HashSet<@NotNull Member> members = new HashSet<>();
public static @NotNull Set<@NotNull Member> get(final @NotNull Nation nation) {
final @NotNull Set<@NotNull Member> members = new HashSet<>();
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `members` WHERE `nation` = ?")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT * FROM `members` WHERE `nation` = ?")
) {
stmt.setString(1, nation.id);
final @NotNull ResultSet rs = stmt.executeQuery();
Expand All @@ -244,8 +214,7 @@ public boolean delete() {

public static int count() {
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) as `n` FROM `members`")
final @NotNull PreparedStatement stmt = SMPCore.getInstance().conn.prepareStatement("SELECT COUNT(*) as `n` FROM `members`")
) {
final @NotNull ResultSet rs = stmt.executeQuery();
rs.next();
Expand All @@ -257,10 +226,12 @@ public static int count() {
}
}

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

@SuppressWarnings("NullableProblems")
public static @NotNull Set<@NotNull String> getAltNames() {
return get().stream().filter(Member::isAlt).map(m -> m.player().getName()).filter(Objects::nonNull).collect(Collectors.toSet());
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/pro/cloudnode/smp/smpcore/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -241,7 +241,7 @@ public Messages() {
}

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

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

public @NotNull Component errorNotPlayer() {
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("error.not-player")));
}

public @NotNull Component errorKickLeadership() {
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("error.kick-leadership")));
}
Expand Down
Loading
Loading