Fixes a crash caused by StargateYamlConfiguration

This commit is contained in:
2023-04-20 16:02:57 +02:00
parent aa480faef2
commit 9d981f2cc6
7 changed files with 88 additions and 46 deletions

View File

@@ -14,6 +14,7 @@ import net.knarcraft.stargate.utility.PortalFileHelper;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.messaging.Messenger;
@@ -48,6 +49,7 @@ public final class StargateConfig {
private String gateFolder;
private String portalFolder;
private String languageName = "en";
private boolean isLoaded = false;
private final Map<ConfigOption, Object> configOptions;
@@ -109,6 +111,17 @@ public final class StargateConfig {
DynmapManager.initialize(dynmapAPI);
DynmapManager.addAllPortalMarkers();
}
this.isLoaded = true;
}
/**
* Gets whether this configuration has been fully loaded
*
* @return <p>True if fully loaded</p>
*/
public boolean isLoaded() {
return this.isLoaded;
}
/**
@@ -365,10 +378,11 @@ public final class StargateConfig {
FileConfiguration newConfig = Stargate.getInstance().getConfiguration();
boolean isMigrating = false;
if (newConfig.getString("lang") != null || newConfig.getString("economy.freeGatesGreen") != null ||
newConfig.getString("economy.taxAccount") == null) {
if (newConfig.getString("lang") != null || newConfig.getString("economy.taxAccount") == null) {
migrateConfig(newConfig);
isMigrating = true;
Stargate.getInstance().reloadConfig();
newConfig = Stargate.getInstance().getConfiguration();
}
//Copy missing default values if any values are missing
@@ -409,6 +423,7 @@ public final class StargateConfig {
//If users have an outdated config, assume they also need to update their default gates
if (isMigrating) {
this.createMissingFolders();
GateHandler.writeDefaultGatesToFolder(gateFolder);
}
@@ -486,6 +501,9 @@ public final class StargateConfig {
// Copy all keys to the new config
for (String key : oldConfiguration.getKeys(true)) {
if (oldConfiguration.get(key) instanceof MemorySection) {
continue;
}
Stargate.debug("Stargate::migrateConfig", "Setting " + key + " to " + oldConfiguration.get(key));
newConfiguration.set(key, oldConfiguration.get(key));
}
@@ -527,12 +545,8 @@ public final class StargateConfig {
* Creates missing folders
*/
private void createMissingFolders() {
File newPortalDir = new File(portalFolder);
if (!newPortalDir.exists()) {
if (!newPortalDir.mkdirs()) {
logger.severe("Unable to create portal directory");
}
}
createMissingFolder(new File(gateFolder), "Unable to create gate directory");
createMissingFolder(new File(portalFolder), "Unable to create portal directory");
File newFile = new File(portalFolder, Stargate.getInstance().getServer().getWorlds().get(0).getName() +
".db");
if (!newFile.exists() && !newFile.getParentFile().exists()) {
@@ -542,6 +556,20 @@ public final class StargateConfig {
}
}
/**
* Creates the given folder if it's missing
*
* @param folder <p>The folder to create</p>
* @param errorMessage <p>The error message to display if unable to create the folder</p>
*/
private void createMissingFolder(File folder, String errorMessage) {
if (!folder.exists()) {
if (!folder.mkdirs()) {
logger.severe(errorMessage);
}
}
}
/**
* Gets the folder all portals are stored in
*

View File

@@ -313,14 +313,16 @@ public final class StargateGateConfig {
try {
PortalSignDrawer.setMainColor(ChatColor.of(mainSignColor.toUpperCase()));
} catch (IllegalArgumentException | NullPointerException exception) {
Stargate.logWarning("You have specified an invalid main sign color in your config.yml. Defaulting to BLACK");
Stargate.logWarning("You have specified an invalid main sign color in your config.yml (" + mainSignColor +
"). Defaulting to BLACK");
PortalSignDrawer.setMainColor(ChatColor.BLACK);
}
try {
PortalSignDrawer.setHighlightColor(ChatColor.of(highlightSignColor.toUpperCase()));
} catch (IllegalArgumentException | NullPointerException exception) {
Stargate.logWarning("You have specified an invalid highlighting sign color in your config.yml. Defaulting to WHITE");
Stargate.logWarning("You have specified an invalid highlighting sign color in your config.yml (" +
highlightSignColor + "). Defaulting to WHITE");
PortalSignDrawer.setHighlightColor(ChatColor.WHITE);
}
}

View File

@@ -16,6 +16,7 @@ import java.util.List;
*/
public class StargateYamlConfiguration extends YamlConfiguration {
public static final String START_OF_COMMENT_LINE = "[HASHTAG]";
static public final String END_OF_COMMENT = "_endOfComment_";
static public final String START_OF_COMMENT = "comment_";
@@ -51,7 +52,8 @@ public class StargateYamlConfiguration extends YamlConfiguration {
for (String line : configString.split("\n")) {
if (line.trim().startsWith("#")) {
//Temporarily store the comment line
currentComment.add(line.trim().replaceFirst("#", ""));
currentComment.add(line.trim().replaceFirst(line.trim().startsWith("# ") ? "# " : "{2}#",
START_OF_COMMENT_LINE));
} else {
//Write the full formatted comment to the StringBuilder
if (!currentComment.isEmpty()) {
@@ -119,6 +121,7 @@ public class StargateYamlConfiguration extends YamlConfiguration {
//Output the empty line as-is, as it's not part of a comment
finalText.append("\n");
} else if (isReadingCommentBlock) {
possibleComment = possibleComment.replace(START_OF_COMMENT_LINE, "");
//Output the comment with correct indentation
finalText.append(addIndentation(commentIndentation)).append("# ").append(possibleComment).append("\n");
} else {