Skip to content

Commit

Permalink
Added a converter to HomeManager to use world uuid instead of world n…
Browse files Browse the repository at this point in the history
…ame now.
  • Loading branch information
andrew121410 committed May 22, 2024
1 parent 82a13d6 commit 0fe821e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 31 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 0fe821e

Please sign in to comment.