feat: auto import the old portals and destinations if the folders don't exist

This commit is contained in:
Sekwah 2024-11-20 04:39:52 +00:00
parent 558d757b9e
commit 9f23a617ff
5 changed files with 188 additions and 130 deletions

View File

@ -114,6 +114,9 @@ public class ConfigRepositoryImpl implements ConfigRepository {
public void loadConfig(DataStorage dataStorage) {
this.dataStorage = dataStorage;
this.config = dataStorage.loadFile(Config.class, "config.yaml");
if(config == null) {
this.config = new Config();
}
}
@Override

View File

@ -4,13 +4,19 @@ import com.sekwah.advancedportals.core.AdvancedPortalsCore;
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 @@ import org.bukkit.plugin.java.JavaPlugin;
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 class AdvancedPortalsPlugin extends JavaPlugin {
Injector injector = module.getInjector();
injector.injectMembers(this);
injector.injectMembers(this.portalsCore);
injector.injectMembers(serverContainer);
@ -63,6 +76,8 @@ public class AdvancedPortalsPlugin extends JavaPlugin {
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 class AdvancedPortalsPlugin extends JavaPlugin {
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();

View File

@ -3,21 +3,19 @@ package com.sekwah.advancedportals.spigot.commands.subcommands.portal;
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 class ImportPortalSubCommand implements SubCommand {
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 class ImportPortalSubCommand implements SubCommand {
getDestinationCount()));
}
private int importPortals() {
ConfigAccessor portalConfig = new ConfigAccessor(
AdvancedPortalsPlugin.getInstance(), "portals.yml");
var config = portalConfig.getConfig();
Set<String> 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<DataTag> 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<String> 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<String> 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<DataTag> 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<String> 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");

View File

@ -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;

View File

@ -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<String> 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<DataTag> 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<String> 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<String> 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<String> 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<DataTag> tags, String arg) {
for (DataTag portalArg : tags) {
if (arg.equals(portalArg.NAME)) {
return portalArg.VALUES[0];
}
}
return null;
}
}