Skip to content

Commit e45af57

Browse files
committed
Added command variant for pasting logs
1 parent cefe7f9 commit e45af57

File tree

4 files changed

+175
-20
lines changed

4 files changed

+175
-20
lines changed

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.md_5.bungee.api.plugin.Command;
1616
import net.md_5.bungee.api.plugin.Listener;
1717
import net.md_5.bungee.api.plugin.Plugin;
18+
import net.md_5.bungee.log.ConciseFormatter;
1819
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
1920
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
2021
import ninja.leaping.configurate.loader.ConfigurationLoader;
@@ -25,7 +26,9 @@
2526
import java.net.URL;
2627
import java.net.URLConnection;
2728
import java.nio.file.Files;
29+
import java.util.logging.Handler;
2830
import java.util.logging.Level;
31+
import java.util.logging.LogRecord;
2932

3033
public class PlayerBalancer extends Plugin {
3134
private boolean failed = false;
@@ -34,17 +37,46 @@ public class PlayerBalancer extends Plugin {
3437
private SectionManager sectionManager;
3538
private NetworkManager networkManager;
3639
private ConfigurationLoader<CommentedConfigurationNode> loader;
40+
private final StringBuilder logsBuilder = new StringBuilder();
3741

3842
private FallbackCommand fallbackCommand;
3943
private Command mainCommand, manageCommand;
4044
private Listener connectListener, kickListener, reloadListener, pluginMessageListener;
4145

46+
@Override
47+
public void onLoad() {
48+
Handler handler = new Handler() {
49+
@Override
50+
public void publish(LogRecord record) {
51+
logsBuilder.append(getFormatter().format(record));
52+
}
53+
54+
@Override
55+
public void flush() {
56+
logsBuilder.setLength(0);
57+
}
58+
59+
@Override
60+
public void close() throws SecurityException {
61+
//Nothing to do
62+
}
63+
};
64+
65+
handler.setFormatter(new ConciseFormatter());
66+
getProxy().getLogger().addHandler(handler);
67+
getProxy().getLogger().setUseParentHandlers(true);
68+
}
69+
4270
@Override
4371
public void onEnable() {
4472
Metrics metrics = new Metrics(this);
45-
metrics.addCustomChart(new SingleLineChart("configured_sections",
46-
() -> sectionManager.getSections().size()
47-
));
73+
metrics.addCustomChart(new SingleLineChart("configured_sections", () -> {
74+
if (sectionManager != null) {
75+
return sectionManager.getSections().size();
76+
} else {
77+
return 0;
78+
}
79+
}));
4880

4981
if (!checkUpToDate()) {
5082
getLogger().info("You are using a version of PlayerBalancer that is not the latest on spigot");
@@ -261,4 +293,8 @@ public NetworkManager getNetworkManager() {
261293
public FallbackCommand getFallbackCommand() {
262294
return fallbackCommand;
263295
}
296+
297+
public StringBuilder getLogsBuilder() {
298+
return logsBuilder;
299+
}
264300
}

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/MainCommand.java

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import com.jaimemartz.playerbalancer.manager.PasteHelper;
66
import net.md_5.bungee.api.ChatColor;
77
import net.md_5.bungee.api.CommandSender;
8+
import net.md_5.bungee.api.chat.ClickEvent;
89
import net.md_5.bungee.api.chat.ComponentBuilder;
10+
import net.md_5.bungee.api.chat.HoverEvent;
11+
import net.md_5.bungee.api.connection.ProxiedPlayer;
912
import net.md_5.bungee.api.plugin.Command;
1013

1114
public class MainCommand extends Command {
@@ -22,8 +25,88 @@ public void execute(final CommandSender sender, String[] args) {
2225
switch (args[0].toLowerCase()) {
2326
case "paste": {
2427
if (sender.hasPermission("playerbalancer.admin")) {
25-
PasteHelper.PLUGIN.send(plugin, sender);
26-
PasteHelper.BUNGEE.send(plugin, sender);
28+
if (args.length == 2) {
29+
switch (args[1].toLowerCase()) {
30+
case "all": {
31+
PasteHelper.PLUGIN.send(plugin, sender);
32+
PasteHelper.BUNGEE.send(plugin, sender);
33+
PasteHelper.LOGS.send(plugin, sender);
34+
break;
35+
}
36+
37+
case "plugin": {
38+
PasteHelper.PLUGIN.send(plugin, sender);
39+
break;
40+
}
41+
42+
case "bungee": {
43+
PasteHelper.BUNGEE.send(plugin, sender);
44+
break;
45+
}
46+
47+
case "logs": {
48+
PasteHelper.LOGS.send(plugin, sender);
49+
break;
50+
}
51+
52+
default: {
53+
sender.sendMessage(new ComponentBuilder("This is not a valid argument for this command! Execute /balancer paste for help").color(ChatColor.RED).create());
54+
}
55+
}
56+
} else {
57+
if (sender instanceof ProxiedPlayer) {
58+
sender.sendMessage(new ComponentBuilder("Available paste types:")
59+
.color(ChatColor.AQUA)
60+
.create());
61+
62+
sender.sendMessage(new ComponentBuilder("Click one:")
63+
.color(ChatColor.AQUA)
64+
.append(new ComponentBuilder(" [")
65+
.color(ChatColor.GRAY)
66+
.append(new ComponentBuilder("All")
67+
.color(ChatColor.RED)
68+
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste all"))
69+
.create())
70+
.append("]")
71+
.color(ChatColor.GRAY)
72+
.create())
73+
.append(new ComponentBuilder(" [")
74+
.color(ChatColor.GRAY)
75+
.append(new ComponentBuilder("Plugin")
76+
.color(ChatColor.RED)
77+
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste plugin"))
78+
.create())
79+
.append("]")
80+
.color(ChatColor.GRAY)
81+
.create())
82+
.append(new ComponentBuilder(" [")
83+
.color(ChatColor.GRAY)
84+
.append(new ComponentBuilder("Bungee")
85+
.color(ChatColor.RED)
86+
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste bungee"))
87+
.create())
88+
.append("]")
89+
.color(ChatColor.GRAY)
90+
.create())
91+
.append(new ComponentBuilder(" [")
92+
.color(ChatColor.GRAY)
93+
.append(new ComponentBuilder("Logs")
94+
.color(ChatColor.RED)
95+
.event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/balancer paste logs"))
96+
.create())
97+
.append("]")
98+
.color(ChatColor.GRAY)
99+
.create())
100+
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
101+
new ComponentBuilder("Click one of the types to paste it")
102+
.color(ChatColor.RED)
103+
.create()))
104+
.create());
105+
} else {
106+
sender.sendMessage(new ComponentBuilder("Usage: /balancer paste [all|plugin|bungee|logs]").color(ChatColor.RED).create());
107+
}
108+
109+
}
27110
} else {
28111
sender.sendMessage(new ComponentBuilder("You do not have permission to execute this command!").color(ChatColor.RED).create());
29112
}
@@ -53,7 +136,7 @@ public void execute(final CommandSender sender, String[] args) {
53136
sender.sendMessage(new ComponentBuilder("PlayerBalancer " + plugin.getDescription().getVersion()).color(ChatColor.GRAY).create());
54137
sender.sendMessage(new ComponentBuilder("Available commands:").color(ChatColor.GRAY).create());
55138
sender.sendMessage(new ComponentBuilder("/balancer").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Shows you this message").color(ChatColor.RED).create());
56-
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());
139+
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());
57140
sender.sendMessage(new ComponentBuilder("/balancer reload").color(ChatColor.AQUA).append(" - ").color(ChatColor.GRAY).append("Reloads the plugin completely").color(ChatColor.RED).create());
58141
sender.sendMessage(new ComponentBuilder(Strings.repeat("-", 53)).strikethrough(true).color(ChatColor.GRAY).create());
59142
}

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/manager/PasteHelper.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public enum PasteHelper {
2727
} else {
2828
sender.sendMessage(new ComponentBuilder("PlayerBalancer configuration link: " + address.toString()).create());
2929
}
30-
}) {
30+
}, true) {
3131
@Override
3232
public URL paste(PlayerBalancer plugin) throws Exception {
3333
File file = new File(plugin.getDataFolder(), "plugin.conf");
@@ -62,7 +62,7 @@ public URL paste(PlayerBalancer plugin) throws Exception {
6262
} else {
6363
sender.sendMessage(new ComponentBuilder("BungeeCord configuration link: " + address.toString()).create());
6464
}
65-
}) {
65+
}, true) {
6666
@Override
6767
public URL paste(PlayerBalancer plugin) throws Exception {
6868
File file = new File("config.yml");
@@ -85,19 +85,51 @@ public URL paste(PlayerBalancer plugin) throws Exception {
8585
}
8686
}
8787
}
88+
},
89+
90+
LOGS((sender, address) -> {
91+
if (sender instanceof ProxiedPlayer) {
92+
sender.sendMessage(new ComponentBuilder("Click me for the plugin logs")
93+
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, address.toString()))
94+
.color(ChatColor.GREEN)
95+
.create()
96+
);
97+
} else {
98+
sender.sendMessage(new ComponentBuilder("Plugin logs link: " + address.toString()).create());
99+
}
100+
}, false) {
101+
@Override
102+
public URL paste(PlayerBalancer plugin) throws Exception {
103+
GuestPaste paste = new GuestPaste("e3ff18d8fb001a3ece08ae0d7d4a87bd",
104+
plugin.getLogsBuilder().toString()
105+
);
106+
107+
paste.setName("{name} ({version} on {bungee_version})"
108+
.replace("{name}", plugin.getDescription().getName())
109+
.replace("{version}", plugin.getDescription().getVersion())
110+
.replace("{bungee_version}", plugin.getProxy().getVersion())
111+
);
112+
113+
paste.setExpiration(GuestPaste.Expiration.ONE_MONTH);
114+
paste.setExposure(GuestPaste.Exposure.UNLISTED);
115+
paste.setFormat("text");
116+
117+
return paste.paste();
118+
}
88119
};
89120

90121
private URL url;
91122

92123
private final BiConsumer<CommandSender, URL> consumer;
124+
private final boolean cache;
93125

94-
PasteHelper(BiConsumer<CommandSender, URL> consumer) {
126+
PasteHelper(BiConsumer<CommandSender, URL> consumer, boolean cache) {
95127
this.consumer = consumer;
128+
this.cache = cache;
96129
}
97130

98131
public void send(PlayerBalancer plugin, CommandSender sender) {
99-
boolean cached = url != null;
100-
if (url == null) {
132+
if (url == null || !cache) {
101133
try {
102134
url = paste(plugin);
103135
} catch (PasteException e) {
@@ -113,16 +145,20 @@ public void send(PlayerBalancer plugin, CommandSender sender) {
113145
);
114146
e.printStackTrace();
115147
}
148+
} else {
149+
sender.sendMessage(new ComponentBuilder("This is a cached link, reload the plugin for it to refresh!")
150+
.color(ChatColor.RED)
151+
.create()
152+
);
116153
}
117154

118155
if (url != null) {
119156
consumer.accept(sender, url);
120-
if (cached) {
121-
sender.sendMessage(new ComponentBuilder("This is a cached link, reload the plugin for it to refresh!")
122-
.color(ChatColor.RED)
123-
.create()
124-
);
125-
}
157+
} else {
158+
sender.sendMessage(new ComponentBuilder("Could not create the paste, try again...")
159+
.color(ChatColor.RED)
160+
.create()
161+
);
126162
}
127163
}
128164

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/settings/props/features/ServerRefreshProps.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public void setEnabled(boolean enabled) {
2323
}
2424

2525
public int getDelay() {
26-
return interval;
26+
return delay;
2727
}
2828

29-
public void setDelay(int interval) {
30-
this.interval = interval;
29+
public void setDelay(int delay) {
30+
this.delay = delay;
3131
}
3232

3333
public int getInterval() {

0 commit comments

Comments
 (0)