Skip to content

Commit

Permalink
Config files will update automatically again
Browse files Browse the repository at this point in the history
  • Loading branch information
nossr50 committed Apr 11, 2023
1 parent 921a022 commit 0ab9358
Show file tree
Hide file tree
Showing 45 changed files with 154 additions and 196 deletions.
4 changes: 4 additions & 0 deletions Changelog.txt
@@ -1,3 +1,7 @@
Version 2.1.220
Config files update automatically again
Default configs are now copied to plugins/mcMMO/defaults for easy reference, these configs will always match the default values of the config in the JAR

Version 2.1.219
Fixed Fishing exploit protection being triggered inappropriately by other plugins (Thanks smudgge)
Fixed wiki url being incorrect in commands
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.nossr50.mcMMO</groupId>
<artifactId>mcMMO</artifactId>
<version>2.1.219</version>
<version>2.1.220-SNAPSHOT</version>
<name>mcMMO</name>
<url>https://github.com/mcMMO-Dev/mcMMO</url>
<scm>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/gmail/nossr50/api/ExperienceAPI.java
Expand Up @@ -41,7 +41,7 @@ public static boolean isValidSkillType(@NotNull String skillType) {

/**
* Start the task that gives combat XP.
* Processes combat XP like mcMMO normally would, so mcMMO will check whether or not the entity should reward XP when giving out the XP
* Processes combat XP like mcMMO normally would, so mcMMO will check whether the entity should reward XP when giving out the XP
*
* @param mcMMOPlayer The attacking player
* @param target The defending entity
Expand All @@ -56,7 +56,7 @@ public static void addCombatXP(McMMOPlayer mcMMOPlayer, LivingEntity target, Pri

/**
* Start the task that gives combat XP.
* Processes combat XP like mcMMO normally would, so mcMMO will check whether or not the entity should reward XP when giving out the XP
* Processes combat XP like mcMMO normally would, so mcMMO will check whether the entity should reward XP when giving out the XP
*
* @param mcMMOPlayer The attacking player
* @param target The defending entity
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/gmail/nossr50/chat/ChatManager.java
Expand Up @@ -43,7 +43,7 @@ public ChatManager(@NotNull mcMMO pluginRef) {
*
* @param mmoPlayer target player
* @param rawMessage the raw message from the player as it was typed
* @param isAsync whether or not this is getting processed via async
* @param isAsync whether this is getting processed via async
*/
public void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull String rawMessage, boolean isAsync) {
processPlayerMessage(mmoPlayer, mmoPlayer.getChatChannel(), rawMessage, isAsync);
Expand All @@ -69,7 +69,7 @@ public void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull String
* @param mmoPlayer target player
* @param chatChannel target chat channel
* @param rawMessage raw chat message as it was typed
* @param isAsync whether or not this is getting processed via async
* @param isAsync whether this is getting processed via async
*/
private void processPlayerMessage(@NotNull McMMOPlayer mmoPlayer, @NotNull ChatChannel chatChannel, @NotNull String rawMessage, boolean isAsync) {
switch (chatChannel) {
Expand Down Expand Up @@ -155,7 +155,7 @@ public void setOrToggleChatChannel(@NotNull McMMOPlayer mmoPlayer, @NotNull Chat
}

/**
* Whether or not the player is allowed to send a message to the chat channel they are targeting
* Whether the player is allowed to send a message to the chat channel they are targeting
* @param mmoPlayer target player
* @return true if the player can send messages to that chat channel
*/
Expand Down Expand Up @@ -197,7 +197,7 @@ public boolean isChatEnabled() {
}

/**
* Whether or not a specific chat channel is enabled
* Whether a specific chat channel is enabled
* ChatChannels are enabled/disabled via user config
*
* If chat is disabled, this always returns false
Expand Down
Expand Up @@ -73,7 +73,7 @@ private void updateLastKnownDisplayName() {
* Sanitized names are associated with a {@link ChatChannel} as different chat channels have different chat name settings
*
* @param chatChannel target chat channel
* @param useDisplayName whether or not to use this authors display name
* @param useDisplayName whether to use this authors display name
*/
private void updateSanitizedNameCache(@NotNull ChatChannel chatChannel, boolean useDisplayName) {
if(useDisplayName) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/gmail/nossr50/chat/author/Author.java
Expand Up @@ -17,14 +17,14 @@ public interface Author extends Identity {
@NotNull String getAuthoredName(@NotNull ChatChannel chatChannel);

/**
* Whether or not this author is a {@link org.bukkit.command.ConsoleCommandSender}
* Whether this author is a {@link org.bukkit.command.ConsoleCommandSender}
*
* @return true if this author is the console
*/
boolean isConsole();

/**
* Whether or not this author is a {@link org.bukkit.entity.Player}
* Whether this author is a {@link org.bukkit.entity.Player}
* @return true if this author is a player
*/
boolean isPlayer();
Expand Down
Expand Up @@ -73,8 +73,8 @@ public void sendMail(@NotNull ChatMessage chatMessage) {
*
* @param author the author
* @param rawString the raw message as the author typed it before any styling
* @param isAsync whether or not this is being processed asynchronously
* @param canColor whether or not the author can use colors in chat
* @param isAsync whether this is being processed asynchronously
* @param canColor whether the author can use colors in chat
*/
public void processChatMessage(@NotNull Author author, @NotNull String rawString, boolean isAsync, boolean canColor) {
AdminChatMessage chatMessage = new AdminChatMessage(pluginRef, author, constructAudience(), rawString, addStyle(author, rawString, canColor));
Expand Down
Expand Up @@ -27,8 +27,8 @@ public PartyChatMailer(@NotNull Plugin pluginRef) {
*
* @param author the author
* @param rawString the raw message as the author typed it before any styling
* @param isAsync whether or not this is being processed asynchronously
* @param canColor whether or not the author can use colors in chat
* @param isAsync whether this is being processed asynchronously
* @param canColor whether the author can use colors in chat
*/
public void processChatMessage(@NotNull Author author, @NotNull String rawString, @NotNull Party party, boolean isAsync, boolean canColor, boolean isLeader) {
PartyChatMessage chatMessage = new PartyChatMessage(pluginRef, author, constructPartyAudience(party), rawString, addStyle(author, rawString, canColor, isLeader), party);
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/gmail/nossr50/config/AdvancedConfig.java
Expand Up @@ -17,11 +17,6 @@ public AdvancedConfig(File dataFolder) {
validate();
}

@Override
public void initDefaults() {
config.addDefault("Skills.General.StartingLevel", 0);
}

@Override
protected boolean validateKeys() {
// Validate all the settings!
Expand Down Expand Up @@ -427,7 +422,7 @@ public boolean allowPlayerTips() {

/**
* This returns the maximum level at which superabilities will stop lengthening from scaling alongside skill level.
* It returns a different value depending on whether or not the server is in retro mode
* It returns a different value depending on whether the server is in retro mode
*
* @return the level at which abilities stop increasing in length
*/
Expand All @@ -440,7 +435,7 @@ public int getAbilityLengthCap() {

/**
* This returns the frequency at which abilities will increase in length
* It returns a different value depending on whether or not the server is in retro mode
* It returns a different value depending on whether the server is in retro mode
*
* @return the number of levels required per ability length increase
*/
Expand Down
185 changes: 75 additions & 110 deletions src/main/java/com/gmail/nossr50/config/BukkitConfig.java
Expand Up @@ -7,53 +7,112 @@

import java.io.*;
import java.util.HashSet;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;

public abstract class BukkitConfig {
public static final String CONFIG_PATCH_PREFIX = "ConfigPatchVersion:";
public static final String CURRENT_CONFIG_PATCH_VER = "ConfigPatchVersion: 2";
public static final char COMMENT_PREFIX = '#';
boolean copyDefaults = true;
protected final String fileName;
protected final File configFile;
protected YamlConfiguration defaultYamlConfig;
protected YamlConfiguration config;
protected @NotNull
final File dataFolder;
protected @NotNull final File dataFolder;

public BukkitConfig(@NotNull String fileName, @NotNull File dataFolder) {
mcMMO.p.getLogger().info("[config] Initializing config: " + fileName);
this.fileName = fileName;
this.dataFolder = dataFolder;
configFile = new File(dataFolder, fileName);
// purgeComments(true);
this.defaultYamlConfig = copyDefaultConfig();
this.config = initConfig();
updateFile();
mcMMO.p.getLogger().info("[config] Config initialized: " + fileName);
}

public BukkitConfig(@NotNull String fileName, @NotNull File dataFolder, boolean copyDefaults) {
mcMMO.p.getLogger().info("[config] Initializing config: " + fileName);
this.copyDefaults = copyDefaults;
this.fileName = fileName;
this.dataFolder = dataFolder;
configFile = new File(dataFolder, fileName);
this.defaultYamlConfig = copyDefaultConfig();
this.config = initConfig();
initDefaults();
updateFile();
mcMMO.p.getLogger().info("[config] Config initialized: " + fileName);
}

@Deprecated
public BukkitConfig(@NotNull String fileName) {
this(fileName, mcMMO.p.getDataFolder());
}

/**
* Initialize default values for the config
*/
public void initDefaults() {}
public BukkitConfig(@NotNull String fileName, boolean copyDefaults) {
this(fileName, mcMMO.p.getDataFolder(), copyDefaults);
}

/**
* Update the file on the disk by copying out any new and missing defaults
*/
public void updateFile() {
try {
if(copyDefaults) {
copyMissingDefaultsFromResource();
}
config.save(configFile);
} catch (IOException e) {
e.printStackTrace();
}
}

private YamlConfiguration initConfig() {
/**
* Copies missing keys and values from the internal resource config within the JAR
*/
private void copyMissingDefaultsFromResource() {
boolean updated = false;
for (String key : defaultYamlConfig.getKeys(true)) {
if (!config.contains(key)) {
config.set(key, defaultYamlConfig.get(key));
updated = true;
}
}

if (updated) {
updateFile();
}
}

/**
* Copies the config from the JAR to defaults/<fileName>
*/
YamlConfiguration copyDefaultConfig() {
mcMMO.p.getLogger().info("[config] Copying default config to disk: " + fileName + " to defaults/" + fileName);
try(InputStream inputStream = mcMMO.p.getResource(fileName)) {
if(inputStream == null) {
mcMMO.p.getLogger().severe("[config] Unable to copy default config: " + fileName);
return null;
}

//Save default file into defaults/<fileName>
File defaultsFolder = new File(dataFolder, "defaults");
if (!defaultsFolder.exists()) {
defaultsFolder.mkdir();
}
File defaultFile = new File(defaultsFolder, fileName);
Path path = defaultFile.toPath();
Files.copy(inputStream, path, java.nio.file.StandardCopyOption.REPLACE_EXISTING);

// Load file into YAML config
YamlConfiguration defaultYamlConfig = new YamlConfiguration();
defaultYamlConfig.load(defaultFile);
return defaultYamlConfig;
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
return null;
}

YamlConfiguration initConfig() {
if (!configFile.exists()) {
mcMMO.p.getLogger().info("[config] User config file not found, copying a default config to disk: " + fileName);
mcMMO.p.saveResource(fileName, false);
Expand Down Expand Up @@ -106,8 +165,8 @@ protected void validate() {
}

public void backup() {
mcMMO.p.getLogger().severe("You are using an old version of the " + fileName + " file.");
mcMMO.p.getLogger().severe("Your old file has been renamed to " + fileName + ".old and has been replaced by an updated version.");
mcMMO.p.getLogger().info("You are using an old version of the " + fileName + " file.");
mcMMO.p.getLogger().info("Your old file has been renamed to " + fileName + ".old and has been replaced by an updated version.");

configFile.renameTo(new File(configFile.getPath() + ".old"));

Expand All @@ -123,98 +182,4 @@ public void backup() {
public File getFile() {
return configFile;
}

// /**
// * Somewhere between December 2021-January 2022 Spigot updated their
// * SnakeYAML dependency/API and due to our own crappy legacy code
// * this introduced a very problematic bug where comments got duplicated
// * <p>
// * This method hotfixes the problem by just deleting any existing comments
// * it's ugly, but it gets the job done
// *
// * @param silentFail when true mcMMO will report errors during the patch process or debug information
// * the option to have it fail silently is because mcMMO wants to check files before they are parsed as a file with a zillion comments will fail to even load
// */
// private void purgeComments(boolean silentFail) {
// if(!configFile.exists())
// return;
//
// int dupedLines = 0, lineCount = 0, lineCountAfter = 0;
// try (FileReader fileReader = new FileReader(configFile);
// BufferedReader bufferedReader = new BufferedReader(fileReader)) {
// StringBuilder stringBuilder = new StringBuilder();
// String line;
// Set<String> seenBefore = new HashSet<>();
//
// stringBuilder.append(CURRENT_CONFIG_PATCH_VER).append(System.lineSeparator());
// boolean noPatchNeeded = false;
//
// // While not at the end of the file
// while ((line = bufferedReader.readLine()) != null) {
// lineCount++;
//
// if(line.startsWith(CURRENT_CONFIG_PATCH_VER)) {
// noPatchNeeded = true;
// break;
// }
//
// //Older version, don't append this line
// if(line.startsWith(CONFIG_PATCH_PREFIX))
// continue;
//
// if (isFirstCharAsciiCharacter(line, COMMENT_PREFIX)) {
// if(seenBefore.contains(line))
// dupedLines++;
// else
// seenBefore.add(line);
//
// continue; //Delete the line by not appending it
// }
//
// stringBuilder
// .append(line) //Convert existing files into two-spaced format
// .append(System.lineSeparator());
// lineCountAfter++;
// }
//
// if(noPatchNeeded)
// return;
//
// if(lineCount == 0 && !silentFail) {
// mcMMO.p.getLogger().info("[config patcher] Config line count: " + lineCount);
// throw new InvalidConfigurationException("[config patcher] Patching of config file resulted in an empty file, this will not be saved. Contact the mcMMO devs!");
// }
//
// if(dupedLines > 0 && !silentFail) {
// mcMMO.p.getLogger().info("[config patcher] Found "+dupedLines+" duplicate comments in config file: " + configFile.getName());
// mcMMO.p.getLogger().info("[config patcher] Purging the duplicate comments... (Nothing is broken, this is just info used for debugging)");
// mcMMO.p.getLogger().info("[config patcher] Line count before: "+lineCount);
// mcMMO.p.getLogger().info("[config patcher] Line count after: "+lineCountAfter);
// }
//
// // Write out the *patched* file
// // AKA the file without any comments
// try (FileWriter fileWriter = new FileWriter(configFile)) {
// fileWriter.write(stringBuilder.toString());
// }
// } catch (IOException | InvalidConfigurationException ex) {
// mcMMO.p.getLogger().severe("Failed to patch config file: " + configFile.getName());
// ex.printStackTrace();
// }
// }

private boolean isFirstCharAsciiCharacter(String line, char character) {
if(line == null || line.isEmpty()) {
return true;
}

for(Character c : line.toCharArray()) {
if(c.equals(' '))
continue;

return c.equals(character);
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/gmail/nossr50/config/ChatConfig.java
Expand Up @@ -40,7 +40,7 @@ public boolean isChatChannelEnabled(@NotNull ChatChannel chatChannel) {
}

/**
* Whether or not to use display names for players in target {@link ChatChannel}
* Whether to use display names for players in target {@link ChatChannel}
*
* @param chatChannel target chat channel
*
Expand Down

0 comments on commit 0ab9358

Please sign in to comment.