-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spigot): add command for porting portals and destinations to the…
… new format
- Loading branch information
Showing
7 changed files
with
300 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...com/sekwah/advancedportals/spigot/commands/subcommands/portal/UpdatePortalSubCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package com.sekwah.advancedportals.spigot.commands.subcommands.portal; | ||
|
||
import com.google.inject.Inject; | ||
import com.sekwah.advancedportals.core.commands.SubCommand; | ||
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer; | ||
import com.sekwah.advancedportals.core.destination.Destination; | ||
import com.sekwah.advancedportals.core.portal.AdvancedPortal; | ||
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.spigot.AdvancedPortalsPlugin; | ||
import com.sekwah.advancedportals.spigot.commands.subcommands.portal.update.ConfigAccessor; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class UpdatePortalSubCommand implements SubCommand { | ||
|
||
@Inject | ||
PortalServices portalServices; | ||
|
||
@Inject | ||
DestinationServices destinationServices; | ||
|
||
@Inject | ||
PortalServices portalService; | ||
|
||
@Override | ||
public void onCommand(CommandSenderContainer sender, String[] args) { | ||
if(args.length > 1 && "confirm".equals(args[1])) { | ||
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translateInsertVariables("command.portal.update.confirm")); | ||
int destinations = importDestinations(); | ||
int portals = importPortals(); | ||
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translateInsertVariables("command.portal.update.complete", portals, destinations)); | ||
return; | ||
} | ||
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translateInsertVariables("command.portal.update", getPortalCount(), 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)); | ||
args.add(new DataTag("triggerblock", config.getString(portalName + ".triggerblock"))); | ||
// It's called bungee as that's the implementation behind it | ||
args.add(new DataTag("bungee", config.getString(portalName + ".bungee"))); | ||
args.add(new DataTag("destination", config.getString(portalName + ".destination"))); | ||
|
||
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); | ||
|
||
var portal = portalService.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 desti = destinationServices.createDesti(new PlayerLocation(config.getString(destiName + ".world"), | ||
config.getDouble(destiName + ".x"), | ||
config.getDouble(destiName + ".y"), | ||
config.getDouble(destiName + ".z"), | ||
(float) config.getDouble(destiName + ".yaw"), | ||
(float) config.getDouble(destiName + ".pitch")), List.of(new DataTag("name", destiName))); | ||
if(desti != null) count++; | ||
} | ||
return count; | ||
} | ||
|
||
public int getDestinationCount() { | ||
ConfigAccessor destiConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "destinations.yml"); | ||
var config = destiConfig.getConfig(); | ||
Set<String> destiSet = config.getKeys(false); | ||
|
||
return destiSet.size(); | ||
} | ||
|
||
public int getPortalCount() { | ||
ConfigAccessor portalConfig = new ConfigAccessor(AdvancedPortalsPlugin.getInstance(), "portals.yml"); | ||
var config = portalConfig.getConfig(); | ||
Set<String> portalSet = config.getKeys(false); | ||
|
||
return portalSet.size(); | ||
} | ||
|
||
@Override | ||
public boolean hasPermission(CommandSenderContainer sender) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<String> onTabComplete(CommandSenderContainer sender, String[] args) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getBasicHelpText() { | ||
return Lang.translate("command.portal.update.help"); | ||
} | ||
|
||
@Override | ||
public String getDetailedHelpText() { | ||
return Lang.translate("command.portal.update.detailedhelp"); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
.../com/sekwah/advancedportals/spigot/commands/subcommands/portal/update/ConfigAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.sekwah.advancedportals.spigot.commands.subcommands.portal.update; | ||
|
||
import org.bukkit.configuration.file.FileConfiguration; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
|
||
public class ConfigAccessor { | ||
|
||
private final String fileName; | ||
private final JavaPlugin plugin; | ||
|
||
private File configFile; | ||
private FileConfiguration fileConfiguration; | ||
|
||
public ConfigAccessor(JavaPlugin plugin, String fileName) { | ||
this.plugin = plugin; | ||
this.fileName = fileName; | ||
} | ||
|
||
// gets all of the | ||
public void reloadConfig() { | ||
if (configFile == null) { | ||
File dataFolder = plugin.getDataFolder(); | ||
if (dataFolder == null) | ||
throw new IllegalStateException(); | ||
configFile = new File(dataFolder, fileName); | ||
} | ||
fileConfiguration = YamlConfiguration.loadConfiguration(configFile); | ||
} | ||
|
||
public FileConfiguration getConfig() { | ||
if (fileConfiguration == null) { | ||
this.reloadConfig(); | ||
} | ||
return fileConfiguration; | ||
} | ||
|
||
// Saves all the data to the selected yml file | ||
public void saveConfig() { | ||
if (fileConfiguration == null || configFile == null) { | ||
return; | ||
} else { | ||
try { | ||
getConfig().save(configFile); | ||
} catch (IOException ex) { | ||
plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex); | ||
} | ||
} | ||
} | ||
|
||
// Saves | ||
|
||
/** | ||
* public void saveDefaultConfig() { | ||
* if (!configFile.exists()) { | ||
* this.plugin.saveResource(fileName, false); | ||
* } | ||
* } | ||
*/ | ||
|
||
// New save default config saving code, it checks if the needed config is in the jar file before | ||
// overriding it. | ||
public void saveDefaultConfig() { | ||
if (configFile == null) { | ||
configFile = new File(plugin.getDataFolder(), fileName); | ||
} | ||
if (!configFile.exists()) { | ||
plugin.saveResource(fileName, false); | ||
} | ||
} | ||
|
||
} |