Added command variant for pasting logs

This commit is contained in:
Jaime Martínez Rincón 2018-01-04 18:46:42 +01:00
parent cefe7f9372
commit e45af57ad8
4 changed files with 175 additions and 20 deletions

View File

@ -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.Listener;
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.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
@ -25,7 +26,9 @@ import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class PlayerBalancer extends Plugin {
private boolean failed = false;
@ -34,17 +37,46 @@ public class PlayerBalancer extends Plugin {
private SectionManager sectionManager;
private NetworkManager networkManager;
private ConfigurationLoader<CommentedConfigurationNode> loader;
private final StringBuilder logsBuilder = new StringBuilder();
private FallbackCommand fallbackCommand;
private Command mainCommand, manageCommand;
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
public void onEnable() {
Metrics metrics = new Metrics(this);
metrics.addCustomChart(new SingleLineChart("configured_sections",
() -> sectionManager.getSections().size()
));
metrics.addCustomChart(new SingleLineChart("configured_sections", () -> {
if (sectionManager != null) {
return sectionManager.getSections().size();
} else {
return 0;
}
}));
if (!checkUpToDate()) {
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() {
return fallbackCommand;
}
public StringBuilder getLogsBuilder() {
return logsBuilder;
}
}

View File

@ -5,7 +5,10 @@ import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.manager.PasteHelper;
import net.md_5.bungee.api.ChatColor;
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.HoverEvent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
public class MainCommand extends Command {
@ -22,8 +25,88 @@ public class MainCommand extends Command {
switch (args[0].toLowerCase()) {
case "paste": {
if (sender.hasPermission("playerbalancer.admin")) {
PasteHelper.PLUGIN.send(plugin, sender);
PasteHelper.BUNGEE.send(plugin, sender);
if (args.length == 2) {
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 {
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("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 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(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
}

View File

@ -27,7 +27,7 @@ public enum PasteHelper {
} else {
sender.sendMessage(new ComponentBuilder("PlayerBalancer configuration link: " + address.toString()).create());
}
}) {
}, true) {
@Override
public URL paste(PlayerBalancer plugin) throws Exception {
File file = new File(plugin.getDataFolder(), "plugin.conf");
@ -62,7 +62,7 @@ public enum PasteHelper {
} else {
sender.sendMessage(new ComponentBuilder("BungeeCord configuration link: " + address.toString()).create());
}
}) {
}, true) {
@Override
public URL paste(PlayerBalancer plugin) throws Exception {
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 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.cache = cache;
}
public void send(PlayerBalancer plugin, CommandSender sender) {
boolean cached = url != null;
if (url == null) {
if (url == null || !cache) {
try {
url = paste(plugin);
} catch (PasteException e) {
@ -113,16 +145,20 @@ public enum PasteHelper {
);
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) {
consumer.accept(sender, url);
if (cached) {
sender.sendMessage(new ComponentBuilder("This is a cached link, reload the plugin for it to refresh!")
.color(ChatColor.RED)
.create()
);
}
} else {
sender.sendMessage(new ComponentBuilder("Could not create the paste, try again...")
.color(ChatColor.RED)
.create()
);
}
}

View File

@ -23,11 +23,11 @@ public class ServerRefreshProps {
}
public int getDelay() {
return interval;
return delay;
}
public void setDelay(int interval) {
this.interval = interval;
public void setDelay(int delay) {
this.delay = delay;
}
public int getInterval() {