From 1d8ce6001c5502fe01b79217dc41e9ad823a168f Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 6 Apr 2022 16:59:34 -0700 Subject: [PATCH 01/11] add getting players by username --- src/main/java/dev/plex/cache/DataUtils.java | 24 +++++------ .../plex/cache/player/MongoPlayerData.java | 12 ++++++ .../dev/plex/cache/player/SQLPlayerData.java | 43 +++++++++++++++++++ src/main/java/dev/plex/player/PlexPlayer.java | 7 +-- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/main/java/dev/plex/cache/DataUtils.java b/src/main/java/dev/plex/cache/DataUtils.java index fadb12c..b86e73e 100644 --- a/src/main/java/dev/plex/cache/DataUtils.java +++ b/src/main/java/dev/plex/cache/DataUtils.java @@ -55,6 +55,18 @@ public class DataUtils } } + public static PlexPlayer getPlayer(String username) + { + if (Plex.get().getStorageType() == StorageType.MONGODB) + { + return Plex.get().getMongoPlayerData().getByName(username); + } + else + { + return Plex.get().getSqlPlayerData().getByName(username); + } + } + /** * Gets a player from cache or from the database * @@ -80,18 +92,6 @@ public class DataUtils } } - /** - * Gets a player from cache or from the database - * - * @param name Username of the player - * @return a PlexPlayer object - * @see PlexPlayer - */ - public static PlexPlayer getPlayer(String name) - { - return getPlayer(Bukkit.getPlayer(name).getUniqueId()); - } - /** * Updates a player's information in the database * diff --git a/src/main/java/dev/plex/cache/player/MongoPlayerData.java b/src/main/java/dev/plex/cache/player/MongoPlayerData.java index c0e816a..f010153 100644 --- a/src/main/java/dev/plex/cache/player/MongoPlayerData.java +++ b/src/main/java/dev/plex/cache/player/MongoPlayerData.java @@ -61,6 +61,18 @@ public class MongoPlayerData return query2.first(); } + public PlexPlayer getByName(String username) + { + PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null); + if (player != null) + { + return player; + } + + Query query2 = datastore.find(PlexPlayer.class).filter(Filters.regex("name").caseInsensitive().pattern(username)); + return query2.first(); + } + /** * Gets the player from cache or from mongo's database * diff --git a/src/main/java/dev/plex/cache/player/SQLPlayerData.java b/src/main/java/dev/plex/cache/player/SQLPlayerData.java index f082c72..62f198a 100644 --- a/src/main/java/dev/plex/cache/player/SQLPlayerData.java +++ b/src/main/java/dev/plex/cache/player/SQLPlayerData.java @@ -92,6 +92,49 @@ public class SQLPlayerData return null; } + public PlexPlayer getByName(String username) + { + PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null); + if (player != null) + { + return player; + } + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE name=? LIMIT 1"); + statement.setString(1, username); + ResultSet set = statement.executeQuery(); + while (set.next()) + { + PlexPlayer plexPlayer = new PlexPlayer(UUID.fromString(set.getString("uuid"))); + String loginMSG = set.getString("login_msg"); + String prefix = set.getString("prefix"); + String rankName = set.getString("rank").toUpperCase(); + long coins = set.getLong("coins"); + boolean vanished = set.getBoolean("vanished"); + boolean commandspy = set.getBoolean("commandspy"); + List ips = new Gson().fromJson(set.getString("ips"), new TypeToken>() + { + }.getType()); + plexPlayer.setName(username); + plexPlayer.setLoginMessage(loginMSG); + plexPlayer.setPrefix(prefix); + plexPlayer.setRank(rankName); + plexPlayer.setIps(ips); + plexPlayer.setCoins(coins); + plexPlayer.setVanished(vanished); + plexPlayer.setCommandSpy(commandspy); + return plexPlayer; + } + return null; + } + catch (SQLException throwables) + { + throwables.printStackTrace(); + } + return null; + } + /** * Gets the player from cache or from the SQL database * diff --git a/src/main/java/dev/plex/player/PlexPlayer.java b/src/main/java/dev/plex/player/PlexPlayer.java index 5cc18ed..1b99f35 100644 --- a/src/main/java/dev/plex/player/PlexPlayer.java +++ b/src/main/java/dev/plex/player/PlexPlayer.java @@ -43,9 +43,10 @@ public class PlexPlayer private boolean vanished; private boolean commandSpy; - private boolean frozen; - private boolean muted; - private boolean lockedUp; + // These fields are transient so MongoDB doesn't automatically drop them in. + private transient boolean frozen; + private transient boolean muted; + private transient boolean lockedUp; private long coins; From 2105f51b44a128c1ef4ab02ad25e682763c0ccfe Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Wed, 6 Apr 2022 20:16:16 -0400 Subject: [PATCH 02/11] comment out useless teleporting not necessary lol --- src/main/java/dev/plex/command/impl/EntityWipeCMD.java | 2 ++ src/main/java/dev/plex/command/impl/MobPurgeCMD.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/dev/plex/command/impl/EntityWipeCMD.java b/src/main/java/dev/plex/command/impl/EntityWipeCMD.java index c49b687..653bd81 100644 --- a/src/main/java/dev/plex/command/impl/EntityWipeCMD.java +++ b/src/main/java/dev/plex/command/impl/EntityWipeCMD.java @@ -57,9 +57,11 @@ public class EntityWipeCMD extends PlexCommand if (useBlacklist ? entityBlacklist.stream().noneMatch(entityName -> entityName.equalsIgnoreCase(type)) : entityWhitelist.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(type))) { + /* Location loc = entity.getLocation(); loc.setY(-500); entity.teleportAsync(loc); + */ entity.remove(); entityCounts.put(type,entityCounts.getOrDefault(type, 0) + 1); diff --git a/src/main/java/dev/plex/command/impl/MobPurgeCMD.java b/src/main/java/dev/plex/command/impl/MobPurgeCMD.java index c20c629..5078052 100644 --- a/src/main/java/dev/plex/command/impl/MobPurgeCMD.java +++ b/src/main/java/dev/plex/command/impl/MobPurgeCMD.java @@ -34,10 +34,12 @@ public class MobPurgeCMD extends PlexCommand { String type = entity.getType().name(); + /* Location loc = entity.getLocation(); loc.setY(-500); entity.teleportAsync(loc); entity.remove(); + */ entityCounts.put(type,entityCounts.getOrDefault(type, 0) + 1); } From f62e0a42d1c06d3e35a66f548f04a84df3d284c0 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Wed, 6 Apr 2022 19:18:12 -0500 Subject: [PATCH 03/11] i have no idea what im doing --- .../dev/plex/cache/notes/PlayerNotes.java | 44 +++++++++++++++++++ .../java/dev/plex/storage/SQLConnection.java | 6 +++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/dev/plex/cache/notes/PlayerNotes.java diff --git a/src/main/java/dev/plex/cache/notes/PlayerNotes.java b/src/main/java/dev/plex/cache/notes/PlayerNotes.java new file mode 100644 index 0000000..658d455 --- /dev/null +++ b/src/main/java/dev/plex/cache/notes/PlayerNotes.java @@ -0,0 +1,44 @@ +package dev.plex.cache.notes; + +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import dev.plex.Plex; +import dev.plex.cache.player.PlayerCache; +import dev.plex.player.PlexPlayer; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.UUID; + +public class PlayerNotes +{ + private final String SELECT = "SELECT * FROM `notes` WHERE uuid=?"; + //private final String UPDATE = "UPDATE `notes` SET name=?, written_by=?, note=? WHERE uuid=?"; + private final String INSERT = "INSERT INTO `notes` (`uuid`, `name`, `written_by`, `note`) VALUES (?, ?, ?, ?);"; + + public PlexPlayer getByUUID(UUID uuid) + { + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(SELECT); + statement.setString(1, uuid.toString()); + ResultSet set = statement.executeQuery(); + PlexPlayer plexPlayer = new PlexPlayer(uuid); + while (set.next()) + { + String name = set.getString("name"); + String writtenBy = set.getString("written_by"); + String note = set.getString("note"); + + } + return plexPlayer; + } + catch (SQLException throwables) + { + throwables.printStackTrace(); + } + return null; + } +} diff --git a/src/main/java/dev/plex/storage/SQLConnection.java b/src/main/java/dev/plex/storage/SQLConnection.java index 79e0d26..07ec342 100644 --- a/src/main/java/dev/plex/storage/SQLConnection.java +++ b/src/main/java/dev/plex/storage/SQLConnection.java @@ -76,6 +76,12 @@ public class SQLConnection extends PlexBase "`active` BOOLEAN, " + "`endDate` BIGINT" + ");").execute(); + con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" + + "`uuid` VARCHAR(46) NOT NULL, " + + "`name` VARCHAR(18), " + + "`written_by` VARCHAR(16), " + + "`note` VARCHAR(2000), " + + ");").execute(); } catch (SQLException throwables) { From f2ee3bf7a5465c7be4c43aa6850703c2faca3d92 Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 6 Apr 2022 17:38:15 -0700 Subject: [PATCH 04/11] begin notes system store task id --- src/main/java/dev/plex/Plex.java | 34 +++++---- .../java/dev/plex/cache/sql/SQLNotes.java | 72 +++++++++++++++++++ .../plex/listener/impl/PlayerListener.java | 4 ++ src/main/java/dev/plex/player/PlexPlayer.java | 22 ++++++ .../java/dev/plex/punishment/extra/Note.java | 22 ++++++ .../dev/plex/services/AbstractService.java | 16 ++--- .../dev/plex/services/ServiceManager.java | 46 +++++++++--- .../java/dev/plex/storage/SQLConnection.java | 2 +- 8 files changed, 185 insertions(+), 33 deletions(-) create mode 100644 src/main/java/dev/plex/cache/sql/SQLNotes.java create mode 100644 src/main/java/dev/plex/punishment/extra/Note.java 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) From 03e5833f0a1b284b1bb9fef0578facc16112cfeb Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 6 Apr 2022 18:39:13 -0700 Subject: [PATCH 05/11] add item builder class --- .../java/dev/plex/util/item/ItemBuilder.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/java/dev/plex/util/item/ItemBuilder.java diff --git a/src/main/java/dev/plex/util/item/ItemBuilder.java b/src/main/java/dev/plex/util/item/ItemBuilder.java new file mode 100644 index 0000000..09ccf4b --- /dev/null +++ b/src/main/java/dev/plex/util/item/ItemBuilder.java @@ -0,0 +1,62 @@ +package dev.plex.util.item; + +import net.kyori.adventure.text.Component; +import org.bukkit.Material; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.Arrays; + +public class ItemBuilder +{ + + private final ItemStack itemStack; + private final ItemMeta meta; + + public ItemBuilder(Material material) + { + this.itemStack = new ItemStack(material); + this.meta = itemStack.getItemMeta(); + } + + public ItemBuilder lore(Component... lore) + { + this.meta.lore(Arrays.asList(lore)); + return this; + } + + public ItemBuilder displayName(Component displayName) + { + this.meta.displayName(displayName); + return this; + } + + public ItemBuilder addEnchantment(Enchantment enchantment, int level) + { + this.meta.addEnchant(enchantment, level, true); + return this; + } + + public ItemBuilder addItemFlag(ItemFlag... flags) + { + this.meta.addItemFlags(flags); + return this; + } + + public ItemBuilder addAttributeModifier(Attribute attribute, AttributeModifier modifier) + { + this.meta.addAttributeModifier(attribute, modifier); + return this; + } + + public ItemStack build() + { + this.itemStack.setItemMeta(this.meta); + return this.itemStack; + } + +} From 705500bddd0dd5610c0e4644cfb125ebce7b30f3 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Wed, 6 Apr 2022 21:48:51 -0400 Subject: [PATCH 06/11] autowipe + toggledrops yay! --- .../dev/plex/command/impl/ToggleDropsCMD.java | 47 +++++++++++++++++++ .../dev/plex/listener/impl/DropListener.java | 14 ++++++ .../dev/plex/services/ServiceManager.java | 2 + .../plex/services/impl/AutoWipeService.java | 40 ++++++++++++++++ src/main/resources/config.yml | 13 +++++ src/main/resources/messages.yml | 5 +- 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/plex/command/impl/ToggleDropsCMD.java create mode 100644 src/main/java/dev/plex/listener/impl/DropListener.java create mode 100644 src/main/java/dev/plex/services/impl/AutoWipeService.java diff --git a/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java b/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java new file mode 100644 index 0000000..b0ac152 --- /dev/null +++ b/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java @@ -0,0 +1,47 @@ +package dev.plex.command.impl; + +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.command.source.RequiredCommandSource; +import dev.plex.rank.enums.Rank; +import dev.plex.services.ServiceManager; +import dev.plex.services.impl.AutoWipeService; +import dev.plex.util.PlexUtils; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + +@CommandPermissions(level = Rank.ADMIN, permission = "plex.toggledrops", source = RequiredCommandSource.ANY) +@CommandParameters(name = "toggledrops", description = "Toggle immediately removing drops.", usage = "/", aliases = "td") +public class ToggleDropsCMD extends PlexCommand +{ + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) + { + if (plugin.config.getBoolean("allowdrops")) + { + plugin.config.set("allowdrops", false); + plugin.config.save(); + sender.sendMessage(messageComponent("allowDropsDisabled")); + } + else + { + plugin.config.set("allowdrops", true); + plugin.config.save(); + sender.sendMessage(messageComponent("allowDropsEnabled")); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/listener/impl/DropListener.java b/src/main/java/dev/plex/listener/impl/DropListener.java new file mode 100644 index 0000000..1aa2dfb --- /dev/null +++ b/src/main/java/dev/plex/listener/impl/DropListener.java @@ -0,0 +1,14 @@ +package dev.plex.listener.impl; + +import dev.plex.listener.PlexListener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerDropItemEvent; + +public class DropListener extends PlexListener +{ + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) + { + if (!plugin.config.getBoolean("allowdrops")) event.setCancelled(true); + } +} diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 7a43fb4..9f030c3 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -2,6 +2,7 @@ package dev.plex.services; import com.google.common.collect.Lists; import dev.plex.Plex; +import dev.plex.services.impl.AutoWipeService; import dev.plex.services.impl.BanService; import dev.plex.services.impl.GameRuleService; import dev.plex.services.impl.UpdateCheckerService; @@ -19,6 +20,7 @@ public class ServiceManager registerService(new BanService()); registerService(new GameRuleService()); registerService(new UpdateCheckerService()); + if (Plex.get().config.getBoolean("autowipe.enabled")) registerService(new AutoWipeService()); } public void startServices() diff --git a/src/main/java/dev/plex/services/impl/AutoWipeService.java b/src/main/java/dev/plex/services/impl/AutoWipeService.java new file mode 100644 index 0000000..4a6fe28 --- /dev/null +++ b/src/main/java/dev/plex/services/impl/AutoWipeService.java @@ -0,0 +1,40 @@ +package dev.plex.services.impl; + +import dev.plex.services.AbstractService; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; + +import java.util.List; + +public class AutoWipeService extends AbstractService +{ + public AutoWipeService() + { + super(true, false); + } + + @Override + public void run() + { + List entities = plugin.config.getStringList("autowipe.entities"); + + for (World world : Bukkit.getWorlds()) + { + for (Entity entity : world.getEntities()) + { + if (entities.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(entity.getType().name()))) + { + entity.remove(); + } + } + } + } + + @Override + public int repeatInSeconds() + { + return Math.max(1, plugin.config.getInt("autowipe.interval")); + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2e4117a..8a0b567 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -141,6 +141,19 @@ entitywipe_list: - "ZOMBIFIED_PIGLIN" - "PUFFERFISH" +# Automatically wipe the specified entities +autowipe: + # Should we automatically wipe entities? + enabled: true + # How often, in seconds, to automatically wipe entities. Default is 5 minutes. + interval: 300 + # Entities to automatically wipe + entities: + - "DROPPED_ITEM" + +# Should we allow drops from players? +allowdrops: true + worlds: flatlands: name: "Flatlands" diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index a6f6b5d..7cbd1ef 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -159,4 +159,7 @@ invalidEntityType: "Notice: Entity type {0} is invalid!" noRemovedEntities: "No entities were removed." # 0 - The command sender # 1 - Number of mobs removed -removedMobs: "{0} - Removed {1} mobs" \ No newline at end of file +removedMobs: "{0} - Removed {1} mobs" +autoWipeDisabled: "Item wiping is currently disabled in the config!" +allowDropsDisabled: "No longer allowing drops from players." +allowDropsEnabled: "Now allowing drops from players." \ No newline at end of file From 41e81449bd705154754423c3d8c6f93ae11329fb Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Wed, 6 Apr 2022 21:00:28 -0500 Subject: [PATCH 07/11] half working --- .../java/dev/plex/cache/sql/SQLNotes.java | 8 +- .../java/dev/plex/command/impl/NotesCMD.java | 112 ++++++++++++++++++ .../java/dev/plex/storage/SQLConnection.java | 1 + 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/main/java/dev/plex/command/impl/NotesCMD.java 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), " + From 34d1eafaafe59c59a2d7b8d44668fcf503dcfb0d Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 6 Apr 2022 19:05:29 -0700 Subject: [PATCH 08/11] reload services and add ids to notes --- .../java/dev/plex/cache/sql/SQLNotes.java | 47 ++++++++++++++----- .../java/dev/plex/command/impl/PlexCMD.java | 5 ++ .../java/dev/plex/punishment/extra/Note.java | 2 + .../dev/plex/services/AbstractService.java | 10 ++++ .../dev/plex/services/ServiceManager.java | 36 ++++++++------ .../java/dev/plex/storage/SQLConnection.java | 2 +- 6 files changed, 75 insertions(+), 27 deletions(-) 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), " + From 21bef1280ddf06c37db86b995934b6e64f9ad25b Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Wed, 6 Apr 2022 22:08:51 -0400 Subject: [PATCH 09/11] fix autowiping so it can work with reloads --- .../java/dev/plex/services/ServiceManager.java | 2 +- .../dev/plex/services/impl/AutoWipeService.java | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 90516eb..0faf424 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -20,7 +20,7 @@ public class ServiceManager registerService(new BanService()); registerService(new GameRuleService()); registerService(new UpdateCheckerService()); - if (Plex.get().config.getBoolean("autowipe.enabled")) registerService(new AutoWipeService()); + registerService(new AutoWipeService()); } public void startServices() diff --git a/src/main/java/dev/plex/services/impl/AutoWipeService.java b/src/main/java/dev/plex/services/impl/AutoWipeService.java index 4a6fe28..aff79b5 100644 --- a/src/main/java/dev/plex/services/impl/AutoWipeService.java +++ b/src/main/java/dev/plex/services/impl/AutoWipeService.java @@ -1,5 +1,6 @@ package dev.plex.services.impl; +import dev.plex.Plex; import dev.plex.services.AbstractService; import org.bukkit.Bukkit; import org.bukkit.World; @@ -18,15 +19,18 @@ public class AutoWipeService extends AbstractService @Override public void run() { - List entities = plugin.config.getStringList("autowipe.entities"); - - for (World world : Bukkit.getWorlds()) + if (Plex.get().config.getBoolean("autowipe.enabled")) { - for (Entity entity : world.getEntities()) + List entities = plugin.config.getStringList("autowipe.entities"); + + for (World world : Bukkit.getWorlds()) { - if (entities.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(entity.getType().name()))) + for (Entity entity : world.getEntities()) { - entity.remove(); + if (entities.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(entity.getType().name()))) + { + entity.remove(); + } } } } From b22a36948a000f9bc573281271a35452617979a3 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Wed, 6 Apr 2022 21:39:45 -0500 Subject: [PATCH 10/11] this isnt working --- src/main/java/dev/plex/cache/sql/SQLNotes.java | 12 ++++++------ src/main/java/dev/plex/command/impl/NotesCMD.java | 14 ++++++++------ .../dev/plex/command/impl/ToggleDropsCMD.java | 15 +++------------ src/main/java/dev/plex/storage/SQLConnection.java | 2 +- 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/main/java/dev/plex/cache/sql/SQLNotes.java b/src/main/java/dev/plex/cache/sql/SQLNotes.java index 4dcd30a..91de294 100644 --- a/src/main/java/dev/plex/cache/sql/SQLNotes.java +++ b/src/main/java/dev/plex/cache/sql/SQLNotes.java @@ -19,7 +19,6 @@ 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` (`id`, `uuid`, `written_by`, `note`, `timestamp`) VALUES(?, ?, ?, ?, ?)"; private static final String DELETE = "DELETE FROM `notes` WHERE uuid=? AND id=?"; @@ -44,7 +43,8 @@ public class SQLNotes note.setId(set.getInt("id")); notes.add(note); } - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } @@ -62,7 +62,8 @@ public class SQLNotes statement.setString(1, uuid.toString()); statement.setInt(2, id); statement.execute(); - } catch (SQLException e) + } + catch (SQLException e) { e.printStackTrace(); } @@ -85,13 +86,12 @@ public class SQLNotes statement.setLong(5, note.getTimestamp().toInstant(ZoneOffset.UTC).toEpochMilli()); statement.execute(); note.setId(notes.size()); - } 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 index f5b84fb..22f233e 100644 --- a/src/main/java/dev/plex/command/impl/NotesCMD.java +++ b/src/main/java/dev/plex/command/impl/NotesCMD.java @@ -9,6 +9,7 @@ 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; @@ -16,6 +17,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.lang3.ArrayUtils; @@ -48,13 +50,13 @@ public class NotesCMD extends PlexCommand case "list": { Component noteList = Component.text("Player notes for: " + plexPlayer.getName()).color(NamedTextColor.GREEN); - int id = 1; - for (Note note : plexPlayer.getNotes()) + /*for (Note note : plugin.getSqlNotes().getNotes(UUID.fromString(plexPlayer.getUuid()))) { - Component noteLine = Component.text(id + ". " + note.getWrittenBy() + ": " + note.getNote()).color(NamedTextColor.GOLD); - noteList.append(Component.empty()).append(noteLine); - id++; - } + 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); return null; } diff --git a/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java b/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java index b0ac152..ca396ea 100644 --- a/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java +++ b/src/main/java/dev/plex/command/impl/ToggleDropsCMD.java @@ -30,18 +30,9 @@ public class ToggleDropsCMD extends PlexCommand @Override protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) { - if (plugin.config.getBoolean("allowdrops")) - { - plugin.config.set("allowdrops", false); - plugin.config.save(); - sender.sendMessage(messageComponent("allowDropsDisabled")); - } - else - { - plugin.config.set("allowdrops", true); - plugin.config.save(); - sender.sendMessage(messageComponent("allowDropsEnabled")); - } + plugin.config.set("allowdrops", !plugin.config.getBoolean("allowdrops")); + plugin.config.save(); + send(sender, plugin.config.getBoolean("allowdrops") ? messageComponent("allowDropsEnabled") : messageComponent("allowDropsDisabled")); return null; } } \ 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 c2bc826..faeb8e0 100644 --- a/src/main/java/dev/plex/storage/SQLConnection.java +++ b/src/main/java/dev/plex/storage/SQLConnection.java @@ -79,7 +79,7 @@ public class SQLConnection extends PlexBase con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" + "`id` INT NOT NULL, " + "`uuid` VARCHAR(46) NOT NULL, " + - "`written_by` VARCHAR(16), " + + "`written_by` VARCHAR(46), " + "`note` VARCHAR(2000), " + "`timestamp` BIGINT" + ");").execute(); From 96de4cdb1debfff419d5acf7a80cdbba8e58b0de Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Wed, 6 Apr 2022 22:46:21 -0400 Subject: [PATCH 11/11] fix!! --- src/main/java/dev/plex/command/impl/MobPurgeCMD.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/plex/command/impl/MobPurgeCMD.java b/src/main/java/dev/plex/command/impl/MobPurgeCMD.java index 5078052..3f9c362 100644 --- a/src/main/java/dev/plex/command/impl/MobPurgeCMD.java +++ b/src/main/java/dev/plex/command/impl/MobPurgeCMD.java @@ -38,8 +38,8 @@ public class MobPurgeCMD extends PlexCommand Location loc = entity.getLocation(); loc.setY(-500); entity.teleportAsync(loc); - entity.remove(); */ + entity.remove(); entityCounts.put(type,entityCounts.getOrDefault(type, 0) + 1); }