From 4d80222604a49a3a3f26fff22b736f249acaea38 Mon Sep 17 00:00:00 2001 From: Thorinwasher Date: Sun, 22 Sep 2024 20:36:23 +0200 Subject: [PATCH] Change filename to nightly --- pom.xml | 2 +- .../migration/MigrationWorldLoadListener.java | 39 +++++++ .../util/LegacyPortalStorageLoader.java | 108 +++++++++--------- 3 files changed, 95 insertions(+), 54 deletions(-) create mode 100644 src/main/java/org/sgrewritten/stargate/migration/MigrationWorldLoadListener.java diff --git a/pom.xml b/pom.xml index 3015e2d9..249c4ff9 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.sgrewritten stargate - 1.0.0.17-ALPHA + 1.0.0.18-NIGHTLY Stargate The original, and still the best, transportation plugin for the Bukkit Ecosystem. https://sgrewritten.org diff --git a/src/main/java/org/sgrewritten/stargate/migration/MigrationWorldLoadListener.java b/src/main/java/org/sgrewritten/stargate/migration/MigrationWorldLoadListener.java new file mode 100644 index 00000000..1b4a408c --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/migration/MigrationWorldLoadListener.java @@ -0,0 +1,39 @@ +package org.sgrewritten.stargate.migration; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.world.WorldLoadEvent; +import org.sgrewritten.stargate.Stargate; +import org.sgrewritten.stargate.api.StargateAPI; +import org.sgrewritten.stargate.util.LegacyPortalStorageLoader; + +import java.io.File; +import java.io.IOException; +import java.util.Set; + +public class MigrationWorldLoadListener implements Listener { + private final Set invalidWorlds; + private final File dir; + private final String defaultNetworkName; + private final StargateAPI stargateAPI; + + + public MigrationWorldLoadListener(Set invalidWorlds, File dir, String defaultNetworkName, StargateAPI stargateAPI) { + this.invalidWorlds = invalidWorlds; + this.dir = dir; + this.defaultNetworkName = defaultNetworkName; + this.stargateAPI = stargateAPI; + } + + @EventHandler + void onWorldLoad(WorldLoadEvent event) { + String worldDatabaseName = event.getWorld().getName() + ".db"; + if (invalidWorlds.contains(worldDatabaseName)) { + try { + LegacyPortalStorageLoader.loadWorld(new File(dir, worldDatabaseName), defaultNetworkName, stargateAPI); + } catch (IOException e) { + Stargate.log(e); + } + } + } +} diff --git a/src/main/java/org/sgrewritten/stargate/util/LegacyPortalStorageLoader.java b/src/main/java/org/sgrewritten/stargate/util/LegacyPortalStorageLoader.java index 4adca295..a89e45b3 100644 --- a/src/main/java/org/sgrewritten/stargate/util/LegacyPortalStorageLoader.java +++ b/src/main/java/org/sgrewritten/stargate/util/LegacyPortalStorageLoader.java @@ -17,6 +17,7 @@ import org.sgrewritten.stargate.exception.name.NameConflictException; import org.sgrewritten.stargate.exception.name.NameLengthException; import org.sgrewritten.stargate.gate.Gate; +import org.sgrewritten.stargate.migration.MigrationWorldLoadListener; import org.sgrewritten.stargate.network.StorageType; import org.sgrewritten.stargate.network.portal.GlobalPortalId; import org.sgrewritten.stargate.network.portal.portaldata.PortalData; @@ -27,8 +28,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import java.util.logging.Level; import java.util.regex.Pattern; @@ -54,60 +54,66 @@ private LegacyPortalStorageLoader() { * @throws InvalidStructureException

If an encountered portal's structure is invalid

* @throws TranslatableException */ - public static @NotNull List loadPortalsFromStorage(File dir, - String defaultNetworkName, StargateAPI stargateAPI) - throws IOException, InvalidStructureException, TranslatableException { + public static @NotNull List loadPortalsFromStorage(File dir, String defaultNetworkName, StargateAPI stargateAPI) throws IOException, InvalidStructureException, TranslatableException { List portals = new ArrayList<>(); File[] files = dir.exists() ? dir.listFiles((directory, name) -> name.endsWith(".db")) : new File[0]; if (files == null) { return new ArrayList<>(); } + Set invalidWorlds = new HashSet<>(); for (File file : files) { - String worldName = DATABASE.matcher(file.getName()).replaceAll(""); - - BufferedReader reader = FileHelper.getBufferedReader(file); - String line = reader.readLine(); - List invalidPortals = new ArrayList<>(); - while (line != null) { - if (line.startsWith("#") || line.trim().isEmpty()) { - continue; - } - World world = Bukkit.getWorld(worldName); - if (world == null) { - Stargate.log(Level.WARNING, "Could not load portal from world: " + worldName); - Stargate.log(Level.WARNING, "Ignoring world..."); - break; - } - try { - Portal portal = readPortal(line, world, stargateAPI, defaultNetworkName); - if(portal != null) { - portals.add(portal); - } else { - String[] portalProperties = line.split(":"); - invalidPortals.add(new GlobalPortalId(portalProperties[0], (portalProperties.length > 9) ? portalProperties[9] : StargateConstant.DEFAULT_NETWORK_ID)); - } - } catch (InvalidStructureException e) { + Optional> worldLoadData = loadWorld(file, defaultNetworkName, stargateAPI); + worldLoadData.ifPresentOrElse(portals::addAll, () -> invalidWorlds.add(file.getName())); + } + if (stargateAPI instanceof Stargate stargate && !invalidWorlds.isEmpty()) { + Bukkit.getPluginManager().registerEvents(new MigrationWorldLoadListener(invalidWorlds, dir, defaultNetworkName, stargateAPI), stargate); + } + return portals; + } + + public static Optional> loadWorld(File file, String defaultNetworkName, StargateAPI stargateAPI) throws IOException { + String worldName = DATABASE.matcher(file.getName()).replaceAll(""); + World world = Bukkit.getWorld(worldName); + if (world == null) { + return Optional.empty(); + } + List portals = new ArrayList<>(); + BufferedReader reader = FileHelper.getBufferedReader(file); + String line = reader.readLine(); + List invalidPortals = new ArrayList<>(); + while (line != null) { + if (line.startsWith("#") || line.trim().isEmpty()) { + continue; + } + try { + Portal portal = readPortal(line, world, stargateAPI, defaultNetworkName); + if (portal != null) { + portals.add(portal); + } else { String[] portalProperties = line.split(":"); - invalidPortals.add(new GlobalPortalId(portalProperties[0], (portalProperties.length > 9) ? portalProperties[9] : StargateConstant.DEFAULT_NETWORK_ID) ); - } catch (Exception e) { - Stargate.log(e); + invalidPortals.add(new GlobalPortalId(portalProperties[0], (portalProperties.length > 9) ? portalProperties[9] : StargateConstant.DEFAULT_NETWORK_ID)); } - line = reader.readLine(); - } - reader.close(); - if(!invalidPortals.isEmpty()){ - final StringBuilder stringBuilder = new StringBuilder("Could not load the following portals:"); - invalidPortals.forEach(globalPortalId -> { - stringBuilder.append("\n"); - stringBuilder.append(globalPortalId.toString()); - }); - stringBuilder.append("\n\n"); - stringBuilder.append("This has most likely been caused by an invalid or absent gate format or an invalid network name"); - Stargate.log(Level.WARNING, stringBuilder.toString()); + } catch (InvalidStructureException e) { + String[] portalProperties = line.split(":"); + invalidPortals.add(new GlobalPortalId(portalProperties[0], (portalProperties.length > 9) ? portalProperties[9] : StargateConstant.DEFAULT_NETWORK_ID)); + } catch (Exception e) { + Stargate.log(e); } + line = reader.readLine(); } - return portals; + reader.close(); + if (!invalidPortals.isEmpty()) { + final StringBuilder stringBuilder = new StringBuilder("Could not load the following portals:"); + invalidPortals.forEach(globalPortalId -> { + stringBuilder.append("\n"); + stringBuilder.append(globalPortalId.toString()); + }); + stringBuilder.append("\n\n"); + stringBuilder.append("This has most likely been caused by an invalid or absent gate format or an invalid network name"); + Stargate.log(Level.WARNING, stringBuilder.toString()); + } + return Optional.of(portals); } /** @@ -119,8 +125,7 @@ private LegacyPortalStorageLoader() { * @throws InvalidStructureException

If the portal's structure is invalid

* @throws TranslatableException

If the portal's name is invalid

*/ - private static Portal readPortal(String line, World world, StargateAPI stargateAPI, - String defaultNetworkName) throws InvalidStructureException, TranslatableException { + private static Portal readPortal(String line, World world, StargateAPI stargateAPI, String defaultNetworkName) throws InvalidStructureException, TranslatableException { String[] portalProperties = line.split(":"); PortalData portalData = PortalStorageHelper.loadPortalData(portalProperties, world, defaultNetworkName); try { @@ -132,13 +137,11 @@ private static Portal readPortal(String line, World world, StargateAPI stargateA throw new InvalidStructureException(); } - Network network = stargateAPI.getRegistry().getNetwork(portalData.networkName(), - portalData.flags().contains(StargateFlag.INTERSERVER) ? StorageType.INTER_SERVER : StorageType.LOCAL); + Network network = stargateAPI.getRegistry().getNetwork(portalData.networkName(), portalData.flags().contains(StargateFlag.INTERSERVER) ? StorageType.INTER_SERVER : StorageType.LOCAL); Stargate.log(Level.INFO, "fetched networkName: " + portalData.networkName()); if (network == null) { - Stargate.log(Level.SEVERE, "Unable to get network " + portalData.networkName() + " during legacy" + - "portal loading"); + Stargate.log(Level.SEVERE, "Unable to get network " + portalData.networkName() + " during legacy" + "portal loading"); return null; } @@ -156,8 +159,7 @@ private static Portal readPortal(String line, World world, StargateAPI stargateA RealPortal portal = PortalCreationHelper.createPortal(network, portalData, gate, stargateAPI); - Stargate.log(Level.FINE, String.format("Saving portal %s in network %s from old storage... ", - portalData.name(), portalData.networkName())); + Stargate.log(Level.FINE, String.format("Saving portal %s in network %s from old storage... ", portalData.name(), portalData.networkName())); stargateAPI.getNetworkManager().savePortal(portal, network); return portal; }