diff --git a/src/main/java/dev/plex/cache/sql/SQLNotes.java b/src/main/java/dev/plex/cache/sql/SQLNotes.java index a3cc848..4dcd30a 100644 --- a/src/main/java/dev/plex/cache/sql/SQLNotes.java +++ b/src/main/java/dev/plex/cache/sql/SQLNotes.java @@ -20,8 +20,8 @@ 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 DELETE = "DELETE FROM `notes` WHERE uuid=? AND note=?"; + private static final String INSERT = "INSERT INTO `notes` (`id`, `uuid`, `written_by`, `note`, `timestamp`) VALUES(?, ?, ?, ?, ?)"; + private static final String DELETE = "DELETE FROM `notes` WHERE uuid=? AND id=?"; public CompletableFuture> getNotes(UUID uuid) { @@ -41,10 +41,10 @@ public class SQLNotes UUID.fromString(set.getString("written_by")), LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("timestamp")), ZoneId.systemDefault()) ); + note.setId(set.getInt("id")); notes.add(note); } - } - catch (SQLException e) + } catch (SQLException e) { e.printStackTrace(); } @@ -52,23 +52,46 @@ public class SQLNotes }); } - public CompletableFuture addNote(Note note) + public CompletableFuture deleteNote(int id, UUID uuid) { return CompletableFuture.runAsync(() -> { try (Connection con = Plex.get().getSqlConnection().getCon()) { - PreparedStatement statement = con.prepareStatement(INSERT); - statement.setString(1, note.getUuid().toString()); - statement.setString(2, note.getWrittenBy().toString()); - statement.setString(3, note.getNote()); - statement.setLong(4, note.getTimestamp().toInstant(ZoneOffset.UTC).toEpochMilli()); + PreparedStatement statement = con.prepareStatement(DELETE); + statement.setString(1, uuid.toString()); + statement.setInt(2, id); statement.execute(); - } - catch (SQLException e) + } catch (SQLException e) { e.printStackTrace(); } }); } + + public CompletableFuture addNote(Note note) + { + return CompletableFuture.runAsync(() -> + { + getNotes(note.getUuid()).whenComplete((notes, throwable) -> + { + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(INSERT); + statement.setInt(1, notes.size()); + statement.setString(2, note.getUuid().toString()); + statement.setString(3, note.getWrittenBy().toString()); + statement.setString(4, note.getNote()); + statement.setLong(5, note.getTimestamp().toInstant(ZoneOffset.UTC).toEpochMilli()); + statement.execute(); + note.setId(notes.size()); + } catch (SQLException e) + { + e.printStackTrace(); + } + + }); + + }); + } } diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index 7c333d2..4031956 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -13,6 +13,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; + +import dev.plex.util.PlexLog; import net.kyori.adventure.text.Component; import org.apache.commons.lang.StringUtils; import org.bukkit.command.CommandSender; @@ -52,6 +54,9 @@ public class PlexCMD extends PlexCommand send(sender, "Imported ranks"); send(sender, "Plex successfully reloaded."); plugin.setSystem(plugin.config.getString("system")); + plugin.getServiceManager().endServices(); + plugin.getServiceManager().startServices(); + PlexLog.debug("Restarted services"); return null; } else if (args[0].equalsIgnoreCase("redis")) diff --git a/src/main/java/dev/plex/punishment/extra/Note.java b/src/main/java/dev/plex/punishment/extra/Note.java index 31ea9b8..411a316 100644 --- a/src/main/java/dev/plex/punishment/extra/Note.java +++ b/src/main/java/dev/plex/punishment/extra/Note.java @@ -15,6 +15,8 @@ public class Note private final UUID writtenBy; private final LocalDateTime timestamp; + private int id; // This will be automatically set from addNote + public String toJSON() { return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).create().toJson(this); diff --git a/src/main/java/dev/plex/services/AbstractService.java b/src/main/java/dev/plex/services/AbstractService.java index 0c9b575..45b4af2 100644 --- a/src/main/java/dev/plex/services/AbstractService.java +++ b/src/main/java/dev/plex/services/AbstractService.java @@ -18,4 +18,14 @@ public abstract class AbstractService extends PlexBase implements IService this.repeating = repeating; this.asynchronous = async; } + + public void onStart() + { + + } + + public void onEnd() + { + + } } diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 9f030c3..90516eb 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -27,22 +27,15 @@ public class ServiceManager { for (AbstractService service : services) { - if (!service.isRepeating()) - { - BukkitTask task = Bukkit.getScheduler().runTask(Plex.get(), service::run); - service.setTaskId(task.getTaskId()); - } else if (service.isRepeating() && service.isAsynchronous()) - { - BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); - service.setTaskId(task.getTaskId()); - } else if (service.isRepeating() && !service.isAsynchronous()) - { - BukkitTask task = Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); - service.setTaskId(task.getTaskId()); - } + startService(service); } } + public void endServices() + { + services.forEach(this::endService); + } + public AbstractService getService(Class clazz) { return services.stream().filter(service -> service.getClass().isAssignableFrom(clazz)).findFirst().orElse(null); @@ -63,11 +56,26 @@ public class ServiceManager BukkitTask task = Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); service.setTaskId(task.getTaskId()); } + if (!services.contains(service)) + { + services.add(service); + } + service.onStart(); + } + + public void endService(AbstractService service, boolean remove) + { + Bukkit.getScheduler().cancelTask(service.getTaskId()); + service.onEnd(); + if (remove) + { + services.remove(service); + } } public void endService(AbstractService service) { - Bukkit.getScheduler().cancelTask(service.getTaskId()); + endService(service, false); } private void registerService(AbstractService service) diff --git a/src/main/java/dev/plex/storage/SQLConnection.java b/src/main/java/dev/plex/storage/SQLConnection.java index 1177961..c2bc826 100644 --- a/src/main/java/dev/plex/storage/SQLConnection.java +++ b/src/main/java/dev/plex/storage/SQLConnection.java @@ -77,7 +77,7 @@ public class SQLConnection extends PlexBase "`endDate` BIGINT" + ");").execute(); con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" + - "`id` INT NOT NULL AUTO_INCREMENT, " + + "`id` INT NOT NULL, " + "`uuid` VARCHAR(46) NOT NULL, " + "`written_by` VARCHAR(16), " + "`note` VARCHAR(2000), " +