Initial BungeeCord edition
This commit is contained in:
parent
4416d55173
commit
23e8e80191
75 changed files with 1931 additions and 396 deletions
|
|
@ -0,0 +1,12 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.entities.BukkitCommandSender;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class BukkitUtils {
|
||||
|
||||
public static ServerCommandSender wrap(CommandSender sender) {
|
||||
return new BukkitCommandSender(sender);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.config.Messenger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class FormatBuilder {
|
||||
|
||||
private final String format;
|
||||
private final List<String[]> valueList;
|
||||
private String[] orderedKeys;
|
||||
private boolean alwaysSend;
|
||||
|
||||
private FormatBuilder(String format) {
|
||||
this.format = format;
|
||||
this.valueList = new ArrayList<>();
|
||||
this.orderedKeys = new String[0];
|
||||
this.alwaysSend = false;
|
||||
}
|
||||
|
||||
public static FormatBuilder create(String format) {
|
||||
return new FormatBuilder(format);
|
||||
}
|
||||
|
||||
public FormatBuilder orderedKeys(String... orderedKeys) {
|
||||
this.orderedKeys = orderedKeys;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormatBuilder add(String... values) {
|
||||
this.valueList.add(values);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormatBuilder alwaysSend(boolean alwaysSend) {
|
||||
this.alwaysSend = alwaysSend;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the format and sends it to the CommandSender.
|
||||
* @param sender The receiver of the list.
|
||||
*/
|
||||
public void sendTo(CommandSender sender) {
|
||||
valueList.forEach(values -> {
|
||||
int length = Math.min(values.length, orderedKeys.length);
|
||||
String message = format;
|
||||
for (int i = 0; i < length; i++) {
|
||||
String value = values[i];
|
||||
if ((value == null || value.isEmpty()) && !alwaysSend) return;
|
||||
message = message.replace(orderedKeys[i], String.valueOf(value));
|
||||
}
|
||||
Messenger.sendRawMessage(sender, message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import net.frankheijden.serverutils.common.utils.PredicateFilter;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class ForwardFilter extends PredicateFilter {
|
||||
|
||||
private boolean warnings;
|
||||
|
||||
/**
|
||||
* Creates a filter which forwards all output to the sender.
|
||||
* @param sender The sender to forward logs to.
|
||||
*/
|
||||
public ForwardFilter(CommandSender sender) {
|
||||
this.warnings = false;
|
||||
|
||||
setPredicate(rec -> {
|
||||
ChatColor color = getColor(rec.getLevel());
|
||||
if (color != ChatColor.GREEN) warnings = true;
|
||||
sender.sendMessage(color + format(rec));
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public boolean hasWarnings() {
|
||||
return warnings;
|
||||
}
|
||||
|
||||
private static ChatColor getColor(Level level) {
|
||||
if (Level.SEVERE.equals(level)) {
|
||||
return ChatColor.RED;
|
||||
} else if (Level.WARNING.equals(level)) {
|
||||
return ChatColor.GOLD;
|
||||
}
|
||||
return ChatColor.GREEN;
|
||||
}
|
||||
|
||||
private static String format(LogRecord record) {
|
||||
String msg = record.getMessage();
|
||||
|
||||
Object[] params = record.getParameters();
|
||||
if (params == null) return msg;
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
msg = msg.replace("{" + i + "}", String.valueOf(params[i]));
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class YamlUtils {
|
||||
|
||||
public static void addDefaults(MemorySection defaults, YamlConfiguration yml) {
|
||||
addDefaults(defaults, yml, "");
|
||||
}
|
||||
|
||||
private static void addDefaults(MemorySection defaults, YamlConfiguration yml, String root) {
|
||||
if (defaults == null) return;
|
||||
for (String key : defaults.getKeys(false)) {
|
||||
String newKey = (root.isEmpty() ? "" : root + ".") + key;
|
||||
Object value = defaults.get(key);
|
||||
if (value instanceof MemorySection) {
|
||||
addDefaults((MemorySection) value, yml, newKey);
|
||||
} else if (yml.get(newKey) == null) {
|
||||
yml.set(newKey, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a YamlConfiguration from a file with associated defaults.
|
||||
* @param file The yml file.
|
||||
* @param def The default YamlConfiguration to be applied.
|
||||
* @return The loaded YamlConfiguration of the file with defaults.
|
||||
*/
|
||||
public static YamlConfiguration init(File file, YamlConfiguration def) {
|
||||
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
|
||||
YamlUtils.addDefaults(def, yml);
|
||||
|
||||
try {
|
||||
// Idk somehow the order messes up
|
||||
// of the messages if we don't do this
|
||||
file.delete();
|
||||
file.createNewFile();
|
||||
|
||||
yml.save(file);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return yml;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue