Last active
March 24, 2024 07:54
-
-
Save MrMaurice211/70e129c6056cec4000a7d6212a33166c to your computer and use it in GitHub Desktop.
Configuration Updater
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
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.nio.file.CopyOption; | |
import java.nio.file.Files; | |
import java.nio.file.StandardCopyOption; | |
import java.time.LocalDate; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.google.common.base.Charsets; | |
public class ConfigUpdater { | |
private int newVersion = 1; // The next version of the Config | |
private boolean instantDeprecated = false; // Just an option to depecrate the config if the version is different | |
private JavaPlugin plugin; | |
private File path; | |
private List<String> lines; | |
public ConfigUpdater(JavaPlugin plugin) { | |
this.plugin = plugin; | |
} | |
public void checkUpdate(int oldV) { | |
path = new File(plugin.getDataFolder(), "config.yml"); | |
if (oldV == 0 || instantDeprecated) { | |
if (oldV != newVersion) | |
deprecateConfig(); | |
return; | |
} | |
if (oldV == newVersion) { | |
Logger#info("The config is updated!"); | |
return; | |
} | |
lines = readFile(path); | |
updateConfig(); | |
} | |
public void updateConfig() { | |
Logger#info("Updating the config!"); | |
List<String> newLines = readInsideFile("/config.yml"); | |
lines.removeIf(s -> s.trim().isEmpty() || s.trim().startsWith("#") || s.split(":").length == 1); | |
lines.forEach(s -> { | |
String[] a = s.split(":"); | |
String newS = joinString(Arrays.copyOfRange(a, 1, a.length), ":"); | |
int index = getIndex(a[0], newLines); | |
if (index > -1) | |
newLines.set(index, newLines.get(index).split(":")[0] + ":" + newS); | |
}); | |
String versionLine = "configVersion: "; | |
newLines.set(getIndex(versionLine, newLines), versionLine + newVersion); | |
writeFile(path, newLines); | |
Logger#info("Config is now updated!"); | |
//Here we reload the config | |
} | |
private void deprecateConfig() { | |
Logger#info("Now your config is deprecated please check your folder for re-setting it!"); | |
String depName = "deprecated_config_" + LocalDate.now(); | |
File old = new File(path.getParentFile(), depName + ".yml"); | |
try { | |
Files.copy(path.toPath(), old.toPath(), | |
new CopyOption[] { StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING }); | |
path.delete(); | |
} catch (Exception e) {} | |
//Here we re-create the config. | |
} | |
public String joinString(String[] text, String character) { | |
return Arrays.stream(text).collect(Collectors.joining(character)); | |
} | |
public int getIndex(String line, List<String> list) { | |
for (String s : list) | |
if (s.startsWith(line) || s.equalsIgnoreCase(line)) | |
return list.indexOf(s); | |
return -1; | |
} | |
public void writeFile(File file, List<String> toWrite) { | |
try { | |
Files.write(file.toPath(), toWrite, Charsets.ISO_8859_1); | |
} catch (Exception e) {} | |
} | |
public List<String> readFile(File file) { | |
try { | |
return Files.readAllLines(file.toPath(), Charsets.ISO_8859_1); | |
} catch (Exception e) { | |
return new ArrayList<>(); | |
} | |
} | |
public List<String> readInsideFile(String path) { | |
try (InputStream in = plugin.getClass().getResourceAsStream(path); | |
BufferedReader input = new BufferedReader(new InputStreamReader(in));) { | |
return input.lines().collect(Collectors.toList()); | |
} catch (Exception e) { | |
return new ArrayList<>(); | |
} | |
} | |
} |
Hello there,
I want to implement this class into one of my plugins and was wondering what's the purpose of the newV you have on line 65. Could you clarify what variable that is?
I really appreciate any help you can provide.Fixed!
Hello there once again xD
I've been testing around with the class and keep getting this error on the same line 70
Just so I can test this class I've done the following:
I've set the oldVer = 1
and newVer = 2
Do you happen to have a discord server where you answer such questions or here is fine?
Thanks again in advance!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed!