diff --git a/src/main/java/dev/plex/Plex.java b/src/main/java/dev/plex/Plex.java index 0469381..bb36c2f 100644 --- a/src/main/java/dev/plex/Plex.java +++ b/src/main/java/dev/plex/Plex.java @@ -6,6 +6,7 @@ import dev.plex.cache.DataUtils; import dev.plex.cache.player.MongoPlayerData; import dev.plex.cache.player.PlayerCache; import dev.plex.cache.player.SQLPlayerData; +import dev.plex.cache.sql.SQLNotes; import dev.plex.cache.sql.SQLPunishment; import dev.plex.config.Config; import dev.plex.handlers.CommandHandler; @@ -23,37 +24,46 @@ import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; import dev.plex.util.UpdateChecker; import dev.plex.world.CustomWorld; -import java.io.File; -import java.io.InputStream; -import java.util.Properties; -import java.util.UUID; import lombok.Getter; import lombok.Setter; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; +import java.io.InputStream; +import java.util.Properties; +import java.util.UUID; + @Getter @Setter public class Plex extends JavaPlugin { public static final BuildProperties build = new BuildProperties(); private static Plex plugin; + public Config config; public Config messages; public Config indefBans; + public File modulesFolder; private StorageType storageType = StorageType.SQLITE; + private SQLConnection sqlConnection; private MongoConnection mongoConnection; private RedisConnection redisConnection; + private MongoPlayerData mongoPlayerData; private SQLPlayerData sqlPlayerData; + private SQLPunishment sqlPunishment; + private SQLNotes sqlNotes; + private ModuleManager moduleManager; private RankManager rankManager; private ServiceManager serviceManager; private PunishmentManager punishmentManager; + private AdminList adminList; private UpdateChecker updateChecker; private String system; @@ -103,8 +113,7 @@ public class Plex extends JavaPlugin { PlexUtils.testConnections(); PlexLog.log("Connected to " + storageType.name().toUpperCase()); - } - catch (Exception e) + } catch (Exception e) { PlexLog.error("Failed to connect to " + storageType.name().toUpperCase()); e.printStackTrace(); @@ -121,8 +130,7 @@ public class Plex extends JavaPlugin { redisConnection.getJedis(); PlexLog.log("Connected to Redis!"); - } - else + } else { PlexLog.log("Redis is disabled in the configuration file, not connecting."); } @@ -130,11 +138,11 @@ public class Plex extends JavaPlugin if (storageType == StorageType.MONGODB) { mongoPlayerData = new MongoPlayerData(); - } - else + } else { sqlPlayerData = new SQLPlayerData(); sqlPunishment = new SQLPunishment(); + sqlNotes = new SQLNotes(); } new ListenerHandler(); @@ -175,8 +183,7 @@ public class Plex extends JavaPlugin if (mongoPlayerData != null) //back to mongo checking { mongoPlayerData.update(plexPlayer); //update the player's document - } - else if (sqlPlayerData != null) //sql checking + } else if (sqlPlayerData != null) //sql checking { sqlPlayerData.update(plexPlayer); } @@ -239,8 +246,7 @@ public class Plex extends JavaPlugin author = props.getProperty("buildAuthor", "unknown"); date = props.getProperty("buildDate", "unknown"); head = props.getProperty("buildHead", "unknown"); - } - catch (Exception ex) + } catch (Exception ex) { PlexLog.error("Could not load build properties! Did you compile with NetBeans/Maven?"); } diff --git a/src/main/java/dev/plex/cache/sql/SQLNotes.java b/src/main/java/dev/plex/cache/sql/SQLNotes.java new file mode 100644 index 0000000..eebe4d2 --- /dev/null +++ b/src/main/java/dev/plex/cache/sql/SQLNotes.java @@ -0,0 +1,72 @@ +package dev.plex.cache.sql; + +import com.google.common.collect.Lists; +import dev.plex.Plex; +import dev.plex.punishment.extra.Note; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +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=?"; + + public CompletableFuture> getNotes(UUID uuid) + { + return CompletableFuture.supplyAsync(() -> + { + List notes = Lists.newArrayList(); + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(SELECT); + statement.setString(1, uuid.toString()); + ResultSet set = statement.executeQuery(); + while (set.next()) + { + Note note = new Note( + uuid, + set.getString("note"), + UUID.fromString(set.getString("written_by")), + LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("timestamp")), ZoneId.systemDefault()) + ); + notes.add(note); + } + } catch (SQLException e) + { + e.printStackTrace(); + } + return notes; + }); + } + + public CompletableFuture addNote(Note note) + { + 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()); + statement.execute(); + } catch (SQLException e) + { + e.printStackTrace(); + } + }); + } +} diff --git a/src/main/java/dev/plex/listener/impl/PlayerListener.java b/src/main/java/dev/plex/listener/impl/PlayerListener.java index 16ab66b..7bab735 100644 --- a/src/main/java/dev/plex/listener/impl/PlayerListener.java +++ b/src/main/java/dev/plex/listener/impl/PlayerListener.java @@ -78,6 +78,10 @@ public class PlayerListener extends PlexListener { PlexUtils.broadcast(MiniMessage.miniMessage().deserialize("" + player.getName() + " is " + loginMessage)); } + + plexPlayer.loadNotes().whenComplete((notes, throwable) -> { + //TODO: Send note messages to admins + }); } // saving the player's data diff --git a/src/main/java/dev/plex/player/PlexPlayer.java b/src/main/java/dev/plex/player/PlexPlayer.java index 1b99f35..64b1ba1 100644 --- a/src/main/java/dev/plex/player/PlexPlayer.java +++ b/src/main/java/dev/plex/player/PlexPlayer.java @@ -1,17 +1,24 @@ package dev.plex.player; import com.google.common.collect.Lists; +import com.google.gson.GsonBuilder; import dev.morphia.annotations.Entity; import dev.morphia.annotations.Id; import dev.morphia.annotations.IndexOptions; import dev.morphia.annotations.Indexed; import dev.plex.Plex; import dev.plex.punishment.Punishment; +import dev.plex.punishment.extra.Note; import dev.plex.rank.enums.Rank; import dev.plex.storage.StorageType; + +import java.time.LocalDateTime; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; + +import dev.plex.util.adapter.LocalDateTimeSerializer; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; @@ -54,6 +61,7 @@ public class PlexPlayer private List ips = Lists.newArrayList(); private List punishments = Lists.newArrayList(); + private List notes = Lists.newArrayList(); public PlexPlayer() { @@ -112,4 +120,18 @@ public class PlexPlayer this.setPunishments(Plex.get().getSqlPunishment().getPunishments(UUID.fromString(this.getUuid())).stream().filter(punishment -> punishment.getPunished().equals(UUID.fromString(this.getUuid()))).collect(Collectors.toList())); } } + + public CompletableFuture> loadNotes() + { + if (Plex.get().getStorageType() != StorageType.MONGODB) + { + return Plex.get().getSqlNotes().getNotes(UUID.fromString(this.getUuid())); + } + return null; + } + + public String toJSON() + { + return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).create().toJson(this); + } } diff --git a/src/main/java/dev/plex/punishment/extra/Note.java b/src/main/java/dev/plex/punishment/extra/Note.java new file mode 100644 index 0000000..31ea9b8 --- /dev/null +++ b/src/main/java/dev/plex/punishment/extra/Note.java @@ -0,0 +1,22 @@ +package dev.plex.punishment.extra; + +import com.google.gson.GsonBuilder; +import dev.plex.util.adapter.LocalDateTimeSerializer; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.UUID; + +@Data +public class Note +{ + private final UUID uuid; + private final String note; + private final UUID writtenBy; + private final LocalDateTime timestamp; + + 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 5ea2edf..0c9b575 100644 --- a/src/main/java/dev/plex/services/AbstractService.java +++ b/src/main/java/dev/plex/services/AbstractService.java @@ -1,25 +1,21 @@ package dev.plex.services; import dev.plex.PlexBase; +import lombok.Getter; +import lombok.Setter; +@Getter public abstract class AbstractService extends PlexBase implements IService { private boolean asynchronous; private boolean repeating; + @Setter + private int taskId; + public AbstractService(boolean repeating, boolean async) { this.repeating = repeating; this.asynchronous = async; } - - public boolean isRepeating() - { - return repeating; - } - - public boolean isAsynchronous() - { - return asynchronous; - } } diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 4577ad7..7a43fb4 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -5,8 +5,10 @@ import dev.plex.Plex; import dev.plex.services.impl.BanService; import dev.plex.services.impl.GameRuleService; import dev.plex.services.impl.UpdateCheckerService; -import java.util.List; import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitTask; + +import java.util.List; public class ServiceManager { @@ -25,19 +27,47 @@ public class ServiceManager { if (!service.isRepeating()) { - Bukkit.getScheduler().runTask(Plex.get(), service::run); - } - else if (service.isRepeating() && service.isAsynchronous()) + BukkitTask task = Bukkit.getScheduler().runTask(Plex.get(), service::run); + service.setTaskId(task.getTaskId()); + } else if (service.isRepeating() && service.isAsynchronous()) { - Bukkit.getScheduler().runTaskTimerAsynchronously(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); - } - 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()) { - Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); + BukkitTask task = Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); + service.setTaskId(task.getTaskId()); } } } + public AbstractService getService(Class clazz) + { + return services.stream().filter(service -> service.getClass().isAssignableFrom(clazz)).findFirst().orElse(null); + } + + public void startService(AbstractService service) + { + 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()); + } + } + + public void endService(AbstractService service) + { + Bukkit.getScheduler().cancelTask(service.getTaskId()); + } + private void registerService(AbstractService service) { services.add(service); diff --git a/src/main/java/dev/plex/storage/SQLConnection.java b/src/main/java/dev/plex/storage/SQLConnection.java index 07ec342..87cdee6 100644 --- a/src/main/java/dev/plex/storage/SQLConnection.java +++ b/src/main/java/dev/plex/storage/SQLConnection.java @@ -78,9 +78,9 @@ public class SQLConnection extends PlexBase ");").execute(); con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" + "`uuid` VARCHAR(46) NOT NULL, " + - "`name` VARCHAR(18), " + "`written_by` VARCHAR(16), " + "`note` VARCHAR(2000), " + + "`timestamp` BIGINT" + ");").execute(); } catch (SQLException throwables)