diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java index 8bbf8dc3..d1f489f3 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/ConfigRepositoryImpl.java @@ -114,6 +114,9 @@ public boolean getDisableGatewayBeam() { public void loadConfig(DataStorage dataStorage) { this.dataStorage = dataStorage; this.config = dataStorage.loadFile(Config.class, "config.yaml"); + if(config == null) { + this.config = new Config(); + } } @Override diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java index fb806036..196182e8 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/AdvancedPortalsPlugin.java @@ -4,13 +4,19 @@ import com.sekwah.advancedportals.core.connector.commands.CommandRegister; import com.sekwah.advancedportals.core.module.AdvancedPortalsModule; import com.sekwah.advancedportals.core.permissions.Permissions; +import com.sekwah.advancedportals.core.services.DestinationServices; +import com.sekwah.advancedportals.core.services.PortalServices; import com.sekwah.advancedportals.core.util.GameScheduler; +import com.sekwah.advancedportals.shadowed.inject.Inject; import com.sekwah.advancedportals.shadowed.inject.Injector; import com.sekwah.advancedportals.spigot.commands.subcommands.portal.ImportPortalSubCommand; import com.sekwah.advancedportals.spigot.connector.command.SpigotCommandRegister; import com.sekwah.advancedportals.spigot.connector.container.SpigotServerContainer; +import com.sekwah.advancedportals.spigot.importer.LegacyImporter; import com.sekwah.advancedportals.spigot.metrics.Metrics; import com.sekwah.advancedportals.spigot.warpeffects.SpigotWarpEffects; + +import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.bukkit.plugin.java.JavaPlugin; @@ -18,6 +24,12 @@ public class AdvancedPortalsPlugin extends JavaPlugin { private AdvancedPortalsCore portalsCore; + @Inject + DestinationServices destinationServices; + + @Inject + PortalServices portalServices; + private static AdvancedPortalsPlugin instance; public static AdvancedPortalsPlugin getInstance() { @@ -48,6 +60,7 @@ public void onEnable() { Injector injector = module.getInjector(); + injector.injectMembers(this); injector.injectMembers(this.portalsCore); injector.injectMembers(serverContainer); @@ -63,6 +76,8 @@ public void onEnable() { injector.injectMembers(warpEffects); warpEffects.registerEffects(); + checkAndCreateConfig(); + // Try to do this after setting up everything that would need to be // injected to. this.portalsCore.onEnable(); @@ -73,6 +88,28 @@ public void onEnable() { new Metrics(this); } + private void checkAndCreateConfig() { + if (!this.getDataFolder().exists()) { + this.getDataFolder().mkdirs(); + } + + File destiFile = new File(this.getDataFolder(), "destinations.yml"); + File destiFolder = new File(this.getDataFolder(), "desti"); + if (destiFile.exists() && !destiFolder.exists()) { + destiFolder.mkdirs(); + getLogger().info("Importing old destinations from destinations.yaml"); + LegacyImporter.importDestinations(this.destinationServices); + } + + File portalFile = new File(this.getDataFolder(), "portals.yml"); + File portalFolder = new File(this.getDataFolder(), "portals"); + if (portalFile.exists() && !portalFolder.exists()) { + portalFolder.mkdirs(); + getLogger().info("Importing old portals from portals.yaml"); + LegacyImporter.importPortals(this.portalServices); + } + } + @Override public void onDisable() { this.portalsCore.onDisable(); diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/ImportPortalSubCommand.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/ImportPortalSubCommand.java index b02ce3e0..61e5168e 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/ImportPortalSubCommand.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/ImportPortalSubCommand.java @@ -3,21 +3,19 @@ import com.sekwah.advancedportals.core.commands.SubCommand; import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer; import com.sekwah.advancedportals.core.permissions.Permissions; -import com.sekwah.advancedportals.core.serializeddata.BlockLocation; -import com.sekwah.advancedportals.core.serializeddata.DataTag; -import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; import com.sekwah.advancedportals.core.services.DestinationServices; import com.sekwah.advancedportals.core.services.PortalServices; import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.shadowed.inject.Inject; import com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin; -import com.sekwah.advancedportals.spigot.commands.subcommands.portal.importer.ConfigAccessor; -import java.util.ArrayList; +import com.sekwah.advancedportals.spigot.importer.ConfigAccessor; +import com.sekwah.advancedportals.spigot.importer.LegacyImporter; + import java.util.List; import java.util.Set; -import org.bukkit.configuration.ConfigurationSection; public class ImportPortalSubCommand implements SubCommand { + @Inject DestinationServices destinationServices; @@ -30,8 +28,8 @@ public void onCommand(CommandSenderContainer sender, String[] args) { sender.sendMessage(Lang.getPositivePrefix() + Lang.translateInsertVariables( "command.portal.import.confirm")); - int destinations = importDestinations(); - int portals = importPortals(); + int destinations = LegacyImporter.importDestinations(destinationServices); + int portals = LegacyImporter.importPortals(portalServices); sender.sendMessage( Lang.getPositivePrefix() + Lang.translateInsertVariables( @@ -44,127 +42,6 @@ public void onCommand(CommandSenderContainer sender, String[] args) { getDestinationCount())); } - private int importPortals() { - ConfigAccessor portalConfig = new ConfigAccessor( - AdvancedPortalsPlugin.getInstance(), "portals.yml"); - var config = portalConfig.getConfig(); - Set portalSet = config.getKeys(false); - - int count = 0; - for (String portalName : portalSet) { - BlockLocation pos1 = - new BlockLocation(config.getString(portalName + ".world"), - config.getInt(portalName + ".pos1.X"), - config.getInt(portalName + ".pos1.Y"), - config.getInt(portalName + ".pos1.Z")); - BlockLocation pos2 = - new BlockLocation(config.getString(portalName + ".world"), - config.getInt(portalName + ".pos2.X"), - config.getInt(portalName + ".pos2.Y"), - config.getInt(portalName + ".pos2.Z")); - List args = new ArrayList<>(); - args.add(new DataTag("name", portalName)); - var triggerblock = config.getString(portalName + ".triggerblock"); - if (triggerblock != null) - args.add(new DataTag("triggerblock", triggerblock.split(","))); - // It's called bungee as that's the implementation behind it - - var destination = config.getString(portalName + ".destination"); - if (destination != null) - args.add(new DataTag("destination", destination.split(","))); - - var bungee = config.getString(portalName + ".bungee"); - if (bungee != null) { - if (destination == null) { - args.add(new DataTag("bungee", bungee.split(","))); - } else { - args.add(new DataTag("proxy", bungee.split(","))); - } - } - - ConfigurationSection portalConfigSection = - config.getConfigurationSection(portalName); - ConfigurationSection portalArgsConf = - portalConfigSection.getConfigurationSection("portalArgs"); - - if (portalArgsConf != null) { - Set argsSet = portalArgsConf.getKeys(true); - for (Object argName : argsSet.toArray()) { - // skip if it argName starts with command. - if (portalArgsConf.isString(argName.toString())) { - args.add(new DataTag( - argName.toString(), - portalArgsConf.getString(argName.toString()))); - } - } - } - // Check if command.1 is set - List commands = new ArrayList<>(); - if (getArg(args, "command.1") != null) { - int i = 1; - while (getArg(args, "command." + i) != null) { - commands.add(getArg(args, "command." + i)); - i++; - } - } - if (!commands.isEmpty()) { - args.add( - new DataTag("commands", commands.toArray(new String[0]))); - } - args.stream() - .filter(dataTag -> dataTag.NAME.startsWith("command.")) - .toList() - .forEach(args::remove); - - // Find an arg called "delayed" and add a new one called portalEvent - var delayed = getArg(args, "delayed"); - if (delayed != null) { - args.add(new DataTag("portalEvent", delayed)); - args.removeIf(dataTag -> dataTag.NAME.equals("delayed")); - } - - var portal = portalServices.createPortal(pos1, pos2, args); - - if (portal != null) - count++; - } - - return count; - } - - public String getArg(List tags, String arg) { - for (DataTag portalArg : tags) { - if (arg.equals(portalArg.NAME)) { - return portalArg.VALUES[0]; - } - } - return null; - } - - public int importDestinations() { - ConfigAccessor destiConfig = new ConfigAccessor( - AdvancedPortalsPlugin.getInstance(), "destinations.yml"); - var config = destiConfig.getConfig(); - Set destiSet = config.getKeys(false); - - int count = 0; - for (String destiName : destiSet) { - var destiPos = destiName + ".pos"; - var desti = destinationServices.createDesti( - new PlayerLocation( - config.getString(destiName + ".world"), - config.getDouble(destiPos + ".X"), - config.getDouble(destiPos + ".Y"), - config.getDouble(destiPos + ".Z"), - (float) config.getDouble(destiPos + ".yaw"), - (float) config.getDouble(destiPos + ".pitch")), - List.of(new DataTag("name", destiName))); - if (desti != null) - count++; - } - return count; - } - public int getDestinationCount() { ConfigAccessor destiConfig = new ConfigAccessor( AdvancedPortalsPlugin.getInstance(), "destinations.yaml"); diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/importer/ConfigAccessor.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/importer/ConfigAccessor.java similarity index 96% rename from spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/importer/ConfigAccessor.java rename to spigot/src/main/java/com/sekwah/advancedportals/spigot/importer/ConfigAccessor.java index c4dce614..a15126af 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/commands/subcommands/portal/importer/ConfigAccessor.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/importer/ConfigAccessor.java @@ -1,4 +1,4 @@ -package com.sekwah.advancedportals.spigot.commands.subcommands.portal.importer; +package com.sekwah.advancedportals.spigot.importer; import java.io.File; import java.io.IOException; diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/importer/LegacyImporter.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/importer/LegacyImporter.java new file mode 100644 index 00000000..73435c17 --- /dev/null +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/importer/LegacyImporter.java @@ -0,0 +1,141 @@ +package com.sekwah.advancedportals.spigot.importer; + +import com.sekwah.advancedportals.core.serializeddata.BlockLocation; +import com.sekwah.advancedportals.core.serializeddata.DataTag; +import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; +import com.sekwah.advancedportals.core.services.DestinationServices; +import com.sekwah.advancedportals.core.services.PortalServices; +import com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin; +import org.bukkit.configuration.ConfigurationSection; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class LegacyImporter { + + private LegacyImporter() { + } + + public static int importPortals(PortalServices portalServices) { + ConfigAccessor portalConfig = new ConfigAccessor( + AdvancedPortalsPlugin.getInstance(), "portals.yml"); + var config = portalConfig.getConfig(); + Set portalSet = config.getKeys(false); + + int count = 0; + for (String portalName : portalSet) { + BlockLocation pos1 = + new BlockLocation(config.getString(portalName + ".world"), + config.getInt(portalName + ".pos1.X"), + config.getInt(portalName + ".pos1.Y"), + config.getInt(portalName + ".pos1.Z")); + BlockLocation pos2 = + new BlockLocation(config.getString(portalName + ".world"), + config.getInt(portalName + ".pos2.X"), + config.getInt(portalName + ".pos2.Y"), + config.getInt(portalName + ".pos2.Z")); + List args = new ArrayList<>(); + args.add(new DataTag("name", portalName)); + var triggerblock = config.getString(portalName + ".triggerblock"); + if (triggerblock != null) + args.add(new DataTag("triggerblock", triggerblock.split(","))); + // It's called bungee as that's the implementation behind it + + var destination = config.getString(portalName + ".destination"); + if (destination != null) + args.add(new DataTag("destination", destination.split(","))); + + var bungee = config.getString(portalName + ".bungee"); + if (bungee != null) { + if (destination == null) { + args.add(new DataTag("bungee", bungee.split(","))); + } else { + args.add(new DataTag("proxy", bungee.split(","))); + } + } + + ConfigurationSection portalConfigSection = + config.getConfigurationSection(portalName); + ConfigurationSection portalArgsConf = + portalConfigSection.getConfigurationSection("portalArgs"); + + if (portalArgsConf != null) { + Set argsSet = portalArgsConf.getKeys(true); + for (Object argName : argsSet.toArray()) { + // skip if it argName starts with command. + if (portalArgsConf.isString(argName.toString())) { + args.add(new DataTag( + argName.toString(), + portalArgsConf.getString(argName.toString()))); + } + } + } + // Check if command.1 is set + List commands = new ArrayList<>(); + if (getArg(args, "command.1") != null) { + int i = 1; + while (getArg(args, "command." + i) != null) { + commands.add(getArg(args, "command." + i)); + i++; + } + } + if (!commands.isEmpty()) { + args.add( + new DataTag("commands", commands.toArray(new String[0]))); + } + args.stream() + .filter(dataTag -> dataTag.NAME.startsWith("command.")) + .toList() + .forEach(args::remove); + + // Find an arg called "delayed" and add a new one called portalEvent + var delayed = getArg(args, "delayed"); + if (delayed != null) { + args.add(new DataTag("portalEvent", delayed)); + args.removeIf(dataTag -> dataTag.NAME.equals("delayed")); + } + + var portal = portalServices.createPortal(pos1, pos2, args); + + if (portal != null) + count++; + } + + return count; + } + + public static int importDestinations(DestinationServices destinationServices) { + ConfigAccessor destiConfig = new ConfigAccessor( + AdvancedPortalsPlugin.getInstance(), "destinations.yml"); + var config = destiConfig.getConfig(); + Set destiSet = config.getKeys(false); + + int count = 0; + for (String destiName : destiSet) { + var destiPos = destiName + ".pos"; + var desti = destinationServices.createDesti( + new PlayerLocation( + config.getString(destiName + ".world"), + config.getDouble(destiPos + ".X"), + config.getDouble(destiPos + ".Y"), + config.getDouble(destiPos + ".Z"), + (float) config.getDouble(destiPos + ".yaw"), + (float) config.getDouble(destiPos + ".pitch")), + List.of(new DataTag("name", destiName))); + if (desti != null) + count++; + } + return count; + } + + public static String getArg(List tags, String arg) { + for (DataTag portalArg : tags) { + if (arg.equals(portalArg.NAME)) { + return portalArg.VALUES[0]; + } + } + return null; + } + +}