Skip to content

Commit

Permalink
Implement script packet for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Oct 4, 2016
1 parent 3be789b commit 0949bc0
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import com.denizenscript.depenizen.bukkit.objects.bungee.dServer;
import com.denizenscript.depenizen.bukkit.support.bungee.BungeeSupport;
import com.denizenscript.depenizen.common.socket.client.packet.ClientPacketOutScript;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.BracedCommand;
import net.aufdemrand.denizencore.utilities.debugging.dB;

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

public class BungeeCommand extends BracedCommand {

Expand All @@ -37,87 +39,69 @@ public class BungeeCommand extends BracedCommand {
// @Usage
// Use to send network-wide join messages.
// - define name <player.name.display>
// - bungee <bungee.list_servers> {
// - bungee <bungee.list_servers>:
// - announce "<yellow>%name% has joined the network."
// }

// @Usage
// Use to keep a player's inventory consistent across 2 creative servers.
// - define player <player>
// - define contents <player.inventory.list_contents>
// - bungee creative2 {
// - bungee creative2:
// - if <def[player].has_played_before||false> {
// - inventory set d:<def[player].inventory> o:<def[contents]>
// }
// }

// -->

public BungeeCommand() {
setBraced();
}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("servers")
&& !scriptEntry.hasObject("all-servers")
&& arg.matchesArgumentList(dServer.class)) {
scriptEntry.addObject("servers", arg.asType(dList.class));
}

else if (!scriptEntry.hasObject("servers")
&& !scriptEntry.hasObject("all-servers")
&& arg.matches("all")) {
scriptEntry.addObject("all-servers", new Element(true));
}

else if (arg.matches("{")) {
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
break;
}

}

if (!scriptEntry.hasObject("servers") && !scriptEntry.hasObject("all-servers")) {
if (!scriptEntry.hasObject("servers")) {
throw new InvalidArgumentsException("Must specify valid server(s)!");
}

if (!scriptEntry.hasObject("braces")) {
throw new InvalidArgumentsException("Must have braces!");
}

scriptEntry.defaultObject("all-servers", new Element(false));
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

Element all = scriptEntry.getElement("all-servers");
// TODO: deprecate

dList servers = scriptEntry.getdObject("servers");
List<ScriptEntry> bracedCommands = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;

dList serverNames = new dList();
serverNames.setPrefix("servers");

if (all.asBoolean()) {
dB.echoError("Argument 'ALL' is deprecated and will be removed in the future. Please use <bungee.list_servers> instead!");
for (dServer server : dServer.getOnlineServers().values()) {
serverNames.add(server.getName());
}
}
else {
for (dServer server : servers.filter(dServer.class)) {
serverNames.add(server.getName());
}
for (dServer server : servers.filter(dServer.class)) {
serverNames.add(server.getName());
}

dB.report(scriptEntry, getName(), serverNames.debug());

if (BungeeSupport.isSocketConnected()) {
dB.echoError("Bungee command is currently not available.");
// boolean debug = scriptEntry.shouldDebug();
// Map<String, String> definitions = scriptEntry.getResidingQueue().getAllDefinitions();
// ClientPacketOutScript packet = new ClientPacketOutScript(serverNames, debug, bracedCommands, definitions);
// BungeeSupport.getSocketClient().send(packet);
boolean debug = scriptEntry.shouldDebug();
Map<String, List<String>> scriptEntries = new HashMap<String, List<String>>();
for (ScriptEntry entry : bracedCommands) {
scriptEntries.put(entry.getCommandName(), entry.getOriginalArguments());
}
Map<String, String> definitions = scriptEntry.getResidingQueue().getAllDefinitions();
ClientPacketOutScript packet = new ClientPacketOutScript(serverNames, debug, scriptEntries, definitions);
BungeeSupport.getSocketClient().trySend(packet);
}
else {
dB.echoError("Server is not connected to a BungeeCord Socket.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

import com.denizenscript.depenizen.bukkit.objects.bungee.dServer;
import com.denizenscript.depenizen.common.socket.client.SocketClient;
import net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizencore.utilities.debugging.dB;

import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BukkitSocketClient extends SocketClient {

public BukkitSocketClient(String ipAddress, int port, String name, char[] password) throws GeneralSecurityException {
super(ipAddress, port, name, password);
}

@Override
protected boolean isBungeeScriptCompatible() {
return true;
}

@Override
public void handleAcceptRegister(String registrationName, List<String> existingServers) {
dServer.addOnlineServer(registrationName);
Expand Down Expand Up @@ -41,4 +52,25 @@ public void handleUpdateServer(String serverName, boolean registered) {
dServer.removeOnlineServer(serverName);
}
}

@Override
protected void handleScript(boolean shouldDebug, Map<String, List<String>> scriptEntries, Map<String, String> definitions) {
List<ScriptEntry> scriptEntryList = new ArrayList<ScriptEntry>();
try {
for (Map.Entry<String, List<String>> entry : scriptEntries.entrySet()) {
List<String> value = entry.getValue();
ScriptEntry scriptEntry = new ScriptEntry(entry.getKey(), value.toArray(new String[value.size()]), null);
scriptEntry.fallbackDebug = shouldDebug;
scriptEntryList.add(scriptEntry);
}
}
catch (ScriptEntryCreationException e) {
dB.echoError(e);
return;
}
InstantQueue queue = new InstantQueue(ScriptQueue.getNextId("BUNGEE_CMD"));
queue.addEntries(scriptEntryList);
queue.getAllDefinitions().putAll(definitions);
queue.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,15 @@ public Map<String, String> readStringMap() {
}
return stringMap;
}

public Map<String, List<String>> readStringListMap() {
Map<String, List<String>> stringListMap = new HashMap<String, List<String>>();
int size = readInt();
for (int i = 0; i < size; i++) {
String key = readString();
List<String> value = readStringList();
stringListMap.put(key, value);
}
return stringListMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class DataSerializer {
Expand Down Expand Up @@ -106,6 +107,19 @@ public void writeStringMap(Map<String, String> stringMap) {
}
}

public void writeStringListMap(Map<String, List<String>> stringListMap) {
if (stringListMap == null) {
writeInt(0);
}
else {
writeInt(stringListMap.size());
for (Map.Entry<String, List<String>> entry : stringListMap.entrySet()) {
writeString(entry.getKey());
writeStringList(entry.getValue());
}
}
}

public byte[] toByteArray() {
return byteArrayOutput.toByteArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public enum ServerBound {
REGISTER(0),
PING(1),
PONG(2),
UPDATE_SERVER(3);
SCRIPT(3),
RUN_SCRIPT(4);

private final int id;

Expand Down Expand Up @@ -48,7 +49,9 @@ public enum ClientBound {
ACCEPT_REGISTER(0),
PING(1),
PONG(2),
UPDATE_SERVER(3);
UPDATE_SERVER(3),
SCRIPT(4),
RUN_SCRIPT(5);

private final int id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Map;

public abstract class SocketClient implements Runnable {

Expand Down Expand Up @@ -113,7 +114,7 @@ public void run() {
byte[] iv = new byte[input.readInt()];
input.read(iv);
this.encryption = new Encryption(password, ENCRYPTION_SALT, iv);
send(new ClientPacketOutRegister(registrationName));
send(new ClientPacketOutRegister(registrationName, isBungeeScriptCompatible()));
connectionLoop:
while (isConnected) {
long timePassed;
Expand Down Expand Up @@ -189,6 +190,11 @@ public void run() {
updateServer.deserialize(data);
handleUpdateServer(updateServer.getName(), updateServer.isRegistered());
break;
case SCRIPT:
ClientPacketInScript script = new ClientPacketInScript();
script.deserialize(data);
handleScript(script.shouldDebug(), script.getScriptEntries(), script.getDefinitions());
break;
}
}
listenThread = null;
Expand All @@ -211,7 +217,11 @@ public void run() {
}
}

public abstract void handleAcceptRegister(String registrationName, List<String> existingServers);
protected abstract boolean isBungeeScriptCompatible();

protected abstract void handleAcceptRegister(String registrationName, List<String> existingServers);

protected abstract void handleUpdateServer(String serverName, boolean registered);

public abstract void handleUpdateServer(String serverName, boolean registered);
protected abstract void handleScript(boolean shouldDebug, Map<String, List<String>> scriptEntries, Map<String, String> definitions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.denizenscript.depenizen.common.socket.client.packet;

import com.denizenscript.depenizen.common.socket.DataDeserializer;
import com.denizenscript.depenizen.common.socket.Packet;

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

public class ClientPacketInScript extends Packet {

private boolean shouldDebug;
private Map<String, List<String>> scriptEntries;
private Map<String, String> definitions;

public boolean shouldDebug() {
return shouldDebug;
}

public Map<String, List<String>> getScriptEntries() {
return scriptEntries;
}

public Map<String, String> getDefinitions() {
return definitions;
}

@Override
public void deserialize(DataDeserializer deserializer) {
DataDeserializer box = new DataDeserializer(deserializer.readByteArray());
shouldDebug = box.readBoolean();
scriptEntries = box.readStringListMap();
definitions = box.readStringMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
public class ClientPacketOutRegister extends Packet {

private String name;
private boolean bungeeScriptCompatible;

public ClientPacketOutRegister(String name) {
public ClientPacketOutRegister(String name, boolean bungeeScriptCompatible) {
this.name = name;
this.bungeeScriptCompatible = bungeeScriptCompatible;
}

@Override
public void serialize(DataSerializer serializer) {
serializer.writeUnsignedByte(ServerBound.REGISTER.getId());
serializer.writeString(name);
serializer.writeBoolean(bungeeScriptCompatible);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.denizenscript.depenizen.common.socket.client.packet;

import com.denizenscript.depenizen.common.socket.DataSerializer;
import com.denizenscript.depenizen.common.socket.Packet;

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

public class ClientPacketOutScript extends Packet {

private List<String> destinations;
private boolean shouldDebug;
private Map<String, List<String>> scriptEntries;
private Map<String, String> definitions;

public ClientPacketOutScript(List<String> destinations, boolean shouldDebug, Map<String, List<String>> scriptEntries,
Map<String, String> definitions) {
this.destinations = destinations;
this.shouldDebug = shouldDebug;
this.scriptEntries = scriptEntries;
this.definitions = definitions;
}

@Override
public void serialize(DataSerializer serializer) {
serializer.writeUnsignedByte(ServerBound.SCRIPT.getId());
serializer.writeStringList(destinations);
DataSerializer box = new DataSerializer();
box.writeBoolean(shouldDebug);
box.writeStringListMap(scriptEntries);
box.writeStringMap(definitions);
serializer.writeByteArray(box.toByteArray());
}
}
Loading

0 comments on commit 0949bc0

Please sign in to comment.