Skip to content

Commit

Permalink
Reimplemented SelfCheck
Browse files Browse the repository at this point in the history
- SelfCheck is back. Only works with MC 1.18+, but now can automatically
adds missing settings!
- Some little improvements
  • Loading branch information
Xitee1 committed Mar 25, 2022
1 parent c059fae commit 6fd7b64
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 21 deletions.
33 changes: 28 additions & 5 deletions src/de/xite/scoreboard/main/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.configuration.file.YamlConfiguration;

import de.xite.scoreboard.utils.Placeholders;
import de.xite.scoreboard.utils.SelfCheck;
import de.xite.scoreboard.utils.UpgradeVersion;

import org.apache.commons.lang.Validate;
Expand All @@ -17,18 +18,28 @@
public class Config {
static PowerBoard pl = PowerBoard.pl;
public static boolean loadConfig() {
pl.getLogger().info(" ");
pl.getLogger().info("Loading configs..");

File folder = new File(PowerBoard.pluginfolder);
if(folder == null || !folder.isDirectory())
folder.mkdirs();

// (create) and load config.yml
pl.getConfig().options().copyDefaults(true);
pl.getConfig().options().parseComments(false);
pl.saveDefaultConfig();
pl.reloadConfig();

// Check if the debug is enabled in the config.yml
PowerBoard.debug = pl.getConfig().getBoolean("debug");

// Run the SelfCheck for config.yml
if(!SelfCheck.checkConfig()) {
pl.getLogger().severe("Severe errors have been found in your config.yml! Please check your configuration!");
return false;
}

// Register hex color syntax
String s = pl.getConfig().getString("placeholder.hexColorSyntax");
if(s.length() != 0) {
Expand Down Expand Up @@ -65,27 +76,39 @@ public static boolean loadConfig() {

// create default tablist.yml
Config.createDefaultTablist();

pl.getLogger().info("Configs loaded!");
pl.getLogger().info(" ");
return true;
}


//----------------------//
// Create default files //
//----------------------//
@SuppressWarnings("deprecation")
public static void createDefaultScoreboard() {
// default scoreboard
File file = new File(PowerBoard.pluginfolder+"/scoreboards/scoreboard.yml");
if(!file.exists()) {
try {
file.createNewFile();
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
cfg.options().header("Here you can customize the scoreboard.\n"
String header = "Here you can customize the scoreboard.\n"
+ "You can add as many animation steps as you like.\n\n"
+ "For every score (line) you can set a different speed.\n"
+ "You can set up to 14 scores. For that, just add a new number like \"'7':\"\n\n"
+ "If you have static scores (no animations or updates needed): Set the 'speed' value to '9999' or higher. Then the scheduler won't start to save performance.\n"
+ "Note: Specify the speed in ticks, not seconds. 20 ticks = one second\n\n"
+ "To use multiple scoreboards, read this wiki: https://github.com/Xitee1/PowerBoard/wiki/Create-and-use-multiple-scoreboards\n");
+ "To use multiple scoreboards, read this wiki: https://github.com/Xitee1/PowerBoard/wiki/Create-and-use-multiple-scoreboards\n";

//if(new Version("1.17").compareTo(PowerBoard.version) == 1) { // For MC Versions 1.18+ we use "setHeader", because "header" is deprecated.
cfg.options().header(header);
//}else {
//cfg.options().setHeader(header);
//}


//Titel
ArrayList<String> title = new ArrayList<String>();
title.add("&4PowerBoard");
Expand Down Expand Up @@ -300,7 +323,6 @@ public static void createDefaultTablist() {
}
}


public static YamlConfiguration loadConfiguration(File file) {
Validate.notNull(file, "File cannot be null");

Expand All @@ -309,12 +331,13 @@ public static YamlConfiguration loadConfiguration(File file) {
try {
config.load(file);
} catch (FileNotFoundException ex) {
pl.getLogger().severe("Failed to load configuration '"+file+"'! File does not exists.");
pl.getLogger().severe("Failed to load configuration '"+file.getAbsolutePath()+"'! File does not exists.");
return null;
} catch (IOException ex) {
return null;
} catch (InvalidConfigurationException ex) {
pl.getLogger().severe("Cannot read configuration '"+file+"'!");
pl.getLogger().severe("Cannot read configuration '"+file.getAbsolutePath()+"'!");
pl.getLogger().severe("This is probably caused by a typing error in your scoreboard config. Check for spaces in the wrong location or other typos. Look closely and use some editor like Notepad++.");
return null;
}

Expand Down
33 changes: 22 additions & 11 deletions src/de/xite/scoreboard/main/PowerBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ public void onEnable() {
pl.getLogger().info("--------------- Loading PowerBoard ---------------");
pl.getLogger().info(" ");

version = getBukkitVersion();

// In 1.13+ a lot of things have changed. For example 128 Chars in the scoreboard instead of 32
if(PowerBoard.getBukkitVersion().compareTo(new Version("1.13")) >= 0)
version = getBukkitVersion();
if(version.compareTo(new Version("1.13")) >= 0)
aboveMC_1_13 = true;

// Migrate from old versions:
UpgradeVersion.rename(); // Rename Scoreboard to PowerBoard - migration will be removed on v3.7

// Load the config - disable plugin if failed
if(!Config.loadConfig()) {
pl.getLogger().severe("There were errors when loading the configuration! You should see more informations above. Disabling plugin...");
sendPluginLoadFailed();
Bukkit.getPluginManager().disablePlugin(pl);
return;
}
Expand All @@ -72,6 +73,7 @@ public void run() {
}
}
});



// ---- Register commands and events ---- //
Expand Down Expand Up @@ -195,13 +197,22 @@ public void run() {
public static Version getBukkitVersion() {
if(version != null)
return version;
String s = Bukkit.getBukkitVersion();
String version = s.substring(0, s.lastIndexOf("-R")).replace("_", ".");
pl.getLogger().info("Detected Server Version (original): "+s);
pl.getLogger().info("Detected Server Version (extracted): "+version);
// compareTo: 1 = a is newer than b
// compareTo: 0 = equals
// compareTo: -1 = a is older than b
return new Version(version);
try {
String s = Bukkit.getBukkitVersion();
String version = s.substring(0, s.lastIndexOf("-R")).replace("_", ".");
pl.getLogger().info("Detected Server Version (original): "+s);
pl.getLogger().info("Detected Server Version (extracted): "+version);
return new Version(version);
}catch (Exception e) {
e.printStackTrace();
pl.getLogger().severe("Could not extract MC version! Defaulting to 1.13.");
return new Version("1.13");
}
}

public static void sendPluginLoadFailed() {
pl.getLogger().severe(" ");
pl.getLogger().severe("---- Errors occurred while loading PowerBoard ----");
pl.getLogger().severe("--------------------------------------------------");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ private ScoreboardManager(String name) {
}
YamlConfiguration cfg = Config.loadConfiguration(f);
if(cfg == null) {
PowerBoard.pl.getLogger().severe("Could not load scoreboard '"+name+"'! This is probably caused by a typing error in your scoreboard config. Check for spaces in the wrong location or other typos. Look closely and use some editor like Notepad++.");
unregister(this);
return;
}
Expand Down
167 changes: 164 additions & 3 deletions src/de/xite/scoreboard/utils/SelfCheck.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,177 @@
package de.xite.scoreboard.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import de.xite.scoreboard.main.Config;
import de.xite.scoreboard.main.PowerBoard;

public class SelfCheck {
static PowerBoard pl = PowerBoard.pl;

public static void checkConfig() {
public static boolean checkConfig() {
String prefix = "(SelfCheck) config.yml -> ";

//Yaml cfg = new Yaml("config.yml", PowerBoard.pluginfolder);
//cfg.setConfigSettings(ConfigSettings.PRESERVE_COMMENTS);
try {
File file = new File(PowerBoard.pluginfolder+"/config.yml");
if(!file.exists()) {
pl.getLogger().severe("Skipped SelfCheck -> config.yml does not exist!");
return false;
}
if(new Version("1.18").compareTo(PowerBoard.version) == 0) {
pl.getLogger().warning("Skipped SelfCheck. Only works on 1.18+.");
return true;
}

// If the boolean is true at the end, the plugin will be disabled
boolean disablePlugin = false;

// If anything has been added, this is true so the config will be saved.
boolean needUpdate = false;

pl.getLogger().info(" ");
pl.getLogger().info(prefix+"Loading...");

/* Load all the configs.
* We need 3 versions of the config.yml.
*
* First, we have the actual config to write to if nessasary.
* We just shorten it here for easier access.
*
* Second, we have the actual config but without the default settings in it if something is missing.
* We need this to check if everything is in there.
*
* Third, we have the default config only. Without any user settings.
* We need this so we can add a value if something is missing.
*
*/

FileConfiguration cfg = pl.getConfig();

YamlConfiguration cfgNoDefaults = YamlConfiguration.loadConfiguration(file);

// Load the default configurations
File config_selfcheck = new File(PowerBoard.pluginfolder+"/.config_selfcheck.yml"); // This file is temporary. We delete it at the end of SelfCheck.
try(InputStream inputStream = pl.getClass().getClassLoader().getResourceAsStream("config.yml");
OutputStream outputStream = new FileOutputStream(config_selfcheck)) {
int length;
byte[] bytes = new byte[1024];
// copy data from input stream to output stream
while ((length = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
}

} catch (IOException ex) {
ex.printStackTrace();
}
YamlConfiguration cfgDefault = Config.loadConfiguration(config_selfcheck);


/* Here we could just use the "isX" fuctions to do everything with a single for-loop. Could.
* The reason we don't do that here is because the user could for example by mistake set an option
* to 'true' instead true, which would make it to a string and cause errors.
*
* The only settings that won't be checked are the ranks list, luckperms-api chat-prefix and placeholder world-names because it is too complicated (at least for me right now).
* But if you are reading this, feel free to implement it.
*
*/


String[] checkBoolean = {
"scoreboard",
"tablist.text",
"tablist.ranks",
"chat.ranks",
"chat.allowHexColors",
"ranks.useUnlimitedLongRanks",
"ranks.luckperms-api.enable",
"ranks.luckperms-api.prefix-suffix-space",
"placeholder.prefer-plugin-placeholders",
"update.notification",
"update.autoupdater",
"debug"
};

String[] checkString = {
"scoreboard-default",
"tablist.text-default",
"chat.colorperm",
"ranks.permissionsystem",
"ranks.luckperms-api.chat-layout",

"placeholder.time-format",
"placeholder.date-format",
"placeholder.hexColorSyntax"
};

String[] checkInt = {
"ranks.update-interval",
"placeholder.money-decimals",
};


for(String s : checkBoolean) {
// Here we check if the option exists.
// We re-add it if it is new or the user (accidently) deleted it.
if(!cfgNoDefaults.contains(s)) {
pl.getLogger().warning(prefix+"The setting \""+s+"\" does not exists! Adding..");
cfg.set(s, cfgDefault.getBoolean(s));
needUpdate = true;
}
// Here we check if it's the correct type
if(!cfg.isBoolean(s)) {
pl.getLogger().severe(prefix+"The setting \""+s+"\" is invalid! Please check your config.yml!");
disablePlugin = true;
}
}

for(String s : checkString) {
if(!cfgNoDefaults.contains(s)) {
pl.getLogger().warning(prefix+"The setting \""+s+"\" does not exists! Adding..");
cfg.set(s, cfgDefault.getString(s));
needUpdate = true;
}
if(!cfg.isString(s)) {
pl.getLogger().severe(prefix+"The setting \""+s+"\" is invalid! Please check your config.yml!");
disablePlugin = true;
}
}

for(String s : checkInt) {
if(!cfgNoDefaults.contains(s)) {
pl.getLogger().warning(prefix+"The setting \""+s+"\" does not exists! Adding..");
cfg.set(s, cfgDefault.getInt(s));
needUpdate = true;
}
if(!cfg.isInt(s)) {
pl.getLogger().severe(prefix+"The setting \""+s+"\" is invalid! Please check your config.yml!");
disablePlugin = true;
}
}


config_selfcheck.delete();

if(needUpdate)
pl.saveConfig();

pl.getLogger().info(prefix+"Finished!");
pl.getLogger().info(" ");

if(disablePlugin)
return false;

return true;
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean checkTablist(String name, YamlConfiguration cfg) {
if(!(cfg.contains("header") || cfg.contains("footer")
Expand Down
7 changes: 6 additions & 1 deletion src/de/xite/scoreboard/utils/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public Version(String version) {
throw new IllegalArgumentException("Invalid version format");
this.version = version;
}


/**
* int = 1: a is newer than b;
* int = 0: equals;
* int = -1: a is older than b
*/
@Override
public int compareTo(Version that) {
if(that == null)
Expand Down

0 comments on commit 6fd7b64

Please sign in to comment.