mirror of
https://github.com/BGHDDevelopment/PlayerBalancer.git
synced 2024-09-19 19:21:00 +02:00
Added command variant for pasting logs
This commit is contained in:
parent
cefe7f9372
commit
e45af57ad8
@ -15,6 +15,7 @@ import com.jaimemartz.playerbalancer.settings.SettingsHolder;
|
|||||||
import net.md_5.bungee.api.plugin.Command;
|
import net.md_5.bungee.api.plugin.Command;
|
||||||
import net.md_5.bungee.api.plugin.Listener;
|
import net.md_5.bungee.api.plugin.Listener;
|
||||||
import net.md_5.bungee.api.plugin.Plugin;
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
import net.md_5.bungee.log.ConciseFormatter;
|
||||||
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
|
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
|
||||||
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
|
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
|
||||||
import ninja.leaping.configurate.loader.ConfigurationLoader;
|
import ninja.leaping.configurate.loader.ConfigurationLoader;
|
||||||
@ -25,7 +26,9 @@ import java.io.*;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.util.logging.Handler;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.LogRecord;
|
||||||
|
|
||||||
public class PlayerBalancer extends Plugin {
|
public class PlayerBalancer extends Plugin {
|
||||||
private boolean failed = false;
|
private boolean failed = false;
|
||||||
@ -34,17 +37,46 @@ public class PlayerBalancer extends Plugin {
|
|||||||
private SectionManager sectionManager;
|
private SectionManager sectionManager;
|
||||||
private NetworkManager networkManager;
|
private NetworkManager networkManager;
|
||||||
private ConfigurationLoader<CommentedConfigurationNode> loader;
|
private ConfigurationLoader<CommentedConfigurationNode> loader;
|
||||||
|
private final StringBuilder logsBuilder = new StringBuilder();
|
||||||
|
|
||||||
private FallbackCommand fallbackCommand;
|
private FallbackCommand fallbackCommand;
|
||||||
private Command mainCommand, manageCommand;
|
private Command mainCommand, manageCommand;
|
||||||
private Listener connectListener, kickListener, reloadListener, pluginMessageListener;
|
private Listener connectListener, kickListener, reloadListener, pluginMessageListener;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoad() {
|
||||||
|
Handler handler = new Handler() {
|
||||||
|
@Override
|
||||||
|
public void publish(LogRecord record) {
|
||||||
|
logsBuilder.append(getFormatter().format(record));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush() {
|
||||||
|
logsBuilder.setLength(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws SecurityException {
|
||||||
|
//Nothing to do
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
handler.setFormatter(new ConciseFormatter());
|
||||||
|
getProxy().getLogger().addHandler(handler);
|
||||||
|
getProxy().getLogger().setUseParentHandlers(true);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
Metrics metrics = new Metrics(this);
|
Metrics metrics = new Metrics(this);
|
||||||
metrics.addCustomChart(new SingleLineChart("configured_sections",
|
metrics.addCustomChart(new SingleLineChart("configured_sections", () -> {
|
||||||
() -> sectionManager.getSections().size()
|
if (sectionManager != null) {
|
||||||
));
|
return sectionManager.getSections().size();
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
if (!checkUpToDate()) {
|
if (!checkUpToDate()) {
|
||||||
getLogger().info("You are using a version of PlayerBalancer that is not the latest on spigot");
|
getLogger().info("You are using a version of PlayerBalancer that is not the latest on spigot");
|
||||||
@ -261,4 +293,8 @@ public class PlayerBalancer extends Plugin {
|
|||||||
public FallbackCommand getFallbackCommand() {
|
public FallbackCommand getFallbackCommand() {
|
||||||
return fallbackCommand;
|
return fallbackCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StringBuilder getLogsBuilder() {
|
||||||
|
return logsBuilder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,10 @@ import com.jaimemartz.playerbalancer.PlayerBalancer;
|
|||||||
import com.jaimemartz.playerbalancer.manager.PasteHelper;
|
import com.jaimemartz.playerbalancer.manager.PasteHelper;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import net.md_5.bungee.api.CommandSender;
|
import net.md_5.bungee.api.CommandSender;
|
||||||
|
import net.md_5.bungee.api.chat.ClickEvent;
|
||||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||||
|
import net.md_5.bungee.api.chat.HoverEvent;
|
||||||
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
import net.md_5.bungee.api.plugin.Command;
|
import net.md_5.bungee.api.plugin.Command;
|
||||||
|
|
||||||
public class MainCommand extends Command {
|
public class MainCommand extends Command {
|
||||||
@ -22,8 +25,88 @@ public class MainCommand extends Command {
|
|||||||
switch (args[0].toLowerCase()) {
|
switch (args[0].toLowerCase()) {
|
||||||
case "paste": {
|
case "paste": {
|
||||||
if (sender.hasPermission("playerbalancer.admin")) {
|
if (sender.hasPermission("playerbalancer.admin")) {
|
||||||
PasteHelper.PLUGIN.send(plugin, sender);
|
if (args.length == 2) {
|
||||||
PasteHelper.BUNGEE.send(plugin, sender);
|
switch (args[1].toLowerCase()) {
|
||||||
|
case "all": {
|
||||||
|
PasteHelper.PLUGIN.send(plugin, sender);
|
||||||
|
PasteHelper.BUNGEE.send(plugin, sender);
|
||||||
|
PasteHelper.LOGS.send(plugin, sender);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case "plugin": {
|
||||||
|
PasteHelper.PLUGIN.send(plugin, sender);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case "bungee": {
|
||||||
|
PasteHelper.BUNGEE.send(plugin, sender);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case "logs": {
|
||||||
|
PasteHelper.LOGS.send(plugin, sender);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command! Execute /balancer paste for help").color(ChatColor.RED).create());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (sender instanceof ProxiedPlayer) {
|
||||||
|
sender.sendMessage(new ComponentBuilder("Available paste types:")
|
||||||
|
.color(ChatColor.AQUA)
|
||||||
|
.create());
|
||||||
|
|
||||||
|
sender.sendMessage(new ComponentBuilder("Click one:")
|
||||||
|
.color(ChatColor.AQUA)
|
||||||
|
.append(new ComponentBuilder(" [")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.append(new ComponentBuilder("All")
|
||||||
|
.color(ChatColor.RED)
|
||||||
|
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste all"))
|
||||||
|
.create())
|
||||||
|
.append("]")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.create())
|
||||||
|
.append(new ComponentBuilder(" [")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.append(new ComponentBuilder("Plugin")
|
||||||
|
.color(ChatColor.RED)
|
||||||
|
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste plugin"))
|
||||||
|
.create())
|
||||||
|
.append("]")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.create())
|
||||||
|
.append(new ComponentBuilder(" [")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.append(new ComponentBuilder("Bungee")
|
||||||
|
.color(ChatColor.RED)
|
||||||
|
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste bungee"))
|
||||||
|
.create())
|
||||||
|
.append("]")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.create())
|
||||||
|
.append(new ComponentBuilder(" [")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.append(new ComponentBuilder("Logs")
|
||||||
|
.color(ChatColor.RED)
|
||||||
|
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste logs"))
|
||||||
|
.create())
|
||||||
|
.append("]")
|
||||||
|
.color(ChatColor.GRAY)
|
||||||
|
.create())
|
||||||
|
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
|
||||||
|
new ComponentBuilder("Click one of the types to paste it")
|
||||||
|
.color(ChatColor.RED)
|
||||||
|
.create()))
|
||||||
|
.create());
|
||||||
|
} else {
|
||||||
|
sender.sendMessage(new ComponentBuilder("Usage: /balancer paste [all|plugin|bungee|logs]").color(ChatColor.RED).create());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage(new ComponentBuilder("You do not have permission to execute this command!").color(ChatColor.RED).create());
|
sender.sendMessage(new ComponentBuilder("You do not have permission to execute this command!").color(ChatColor.RED).create());
|
||||||
}
|
}
|
||||||
@ -53,7 +136,7 @@ public class MainCommand extends Command {
|
|||||||
sender.sendMessage(new ComponentBuilder("PlayerBalancer " + plugin.getDescription().getVersion()).color(ChatColor.GRAY).create());
|
sender.sendMessage(new ComponentBuilder("PlayerBalancer " + plugin.getDescription().getVersion()).color(ChatColor.GRAY).create());
|
||||||
sender.sendMessage(new ComponentBuilder("Available commands:").color(ChatColor.GRAY).create());
|
sender.sendMessage(new ComponentBuilder("Available commands:").color(ChatColor.GRAY).create());
|
||||||
sender.sendMessage(new ComponentBuilder("/balancer").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Shows you this message").color(ChatColor.RED).create());
|
sender.sendMessage(new ComponentBuilder("/balancer").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Shows you this message").color(ChatColor.RED).create());
|
||||||
sender.sendMessage(new ComponentBuilder("/balancer paste").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Creates a paste with the important files").color(ChatColor.RED).create());
|
sender.sendMessage(new ComponentBuilder("/balancer paste [all|plugin|bungee|logs]").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Creates a paste with the important files").color(ChatColor.RED).create());
|
||||||
sender.sendMessage(new ComponentBuilder("/balancer reload").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Reloads the plugin completely").color(ChatColor.RED).create());
|
sender.sendMessage(new ComponentBuilder("/balancer reload").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Reloads the plugin completely").color(ChatColor.RED).create());
|
||||||
sender.sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
|
sender.sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ public enum PasteHelper {
|
|||||||
} else {
|
} else {
|
||||||
sender.sendMessage(new ComponentBuilder("PlayerBalancer configuration link: " + address.toString()).create());
|
sender.sendMessage(new ComponentBuilder("PlayerBalancer configuration link: " + address.toString()).create());
|
||||||
}
|
}
|
||||||
}) {
|
}, true) {
|
||||||
@Override
|
@Override
|
||||||
public URL paste(PlayerBalancer plugin) throws Exception {
|
public URL paste(PlayerBalancer plugin) throws Exception {
|
||||||
File file = new File(plugin.getDataFolder(), "plugin.conf");
|
File file = new File(plugin.getDataFolder(), "plugin.conf");
|
||||||
@ -62,7 +62,7 @@ public enum PasteHelper {
|
|||||||
} else {
|
} else {
|
||||||
sender.sendMessage(new ComponentBuilder("BungeeCord configuration link: " + address.toString()).create());
|
sender.sendMessage(new ComponentBuilder("BungeeCord configuration link: " + address.toString()).create());
|
||||||
}
|
}
|
||||||
}) {
|
}, true) {
|
||||||
@Override
|
@Override
|
||||||
public URL paste(PlayerBalancer plugin) throws Exception {
|
public URL paste(PlayerBalancer plugin) throws Exception {
|
||||||
File file = new File("config.yml");
|
File file = new File("config.yml");
|
||||||
@ -85,19 +85,51 @@ public enum PasteHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
LOGS((sender, address) -> {
|
||||||
|
if (sender instanceof ProxiedPlayer) {
|
||||||
|
sender.sendMessage(new ComponentBuilder("Click me for the plugin logs")
|
||||||
|
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, address.toString()))
|
||||||
|
.color(ChatColor.GREEN)
|
||||||
|
.create()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
sender.sendMessage(new ComponentBuilder("Plugin logs link: " + address.toString()).create());
|
||||||
|
}
|
||||||
|
}, false) {
|
||||||
|
@Override
|
||||||
|
public URL paste(PlayerBalancer plugin) throws Exception {
|
||||||
|
GuestPaste paste = new GuestPaste("e3ff18d8fb001a3ece08ae0d7d4a87bd",
|
||||||
|
plugin.getLogsBuilder().toString()
|
||||||
|
);
|
||||||
|
|
||||||
|
paste.setName("{name} ({version} on {bungee_version})"
|
||||||
|
.replace("{name}", plugin.getDescription().getName())
|
||||||
|
.replace("{version}", plugin.getDescription().getVersion())
|
||||||
|
.replace("{bungee_version}", plugin.getProxy().getVersion())
|
||||||
|
);
|
||||||
|
|
||||||
|
paste.setExpiration(GuestPaste.Expiration.ONE_MONTH);
|
||||||
|
paste.setExposure(GuestPaste.Exposure.UNLISTED);
|
||||||
|
paste.setFormat("text");
|
||||||
|
|
||||||
|
return paste.paste();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private URL url;
|
private URL url;
|
||||||
|
|
||||||
private final BiConsumer<CommandSender, URL> consumer;
|
private final BiConsumer<CommandSender, URL> consumer;
|
||||||
|
private final boolean cache;
|
||||||
|
|
||||||
PasteHelper(BiConsumer<CommandSender, URL> consumer) {
|
PasteHelper(BiConsumer<CommandSender, URL> consumer, boolean cache) {
|
||||||
this.consumer = consumer;
|
this.consumer = consumer;
|
||||||
|
this.cache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(PlayerBalancer plugin, CommandSender sender) {
|
public void send(PlayerBalancer plugin, CommandSender sender) {
|
||||||
boolean cached = url != null;
|
if (url == null || !cache) {
|
||||||
if (url == null) {
|
|
||||||
try {
|
try {
|
||||||
url = paste(plugin);
|
url = paste(plugin);
|
||||||
} catch (PasteException e) {
|
} catch (PasteException e) {
|
||||||
@ -113,16 +145,20 @@ public enum PasteHelper {
|
|||||||
);
|
);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
sender.sendMessage(new ComponentBuilder("This is a cached link, reload the plugin for it to refresh!")
|
||||||
|
.color(ChatColor.RED)
|
||||||
|
.create()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
consumer.accept(sender, url);
|
consumer.accept(sender, url);
|
||||||
if (cached) {
|
} else {
|
||||||
sender.sendMessage(new ComponentBuilder("This is a cached link, reload the plugin for it to refresh!")
|
sender.sendMessage(new ComponentBuilder("Could not create the paste, try again...")
|
||||||
.color(ChatColor.RED)
|
.color(ChatColor.RED)
|
||||||
.create()
|
.create()
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,11 +23,11 @@ public class ServerRefreshProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getDelay() {
|
public int getDelay() {
|
||||||
return interval;
|
return delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDelay(int interval) {
|
public void setDelay(int delay) {
|
||||||
this.interval = interval;
|
this.delay = delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInterval() {
|
public int getInterval() {
|
||||||
|
Loading…
Reference in New Issue
Block a user