From 11892e58ca98a3772f95f9d9dc993b28ab38852a Mon Sep 17 00:00:00 2001 From: Xiao-MoMi <70987828+Xiao-MoMi@users.noreply.github.com> Date: Sat, 12 Nov 2022 12:56:37 +0800 Subject: [PATCH] 2.1.3 --- build.gradle | 2 +- .../commands/subcmd/BubblesEquipCommand.java | 11 ++++++--- .../subcmd/BubblesForceEquipCommand.java | 10 +++++--- .../subcmd/BubblesForceUnequipCommand.java | 9 +++++--- .../subcmd/BubblesUnequipCommand.java | 10 +++++--- .../subcmd/NameplatesEquipCommand.java | 17 +++++++++----- .../subcmd/NameplatesForceEquipCommand.java | 17 ++++++++------ .../subcmd/NameplatesForceUnequipCommand.java | 9 ++++++-- .../subcmd/NameplatesUnequipCommand.java | 8 +++++-- .../customnameplates/manager/DataManager.java | 1 + .../objects/data/MySQLStorageImpl.java | 22 ++++++++++++++---- .../objects/data/SqlConnection.java | 23 ++++++------------- .../mode/bubbles/BBPacketsHandle.java | 20 ++++++++-------- 13 files changed, 99 insertions(+), 60 deletions(-) diff --git a/build.gradle b/build.gradle index 33288f2..62f6ff2 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'net.momirealms' -version = '2.1.2' +version = '2.1.3' repositories { mavenCentral() diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesEquipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesEquipCommand.java index 7557fa8..6e88e78 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesEquipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesEquipCommand.java @@ -5,6 +5,7 @@ import net.momirealms.customnameplates.commands.AbstractSubCommand; import net.momirealms.customnameplates.commands.SubCommand; import net.momirealms.customnameplates.manager.MessageManager; import net.momirealms.customnameplates.utils.AdventureUtil; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -39,9 +40,13 @@ public class BubblesEquipCommand extends AbstractSubCommand { AdventureUtil.playerMessage((Player) sender, MessageManager.prefix + MessageManager.bb_notAvailable); return true; } - CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles(args.get(0)); - CustomNameplates.plugin.getDataManager().saveData(player); - AdventureUtil.playerMessage((Player) sender, MessageManager.prefix + MessageManager.bb_equip.replace("{Bubble}", CustomNameplates.plugin.getResourceManager().getBubbleConfig(args.get(0)).name())); + + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles(args.get(0)); + CustomNameplates.plugin.getDataManager().saveData(player); + AdventureUtil.playerMessage((Player) sender, MessageManager.prefix + MessageManager.bb_equip.replace("{Bubble}", CustomNameplates.plugin.getResourceManager().getBubbleConfig(args.get(0)).name())); + }); + return true; } diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceEquipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceEquipCommand.java index fb14fd5..e0d5de7 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceEquipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceEquipCommand.java @@ -40,10 +40,14 @@ public class BubblesForceEquipCommand extends AbstractSubCommand { AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.bb_not_exist); return true; } - CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles(args.get(0)); - CustomNameplates.plugin.getDataManager().saveData(player); - AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.bb_force_equip.replace("{Bubble}", CustomNameplates.plugin.getResourceManager().getBubbleConfig(args.get(1)).name()).replace("{Player}", args.get(0))); + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles(args.get(0)); + CustomNameplates.plugin.getDataManager().saveData(player); + + AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.bb_force_equip.replace("{Bubble}", CustomNameplates.plugin.getResourceManager().getBubbleConfig(args.get(1)).name()).replace("{Player}", args.get(0))); + }); + return true; } diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceUnequipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceUnequipCommand.java index 75b2353..e157ddd 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceUnequipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesForceUnequipCommand.java @@ -40,9 +40,12 @@ public class BubblesForceUnequipCommand extends AbstractSubCommand { return true; } - CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles("none"); - CustomNameplates.plugin.getDataManager().saveData(player); - AdventureUtil.sendMessage(sender,MessageManager.prefix + MessageManager.bb_force_unEquip.replace("{Player}", args.get(0))); + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles("none"); + CustomNameplates.plugin.getDataManager().saveData(player); + AdventureUtil.sendMessage(sender,MessageManager.prefix + MessageManager.bb_force_unEquip.replace("{Player}", args.get(0))); + }); + return true; } diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesUnequipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesUnequipCommand.java index 9104626..cf39a1a 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesUnequipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/BubblesUnequipCommand.java @@ -5,6 +5,7 @@ import net.momirealms.customnameplates.commands.AbstractSubCommand; import net.momirealms.customnameplates.commands.SubCommand; import net.momirealms.customnameplates.manager.MessageManager; import net.momirealms.customnameplates.utils.AdventureUtil; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -30,9 +31,12 @@ public class BubblesUnequipCommand extends AbstractSubCommand { return true; } - CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles("none"); - CustomNameplates.plugin.getDataManager().saveData(player); - AdventureUtil.playerMessage(player, MessageManager.prefix + MessageManager.bb_unEquip); + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + CustomNameplates.plugin.getDataManager().getPlayerData(player).setBubbles("none"); + CustomNameplates.plugin.getDataManager().saveData(player); + AdventureUtil.playerMessage(player, MessageManager.prefix + MessageManager.bb_unEquip); + }); + return true; } } diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesEquipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesEquipCommand.java index ca48ef7..fa919cb 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesEquipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesEquipCommand.java @@ -6,6 +6,7 @@ import net.momirealms.customnameplates.commands.SubCommand; import net.momirealms.customnameplates.manager.MessageManager; import net.momirealms.customnameplates.objects.nameplates.NameplatesTeam; import net.momirealms.customnameplates.utils.AdventureUtil; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -37,12 +38,16 @@ public class NameplatesEquipCommand extends AbstractSubCommand { AdventureUtil.playerMessage((Player) sender, MessageManager.prefix + MessageManager.np_not_exist); return true; } - CustomNameplates.plugin.getDataManager().getPlayerData(player).equipNameplate(args.get(0)); - CustomNameplates.plugin.getDataManager().saveData(player); - NameplatesTeam nameplatesTeam = CustomNameplates.plugin.getNameplateManager().getTeamManager().getNameplatesTeam(player); - if (nameplatesTeam != null) nameplatesTeam.updateNameplates(); - CustomNameplates.plugin.getNameplateManager().getTeamManager().sendUpdateToAll(player, true); - AdventureUtil.playerMessage((Player) sender, MessageManager.prefix + MessageManager.np_equip.replace("{Nameplate}", CustomNameplates.plugin.getResourceManager().getNameplateConfig(args.get(0)).name())); + + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + CustomNameplates.plugin.getDataManager().getPlayerData(player).equipNameplate(args.get(0)); + CustomNameplates.plugin.getDataManager().saveData(player); + NameplatesTeam nameplatesTeam = CustomNameplates.plugin.getNameplateManager().getTeamManager().getNameplatesTeam(player); + if (nameplatesTeam != null) nameplatesTeam.updateNameplates(); + CustomNameplates.plugin.getNameplateManager().getTeamManager().sendUpdateToAll(player, true); + AdventureUtil.playerMessage((Player) sender, MessageManager.prefix + MessageManager.np_equip.replace("{Nameplate}", CustomNameplates.plugin.getResourceManager().getNameplateConfig(args.get(0)).name())); + }); + } else { AdventureUtil.playerMessage((Player) sender, MessageManager.prefix + MessageManager.np_notAvailable); diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceEquipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceEquipCommand.java index 913374a..f20b874 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceEquipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceEquipCommand.java @@ -43,14 +43,17 @@ public class NameplatesForceEquipCommand extends AbstractSubCommand { AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.np_not_exist); return true; } - CustomNameplates.plugin.getDataManager().getPlayerData(player).equipNameplate(args.get(1)); - CustomNameplates.plugin.getDataManager().saveData(player); - NameplatesTeam nameplatesTeam = CustomNameplates.plugin.getNameplateManager().getTeamManager().getNameplatesTeam(player); - if (nameplatesTeam != null) nameplatesTeam.updateNameplates(); - CustomNameplates.plugin.getNameplateManager().getTeamManager().sendUpdateToAll(player, true); - AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.np_force_equip.replace("{Nameplate}", CustomNameplates.plugin.getResourceManager().getNameplateConfig(args.get(1)).name()).replace("{Player}", args.get(0))); - return super.onCommand(sender, args); + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + CustomNameplates.plugin.getDataManager().getPlayerData(player).equipNameplate(args.get(1)); + CustomNameplates.plugin.getDataManager().saveData(player); + NameplatesTeam nameplatesTeam = CustomNameplates.plugin.getNameplateManager().getTeamManager().getNameplatesTeam(player); + if (nameplatesTeam != null) nameplatesTeam.updateNameplates(); + CustomNameplates.plugin.getNameplateManager().getTeamManager().sendUpdateToAll(player, true); + AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.np_force_equip.replace("{Nameplate}", CustomNameplates.plugin.getResourceManager().getNameplateConfig(args.get(1)).name()).replace("{Player}", args.get(0))); + }); + + return true; } @Override diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceUnequipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceUnequipCommand.java index 3810a39..34abc46 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceUnequipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesForceUnequipCommand.java @@ -1,5 +1,6 @@ package net.momirealms.customnameplates.commands.subcmd; +import net.momirealms.customnameplates.CustomNameplates; import net.momirealms.customnameplates.commands.AbstractSubCommand; import net.momirealms.customnameplates.commands.SubCommand; import net.momirealms.customnameplates.manager.MessageManager; @@ -37,8 +38,12 @@ public class NameplatesForceUnequipCommand extends AbstractSubCommand { AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.not_online.replace("{Player}",args.get(0))); return true; } - super.unequipNameplate(player); - AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.np_force_unEquip.replace("{Player}", args.get(0))); + + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + super.unequipNameplate(player); + AdventureUtil.sendMessage(sender, MessageManager.prefix + MessageManager.np_force_unEquip.replace("{Player}", args.get(0))); + }); + return true; } diff --git a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesUnequipCommand.java b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesUnequipCommand.java index 8db5611..41d28d3 100644 --- a/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesUnequipCommand.java +++ b/src/main/java/net/momirealms/customnameplates/commands/subcmd/NameplatesUnequipCommand.java @@ -1,9 +1,11 @@ package net.momirealms.customnameplates.commands.subcmd; +import net.momirealms.customnameplates.CustomNameplates; import net.momirealms.customnameplates.commands.AbstractSubCommand; import net.momirealms.customnameplates.commands.SubCommand; import net.momirealms.customnameplates.manager.MessageManager; import net.momirealms.customnameplates.utils.AdventureUtil; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -30,8 +32,10 @@ public class NameplatesUnequipCommand extends AbstractSubCommand { return true; } - super.unequipNameplate(player); - AdventureUtil.playerMessage(player, MessageManager.prefix + MessageManager.np_unEquip); + Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { + super.unequipNameplate(player); + AdventureUtil.playerMessage(player, MessageManager.prefix + MessageManager.np_unEquip); + }); return super.onCommand(sender, args); } } diff --git a/src/main/java/net/momirealms/customnameplates/manager/DataManager.java b/src/main/java/net/momirealms/customnameplates/manager/DataManager.java index d9793a3..5baaf37 100644 --- a/src/main/java/net/momirealms/customnameplates/manager/DataManager.java +++ b/src/main/java/net/momirealms/customnameplates/manager/DataManager.java @@ -66,6 +66,7 @@ public class DataManager extends Function { public void onJoin(Player player) { Bukkit.getScheduler().runTaskAsynchronously(CustomNameplates.plugin, () -> { PlayerData playerData = dataStorageInterface.loadData(player); + if (playerData == null) return; playerDataCache.put(player.getUniqueId(), playerData); //wait if (ConfigUtil.isModuleEnabled("nameplate")) { diff --git a/src/main/java/net/momirealms/customnameplates/objects/data/MySQLStorageImpl.java b/src/main/java/net/momirealms/customnameplates/objects/data/MySQLStorageImpl.java index 8ef89bb..828d12b 100644 --- a/src/main/java/net/momirealms/customnameplates/objects/data/MySQLStorageImpl.java +++ b/src/main/java/net/momirealms/customnameplates/objects/data/MySQLStorageImpl.java @@ -48,6 +48,7 @@ public class MySQLStorageImpl implements DataStorageInterface { PlayerData playerData = null; try { Connection connection = sqlConnection.getConnectionAndCheck(); + if (connection == null) return new PlayerData(player, NameplateManager.defaultNameplate, ChatBubblesManager.defaultBubble); String sql = String.format(SqlConstants.SQL_SELECT_BY_UUID, sqlConnection.getTable_name()); PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, player.getUniqueId().toString()); @@ -60,6 +61,7 @@ public class MySQLStorageImpl implements DataStorageInterface { else { playerData = new PlayerData(player, NameplateManager.defaultNameplate, ChatBubblesManager.defaultBubble); } + connection.close(); } catch (SQLException e) { e.printStackTrace(); } @@ -79,8 +81,11 @@ public class MySQLStorageImpl implements DataStorageInterface { private void createTableIfNotExist(String table) { String sql = String.format(SqlConstants.SQL_CREATE_TABLE, table); - try (Connection connection = sqlConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql)) { + try { + Connection connection = sqlConnection.getConnection(); + PreparedStatement statement = connection.prepareStatement(sql); statement.executeUpdate(); + connection.close(); } catch (SQLException ex) { AdventureUtil.consoleMessage("[CustomNameplates] Failed to create table"); } @@ -88,11 +93,14 @@ public class MySQLStorageImpl implements DataStorageInterface { private void insertData(UUID uuid, String nameplate, String bubbles) { String sql = String.format(SqlConstants.SQL_INSERT, sqlConnection.getTable_name()); - try (Connection connection = sqlConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql)) { + try { + Connection connection = sqlConnection.getConnection(); + PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, uuid.toString()); statement.setString(2, nameplate); statement.setString(3, bubbles); statement.executeUpdate(); + connection.close(); } catch (SQLException ex) { AdventureUtil.consoleMessage("[CustomNameplates] Failed to insert data for " + uuid); } @@ -100,11 +108,14 @@ public class MySQLStorageImpl implements DataStorageInterface { private void updateData(UUID uuid, String nameplate, String bubbles) { String sql = String.format(SqlConstants.SQL_UPDATE_BY_UUID, sqlConnection.getTable_name()); - try (Connection connection = sqlConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql)) { + try { + Connection connection = sqlConnection.getConnection(); + PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, nameplate); statement.setString(2, bubbles); statement.setString(3, uuid.toString()); statement.executeUpdate(); + connection.close(); } catch (SQLException ex) { AdventureUtil.consoleMessage("[CustomNameplates] Failed to update data for " + uuid); } @@ -113,10 +124,13 @@ public class MySQLStorageImpl implements DataStorageInterface { public boolean exists(UUID uuid) { String sql = String.format(SqlConstants.SQL_SELECT_BY_UUID, sqlConnection.getTable_name()); boolean exist; - try (Connection connection = sqlConnection.getConnection(); PreparedStatement statement = connection.prepareStatement(sql)) { + try { + Connection connection = sqlConnection.getConnection(); + PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); exist = rs.next(); + connection.close(); } catch (SQLException ex) { AdventureUtil.consoleMessage("[CustomNameplates] Failed to select data for " + uuid); return false; diff --git a/src/main/java/net/momirealms/customnameplates/objects/data/SqlConnection.java b/src/main/java/net/momirealms/customnameplates/objects/data/SqlConnection.java index e480c33..4a781aa 100644 --- a/src/main/java/net/momirealms/customnameplates/objects/data/SqlConnection.java +++ b/src/main/java/net/momirealms/customnameplates/objects/data/SqlConnection.java @@ -53,6 +53,7 @@ public class SqlConnection { hikariConfig.setMaximumPoolSize(config.getInt(storageMode + ".Pool-Settings.maximum-pool-size")); hikariConfig.setMinimumIdle(config.getInt(storageMode + ".Pool-Settings.minimum-idle")); hikariConfig.setMaxLifetime(config.getInt(storageMode + ".Pool-Settings.maximum-lifetime")); + hikariConfig.setConnectionTimeout(5000); for (String property : config.getConfigurationSection(storageMode + ".properties").getKeys(false)) { hikariConfig.addDataSourceProperty(property, config.getString(storageMode + ".properties." + property)); } @@ -64,7 +65,6 @@ public class SqlConnection { try { hikariDataSource = new HikariDataSource(hikariConfig); - } catch (HikariPool.PoolInitializationException e) { AdventureUtil.consoleMessage("[CustomNameplates] Failed to create sql connection"); } @@ -72,22 +72,13 @@ public class SqlConnection { } public boolean setGlobalConnection() { - try { - createNewHikariConfiguration(); - Connection connection = getConnection(); - connection.close(); - if (secondTry) { - AdventureUtil.consoleMessage("[CustomNameplates] Successfully reconnect to SQL!"); - } else { - secondTry = true; - } - return true; - } catch (SQLException e) { - AdventureUtil.consoleMessage("[CustomNameplates] Error! Failed to connect to SQL!"); - e.printStackTrace(); - close(); - return false; + createNewHikariConfiguration(); + if (secondTry) { + AdventureUtil.consoleMessage("[CustomNameplates] Successfully reconnect to SQL!"); + } else { + secondTry = true; } + return true; } public Connection getConnection() throws SQLException { diff --git a/src/main/java/net/momirealms/customnameplates/objects/nameplates/mode/bubbles/BBPacketsHandle.java b/src/main/java/net/momirealms/customnameplates/objects/nameplates/mode/bubbles/BBPacketsHandle.java index fb0868c..47f2d51 100644 --- a/src/main/java/net/momirealms/customnameplates/objects/nameplates/mode/bubbles/BBPacketsHandle.java +++ b/src/main/java/net/momirealms/customnameplates/objects/nameplates/mode/bubbles/BBPacketsHandle.java @@ -28,25 +28,25 @@ import java.util.List; public class BBPacketsHandle extends PacketsHandler { private final ChatBubblesManager chatBubblesManager; - private EntityDestroyListener entityDestroyListener; - private EntityMoveListener entityMoveListener; - private EntitySpawnListener entitySpawnListener; - private EntityTeleportListener entityTeleportListener; - private EntityLookListener entityLookListener; + private final EntityDestroyListener entityDestroyListener; + private final EntityMoveListener entityMoveListener; + private final EntitySpawnListener entitySpawnListener; + private final EntityTeleportListener entityTeleportListener; + private final EntityLookListener entityLookListener; public BBPacketsHandle(ChatBubblesManager chatBubblesManager) { super(chatBubblesManager); this.chatBubblesManager = chatBubblesManager; - } - - @Override - public void load() { - super.load(); this.entityDestroyListener = new EntityDestroyListener(this); this.entityMoveListener = new EntityMoveListener(this); this.entitySpawnListener = new EntitySpawnListener(this); this.entityTeleportListener = new EntityTeleportListener(this); this.entityLookListener = new EntityLookListener(this); + } + + @Override + public void load() { + super.load(); CustomNameplates.protocolManager.addPacketListener(entityDestroyListener); CustomNameplates.protocolManager.addPacketListener(entityMoveListener); CustomNameplates.protocolManager.addPacketListener(entitySpawnListener);