Initial BungeeCord edition
This commit is contained in:
parent
4416d55173
commit
23e8e80191
75 changed files with 1931 additions and 396 deletions
|
|
@ -0,0 +1,15 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
public class Config extends YamlResource {
|
||||
|
||||
private static Config instance;
|
||||
|
||||
public Config(String fileName, String resource) {
|
||||
super(fileName, resource);
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static Config getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.utils.StringUtils;
|
||||
|
||||
public class Messenger extends YamlResource {
|
||||
|
||||
private static Messenger instance;
|
||||
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||
|
||||
public Messenger(String fileName, String resource) {
|
||||
super(fileName, resource);
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static Messenger getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a message from the config.
|
||||
* @param path The yml path to the message.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
* @return The config message with translated placeholders.
|
||||
*/
|
||||
public static String getMessage(String path, String... replacements) {
|
||||
String message = instance.getConfig().getString(path);
|
||||
if (message != null) {
|
||||
return StringUtils.apply(message, replacements);
|
||||
} else {
|
||||
instance.plugin.getLogger().severe("Missing locale in messages.yml at path '" + path + "'!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a player with translated placeholders.
|
||||
* @param sender The receiver.
|
||||
* @param msg The message to be sent.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
*/
|
||||
public static void sendRawMessage(ServerCommandSender sender, String msg, String... replacements) {
|
||||
String message = StringUtils.apply(msg, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(instance.plugin.getColorProvider().apply(message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message from the specified config path to a player with translated placeholders.
|
||||
* @param sender The receiver.
|
||||
* @param path The yml path to the message.
|
||||
* @param replacements The replacements to be taken into account.
|
||||
*/
|
||||
public static void sendMessage(ServerCommandSender sender, String path, String... replacements) {
|
||||
String message = getMessage(path, replacements);
|
||||
if (message != null) {
|
||||
sender.sendMessage(instance.plugin.getColorProvider().apply(message));
|
||||
}
|
||||
}
|
||||
|
||||
public static String color(String str) {
|
||||
return instance.plugin.getColorProvider().apply(str);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface YamlConfig {
|
||||
|
||||
Object get(String path);
|
||||
|
||||
void set(String path, Object value);
|
||||
|
||||
String getString(String path);
|
||||
|
||||
boolean getBoolean(String path);
|
||||
|
||||
Collection<? extends String> getKeys();
|
||||
|
||||
void save() throws IOException;
|
||||
|
||||
static void addDefaults(YamlConfig def, YamlConfig conf) {
|
||||
addDefaults(def, conf, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds defaults if keys don't exist to the configuration specified.
|
||||
* @param def The defaults to copy values over from.
|
||||
* @param conf The configuration to copy the defaults to.
|
||||
* @param root The current root path of the iteration.
|
||||
*/
|
||||
static void addDefaults(YamlConfig def, YamlConfig conf, String root) {
|
||||
if (def == null) return;
|
||||
for (String key : def.getKeys()) {
|
||||
String newKey = (root.isEmpty() ? "" : root + ".") + key;
|
||||
Object value = def.get(key);
|
||||
if (value instanceof YamlConfig) {
|
||||
addDefaults((YamlConfig) value, conf, newKey);
|
||||
} else if (conf.get(newKey) == null) {
|
||||
conf.set(newKey, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a Configuration from a file with associated defaults.
|
||||
* @param def The default Configuration to be applied.
|
||||
* @param conf The Configuration where the defaults will be applied to.
|
||||
* @return The loaded Configuration of the file with defaults.
|
||||
*/
|
||||
static YamlConfig init(YamlConfig def, YamlConfig conf) {
|
||||
YamlConfig.addDefaults(def, conf);
|
||||
|
||||
try {
|
||||
conf.save();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return conf;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package net.frankheijden.serverutils.common.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
|
||||
public class YamlResource {
|
||||
|
||||
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||
|
||||
private final YamlConfig config;
|
||||
|
||||
/**
|
||||
* Creates a new YamlResource instance.
|
||||
* Loads the resource from the jar file.
|
||||
* @param fileName The destination file.
|
||||
* @param resource The resource from the jar file.
|
||||
*/
|
||||
public YamlResource(String fileName, String resource) {
|
||||
ResourceProvider provider = plugin.getResourceProvider();
|
||||
InputStream is = provider.getResource(resource);
|
||||
File file = plugin.copyResourceIfNotExists(fileName, resource);
|
||||
config = YamlConfig.init(provider.load(is), provider.load(file));
|
||||
}
|
||||
|
||||
public YamlConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue