HolographicDisplays/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/config/Settings.java

152 lines
5.8 KiB
Java
Raw Normal View History

/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
2021-07-31 17:28:19 +02:00
package me.filoghost.holographicdisplays.plugin.config;
import me.filoghost.fcommons.Strings;
2021-03-14 20:59:34 +01:00
import me.filoghost.fcommons.logging.ErrorCollector;
2021-06-20 11:14:26 +02:00
import me.filoghost.holographicdisplays.common.DebugLogger;
2021-06-24 17:13:32 +02:00
import me.filoghost.holographicdisplays.plugin.format.DisplayFormat;
2021-01-24 12:01:32 +01:00
import java.time.DateTimeException;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
2021-03-19 19:48:13 +01:00
import java.util.HashMap;
import java.util.Map;
2021-06-24 17:38:38 +02:00
public class Settings {
2021-06-20 17:23:48 +02:00
2020-11-10 10:27:41 +01:00
public static double spaceBetweenLines;
public static boolean quickEditCommands;
public static DateTimeFormatter timeFormat;
public static boolean updateNotification;
public static boolean placeholderAPIEnabled;
public static int placeholderAPIDefaultRefreshInternalTicks;
2020-11-10 10:27:41 +01:00
public static String imageSymbol;
public static String transparencySymbol;
2020-11-10 10:27:41 +01:00
public static int bungeeRefreshSeconds;
public static boolean useRedisBungee;
2021-02-20 09:30:28 +01:00
public static boolean pingerEnabled;
2020-11-10 10:27:41 +01:00
public static int pingerTimeout;
public static String pingerOfflineMotd;
public static String pingerStatusOnline;
public static String pingerStatusOffline;
public static boolean pingerTrimMotd;
2021-03-19 19:48:13 +01:00
public static Map<String, ServerAddress> pingerServerAddresses;
2021-06-20 17:23:48 +02:00
2021-06-24 17:38:38 +02:00
public static void load(SettingsModel config, ErrorCollector errorCollector) {
spaceBetweenLines = config.spaceBetweenLines;
quickEditCommands = config.quickEditCommands;
timeFormat = parseTimeFormatter(config.timeFormat, config.timeZone, errorCollector);
updateNotification = config.updateNotification;
2021-06-20 17:23:48 +02:00
placeholderAPIEnabled = config.placeholderAPIEnabled;
placeholderAPIDefaultRefreshInternalTicks = config.placeholderAPIDefaultRefreshIntervalTicks;
2021-07-11 11:49:41 +02:00
imageSymbol = DisplayFormat.apply(config.imageRenderingSolidPixel);
transparencySymbol = DisplayFormat.apply(config.imageRenderingTransparentPixel);
2021-06-20 17:23:48 +02:00
2021-03-14 20:59:34 +01:00
bungeeRefreshSeconds = parseBungeeRefreshInterval(config.bungeeRefreshSeconds, errorCollector);
useRedisBungee = config.useRedisBungee;
2021-02-20 09:30:28 +01:00
pingerEnabled = config.pingerEnable;
2021-03-14 20:59:34 +01:00
pingerTimeout = parsePingerTimeout(config.pingerTimeout, errorCollector);
2021-06-24 17:13:32 +02:00
pingerOfflineMotd = DisplayFormat.apply(config.pingerOfflineMotd);
pingerStatusOnline = DisplayFormat.apply(config.pingerStatusOnline);
pingerStatusOffline = DisplayFormat.apply(config.pingerStatusOffline);
pingerTrimMotd = config.pingerTrimMotd;
2021-03-19 19:48:13 +01:00
pingerServerAddresses = new HashMap<>();
2021-02-20 09:30:28 +01:00
if (pingerEnabled) {
for (String singleServer : config.pingerServers) {
2021-03-14 20:59:34 +01:00
ServerAddress serverAddress = parseServerAddress(singleServer, errorCollector);
if (serverAddress != null) {
2021-03-19 19:48:13 +01:00
pingerServerAddresses.put(serverAddress.getName(), serverAddress);
2020-11-10 10:27:41 +01:00
}
}
}
DebugLogger.setDebugEnabled(config.debug);
}
2021-03-14 20:59:34 +01:00
private static DateTimeFormatter parseTimeFormatter(String pattern, String timeZone, ErrorCollector errorCollector) {
DateTimeFormatter timeFormat;
2020-11-10 10:27:41 +01:00
try {
timeFormat = DateTimeFormatter.ofPattern(pattern);
2020-11-10 10:27:41 +01:00
} catch (IllegalArgumentException ex) {
2021-01-24 12:01:32 +01:00
timeFormat = DateTimeFormatter.ofPattern("H:mm");
2021-03-14 20:59:34 +01:00
errorCollector.add("time format not valid in the configuration, using the default");
2020-11-10 10:27:41 +01:00
}
2021-01-24 12:01:32 +01:00
try {
timeFormat = timeFormat.withZone(ZoneId.of(timeZone));
2021-01-24 12:01:32 +01:00
} catch (DateTimeException e) {
2021-03-14 20:59:34 +01:00
errorCollector.add("time zone not valid in the configuration, using the default");
2021-01-24 12:01:32 +01:00
}
return timeFormat;
}
2021-03-14 20:59:34 +01:00
private static int parseBungeeRefreshInterval(int interval, ErrorCollector errorCollector) {
if (interval < 1) {
2021-03-14 20:59:34 +01:00
errorCollector.add("the minimum interval for pinging BungeeCord's servers is 1 second. It has been automatically set");
return 1;
} else if (interval > 60) {
2021-03-14 20:59:34 +01:00
errorCollector.add("the maximum interval for pinging BungeeCord's servers is 60 seconds. It has been automatically set");
return 60;
} else {
return interval;
2020-11-10 10:27:41 +01:00
}
}
2021-03-14 20:59:34 +01:00
private static int parsePingerTimeout(int timeout, ErrorCollector errorCollector) {
if (timeout < 100) {
2021-03-14 20:59:34 +01:00
errorCollector.add("the minimum timeout for pinging BungeeCord's servers is 100 milliseconds. It has been automatically set");
return 100;
} else if (timeout > 10000) {
2021-03-14 20:59:34 +01:00
errorCollector.add("the maximum timeout for pinging BungeeCord's servers is 10000 milliseconds. It has been automatically set");
return 10000;
} else {
return timeout;
}
}
2021-03-14 20:59:34 +01:00
private static ServerAddress parseServerAddress(String singleServer, ErrorCollector errorCollector) {
String[] nameAndAddress = Strings.splitAndTrim(singleServer, ":", 2);
if (nameAndAddress.length < 2) {
2021-06-20 17:23:48 +02:00
errorCollector.add("the server info \"" + singleServer + "\" is not valid."
+ " There should be a name and an address, separated by a colon");
return null;
}
String name = nameAndAddress[0];
String address = nameAndAddress[1];
String ip;
int port;
if (address.contains(":")) {
String[] ipAndPort = Strings.splitAndTrim(address, ":", 2);
ip = ipAndPort[0];
try {
port = Integer.parseInt(ipAndPort[1]);
} catch (NumberFormatException e) {
2021-03-14 20:59:34 +01:00
errorCollector.add("invalid port number in the server info \"" + singleServer + "\"");
return null;
}
} else {
ip = address;
2021-05-17 18:19:09 +02:00
port = 25565; // Default Minecraft server port
}
2021-06-20 17:23:48 +02:00
return new ServerAddress(name, ip, port);
}
}