Bring back customisable messages for reloading ServerUtils using wacky methods

This commit is contained in:
Frank van der Heijden 2020-07-20 14:14:23 +02:00
parent 6c8a93dc1b
commit a0bdaeb2fc
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
5 changed files with 57 additions and 22 deletions

View file

@ -29,6 +29,7 @@ import net.frankheijden.serverutils.common.entities.Result;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import net.frankheijden.serverutils.common.utils.FormatBuilder;
import net.frankheijden.serverutils.common.utils.ForwardFilter;
import net.frankheijden.serverutils.common.utils.HexUtils;
import net.frankheijden.serverutils.common.utils.ListBuilder;
import net.frankheijden.serverutils.common.utils.ListFormat;
import org.bukkit.Bukkit;
@ -196,18 +197,16 @@ public class CommandServerUtils extends BaseCommand {
@CommandPermission("serverutils.reloadplugin")
@Description("Reloads a specified plugin.")
public void onReloadPlugin(CommandSender sender, String pluginName) {
if (pluginName.equalsIgnoreCase("ServerUtils")) {
String result = BukkitPluginManager.get().reloadPlugin(pluginName).toString();
if (result.equals("SUCCESS")) {
sender.sendMessage(ChatColor.GREEN + "Successfully reloaded ServerUtils.");
} else {
sender.sendMessage(ChatColor.RED + "Something went wrong reloading ServerUtils.");
}
return;
}
// Wacky method to have the resources needed for the reload in memory, in case of a self reload.
HexUtils utils = new HexUtils();
Map<String, Object> section = Messenger.getInstance().getConfig().getMap("serverutils");
String result = BukkitPluginManager.get().reloadPlugin(pluginName).toString();
Result result = BukkitPluginManager.get().reloadPlugin(pluginName);
result.sendTo(BukkitUtils.wrap(sender), "reload", pluginName);
String msg = (String) section.get(result.toLowerCase());
if (msg != null && !msg.isEmpty()) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', utils.convertHexString(
msg.replace("%action%", "reload").replace("%what%", pluginName))));
}
}
/**

View file

@ -5,6 +5,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import net.frankheijden.serverutils.common.config.YamlConfig;
import org.bukkit.configuration.MemorySection;
@ -37,6 +39,16 @@ public class BukkitYamlConfig implements YamlConfig {
return obj;
}
@Override
public Map<String, Object> getMap(String path) {
Object obj = config.get(path);
if (obj instanceof MemorySection) {
System.out.println("yes");
return ((MemorySection) obj).getValues(false);
}
return new HashMap<>();
}
@Override
public void set(String path, Object value) {
config.set(path, value);

View file

@ -24,6 +24,7 @@ import net.frankheijden.serverutils.common.entities.CloseableResult;
import net.frankheijden.serverutils.common.entities.Result;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
import net.frankheijden.serverutils.common.utils.FormatBuilder;
import net.frankheijden.serverutils.common.utils.HexUtils;
import net.frankheijden.serverutils.common.utils.ListBuilder;
import net.frankheijden.serverutils.common.utils.ListFormat;
import net.md_5.bungee.api.ChatColor;
@ -137,18 +138,16 @@ public class CommandServerUtils extends BaseCommand {
@CommandPermission("serverutils.reloadplugin")
@Description("Reloads a specified plugin.")
public void onReloadPlugin(CommandSender sender, String pluginName) {
if (pluginName.equalsIgnoreCase("ServerUtils")) {
String result = BungeePluginManager.get().reloadPlugin(pluginName).toString();
if (result.equals("SUCCESS")) {
sender.sendMessage(ChatColor.GREEN + "Successfully reloaded ServerUtils.");
} else {
sender.sendMessage(ChatColor.RED + "Something went wrong reloading ServerUtils.");
}
return;
}
// Wacky method to have the resources needed for the reload in memory, in case of a self reload.
HexUtils utils = new HexUtils();
Map<String, Object> section = Messenger.getInstance().getConfig().getMap("serverutils");
String result = BungeePluginManager.get().reloadPlugin(pluginName).toString();
Result result = BungeePluginManager.get().reloadPlugin(pluginName);
result.sendTo(BungeeUtils.wrap(sender), "reload", pluginName);
String msg = (String) section.get(result.toLowerCase());
if (msg != null && !msg.isEmpty()) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', utils.convertHexString(
msg.replace("%action%", "reload").replace("%what%", pluginName))));
}
}
/**

View file

@ -4,6 +4,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import net.frankheijden.serverutils.common.config.YamlConfig;
import net.md_5.bungee.config.Configuration;
@ -38,6 +40,21 @@ public class BungeeYamlConfig implements YamlConfig {
return obj;
}
@Override
public Map<String, Object> getMap(String path) {
Object obj = config.get(path);
if (obj instanceof Configuration) {
Configuration section = (Configuration) obj;
Collection<String> keys = section.getKeys();
Map<String, Object> map = new HashMap<>(keys.size());
for (String key : keys) {
map.put(key, section.get(key));
}
return map;
}
return new HashMap<>();
}
@Override
public void set(String path, Object value) {
config.set(path, value);

View file

@ -2,6 +2,7 @@ package net.frankheijden.serverutils.common.config;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
/**
* A wrap for a Yaml Configuration file.
@ -15,6 +16,13 @@ public interface YamlConfig {
*/
Object get(String path);
/**
* Retrieves a map with key/values for the path specified.
* @param path The path.
* @return The map object with key/values.
*/
Map<String, Object> getMap(String path);
/**
* Sets a value to a path.
* @param path The path.