Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from ModdedMinecraftClub/feature/command-override
Browse files Browse the repository at this point in the history
Feature/command override
  • Loading branch information
john01dav authored May 21, 2020
2 parents fa66574 + 33905b7 commit b7188a3
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public abstract class AbstractMessage{
public abstract class AbstractMessage {

protected abstract void send(DataOutputStream dataOutputStream) throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public final class BroadcastMessage extends AbstractMessage{
public final class BroadcastMessage extends AbstractMessage {
protected static final short MESSAGE_TYPE_ID = 0;
private final String prefix, message;
private final int prefix_color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public final class ChatMessage extends AbstractMessage{
public final class ChatMessage extends AbstractMessage {
protected static final short MESSAGE_TYPE_ID = 1;
private final String username, message, componentJson;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,66 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class CommandMessage extends AbstractMessage{
public class CommandMessage extends AbstractMessage {

protected static final short MESSAGE_TYPE_ID = 6;
private final String serverID;
private final String command;
private final String channel;
private final String serverID, name, defaultcmd, channel;
private final int listSize;
private final ArrayList<String> args;

public CommandMessage(String serverID, String command, String channel) {
public CommandMessage(String serverID, String name, String defaultcmd, ArrayList<String> args, String channel) {
this.serverID = serverID;
this.command = command;
this.name = name;
this.defaultcmd = defaultcmd;
this.args = args;
this.listSize = args.size();
this.channel = channel;
}

public CommandMessage(DataInputStream istream) throws IOException {
this.serverID = istream.readUTF();
this.command = istream.readUTF();
this.name = istream.readUTF();
this.defaultcmd = istream.readUTF();
this.listSize = istream.readInt();
this.args = new ArrayList<>();
for (int i = 0; i < listSize; i++) {
args.add(istream.readUTF());
}
this.channel = istream.readUTF();
}

public ArrayList<String> getArgs() {
return args;
}

public String getServerID() {
return serverID;
}

public String getCommand() {
return command;
return defaultcmd;
}

public String getChannel() {
return channel;
}

public String getName() {
return name;
}

@Override
protected void send(DataOutputStream dataOutputStream) throws IOException {
dataOutputStream.writeShort(MESSAGE_TYPE_ID);
dataOutputStream.writeUTF(serverID);
dataOutputStream.writeUTF(command);
dataOutputStream.writeUTF(name);
dataOutputStream.writeUTF(defaultcmd);
dataOutputStream.writeInt(this.args.size());
for (String arg : this.args) {
dataOutputStream.writeUTF(arg);
}
dataOutputStream.writeUTF(channel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,29 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class CommandOutputMessage extends AbstractMessage{
public class CommandOutputMessage extends AbstractMessage {

protected static final short MESSAGE_TYPE_ID = 7;
private final String command;
private final String commandOutput;
private final String channel;
private final String serverID;
private final String color;

public CommandOutputMessage(String serverID, String command, String commandOutput, String channel) {
public CommandOutputMessage(String serverID, String command, String commandOutput, String channel, String color) {
this.serverID = serverID;
this.command = command;
this.commandOutput = commandOutput;
this.channel = channel;
this.color = color;
}

public CommandOutputMessage(DataInputStream istream) throws IOException {
this.serverID = istream.readUTF();
this.command = istream.readUTF();
this.commandOutput = istream.readUTF();
this.channel = istream.readUTF();
this.color = istream.readUTF();
}

public String getServerID() {
Expand All @@ -62,13 +65,18 @@ public String getChannel() {
return channel;
}

public String getColor() {
return color;
}

@Override
protected void send(DataOutputStream dataOutputStream) throws IOException {
dataOutputStream.writeShort(MESSAGE_TYPE_ID);
dataOutputStream.writeUTF(serverID);
dataOutputStream.writeUTF(command);
dataOutputStream.writeUTF(commandOutput);
dataOutputStream.writeUTF(channel);
dataOutputStream.writeUTF(color);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import java.io.IOException;
import java.util.ArrayList;

public class PlayerListMessage extends AbstractMessage{
public class PlayerListMessage extends AbstractMessage {

protected static final short MESSAGE_TYPE_ID = 5;
private final String serverID;
private final int listSize;
private ArrayList<String> playerList;
private final ArrayList<String> playerList;

public PlayerListMessage(String serverID, ArrayList<String> playerList) {
this.serverID = serverID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class PlayerStatusMessage extends AbstractMessage{
public class PlayerStatusMessage extends AbstractMessage {
protected static final short MESSAGE_TYPE_ID = 3;
private final String userName, serverID, prefixJson;
private final boolean joined, silent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

//Sends data to main polychat server when the game launches
//Data is used when displaying info about all servers in discord
public class ServerInfoMessage extends AbstractMessage{
public class ServerInfoMessage extends AbstractMessage {
protected static final short MESSAGE_TYPE_ID = 4;
private final String serverID, serverName, serverAddress;
private final int maxPlayers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.io.IOException;

//Used for broadcasting server online/offline events
public class ServerStatusMessage extends AbstractMessage{
public class ServerStatusMessage extends AbstractMessage {
protected static final short MESSAGE_TYPE_ID = 2;
private final String serverID, prefixJson;
private final short state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public abstract class ThreadedQueue<T> {
public final void start() {
executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
try{
try {
init();
}catch(Throwable t){
} catch (Throwable t) {
t.printStackTrace();
}
});
Expand All @@ -40,9 +40,9 @@ public final void stop() {

public final synchronized void enqueue(final T obj) {
executorService.submit(() -> {
try{
try {
handle(obj);
}catch(Throwable t){
} catch (Throwable t) {
t.printStackTrace();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,30 @@
import discord4j.core.object.entity.*;

import java.awt.*;
import java.util.HashMap;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PrintMessageQueue extends ThreadedQueue<MessageData> {
private static final HashMap<String, Color> colorHashMap = new HashMap<String, Color>() {{
put("0", new Color(0x000000));
put("1", new Color(0x0000AA));
put("2", new Color(0x00AA00));
put("3", new Color(0x00AAAA));
put("4", new Color(0xAA0000));
put("5", new Color(0xAA00AA));
put("6", new Color(0xFFAA00));
put("7", new Color(0xAAAAAA));
put("8", new Color(0x555555));
put("9", new Color(0x5555FF));
put("a", new Color(0x55FF55));
put("b", new Color(0x55FFFF));
put("c", new Color(0xFF5555));
put("d", new Color(0xFF55FF));
put("e", new Color(0xFFFF55));
put("f", new Color(0xFFFFFF));
}};

@Override
protected void init() {
Expand Down Expand Up @@ -91,7 +110,7 @@ protected void handle(MessageData messageData) {
}
embedSpec.setDescription(message.getCommandOutput());
Random random = new Random(System.currentTimeMillis());
embedSpec.setColor(new Color(random.nextInt(0xFFFFFF)));
embedSpec.setColor(colorHashMap.get(message.getColor()));
}).block();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public String run(String[] args, String channel) {
String prefix = Main.config.getProperty("prefix", "!");
info.append("WARNING: This command is deprecated and will soon be removed in favor of ").append(prefix).append(oc).append(".\n");
}
info.append(String.format("**Total Players Online: %d**", Main.serverInfo.getTotalPlayers())).append("\n");
info.append(String.format("**Servers Online [%d]:**", Main.serverInfo.getServers().size())).append("\n");
for (OnlineServer server : Main.serverInfo.getServers()) {
info.append(String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@
import club.moddedminecraft.polychat.server.info.OnlineServer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MinecraftCommand extends RoleCommand {

private final String command;
private final String defaultcmd;
private final int argCount;
private String channel;

public MinecraftCommand(String name, Map<String, Object> args) {
super(name, args);
this.command = (String) args.get("command");
this.argCount = calculateParameters(command);
this.defaultcmd = (String) args.get("command");
this.argCount = calculateParameters(defaultcmd);
}

public int calculateParameters(String command) {
Expand All @@ -48,23 +49,13 @@ public int calculateParameters(String command) {
}

public String run(String[] inputArgs, String channel) {
ArrayList<OnlineServer> executeServers = new ArrayList<>();
String command = this.command;

if (inputArgs.length < 1) {
return "Error running command: Server prefix required";
}

if (inputArgs.length < (this.argCount - 1)) {
return "Expected at least " + this.argCount + " parameters, received " + (inputArgs.length - 1);
}

String serverID = inputArgs[0];
ArrayList<String> args = new ArrayList<>();
for (int i = 1; i < inputArgs.length; i++) {
args.add(inputArgs[i]);
}

ArrayList<OnlineServer> executeServers = new ArrayList<>();
if (serverID.equals("<all>")) {
executeServers.addAll(Main.serverInfo.getServers());
} else {
Expand All @@ -75,24 +66,12 @@ public String run(String[] inputArgs, String channel) {
executeServers.add(server);
}

// get the last instance of every unique $(number)
// ie. /ranks set $1 $2 $1 $3 returns $2 $1 $3
Pattern pattern = Pattern.compile("(\\$\\d+)(?!.*\\1)");
Matcher matcher = pattern.matcher(this.command);

while (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
String toBeReplaced = matcher.group(i);
String replaceWith;
int argNum = Integer.parseInt(toBeReplaced.substring(1));
replaceWith = args.get(argNum - 1);
command = command.replace(toBeReplaced, replaceWith);
}
}
command = command.replace("$args", String.join(" ", args));
// process arguments into ArrayList (exclude the first element which is the prefix)
ArrayList<String> args = new ArrayList<>(Arrays.asList(inputArgs).subList(1, inputArgs.length));

for (OnlineServer server : executeServers) {
server.getMessageBus().sendMessage(new CommandMessage(server.getServerID(), command, channel));
server.getMessageBus().sendMessage(new CommandMessage(server.getServerID(), this.getName(), defaultcmd, args, channel));
}

return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public int playerCount() {
}

public ArrayList<String> getOnlinePlayers() {
return this.onlinePlayers;
ArrayList<String> players = new ArrayList<>();
for (String player : this.onlinePlayers) {
players.add(player.replace("_", "\\_")); // escape underscore from italicizing on Discord
}
return players;
}

//The maximum players on this server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public ArrayList<OnlineServer> getServers() {
}

public OnlineServer getServer(String serverID) {
System.out.println(serverMap);
return serverMap.getOrDefault(serverID, null);
}

Expand Down Expand Up @@ -102,4 +101,12 @@ public void playerLeave(String serverID, String username) {
}
}

public int getTotalPlayers() {
int total = 0;
for (OnlineServer server : onlineServers) {
total += server.getOnlinePlayers().size();
}
return total;
}

}

0 comments on commit b7188a3

Please sign in to comment.