diff --git a/src/main/java/dev/plex/cache/sql/SQLNotes.java b/src/main/java/dev/plex/cache/sql/SQLNotes.java index eebe4d2..a3cc848 100644 --- a/src/main/java/dev/plex/cache/sql/SQLNotes.java +++ b/src/main/java/dev/plex/cache/sql/SQLNotes.java @@ -20,7 +20,7 @@ public class SQLNotes { private static final String SELECT = "SELECT * FROM `notes` WHERE uuid=?"; - private static final String INSERT = "INSERT INTO `notes` (`uuid`, `written_by`, `note`, timestamp`) VALUES(?, ?, ?, ?)"; + private static final String INSERT = "INSERT INTO `notes` (`uuid`, `written_by`, `note`, `timestamp`) VALUES(?, ?, ?, ?)"; private static final String DELETE = "DELETE FROM `notes` WHERE uuid=? AND note=?"; public CompletableFuture> getNotes(UUID uuid) @@ -43,7 +43,8 @@ public class SQLNotes ); notes.add(note); } - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } @@ -63,7 +64,8 @@ public class SQLNotes statement.setString(3, note.getNote()); statement.setLong(4, note.getTimestamp().toInstant(ZoneOffset.UTC).toEpochMilli()); statement.execute(); - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/dev/plex/command/impl/NotesCMD.java b/src/main/java/dev/plex/command/impl/NotesCMD.java new file mode 100644 index 0000000..f5b84fb --- /dev/null +++ b/src/main/java/dev/plex/command/impl/NotesCMD.java @@ -0,0 +1,112 @@ +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.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 net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +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; +import org.jetbrains.annotations.Nullable; + +@CommandParameters(name = "notes", description = "Manage notes for a player", usage = "/ | remove | clear>") +@CommandPermissions(level = Rank.ADMIN, permission = "plex.notes") +public class NotesCMD extends PlexCommand +{ + private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a"); + + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) + { + if (args.length < 2) + { + return usage(); + } + + Player player = getNonNullPlayer(args[0]); + PlexPlayer plexPlayer = getPlexPlayer(player); + + switch (args[1].toLowerCase()) + { + case "list": + { + Component noteList = Component.text("Player notes for: " + plexPlayer.getName()).color(NamedTextColor.GREEN); + int id = 1; + for (Note note : plexPlayer.getNotes()) + { + Component noteLine = Component.text(id + ". " + note.getWrittenBy() + ": " + note.getNote()).color(NamedTextColor.GOLD); + noteList.append(Component.empty()).append(noteLine); + id++; + } + send(sender, noteList); + return null; + } + case "add": + { + if (args.length < 3) + { + return usage(); + } + 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()); + plexPlayer.getNotes().add(note); + plugin.getSqlNotes().addNote(note); + return Component.text("Note added.").color(NamedTextColor.GREEN); + } + } + case "remove": + { + return null; + } + case "clear": + { + int count = plexPlayer.getNotes().size(); + final List notes = plexPlayer.getNotes(); + for (Note note : notes) + { + plexPlayer.getNotes().remove(note); + count++; + } + DataUtils.update(plexPlayer); + return Component.text("Cleared " + count + " note(s).").color(NamedTextColor.GREEN); + } + default: + { + return usage(); + } + } + } + + @Override + public @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException + { + if (args.length == 1) + { + return PlexUtils.getPlayerNameList(); + } + if (args.length == 2) + { + return Arrays.asList("list", "add", "remove", "clear"); + } + return Collections.emptyList(); + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/storage/SQLConnection.java b/src/main/java/dev/plex/storage/SQLConnection.java index 87cdee6..1177961 100644 --- a/src/main/java/dev/plex/storage/SQLConnection.java +++ b/src/main/java/dev/plex/storage/SQLConnection.java @@ -77,6 +77,7 @@ public class SQLConnection extends PlexBase "`endDate` BIGINT" + ");").execute(); con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" + + "`id` INT NOT NULL AUTO_INCREMENT, " + "`uuid` VARCHAR(46) NOT NULL, " + "`written_by` VARCHAR(16), " + "`note` VARCHAR(2000), " +