diff --git a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java index 89a9017b7..59156e1bb 100644 --- a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java +++ b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java @@ -12,31 +12,66 @@ public class UpgradeDatabase { private static final HuskHomes plugin = HuskHomes.getInstance(); - //todo this stuff doesn't work. TEST BEFORE LAUNCH + private static final String[] mySQLUpgradeStatements = { + "ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + + " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0, " + + "ADD `offline_location_id` integer NULL DEFAULT NULL, " + + "ADD FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION;"}; + + private static final String[] SQLiteUpgradeStatements = { + "ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0;", + + "ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + " ADD `offline_location_id` integer NULL DEFAULT NULL;", + + "CREATE TABLE " + HuskHomes.getSettings().getPlayerDataTable() + "_dg_tmp (" + + "`player_id` integer NOT NULL PRIMARY KEY," + + "`user_uuid` char(36) NOT NULL UNIQUE," + + "`username` varchar(16) NOT NULL," + + "`home_slots` integer NOT NULL," + + "`rtp_cooldown` integer NOT NULL," + + "`is_teleporting` boolean NOT NULL," + + "`dest_location_id` integer REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " ON DELETE SET NULL," + + "`last_location_id` integer REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " ON DELETE SET NULL," + + "`offline_location_id` integer DEFAULT NULL CONSTRAINT " + HuskHomes.getSettings().getPlayerDataTable() + "_" + HuskHomes.getSettings().getLocationsDataTable() + "_location_id_fk REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + "," + + "`is_ignoring_requests` boolean DEFAULT false NOT NULL" + + ");", + + "INSERT INTO " + HuskHomes.getSettings().getPlayerDataTable() + "_dg_tmp(player_id, user_uuid, username, home_slots, rtp_cooldown, is_teleporting, dest_location_id, last_location_id, offline_location_id, is_ignoring_requests) select player_id, user_uuid, username, home_slots, rtp_cooldown, is_teleporting, dest_location_id, last_location_id, offline_location_id, is_ignoring_requests FROM " + HuskHomes.getSettings().getPlayerDataTable() + ";", + + "DROP TABLE " + HuskHomes.getSettings().getPlayerDataTable() + ";", + + "ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + "_dg_tmp RENAME TO " + HuskHomes.getSettings().getPlayerDataTable() + ";" + }; // Upgrade the database system if the config file version is not high enough public static void upgradeDatabase() { plugin.reloadConfig(); if (plugin.getConfig().getInt("config_file_version", 1) <= 6) { - plugin.getLogger().info("Detected that the database might need updating. Running database upgrade..."); - try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement( - "ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() - + " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0, " - + "ADD `offline_location_id` integer NULL DEFAULT NULL, " - + "ADD FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION;")) { - tableUpdateStatement.executeUpdate(); - plugin.getLogger().info("Database update complete!"); - } catch (SQLException e) { - plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning."); - e.printStackTrace(); - } finally { - // Update the config file version - plugin.getConfig().set("config_file_version", 7); - plugin.saveConfig(); + plugin.getLogger().info("Database upgrade needed: Adding logout position tracking and ignoring request data..."); + String[] statements = SQLiteUpgradeStatements; + if (HuskHomes.getSettings().getDatabaseType().equalsIgnoreCase("mysql")) { + statements = mySQLUpgradeStatements; + } + int i = 1; + for (String statement : statements) { + try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(statement)) { + tableUpdateStatement.execute(); + plugin.getLogger().info("Database upgrade in progress... (" + i + "/" + statements.length + ")"); + i++; + } catch (SQLException e) { + plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning."); + e.printStackTrace(); + } } + + // Update the config file version + plugin.getLogger().info("Logout position and ignoring request database upgrade complete! (v7)"); + plugin.getConfig().set("config_file_version", 7); + plugin.saveConfig(); } + if (plugin.getConfig().getInt("config_file_version", 1) <= 7) { - plugin.getLogger().info("Detected that the database might need updating. Running database upgrade..."); + plugin.getLogger().info("Database upgrade needed: Adding creation timestamps to homes and warps..."); try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement( "ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable() + " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) { @@ -45,30 +80,30 @@ public static void upgradeDatabase() { "UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) { timeSettingStatement.executeUpdate(); } - plugin.getLogger().info("Database update complete!"); + plugin.getLogger().info("Database upgrade in progress... (1/2)"); } catch (SQLException e) { plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning."); e.printStackTrace(); - } finally { - try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement( - "ALTER TABLE " + HuskHomes.getSettings().getWarpsDataTable() - + " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) { - tableUpdateStatement.execute(); - try (PreparedStatement timeSettingStatement = HuskHomes.getConnection().prepareStatement( - "UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) { - timeSettingStatement.executeUpdate(); - } - plugin.getLogger().info("Database update complete!"); - } catch (SQLException e) { - plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning."); - e.printStackTrace(); - } finally { - // Update the config file version - plugin.getConfig().set("config_file_version", 8); - plugin.saveConfig(); + } + try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement( + "ALTER TABLE " + HuskHomes.getSettings().getWarpsDataTable() + + " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) { + tableUpdateStatement.execute(); + try (PreparedStatement timeSettingStatement = HuskHomes.getConnection().prepareStatement( + "UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) { + timeSettingStatement.executeUpdate(); } + plugin.getLogger().info("Database upgrade in progress... (2/2)"); + } catch (SQLException e) { + plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning."); + e.printStackTrace(); } + plugin.getLogger().info("Creation timestamp upgrade complete! (v8)"); + // Update the config file version + plugin.getLogger().info("All database upgrades have been completed!"); + plugin.getConfig().set("config_file_version", 8); + plugin.saveConfig(); } } diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java b/src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java index 6c170d49e..c4238db9f 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/SetPoint.java @@ -71,6 +71,9 @@ public long getCreationTime() { * @return The formatted creation time string */ public String getFormattedCreationTime() { + if (creationTime == 0) { + return "N/A"; + } return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT) .withLocale(Locale.getDefault()) .withZone(ZoneId.systemDefault())