Initial commit
This commit is contained in:
commit
392479abde
30 changed files with 1622 additions and 0 deletions
|
|
@ -0,0 +1,46 @@
|
|||
package net.frankheijden.serverutils.utils;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.logging.*;
|
||||
|
||||
public class ForwardFilter extends PredicateFilter {
|
||||
|
||||
private boolean warnings;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package net.frankheijden.serverutils.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class ListBuilder<T> {
|
||||
|
||||
private final Collection<T> collection;
|
||||
private ListFormat<T> formatter;
|
||||
private String seperator;
|
||||
private String lastSeperator;
|
||||
|
||||
private ListBuilder(Collection<T> collection) {
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public static <T> ListBuilder<T> create(Collection<T> collection) {
|
||||
return new ListBuilder<>(collection);
|
||||
}
|
||||
|
||||
public ListBuilder<T> format(ListFormat<T> formatter) {
|
||||
this.formatter = formatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ListBuilder<T> seperator(String seperator) {
|
||||
this.seperator = seperator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ListBuilder<T> lastSeperator(String lastSeperator) {
|
||||
this.lastSeperator = lastSeperator;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (collection.size() == 1) {
|
||||
return formatter.format(collection.iterator().next());
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
int i = 1;
|
||||
for (T t : collection) {
|
||||
sb.append(formatter.format(t));
|
||||
if (i == collection.size() - 1) {
|
||||
sb.append(lastSeperator);
|
||||
} else if (i != collection.size()) {
|
||||
sb.append(seperator);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package net.frankheijden.serverutils.utils;
|
||||
|
||||
public interface ListFormat<T> {
|
||||
|
||||
String format(T t);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package net.frankheijden.serverutils.utils;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.logging.*;
|
||||
|
||||
public class PredicateFilter implements Filter {
|
||||
|
||||
private Predicate<LogRecord> predicate;
|
||||
private Filter filter;
|
||||
|
||||
public void setPredicate(Predicate<LogRecord> predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
public void start(Logger logger) {
|
||||
this.filter = logger.getFilter();
|
||||
logger.setFilter(this);
|
||||
}
|
||||
|
||||
public void stop(Logger logger) {
|
||||
logger.setFilter(filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoggable(LogRecord record) {
|
||||
return predicate.test(record);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package net.frankheijden.serverutils.utils;
|
||||
|
||||
public interface ReloadHandler {
|
||||
|
||||
void handle() throws Exception;
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue