Last active
November 1, 2017 19:07
-
-
Save TheMasteredPanda/64639f64e082c8bc3cb1d5e33f1b66b7 to your computer and use it in GitHub Desktop.
ServerCommand Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.pucman.bungee.command; | |
import com.google.common.util.concurrent.ListeningExecutorService; | |
import com.google.common.util.concurrent.MoreExecutors; | |
import io.pucman.bungee.PLibrary; | |
import io.pucman.bungee.manager.Manager; | |
import lombok.AccessLevel; | |
import lombok.Getter; | |
import net.md_5.bungee.api.plugin.Plugin; | |
import javax.annotation.ParametersAreNonnullByDefault; | |
import java.util.concurrent.Executors; | |
/** | |
* Command manager for Pucman Commands. | |
* | |
* @see PucmanCommand | |
*/ | |
@ParametersAreNonnullByDefault | |
public class CommandManager extends Manager<PLibrary> | |
{ | |
/** | |
* Used for asynchronous execution of the three bodies in the command wrapper. | |
* | |
* @see PucmanCommand | |
*/ | |
@Getter(value = AccessLevel.PROTECTED) | |
private ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); | |
public CommandManager(PLibrary instance) | |
{ | |
super(instance, Priority.HIGH); | |
} | |
/** | |
* To register commands. | |
* @param instance - the instance of the plugin the command is written for. | |
* @param commands - the command array. | |
* @param <P> - the plugin generic type. | |
*/ | |
public <P extends Plugin> void register(P instance, PucmanCommand... commands) | |
{ | |
for (PucmanCommand command : commands) { | |
this.instance.getPluginManager().registerCommand(instance, command); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.zoramagic.bungee.core.api.module; | |
import io.pucman.bungee.command.PucmanCommand; | |
import io.pucman.bungee.locale.Locale; | |
import io.pucman.common.exception.DeveloperException; | |
import lombok.Getter; | |
import net.zoramagic.bungee.core.ZCore; | |
import javax.annotation.ParametersAreNonnullByDefault; | |
import javax.annotation.concurrent.NotThreadSafe; | |
import java.util.Arrays; | |
import java.util.logging.Level; | |
@Getter | |
@NotThreadSafe | |
@ParametersAreNonnullByDefault | |
public abstract class CommandModule extends PucmanCommand<ZCore> implements Module | |
{ | |
/** | |
* Name of the module. | |
*/ | |
private String name; | |
/** | |
* Module version. | |
*/ | |
private double version; | |
/** | |
* Module authors. | |
*/ | |
private String[] authors; | |
/** | |
* Whether the module is enabled or not. | |
*/ | |
private boolean enabled; | |
public CommandModule(ZCore instance, String moduleName, double moduleVersion, String[] authors, Locale locale, String name, String permission, String description, boolean playerOnlyCommand, int asynchronousState, String... aliases) | |
{ | |
super(instance, locale, name, permission, description, playerOnlyCommand, asynchronousState, aliases); | |
this.name = moduleName; | |
this.version = moduleVersion; | |
this.authors = authors; | |
instance.register(this); | |
} | |
@Override | |
public void log(Level level, String... messages) | |
{ | |
Arrays.stream(messages).forEachOrdered(msg -> this.instance.getLogger().log(level, "[" + this.name + "] " + msg)); | |
} | |
@Override | |
public final void init() | |
{ | |
if (!this.enabled) { | |
try { | |
this.log(Level.INFO, "Enabling module " + this.name + "-v" + String.valueOf(this.version) + " by " + "author."); | |
this.onEnable(); | |
this.enabled = true; | |
this.log(Level.INFO, "Enabled module " + this.name + "-v" + String.valueOf(this.version) + "."); | |
} catch (Exception e) { | |
this.log(Level.SEVERE, "Could not enable module " + this.name + "-v" + String.valueOf(this.version) + "."); | |
this.enabled = false; | |
throw new DeveloperException(e); | |
} | |
} | |
} | |
@Override | |
public final void shutdown() | |
{ | |
if (this.enabled) { | |
try { | |
this.log(Level.INFO, "Disabling module " + this.name + "-v" + String.valueOf(this.version) + " by " + "author"); | |
this.onDisable(); | |
this.enabled = false; | |
this.log(Level.INFO, "Disabled module " + this.name + "-v" + String.valueOf(this.version) + "."); | |
} catch (Exception e) { | |
this.log(Level.SEVERE, "Could not disable module " + this.name + "-v" + String.valueOf(this.version)); | |
this.enabled = false; | |
throw new DeveloperException(e); | |
} | |
} | |
} | |
@Override | |
public void onEnable() | |
{ | |
} | |
@Override | |
public void onDisable() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.zoramagic.bungee.core.modules.commands; | |
import com.google.common.collect.Lists; | |
import io.pucman.bungee.command.ArgumentField; | |
import io.pucman.bungee.command.CommandResponse; | |
import io.pucman.bungee.command.PucmanCommand; | |
import io.pucman.bungee.file.ConfigPopulate; | |
import io.pucman.bungee.locale.Format; | |
import io.pucman.bungee.sender.Sender; | |
import io.pucman.common.math.NumberUtil; | |
import io.pucman.depend.google.common.collect.LinkedListMultimap; | |
import net.md_5.bungee.api.CommandSender; | |
import net.md_5.bungee.api.ProxyServer; | |
import net.md_5.bungee.api.chat.BaseComponent; | |
import net.md_5.bungee.api.chat.ClickEvent; | |
import net.md_5.bungee.api.chat.HoverEvent; | |
import net.md_5.bungee.api.chat.TextComponent; | |
import net.md_5.bungee.api.config.ServerInfo; | |
import net.zoramagic.bungee.core.ZCore; | |
import net.zoramagic.bungee.core.api.module.CommandModule; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Map; | |
public class ServerCommand extends CommandModule | |
{ | |
@ConfigPopulate(value = "Modules.Commands.Server.Header", color = true) | |
private String LIST_HEADER; | |
@ConfigPopulate(value = "Modules.Commands.Server.Footer", color = true) | |
private String LIST_FOOTER; | |
@ConfigPopulate(value = "Modules.Commands.Server.TemplateServerEntry", color = true) | |
private String SERVER_LIST_ENTRY; | |
@ConfigPopulate(value = "Modules.Commands.Server.ServerInfoTemplate", color = true) | |
private String SERVER_INFO_TEMPLATE; | |
@ConfigPopulate(value = "Modules.Commands.Server.ServerNotOnline", color = true) | |
private String SERVER_NOT_ONLINE; | |
public ServerCommand(ZCore instance) | |
{ | |
super(instance, "CMDServer", 1.0, new String[] {"TheMasteredPanda"}, instance.getLocale(), "list", "zcore.bungee.list", "Lists all servers connected to this Proxy", false, 1, "ls"); | |
this.addChildCommands(new ServerListCommand(instance)); | |
} | |
@Override | |
public CommandResponse execute(CommandSender sender, LinkedList<String> arguments) | |
{ | |
Map<String, ServerInfo> servers = ProxyServer.getInstance().getServers(); | |
List<TextComponent> context = Lists.newLinkedList(); | |
for (Map.Entry<String, ServerInfo> entry : servers.entrySet()) { | |
ServerInfo server = entry.getValue(); | |
server.ping((serverPing, throwable) -> { | |
TextComponent component; | |
if (throwable != null) { | |
component = new TextComponent(this.SERVER_LIST_ENTRY.replace("{server}", entry.getKey()).replace("{color}", "&a")); | |
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/server " + entry.getKey())); | |
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(this.SERVER_INFO_TEMPLATE.replace("{totalplayers}", String.valueOf(server.getPlayers().size())))})); | |
} else { | |
component = new TextComponent(this.SERVER_LIST_ENTRY.replace("{server}", entry.getKey()).replace("{color}", "&c")); | |
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(this.SERVER_NOT_ONLINE)})); | |
} | |
context.add(component); | |
}); | |
LinkedListMultimap<Integer, TextComponent> pages = Format.paginate(context, new TextComponent(this.LIST_HEADER), new TextComponent(this.LIST_FOOTER), 10); | |
if (arguments.isEmpty()) { | |
sender.sendMessage(pages.get(1).toArray(new TextComponent[pages.get(1).size()])); | |
} else { | |
if (NumberUtil.parseable(arguments.get(0), Integer.class)) { | |
int pageNumber = NumberUtil.parse(arguments.get(0), Integer.class); | |
sender.sendMessage(pages.get(pageNumber).toArray(new TextComponent[pages.get(pageNumber).size()])); | |
} else { | |
return new CommandResponse(sender, arguments, CommandResponse.Type.FAILURE).with("incorrectArgumentField", "pageNumber"); | |
} | |
} | |
} | |
return new CommandResponse(sender, arguments, CommandResponse.Type.SUCCESS); | |
} | |
@Override | |
public void onFailure(CommandSender sender, Map<String, Object> data, LinkedList<String> parameters) | |
{ | |
Sender.send(sender, this.INCORRECT_ARGUMENT_INPUT.replace("{argument}", (String) data.get("incorrectArgumentField"))); | |
} | |
public class ServerListCommand extends PucmanCommand<ZCore> | |
{ | |
public ServerListCommand(ZCore instance) | |
{ | |
super(instance, instance.getLocale(), "list", "zcore.bungee.server.list", "List all servers linked to this proxy", false, 1, "ls"); | |
this.arguments(new ArgumentField("page number", "1")); | |
} | |
@Override | |
public CommandResponse execute(CommandSender sender, LinkedList<String> arguments) | |
{ | |
Map<String, ServerInfo> servers = ProxyServer.getInstance().getServers(); | |
List<TextComponent> context = Lists.newLinkedList(); | |
for (Map.Entry<String, ServerInfo> entry : servers.entrySet()) { | |
ServerInfo server = entry.getValue(); | |
server.ping((serverPing, throwable) -> { | |
TextComponent component; | |
if (throwable != null) { | |
component = new TextComponent(SERVER_LIST_ENTRY.replace("{server}", entry.getKey()).replace("{color}", "&a")); | |
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/server " + entry.getKey())); | |
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(SERVER_INFO_TEMPLATE.replace("{totalplayers}", String.valueOf(server.getPlayers().size())))})); | |
} else { | |
component = new TextComponent(SERVER_LIST_ENTRY.replace("{server}", entry.getKey()).replace("{color}", "&c")); | |
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(SERVER_NOT_ONLINE)})); | |
} | |
context.add(component); | |
}); | |
LinkedListMultimap<Integer, TextComponent> pages = Format.paginate(context, new TextComponent(LIST_HEADER), new TextComponent(LIST_FOOTER), 10); | |
if (arguments.isEmpty()) { | |
sender.sendMessage(pages.get(1).toArray(new TextComponent[pages.get(1).size()])); | |
} else { | |
if (NumberUtil.parseable(arguments.get(0), Integer.class)) { | |
int pageNumber = NumberUtil.parse(arguments.get(0), Integer.class); | |
sender.sendMessage(pages.get(pageNumber).toArray(new TextComponent[pages.get(pageNumber).size()])); | |
} else { | |
return new CommandResponse(sender, arguments, CommandResponse.Type.FAILURE).with("incorrectArgumentField", "pageNumber"); | |
} | |
} | |
} | |
return new CommandResponse(sender, arguments, CommandResponse.Type.SUCCESS); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment