From 0fe821eff12cceace683c3b2d3e8e4a13fd47304 Mon Sep 17 00:00:00 2001 From: Andrew121410 Date: Tue, 21 May 2024 22:21:23 -0400 Subject: [PATCH] Added a converter to HomeManager to use world uuid instead of world name now. --- build.gradle.kts | 4 +- .../managers/HomeManager.java | 51 +++++++++++++- .../managers/WarpManager.java | 70 ++++++++++++------- 3 files changed, 94 insertions(+), 31 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4ea480ed..497706df 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -75,8 +75,8 @@ repositories { dependencies { api("org.bstats:bstats-bukkit:3.0.2") compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT") - compileOnly("com.github.World1-6.World1-6Utils:World1-6Utils-Plugin:b1765b209d") - compileOnly("com.github.World1-6.World1-6Utils:World1-6Utils_CMI_API:b1765b209d") + compileOnly("com.github.World1-6.World1-6Utils:World1-6Utils-Plugin:f03b40d4c6") + compileOnly("com.github.World1-6.World1-6Utils:World1-6Utils_CMI_API:f03b40d4c6") compileOnly("net.essentialsx:EssentialsX:2.21.0-SNAPSHOT") compileOnly("org.geysermc.floodgate:api:2.2.3-SNAPSHOT") } diff --git a/src/main/java/com/andrew121410/mc/world16essentials/managers/HomeManager.java b/src/main/java/com/andrew121410/mc/world16essentials/managers/HomeManager.java index 2f5037e8..1bfd8c9c 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/managers/HomeManager.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/managers/HomeManager.java @@ -10,6 +10,7 @@ import com.andrew121410.mc.world16utils.utils.ccutils.storage.easy.EasySQL; import com.andrew121410.mc.world16utils.utils.ccutils.storage.easy.MultiTableEasySQL; import com.andrew121410.mc.world16utils.utils.ccutils.storage.easy.SQLDataStore; +import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionAttachmentInfo; @@ -148,7 +149,53 @@ public UnlinkedWorldLocation getLocation(SQLDataStore sqlDataStore) { String stringPitch = sqlDataStore.get("PITCH"); String stringWorld = sqlDataStore.get("World"); - return new UnlinkedWorldLocation(stringWorld, Double.parseDouble(stringX), Double.parseDouble(stringY), Double.parseDouble(stringZ), Float.parseFloat(stringYaw), Float.parseFloat(stringPitch)); + // Convert old data if needed. + UUID worldUUID = convertOldDataIfNeeded(sqlDataStore); + + return new UnlinkedWorldLocation(worldUUID, Double.parseDouble(stringX), Double.parseDouble(stringY), Double.parseDouble(stringZ), Float.parseFloat(stringYaw), Float.parseFloat(stringPitch)); + } + + private UUID convertOldDataIfNeeded(SQLDataStore sqlDataStore) { + String stringX = sqlDataStore.get("X"); + String stringY = sqlDataStore.get("Y"); + String stringZ = sqlDataStore.get("Z"); + String stringYaw = sqlDataStore.get("YAW"); + String stringPitch = sqlDataStore.get("PITCH"); + String stringWorld = sqlDataStore.get("World"); + + UUID worldUUID = null; + + // Test to see if the world is a UUID or a string. + try { + worldUUID = UUID.fromString(stringWorld); + } catch (IllegalArgumentException e) { + Location location = new Location(this.plugin.getServer().getWorld(stringWorld), Double.parseDouble(stringX), Double.parseDouble(stringY), Double.parseDouble(stringZ), Float.parseFloat(stringYaw), Float.parseFloat(stringPitch)); + + // Delete the old data. + SQLDataStore toDelete = new SQLDataStore(); + toDelete.put("UUID", sqlDataStore.get("UUID")); + toDelete.put("HomeName", sqlDataStore.get("HomeName")); + try { + easySQL.delete(toDelete); + } catch (SQLException ex) { + ex.printStackTrace(); + } + + // Save the new data. + SQLDataStore newData = makeSQLDataStore(UUID.fromString(sqlDataStore.get("UUID")), sqlDataStore.get("PlayerName"), sqlDataStore.get("HomeName"), new UnlinkedWorldLocation(location)); + try { + easySQL.save(newData); + } catch (SQLException ex) { + ex.printStackTrace(); + } + + this.plugin.getServer().getLogger().info("Converted old home data for " + sqlDataStore.get("PlayerName") + " for home " + sqlDataStore.get("HomeName") + " to the new format."); + + // Set the worldUUID to the new world. + worldUUID = location.getWorld().getUID(); + } + + return worldUUID; } public SQLDataStore makeSQLDataStore(UUID uuid, String playerName, String homeName, UnlinkedWorldLocation location) { @@ -162,7 +209,7 @@ public SQLDataStore makeSQLDataStore(UUID uuid, String playerName, String homeNa sqlDataStore.put("Z", String.valueOf(location.getZ())); sqlDataStore.put("YAW", String.valueOf(location.getYaw())); sqlDataStore.put("PITCH", String.valueOf(location.getPitch())); - sqlDataStore.put("World", location.getWorldName()); + sqlDataStore.put("World", String.valueOf(location.getWorldUUID())); return sqlDataStore; } diff --git a/src/main/java/com/andrew121410/mc/world16essentials/managers/WarpManager.java b/src/main/java/com/andrew121410/mc/world16essentials/managers/WarpManager.java index 41f5782f..7460e6cd 100644 --- a/src/main/java/com/andrew121410/mc/world16essentials/managers/WarpManager.java +++ b/src/main/java/com/andrew121410/mc/world16essentials/managers/WarpManager.java @@ -27,39 +27,55 @@ public void loadAllWarps() { for (String key : cs.getKeys(false)) { ConfigurationSection warpCs = cs.getConfigurationSection(key); - Object object = warpCs.get("Location"); - - // Convert old location to UnlinkedWorldLocation - if (!(object instanceof UnlinkedWorldLocation)) { - ConfigurationSection locationCs = warpCs.getConfigurationSection("Location"); - if (locationCs != null) { - String world = locationCs.getString("world", null); - String x = locationCs.getString("x", null); - String y = locationCs.getString("y", null); - String z = locationCs.getString("z", null); - String yaw = locationCs.getString("yaw", null); - String pitch = locationCs.getString("pitch", null); - - if (world != null && x != null && y != null && z != null && yaw != null && pitch != null) { - UnlinkedWorldLocation unlinkedWorldLocation = new UnlinkedWorldLocation( - world, - Double.parseDouble(x), - Double.parseDouble(y), - Double.parseDouble(z), - Float.parseFloat(yaw), - Float.parseFloat(pitch)); - - warpCs.set("Location", unlinkedWorldLocation); - this.warpsYml.saveConfig(); - } - } - } + // If the warpCs is null, continue. + if (warpCs == null) continue; + + // Convert old data if needed. + convertOldDataIfNeeded(key, warpCs); UnlinkedWorldLocation location = (UnlinkedWorldLocation) warpCs.get("Location"); this.warpsMap.putIfAbsent(key, location); } } + private void convertOldDataIfNeeded(String key, ConfigurationSection warpCs) { + Object object = warpCs.get("Location"); + + // Convert old data (Location to UnlinkedWorldLocation) + if (!(object instanceof UnlinkedWorldLocation)) { + ConfigurationSection locationCs = warpCs.getConfigurationSection("Location"); + if (locationCs != null) { + String world = locationCs.getString("world", null); + String x = locationCs.getString("x", null); + String y = locationCs.getString("y", null); + String z = locationCs.getString("z", null); + String yaw = locationCs.getString("yaw", null); + String pitch = locationCs.getString("pitch", null); + + // If the values are null then return. + if (world == null || x == null || y == null || z == null || yaw == null || pitch == null) return; + + Location location = new Location( + this.plugin.getServer().getWorld(world), // Get the world by name + Double.parseDouble(x), + Double.parseDouble(y), + Double.parseDouble(z), + Float.parseFloat(yaw), + Float.parseFloat(pitch)); + + UnlinkedWorldLocation unlinkedWorldLocation = new UnlinkedWorldLocation(location); + + warpCs.set("Location", unlinkedWorldLocation); + this.warpsYml.saveConfig(); + + this.plugin.getServer().getLogger().info("Converted very old warp " + key + " to use UnlinkedWorldLocation instead of Location."); + } + } + + // Convert old data (UnlinkedWorldLocation.world to UUID) + // This is done automatically by the UnlinkedWorldLocation class. + } + public void add(String name, Location location) { String newWarpName = name.toLowerCase();