diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/Config.java b/src/main/java/co/virtualdragon/vanillaVotifier/Config.java index 38b768a..c7be134 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/Config.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/Config.java @@ -68,17 +68,17 @@ public static interface RconConfig { List getCommands(); } - public static class AbstractRconConfig implements RconConfig { + public static class VanillaVotifierRconConfig implements RconConfig { private InetSocketAddress inetSocketAddress; private String password; private ArrayList commands; - public AbstractRconConfig(InetSocketAddress inetSocketAddress, String password) { + public VanillaVotifierRconConfig(InetSocketAddress inetSocketAddress, String password) { this(inetSocketAddress, password, new ArrayList()); } - public AbstractRconConfig(InetSocketAddress inetSocketAddress, String password, ArrayList commands) { + public VanillaVotifierRconConfig(InetSocketAddress inetSocketAddress, String password, ArrayList commands) { this.inetSocketAddress = inetSocketAddress; this.password = password; if (commands == null) { diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/Rcon.java b/src/main/java/co/virtualdragon/vanillaVotifier/Rcon.java index 3e67ad3..379cd47 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/Rcon.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/Rcon.java @@ -21,9 +21,9 @@ import java.io.UnsupportedEncodingException; public interface Rcon { - + RconConfig getRconConfig(); - + void setRconConfig(RconConfig rconConfig); int getRequestId(); @@ -32,56 +32,110 @@ public interface Rcon { boolean isConnected(); - Packet logIn() throws UnsupportedEncodingException, IOException; + VanillaVotifierPacket logIn() throws UnsupportedEncodingException, IOException; + + VanillaVotifierPacket sendRequest(VanillaVotifierPacket request) throws UnsupportedEncodingException, IOException; + + public static interface Packet { + + int getLength(); + + void setLength(int length); + + int getRequestId(); + + void setRequestId(int requestId); + + Type getType(); + + void setType(Type type); - Packet sendRequest(Packet request) throws UnsupportedEncodingException, IOException; + String getPayload(); + + void setPayload(String payload); + + public static enum Type { + + COMMAND_RESPONSE(0), COMMAND(2), LOG_IN(3); + + private int i; + + private Type(int i) { + this.i = i; + } + + public int toInt() { + return i; + } + + public static Type fromInt(int i) { + if (i == 0) { + return COMMAND_RESPONSE; + } else if (i == 2) { + return COMMAND; + } else if (i == 3) { + return LOG_IN; + } else { + throw new IllegalArgumentException("i has to be equal to 0, 2, or 3!"); + } + } + } + } - public static class Packet { + public static class VanillaVotifierPacket implements Packet { private int length; private int requestId; private Type type; private String payload; - public Packet(int requestId, Type type, String payload) { + public VanillaVotifierPacket(int requestId, Type type, String payload) { this(Integer.SIZE / 8 + Integer.SIZE / 8 + payload.length() + Byte.SIZE / 8 * 2, requestId, type, payload); } - public Packet(int length, int requestId, Type type, String payload) { + public VanillaVotifierPacket(int length, int requestId, Type type, String payload) { this.length = length; this.requestId = requestId; this.type = type; this.payload = payload; } + @Override public int getLength() { return length; } + @Override public void setLength(int length) { this.length = length; } + @Override public int getRequestId() { return requestId; } + @Override public void setRequestId(int requestId) { this.requestId = requestId; } + @Override public Type getType() { return type; } + @Override public void setType(Type type) { this.type = type; } + @Override public String getPayload() { return payload; } + @Override public void setPayload(String payload) { this.payload = payload; } @@ -90,32 +144,5 @@ public void setPayload(String payload) { public String toString() { return length + "\t" + requestId + "\t" + type.toInt() + "\t" + payload; } - - public static enum Type { - - COMMAND_RESPONSE(0), COMMAND(2), LOG_IN(3); - - private int i; - - private Type(int i) { - this.i = i; - } - - public int toInt() { - return i; - } - - public static Type fromInt(int i) { - if (i == 0) { - return COMMAND_RESPONSE; - } else if (i == 2) { - return COMMAND; - } else if (i == 3) { - return LOG_IN; - } else { - throw new IllegalArgumentException("i has to be equal to 0, 2, or 3!"); - } - } - } } } diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java index a225dea..eec78fb 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/JsonConfig.java @@ -32,7 +32,6 @@ import java.net.InetSocketAddress; import java.security.KeyPair; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import org.bouncycastle.util.io.pem.PemObject; import org.bouncycastle.util.io.pem.PemReader; @@ -122,7 +121,7 @@ public void load() throws Exception { rconConfigs = new ArrayList(); for (int i = 0; i < config.getJSONArray("rcon-list").length(); i++) { JSONObject jsonObject = config.getJSONArray("rcon-list").getJSONObject(i); - JsonRconConfig rconConfig = new JsonRconConfig(new InetSocketAddress(jsonObject.getString("ip"), jsonObject.getInt("port")), jsonObject.getString("password")); + VanillaVotifierRconConfig rconConfig = new VanillaVotifierRconConfig(new InetSocketAddress(jsonObject.getString("ip"), jsonObject.getInt("port")), jsonObject.getString("password")); for (int j = 0; j < jsonObject.getJSONArray("commands").length(); j++) { rconConfig.getCommands().add(jsonObject.getJSONArray("commands").getString(j)); } @@ -286,15 +285,4 @@ public void save() throws IOException { privatePemWriter.flush(); privatePemWriter.close(); } - - public static class JsonRconConfig extends AbstractRconConfig { - - public JsonRconConfig(InetSocketAddress inetSocketAddress, String password) { - super(inetSocketAddress, password); - } - - public JsonRconConfig(InetSocketAddress inetSocketAddress, String password, ArrayList commands) { - super(inetSocketAddress, password, commands); - } - } } diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconCommandSender.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconCommandSender.java index 256e4ac..b68ddd1 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconCommandSender.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconCommandSender.java @@ -18,7 +18,7 @@ import co.virtualdragon.vanillaVotifier.CommandSender; import co.virtualdragon.vanillaVotifier.Rcon; -import co.virtualdragon.vanillaVotifier.Rcon.Packet; +import co.virtualdragon.vanillaVotifier.Rcon.VanillaVotifierPacket; import co.virtualdragon.vanillaVotifier.Votifier; import java.net.SocketException; @@ -35,15 +35,15 @@ public String sendCommand(Rcon rcon, String command) throws Exception { if (!rcon.isConnected()) { rcon.connect(); } - Packet packet = null; + VanillaVotifierPacket packet = null; try { - packet = rcon.sendRequest(new Packet(rcon.getRequestId(), Packet.Type.LOG_IN, rcon.getRconConfig().getPassword())); + packet = rcon.sendRequest(new VanillaVotifierPacket(rcon.getRequestId(), VanillaVotifierPacket.Type.LOG_IN, rcon.getRconConfig().getPassword())); } catch (SocketException e) { rcon.connect(); - packet = rcon.sendRequest(new Packet(rcon.getRequestId(), Packet.Type.LOG_IN, rcon.getRconConfig().getPassword())); + packet = rcon.sendRequest(new VanillaVotifierPacket(rcon.getRequestId(), VanillaVotifierPacket.Type.LOG_IN, rcon.getRconConfig().getPassword())); } if (packet.getRequestId() != -1) { - return rcon.sendRequest(new Packet(rcon.getRequestId(), Packet.Type.COMMAND, command)).getPayload(); + return rcon.sendRequest(new VanillaVotifierPacket(rcon.getRequestId(), VanillaVotifierPacket.Type.COMMAND, command)).getPayload(); } else { throw new Exception("Invalid password."); } diff --git a/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java b/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java index 9d836c5..b040d2b 100644 --- a/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java +++ b/src/main/java/co/virtualdragon/vanillaVotifier/impl/RconConnection.java @@ -18,8 +18,8 @@ import co.virtualdragon.vanillaVotifier.Config.RconConfig; import co.virtualdragon.vanillaVotifier.Rcon; -import co.virtualdragon.vanillaVotifier.Rcon.Packet; import co.virtualdragon.vanillaVotifier.Rcon.Packet.Type; +import co.virtualdragon.vanillaVotifier.Rcon.VanillaVotifierPacket; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.Socket; @@ -74,12 +74,12 @@ public int getRequestId() { } @Override - public Packet logIn() throws UnsupportedEncodingException, IOException { - return sendRequest(new Packet(requestId, Type.LOG_IN, rconConfig.getPassword())); + public VanillaVotifierPacket logIn() throws UnsupportedEncodingException, IOException { + return sendRequest(new VanillaVotifierPacket(requestId, Type.LOG_IN, rconConfig.getPassword())); } @Override - public Packet sendRequest(Packet request) throws UnsupportedEncodingException, IOException { + public VanillaVotifierPacket sendRequest(VanillaVotifierPacket request) throws UnsupportedEncodingException, IOException { byte[] requestBytes = new byte[request.getLength() + Integer.SIZE / 8]; ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); requestBuffer.order(ByteOrder.LITTLE_ENDIAN); @@ -106,6 +106,6 @@ public Packet sendRequest(Packet request) throws UnsupportedEncodingException, I responseBuffer.get(responsePayload); responseBuffer.get(); responseBuffer.get(); - return new Packet(responseLength, responseRequestId, responseType, new String(responsePayload, "UTF-8")); + return new VanillaVotifierPacket(responseLength, responseRequestId, responseType, new String(responsePayload, "UTF-8")); } }