From 5012d0478bd335ad3de0f36607454fbb553aeb9a Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Wed, 6 Apr 2022 22:55:01 -0500 Subject: [PATCH 1/2] Switch UUID in PlexPlayer to an actual UUID and fix notes --- src/main/java/dev/plex/Plex.java | 4 +- .../dev/plex/cache/player/SQLPlayerData.java | 4 +- .../java/dev/plex/cache/sql/SQLNotes.java | 2 +- .../java/dev/plex/command/impl/FreezeCMD.java | 2 +- .../java/dev/plex/command/impl/ListCMD.java | 2 +- .../java/dev/plex/command/impl/MuteCMD.java | 2 +- .../java/dev/plex/command/impl/NotesCMD.java | 75 +++++++++++++------ .../dev/plex/event/PunishedPlayerEvent.java | 2 +- .../plex/listener/impl/PlayerListener.java | 2 +- src/main/java/dev/plex/player/PlexPlayer.java | 12 +-- .../plex/punishment/PunishmentManager.java | 6 +- 11 files changed, 70 insertions(+), 43 deletions(-) diff --git a/src/main/java/dev/plex/Plex.java b/src/main/java/dev/plex/Plex.java index bb36c2f..e8c16ef 100644 --- a/src/main/java/dev/plex/Plex.java +++ b/src/main/java/dev/plex/Plex.java @@ -177,7 +177,7 @@ public class Plex extends JavaPlugin if (plugin.getRankManager().isAdmin(plexPlayer)) { - plugin.getAdminList().removeFromCache(UUID.fromString(plexPlayer.getUuid())); + plugin.getAdminList().removeFromCache(plexPlayer.getUuid()); } if (mongoPlayerData != null) //back to mongo checking @@ -215,7 +215,7 @@ public class Plex extends JavaPlugin PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache if (plugin.getRankManager().isAdmin(plexPlayer)) { - Admin admin = new Admin(UUID.fromString(plexPlayer.getUuid())); + Admin admin = new Admin(plexPlayer.getUuid()); admin.setRank(plexPlayer.getRankFromString()); plugin.getAdminList().addToCache(admin); diff --git a/src/main/java/dev/plex/cache/player/SQLPlayerData.java b/src/main/java/dev/plex/cache/player/SQLPlayerData.java index 62f198a..95df6ac 100644 --- a/src/main/java/dev/plex/cache/player/SQLPlayerData.java +++ b/src/main/java/dev/plex/cache/player/SQLPlayerData.java @@ -209,7 +209,7 @@ public class SQLPlayerData statement.setLong(6, player.getCoins()); statement.setBoolean(7, player.isVanished()); statement.setBoolean(8, player.isCommandSpy()); - statement.setString(9, player.getUuid()); + statement.setString(9, player.getUuid().toString()); statement.executeUpdate(); } catch (SQLException throwables) @@ -229,7 +229,7 @@ public class SQLPlayerData try (Connection con = Plex.get().getSqlConnection().getCon()) { PreparedStatement statement = con.prepareStatement(INSERT); - statement.setString(1, player.getUuid()); + statement.setString(1, player.getUuid().toString()); statement.setString(2, player.getName()); statement.setString(3, player.getLoginMessage()); statement.setString(4, player.getPrefix()); diff --git a/src/main/java/dev/plex/cache/sql/SQLNotes.java b/src/main/java/dev/plex/cache/sql/SQLNotes.java index 91de294..f3cb037 100644 --- a/src/main/java/dev/plex/cache/sql/SQLNotes.java +++ b/src/main/java/dev/plex/cache/sql/SQLNotes.java @@ -79,7 +79,7 @@ public class SQLNotes try (Connection con = Plex.get().getSqlConnection().getCon()) { PreparedStatement statement = con.prepareStatement(INSERT); - statement.setInt(1, notes.size()); + statement.setInt(1, notes.size() + 1); statement.setString(2, note.getUuid().toString()); statement.setString(3, note.getWrittenBy().toString()); statement.setString(4, note.getNote()); diff --git a/src/main/java/dev/plex/command/impl/FreezeCMD.java b/src/main/java/dev/plex/command/impl/FreezeCMD.java index c29a239..5a9e7b1 100644 --- a/src/main/java/dev/plex/command/impl/FreezeCMD.java +++ b/src/main/java/dev/plex/command/impl/FreezeCMD.java @@ -50,7 +50,7 @@ public class FreezeCMD extends PlexCommand } } - Punishment punishment = new Punishment(UUID.fromString(punishedPlayer.getUuid()), getUUID(sender)); + Punishment punishment = new Punishment(punishedPlayer.getUuid(), getUUID(sender)); punishment.setCustomTime(false); LocalDateTime date = LocalDateTime.now(); punishment.setEndDate(date.plusMinutes(5)); diff --git a/src/main/java/dev/plex/command/impl/ListCMD.java b/src/main/java/dev/plex/command/impl/ListCMD.java index bebbbfd..0ca1355 100644 --- a/src/main/java/dev/plex/command/impl/ListCMD.java +++ b/src/main/java/dev/plex/command/impl/ListCMD.java @@ -15,7 +15,7 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -@CommandParameters(name = "list", description = "Show a list of all online players") +@CommandParameters(name = "list", description = "Show a list of all online players", aliases = "lsit") @CommandPermissions(level = Rank.OP, permission = "plex.list") @System(value = "ranks") public class ListCMD extends PlexCommand diff --git a/src/main/java/dev/plex/command/impl/MuteCMD.java b/src/main/java/dev/plex/command/impl/MuteCMD.java index 429466c..e300c19 100644 --- a/src/main/java/dev/plex/command/impl/MuteCMD.java +++ b/src/main/java/dev/plex/command/impl/MuteCMD.java @@ -50,7 +50,7 @@ public class MuteCMD extends PlexCommand } } - Punishment punishment = new Punishment(UUID.fromString(punishedPlayer.getUuid()), getUUID(sender)); + Punishment punishment = new Punishment(punishedPlayer.getUuid(), getUUID(sender)); punishment.setCustomTime(false); LocalDateTime date = LocalDateTime.now(); punishment.setEndDate(date.plusMinutes(5)); diff --git a/src/main/java/dev/plex/command/impl/NotesCMD.java b/src/main/java/dev/plex/command/impl/NotesCMD.java index 22f233e..e0a8c04 100644 --- a/src/main/java/dev/plex/command/impl/NotesCMD.java +++ b/src/main/java/dev/plex/command/impl/NotesCMD.java @@ -1,28 +1,24 @@ package dev.plex.command.impl; -import com.google.common.collect.ImmutableList; import dev.plex.cache.DataUtils; import dev.plex.command.PlexCommand; import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandPermissions; -import dev.plex.command.annotation.System; import dev.plex.player.PlexPlayer; import dev.plex.punishment.extra.Note; import dev.plex.rank.enums.Rank; -import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.UUID; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; -import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -49,15 +45,23 @@ public class NotesCMD extends PlexCommand { case "list": { - Component noteList = Component.text("Player notes for: " + plexPlayer.getName()).color(NamedTextColor.GREEN); - /*for (Note note : plugin.getSqlNotes().getNotes(UUID.fromString(plexPlayer.getUuid()))) + plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) -> { - PlexLog.debug("We got here"); - Component noteLine = Component.text(note.getId() + ". " + note.getWrittenBy() + ": " + note.getNote()).color(NamedTextColor.GOLD); - noteList.append(Component.empty()); - noteList.append(noteLine); - }*/ - send(sender, noteList); + if (notes.size() == 0) + { + send(sender, mmString("This player has no notes!")); + return; + } + AtomicReference noteList = new AtomicReference<>(Component.text("Player notes for: " + plexPlayer.getName()).color(NamedTextColor.GREEN)); + for (Note note : notes) + { + Component noteLine = Component.text(note.getId() + " - Written by: " + DataUtils.getPlayer(note.getWrittenBy()).getName() + " on " + DATE_FORMAT.format(note.getTimestamp())).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false); + noteLine = noteLine.append(Component.text(note.getNote())).color(NamedTextColor.GOLD).decoration(TextDecoration.ITALIC, true); + noteList.set(noteList.get().append(Component.newline())); + noteList.set(noteList.get().append(noteLine)); + } + send(sender, noteList.get()); + }); return null; } case "add": @@ -69,7 +73,7 @@ public class NotesCMD extends PlexCommand String content = StringUtils.join(ArrayUtils.subarray(args, 2, args.length), " "); if (playerSender != null) { - Note note = new Note(UUID.fromString(plexPlayer.getUuid()), content, playerSender.getUniqueId(), LocalDateTime.now()); + Note note = new Note(plexPlayer.getUuid(), content, playerSender.getUniqueId(), LocalDateTime.now()); plexPlayer.getNotes().add(note); plugin.getSqlNotes().addNote(note); return Component.text("Note added.").color(NamedTextColor.GREEN); @@ -77,19 +81,42 @@ public class NotesCMD extends PlexCommand } case "remove": { - return null; + int id; + try + { + id = Integer.parseInt(args[2]); + } + catch (NumberFormatException ignored) + { + return Component.text("Invalid number: " + args[2]).color(NamedTextColor.RED); + } + plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) -> + { + for (Note note : notes) + { + if (note.getId() == id) + { + plugin.getSqlNotes().deleteNote(id, plexPlayer.getUuid()).whenComplete((notes1, ex1) -> + send(sender, Component.text("Removed note with ID: " + id).color(NamedTextColor.GREEN))); + } + else + { + send(sender, mmString("A note with this ID could not be found")); + } + } + }); } case "clear": { - int count = plexPlayer.getNotes().size(); - final List notes = plexPlayer.getNotes(); - for (Note note : notes) + plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) -> { - plexPlayer.getNotes().remove(note); - count++; - } - DataUtils.update(plexPlayer); - return Component.text("Cleared " + count + " note(s).").color(NamedTextColor.GREEN); + for (Note note : notes) + { + plugin.getSqlNotes().deleteNote(note.getId(), plexPlayer.getUuid()); + } + send(sender, Component.text("Cleared " + notes.size() + " note(s).").color(NamedTextColor.GREEN)); + }); + return null; } default: { diff --git a/src/main/java/dev/plex/event/PunishedPlayerEvent.java b/src/main/java/dev/plex/event/PunishedPlayerEvent.java index 198ce44..92bf4e7 100644 --- a/src/main/java/dev/plex/event/PunishedPlayerEvent.java +++ b/src/main/java/dev/plex/event/PunishedPlayerEvent.java @@ -33,7 +33,7 @@ public abstract class PunishedPlayerEvent extends PlayerEvent implements Cancell */ protected PunishedPlayerEvent(PlexPlayer punishedPlayer) { - super(Bukkit.getPlayer(UUID.fromString(punishedPlayer.getUuid()))); + super(Bukkit.getPlayer(punishedPlayer.getUuid())); this.punishedPlayer = punishedPlayer; } } \ No newline at end of file diff --git a/src/main/java/dev/plex/listener/impl/PlayerListener.java b/src/main/java/dev/plex/listener/impl/PlayerListener.java index 7bab735..fcf332a 100644 --- a/src/main/java/dev/plex/listener/impl/PlayerListener.java +++ b/src/main/java/dev/plex/listener/impl/PlayerListener.java @@ -92,7 +92,7 @@ public class PlayerListener extends PlexListener if (plugin.getRankManager().isAdmin(plexPlayer)) { - plugin.getAdminList().removeFromCache(UUID.fromString(plexPlayer.getUuid())); + plugin.getAdminList().removeFromCache(plexPlayer.getUuid()); } DataUtils.update(plexPlayer); diff --git a/src/main/java/dev/plex/player/PlexPlayer.java b/src/main/java/dev/plex/player/PlexPlayer.java index 64b1ba1..413a84d 100644 --- a/src/main/java/dev/plex/player/PlexPlayer.java +++ b/src/main/java/dev/plex/player/PlexPlayer.java @@ -38,7 +38,7 @@ public class PlexPlayer @Setter(AccessLevel.NONE) @Indexed(options = @IndexOptions(unique = true)) - private String uuid; + private UUID uuid; @Indexed private String name; @@ -69,9 +69,9 @@ public class PlexPlayer public PlexPlayer(UUID playerUUID) { - this.uuid = playerUUID.toString(); + this.uuid = playerUUID; - this.id = uuid.substring(0, 8); + this.id = uuid.toString().substring(0, 8); this.name = ""; this.player = Bukkit.getPlayer(name); @@ -95,7 +95,7 @@ public class PlexPlayer public Rank getRankFromString() { - OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid)); + OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); if (rank.isEmpty()) { if (player.isOp()) @@ -117,7 +117,7 @@ public class PlexPlayer { if (Plex.get().getStorageType() != StorageType.MONGODB) { - this.setPunishments(Plex.get().getSqlPunishment().getPunishments(UUID.fromString(this.getUuid())).stream().filter(punishment -> punishment.getPunished().equals(UUID.fromString(this.getUuid()))).collect(Collectors.toList())); + this.setPunishments(Plex.get().getSqlPunishment().getPunishments(this.getUuid()).stream().filter(punishment -> punishment.getPunished().equals(this.getUuid())).collect(Collectors.toList())); } } @@ -125,7 +125,7 @@ public class PlexPlayer { if (Plex.get().getStorageType() != StorageType.MONGODB) { - return Plex.get().getSqlNotes().getNotes(UUID.fromString(this.getUuid())); + return Plex.get().getSqlNotes().getNotes(this.getUuid()); } return null; } diff --git a/src/main/java/dev/plex/punishment/PunishmentManager.java b/src/main/java/dev/plex/punishment/PunishmentManager.java index 978a0d6..b16235d 100644 --- a/src/main/java/dev/plex/punishment/PunishmentManager.java +++ b/src/main/java/dev/plex/punishment/PunishmentManager.java @@ -140,7 +140,7 @@ public class PunishmentManager extends PlexBase public boolean isBanned(PlexPlayer player) { - return isBanned(UUID.fromString(player.getUuid())); + return isBanned(player.getUuid()); } public CompletableFuture> getActiveBans() @@ -206,7 +206,7 @@ public class PunishmentManager extends PlexBase return; } player.setFrozen(false); - Bukkit.broadcast(PlexUtils.messageComponent("unfrozePlayer", "Plex", Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())).getName())); + Bukkit.broadcast(PlexUtils.messageComponent("unfrozePlayer", "Plex", Bukkit.getOfflinePlayer(player.getUuid()).getName())); } }.runTaskLater(Plex.get(), 20 * seconds); } @@ -227,7 +227,7 @@ public class PunishmentManager extends PlexBase return; } player.setMuted(false); - Bukkit.broadcast(PlexUtils.messageComponent("unmutedPlayer", "Plex", Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())).getName())); + Bukkit.broadcast(PlexUtils.messageComponent("unmutedPlayer", "Plex", Bukkit.getOfflinePlayer(player.getUuid()).getName())); } }.runTaskLater(Plex.get(), 20 * seconds); } From 1f653ab5df85f8101b57e3b10525810de30fb149 Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 6 Apr 2022 20:55:54 -0700 Subject: [PATCH 2/2] Add /plex update --- .../java/dev/plex/command/impl/PlexCMD.java | 11 +++++ .../java/dev/plex/util/UpdateChecker.java | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index 4031956..c2932c5 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -15,9 +15,11 @@ import java.util.List; import java.util.stream.Collectors; import dev.plex.util.PlexLog; +import dev.plex.util.PlexUtils; import net.kyori.adventure.text.Component; import org.apache.commons.lang.StringUtils; import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -88,6 +90,15 @@ public class PlexCMD extends PlexCommand return componentFromString("All modules reloaded!"); } } + else if (args[0].equalsIgnoreCase("update")) + { + if (sender instanceof Player player && !PlexUtils.DEVELOPERS.contains(player.getUniqueId().toString())) + { + return messageComponent("noPermissionConsole"); + } + plugin.getUpdateChecker().updateJar(); + return null; + } else { return usage(); diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index 8d37e02..bb8c855 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -7,14 +7,26 @@ import com.google.gson.JsonSyntaxException; import dev.plex.Plex; import dev.plex.PlexBase; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.CompletableFuture; import javax.annotation.Nonnull; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import org.apache.commons.io.FileUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.bukkit.command.CommandSender; +import org.bukkit.util.FileUtil; +import org.json.JSONArray; +import org.json.JSONObject; public class UpdateChecker extends PlexBase { @@ -107,8 +119,39 @@ public class UpdateChecker extends PlexBase default -> { sender.sendMessage(Component.text("Your version of Plex is not up to date!", NamedTextColor.RED)); sender.sendMessage(Component.text("Download a new version at: " + DOWNLOAD_PAGE).color(NamedTextColor.RED)); + sender.sendMessage(Component.text("Or run: /plex update").color(NamedTextColor.RED)); return true; } } } + + public void updateJar() + { + CloseableHttpClient client = HttpClients.createDefault(); + HttpGet get = new HttpGet(DOWNLOAD_PAGE + "lastSuccessfulBuild/api/json"); + try + { + HttpResponse response = client.execute(get); + JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); + JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0); + String name = artifact.getString("displayPath"); + PlexLog.log("Downloading latest Plex jar file: " + name); + CompletableFuture.runAsync(() -> { + try + { + FileUtils.copyURLToFile( + new URL(DOWNLOAD_PAGE + "lastSuccessfulBuild/artifact/build/libs/" + name), + new File(Plex.get().getDataFolder() + File.separator + "..", name) + ); + PlexLog.log("Saved new jar. Please restart your server."); + } catch (IOException e) + { + e.printStackTrace(); + } + }); + } catch (IOException e) + { + e.printStackTrace(); + } + } }