Skip to content

Commit

Permalink
Plugin doesn't just crash on load now -_-
Browse files Browse the repository at this point in the history
The config system now backs up the old file instead of overwriting on a config version bump

Signed-off-by: itsTyrion <[email protected]>

Took 1 hour 5 minutes
  • Loading branch information
itsTyrion committed Jan 11, 2022
1 parent 965950a commit a7af6be
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'de.itsTyrion'
version '1.6'
version '1.6.1'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AntiVPNBungee extends Plugin {
@Override
public void onEnable() {
try {
config = new ConfigBungee(new File(getDataFolder(), "config.json")).load();
config = new ConfigBungee(new File(getDataFolder(), "config.json")).init();
} catch (IOException e) {
e.printStackTrace();
getLogger().severe("Couldn't load/init config. Shutting down");
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/de/itsTyrion/antiVPN/bungee/ConfigBungee.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.itsTyrion.antiVPN.bungee;

import de.itsTyrion.antiVPN.util.Config;
import lombok.val;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.config.Configuration;
Expand All @@ -23,13 +22,23 @@ public ConfigBungee(File configFile) {
}

@Override
protected ConfigBungee load() throws IOException {
val wrongVersion = config.getInt("VERSION-DontTouch") < 5;
createConfigFile(wrongVersion);
config = configProvider.load(configFile);
protected ConfigBungee init() throws IOException {
if (configFile.getParentFile().mkdirs() || !configFile.exists()) {
createConfigFile(false);
} else {
load();
if (config.getInt("VERSION-DontTouch") < CONFIG_VERSION)
createConfigFile(true);
}
load();
return this;
}

@Override
protected void load() throws IOException {
config = configProvider.load(configFile);
}

private final Pattern pattern = Pattern.compile("&([0-9a-flmnokr])"); // Pattern to translate & color codes to §

@Override
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/de/itsTyrion/antiVPN/util/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.common.cache.CacheBuilder;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import lombok.AllArgsConstructor;
import lombok.val;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -19,11 +18,16 @@
* @author itsTyrion
* Created on 20.01.2021
*/
@AllArgsConstructor
public class Check {
private final LoggerWrapper loggerWrapper;
private final Config config;
private final IPCache ipCache = new IPCache();
private final IPCache ipCache;

public Check(LoggerWrapper loggerWrapper, Config config) {
this.loggerWrapper = loggerWrapper;
this.config = config;
ipCache = new IPCache();
}

public boolean isBadIP(String address, String username) {
try {
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/de/itsTyrion/antiVPN/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,32 @@
public abstract class Config {

protected final File configFile;
protected static final int CONFIG_VERSION = 5;


/**
* Tries to read the plugin's configuration file and parse it.
* In case it doesn't exist, the default configuration
* from the.jar will be written.
* Checks whether the config file is present and in the current version on-disk.
* If not, it's copied from the .jar file. If there was an outdated config, it's backed up
*
* @throws IOException If the file could not be read or written
* @return this instance
*/
protected abstract Config load() throws IOException;

protected final void createConfigFile(boolean overwrite) throws IOException {
if (!configFile.getParentFile().exists())
configFile.getParentFile().mkdirs();
protected abstract Config init() throws IOException;

if (!configFile.exists() || overwrite) {
/**
* Tries to read the plugin's configuration file and parse it.
*
* @throws IOException If the file could not be read or written
*/
protected abstract void load() throws IOException;

protected final void createConfigFile(boolean oldVersionPresent) throws IOException {
if (oldVersionPresent) {
Files.move(configFile.toPath(), configFile.toPath().getParent().resolve("config_old.json"));
System.err.println("New config version! Your old config has been renamed to config_old");
} else
System.err.println("No config file found! Please set up.");
if (!configFile.exists()) {
try (val input = getClass().getResourceAsStream("/" + configFile.getName())) {
if (input == null) {
throw new IOException("Could not read config from jar. Please re-download.");
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/de/itsTyrion/antiVPN/util/LoggerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
import lombok.Getter;
import org.jetbrains.annotations.Contract;

import java.util.logging.Logger;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class LoggerWrapper {
private Logger bungeeLogger;

private java.util.logging.Logger bungeeLogger;
private org.slf4j.Logger velocityLogger;

@Contract("_ -> new")
public static LoggerWrapper bungee(Logger logger) {
public static LoggerWrapper bungee(java.util.logging.Logger logger) {
return new LoggerWrapper(logger, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AntiVPNVelocity(ProxyServer server, Logger logger, @DataDirectory Path fo
this.server = server;
ConfigVelocity cfg = null;
try {
cfg = new ConfigVelocity(new File(folder.toFile(), "config.json")).load();
cfg = new ConfigVelocity(new File(folder.toFile(), "config.json")).init();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/de/itsTyrion/antiVPN/velocity/ConfigVelocity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,27 @@ public ConfigVelocity(File configFile) {
}

@Override
protected ConfigVelocity load() throws IOException {
val wrongVersion = config.getInt("VERSION-DontTouch") < 5;
createConfigFile(wrongVersion);
protected ConfigVelocity init() throws IOException {
if (configFile.getParentFile().mkdirs() || !configFile.exists()) {
createConfigFile(false);
} else {
load();
if (config.getInt("VERSION-DontTouch") < CONFIG_VERSION)
createConfigFile(true);
}
load();
return this;
}

@Override
protected void load() throws IOException {
try {
config = JsonParser.object().from(new FileInputStream(configFile));
val fileStream = new FileInputStream(configFile);
config = JsonParser.object().from(fileStream);
fileStream.close();
} catch (JsonParserException ex) {
System.err.println("Couldn't parse config (" + ex.getMessage() + ')');
}
return this;
}

private final Pattern pattern = Pattern.compile("&([0-9a-flmnokr])"); // Pattern to translate & color codes to §
Expand Down

0 comments on commit a7af6be

Please sign in to comment.