Skip to content

Commit

Permalink
feat: auto import the old portals and destinations if the folders don…
Browse files Browse the repository at this point in the history
…'t exist
  • Loading branch information
sekwah41 committed Nov 20, 2024
1 parent 558d757 commit 9f23a61
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@
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;

public class AdvancedPortalsPlugin extends JavaPlugin {
private AdvancedPortalsCore portalsCore;

@Inject
DestinationServices destinationServices;

@Inject
PortalServices portalServices;

private static AdvancedPortalsPlugin instance;

public static AdvancedPortalsPlugin getInstance() {
Expand Down Expand Up @@ -48,6 +60,7 @@ public void onEnable() {

Injector injector = module.getInjector();

injector.injectMembers(this);
injector.injectMembers(this.portalsCore);
injector.injectMembers(serverContainer);

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

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

0 comments on commit 9f23a61

Please sign in to comment.