Skip to content

Commit

Permalink
Rewrite the SubServers.Console commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Jan 7, 2019
1 parent 3635fea commit 0689896
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 186 deletions.
@@ -0,0 +1,217 @@
package net.ME1312.SubServers.Console;

import net.ME1312.SubServers.Bungee.Host.Host;
import net.ME1312.SubServers.Bungee.Host.SubServer;
import net.ME1312.SubServers.Bungee.Library.Callback;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.command.ConsoleCommandSender;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public final class ConsoleCommand {
private ConsoleCommand() {}

public static class POPOUT extends Command {
private ConsolePlugin plugin;
private String label;

public POPOUT(ConsolePlugin plugin, String command) {
super(command);
this.plugin = plugin;
this.label = command;
}

@Override
public void execute(final CommandSender sender, String[] args) {
if (sender instanceof ConsoleCommandSender) {
if (args.length > 0) {
final String type = (args.length > 1)?args[0]:null;
final String name = args[(type != null)?1:0];

final Runnable forServer = new Runnable() {
@Override
public void run() {
SubServer server = plugin.getProxy().api.getSubServer(name);
if (server != null) {
if (server.isRunning()) {
System.out.println("SubConsole > Opening console window...");
if (!plugin.sCurrent.keySet().contains(name.toLowerCase())) {
ConsoleWindow window = new ConsoleWindow(plugin, server.getLogger());
plugin.sCurrent.put(name.toLowerCase(), window);
window.open();
} else {
plugin.sCurrent.get(name.toLowerCase()).open();
}
} else {
sender.sendMessage("SubConsole > That SubServer is not running right now");
}
} else {
if (type == null) {
sender.sendMessage("SubServers > There is no object with that name");
} else {
sender.sendMessage("SubServers > There is no subserver with that name");
}
}
}
};
final Runnable forCreator = new Runnable() {
@Override
public void run() {
Host host = plugin.getProxy().api.getHost(name);
if (host != null) {
if (host.getCreator().getReservedNames().size() > 0) {
sender.sendMessage("SubConsole > Opening console window" + ((host.getCreator().getReservedNames().size() == 1)?"":"s") + "...");
for (String reserved : host.getCreator().getReservedNames()) {
if (!plugin.cCurrent.keySet().contains(reserved.toLowerCase())) {
ConsoleWindow window = new ConsoleWindow(plugin, host.getCreator().getLogger(reserved));
plugin.cCurrent.put(reserved.toLowerCase(), window);
window.open();
} else {
plugin.cCurrent.get(reserved.toLowerCase()).open();
}
}
} else {
sender.sendMessage("SubConsole > That Host is not running SubCreator right now");
}
} else {
if (type == null) {
forServer.run();
} else {
sender.sendMessage("SubConsole > There is no host with that name");
}
}
}
};

if (type == null) {
forCreator.run();
} else {
switch (type.toLowerCase()) {
case "h":
case "host":
case "c":
case "creator":
case "subcreator":
forCreator.run();
break;
case "s":
case "server":
case "subserver":
forServer.run();
break;
default:
sender.sendMessage("SubConsole > There is no object type with that name");
}
}
} else {
System.out.println("SubConsole > Usage: /" + label + " [host|server] <Name>");
}
} else {
String str = label;
for (String arg : args) str += ' ' + arg;
((ProxiedPlayer) sender).chat(str);
}
}
}

public static class AUTO_POPOUT extends Command {
private ConsolePlugin plugin;
private String label;

public AUTO_POPOUT(ConsolePlugin plugin, String command) {
super(command);
this.plugin = plugin;
this.label = command;
}

@Override
public void execute(final CommandSender sender, String[] args) {
if (sender instanceof ConsoleCommandSender) {
if (args.length > 0) {
final String type = (args.length > 1)?args[0]:null;
final String name = args[(type != null)?1:0];

final Runnable forServer = new Runnable() {
@Override
public void run() {
SubServer server = plugin.getProxy().api.getSubServer(name);
List<String> list = plugin.config.get().getStringList("Enabled-Servers");
if (!plugin.config.get().getStringList("Enabled-Servers").contains(name.toLowerCase())) {
list.add(name.toLowerCase());
if (server == null) plugin.getProxy().getLogger().warning("SubConsole > SubServer with name \"" + name + "\" does not exist");
sender.sendMessage("SubConsole > " + ((server == null)?name:server.getName()) + " will now popout its console by default");
} else {
list.remove(name.toLowerCase());
sender.sendMessage("SubConsole > " + ((server == null)?name:server.getName()) + " will no longer popout its console by default");
}
plugin.config.get().set("Enabled-Servers", list);

try {
plugin.config.save();
} catch (Exception e) {
e.printStackTrace();
}
}
};
final Callback<Boolean> forCreator = new Callback<Boolean>() {
@Override
public void run(Boolean force) {
Host host = plugin.getProxy().api.getHost(name);
if (force || host != null) {
List<String> list = plugin.config.get().getStringList("Enabled-Creators");
if (!plugin.config.get().getStringList("Enabled-Creators").contains(name.toLowerCase())) {
list.add(name.toLowerCase());
if (host == null) plugin.getProxy().getLogger().warning("SubConsole > Host with name \"" + name + "\" does not exist");
sender.sendMessage("SubConsole > " + ((host == null)?name:host.getName()) + " will now popout SubCreator's console by default");
} else {
list.remove(name.toLowerCase());
sender.sendMessage("SubConsole > " + ((host == null)?name:host.getName()) + " will no longer popout SubCreator's console by default");
}
plugin.config.get().set("Enabled-Creators", list);

try {
plugin.config.save();
} catch (Exception e) {
e.printStackTrace();
}
} else {
forServer.run();
}
}
};

if (type == null) {
forCreator.run(false);
} else {
switch (type.toLowerCase()) {
case "h":
case "host":
case "c":
case "creator":
case "subcreator":
forCreator.run(true);
break;
case "s":
case "server":
case "subserver":
forServer.run();
break;
default:
sender.sendMessage("SubConsole > There is no object type with that name");
}
}
} else {
System.out.println("SubConsole > Usage: /" + label + " [host|server] <Name>");
}
} else {
String str = label;
for (String arg : args) str += ' ' + arg;
((ProxiedPlayer) sender).chat(str);
}
}
}
}
File renamed without changes
Expand Up @@ -55,9 +55,9 @@ public void enable() {
if (save) config.save();

getProxy().getPluginManager().registerListener(this, this);
getProxy().getPluginManager().registerCommand(this, new PopoutCommand.SERVER(this, "popout"));
getProxy().getPluginManager().registerCommand(this, new PopoutCommand.SERVER(this, "popouts"));
getProxy().getPluginManager().registerCommand(this, new PopoutCommand.CREATOR(this, "popoutc"));
getProxy().getPluginManager().registerCommand(this, new ConsoleCommand.POPOUT(this, "popout"));
getProxy().getPluginManager().registerCommand(this, new ConsoleCommand.AUTO_POPOUT(this, "apopout"));
getProxy().getPluginManager().registerCommand(this, new ConsoleCommand.AUTO_POPOUT(this, "autopopout"));

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Expand All @@ -72,23 +72,29 @@ public void enable() {

@EventHandler(priority = EventPriority.HIGHEST)
public void onServerCreate(SubCreateEvent event) {
if (!event.isCancelled() && config.get().getStringList("Enabled-Creators").contains(event.getHost().getName().toLowerCase())) {
if (!event.isCancelled()) {
if (!cCurrent.keySet().contains(event.getHost().getName().toLowerCase())) {
cCurrent.put(event.getName().toLowerCase(), new ConsoleWindow(this, event.getHost().getCreator().getLogger(event.getName().toLowerCase())));
} else {
cCurrent.get(event.getName().toLowerCase()).clear();
}

if (config.get().getStringList("Enabled-Creators").contains(event.getHost().getName().toLowerCase()))
cCurrent.get(event.getName().toLowerCase()).open();
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onServerStart(SubStartEvent event) {
if (!event.isCancelled() && config.get().getStringList("Enabled-Servers").contains(event.getServer().getName().toLowerCase())) {
if (!event.isCancelled()) {
if (!sCurrent.keySet().contains(event.getServer().getName().toLowerCase())) {
sCurrent.put(event.getServer().getName().toLowerCase(), new ConsoleWindow(this, event.getServer().getLogger()));
} else {
sCurrent.get(event.getServer().getName().toLowerCase()).clear();
}

if (config.get().getStringList("Enabled-Servers").contains(event.getServer().getName().toLowerCase()))
sCurrent.get(event.getServer().getName().toLowerCase()).open();
}
}

Expand All @@ -102,16 +108,12 @@ public void onServerCommand(SubSendCommandEvent event) {
public void onClose(ConsoleWindow window) {
if (window.getLogger().getHandler() instanceof SubServer) {
SubServer server = (SubServer) window.getLogger().getHandler();
if (!config.get().getStringList("Enabled-Servers").contains(server.getName().toLowerCase())) {
window.destroy();
sCurrent.remove(server.getName().toLowerCase());
}
window.destroy();
sCurrent.remove(server.getName().toLowerCase());
} else if (window.getLogger().getHandler() instanceof SubCreator) {
Host host = ((SubCreator) window.getLogger().getHandler()).getHost();
if (!config.get().getStringList("Enabled-Creators").contains(host.getName().toLowerCase())) {
window.destroy();
sCurrent.remove(host.getName().toLowerCase());
}
window.destroy();
sCurrent.remove(host.getName().toLowerCase());
} else {
window.destroy();
}
Expand Down
Expand Up @@ -24,7 +24,7 @@
import java.util.logging.Level;

public final class ConsoleWindow implements SubLogFilter {
private static final int MAX_SCROLLBACK = (Integer.getInteger("subservers.console.max_scrollback", 15000) >= 128)?Integer.getInteger("subservers.console.max_scrollback", 15000):15000;
private static final int MAX_SCROLLBACK = (Integer.getInteger("subservers.console.max_scrollback", 0) >= 128)?Integer.getInteger("subservers.console.max_scrollback"):15000;
private static final String RESET_VALUE = "\n\u00A0\n\u00A0";
private ConsolePlugin plugin;
private JFrame window;
Expand Down Expand Up @@ -326,7 +326,7 @@ public void actionPerformed(ActionEvent event) {
Util.isException(new Util.ExceptionRunnable() {
@Override
public void run() throws Throwable {
window.setIconImage(ImageIO.read(ConsolePlugin.class.getResourceAsStream("/SubServers.png")));
window.setIconImage(ImageIO.read(ConsolePlugin.class.getResourceAsStream("/net/ME1312/SubServers/Console/ConsoleIcon.png")));
}
});
window.setTitle(logger.getName() + " \u2014 SubServers 2");
Expand Down Expand Up @@ -558,7 +558,6 @@ public void actionPerformed(ActionEvent event) {
log.setText(RESET_VALUE);
loadContent();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keys);
if (logger.isLogging() && !open) open();
}
private void hScroll() {
hScroll.setMaximum(vScroll.getHorizontalScrollBar().getMaximum());
Expand Down Expand Up @@ -597,10 +596,6 @@ public void clear() {
hScroll();
}

@Override
public void start() {
open();
}
public void open() {
if (!open) {
window.setVisible(true);
Expand All @@ -613,6 +608,8 @@ public boolean isOpen() {
return open;
}

@Override
public void start() {}
private void loadContent() {
if (file != null) {
try (FileInputStream reader = new FileInputStream(file)) {
Expand All @@ -629,18 +626,9 @@ private void loadContent() {

@Override
public void stop() {
close();
clear();
if (filewriter != null) try {
filewriter.close();
} catch (Exception e) {}
if (file != null) try {
filewriter = new FileOutputStream(file, false);
} catch (Exception e) {
e.printStackTrace();
}

plugin.onClose(this);
}

public void close() {
if (open) {
this.open = false;
Expand All @@ -650,7 +638,6 @@ public void close() {
findO = 0;
}
window.setVisible(false);
plugin.onClose(this);
}
}

Expand Down

0 comments on commit 0689896

Please sign in to comment.