From 4bd478de45aedf21bcdea2730682a0251d8984de Mon Sep 17 00:00:00 2001 From: DefinitlyEvil Date: Tue, 26 Dec 2017 21:17:59 +0800 Subject: [PATCH] modal form things --- .../proxy/commands/defaults/TestCommand.java | 23 ++++++++++- .../proxy/gui/BaseModalFormComponent.java | 28 +++++++++++++ .../proxy/gui/CustomFormComponent.java | 39 +++++++++++++++++++ .../dragonet/proxy/gui/DropDownComponent.java | 27 +++++++++++++ .../dragonet/proxy/gui/LabelComponent.java | 25 ++++++++++++ .../proxy/gui/ModalFormComponent.java | 16 ++++++++ .../org/dragonet/proxy/protocol/Protocol.java | 3 ++ .../packets/ModalFormRequestPacket.java | 30 ++++++++++++++ .../packets/ModalFormResponsePacket.java | 30 ++++++++++++++ 9 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/dragonet/proxy/gui/BaseModalFormComponent.java create mode 100644 src/main/java/org/dragonet/proxy/gui/CustomFormComponent.java create mode 100644 src/main/java/org/dragonet/proxy/gui/DropDownComponent.java create mode 100644 src/main/java/org/dragonet/proxy/gui/LabelComponent.java create mode 100644 src/main/java/org/dragonet/proxy/gui/ModalFormComponent.java create mode 100644 src/main/java/org/dragonet/proxy/protocol/packets/ModalFormRequestPacket.java create mode 100644 src/main/java/org/dragonet/proxy/protocol/packets/ModalFormResponsePacket.java diff --git a/src/main/java/org/dragonet/proxy/commands/defaults/TestCommand.java b/src/main/java/org/dragonet/proxy/commands/defaults/TestCommand.java index 604c7eaf0..d03c3b6e8 100644 --- a/src/main/java/org/dragonet/proxy/commands/defaults/TestCommand.java +++ b/src/main/java/org/dragonet/proxy/commands/defaults/TestCommand.java @@ -2,6 +2,9 @@ import org.dragonet.proxy.DragonProxy; import org.dragonet.proxy.commands.Command; +import org.dragonet.proxy.gui.CustomFormComponent; +import org.dragonet.proxy.gui.DropDownComponent; +import org.dragonet.proxy.gui.LabelComponent; import org.dragonet.proxy.network.UpstreamSession; import org.dragonet.proxy.protocol.packets.*; import org.dragonet.proxy.protocol.type.chunk.ChunkData; @@ -21,6 +24,10 @@ public TestCommand(String name) { } public void execute(DragonProxy proxy, String[] args) { + if(args.length == 0) { + System.out.println("This is a developer's command! "); + return; + } UpstreamSession player = proxy.getSessionRegister().getAll().values().toArray(new UpstreamSession[1])[0]; if (args[0].equalsIgnoreCase("status")) { PlayStatusPacket s = new PlayStatusPacket(); @@ -84,6 +91,20 @@ public void execute(DragonProxy proxy, String[] args) { data.encode(); chunk.payload = data.getBuffer(); player.sendPacket(chunk); - } + } else if (args[0].equalsIgnoreCase("form")) { + testForm(player); + } } + + public static void testForm(UpstreamSession player) { + ModalFormRequestPacket p = new ModalFormRequestPacket(); + CustomFormComponent form = new CustomFormComponent("\u00a7dTest Form"); + form.addComponent(new LabelComponent("\u00a71Text \u00a7ki")); + form.addComponent(new LabelComponent("LABEL 2")); + form.addComponent(new DropDownComponent("DROP DOWN", Arrays.asList("option 1", "option 2"))); + System.out.println(form.serializeToJson().toString()); + p.formId = 1; + p.formData = form.serializeToJson().toString(); + player.sendPacket(p); + } } diff --git a/src/main/java/org/dragonet/proxy/gui/BaseModalFormComponent.java b/src/main/java/org/dragonet/proxy/gui/BaseModalFormComponent.java new file mode 100644 index 000000000..834322c8a --- /dev/null +++ b/src/main/java/org/dragonet/proxy/gui/BaseModalFormComponent.java @@ -0,0 +1,28 @@ +package org.dragonet.proxy.gui; + +import org.json.JSONObject; + +/** + * Created on 2017/12/26. + */ +public abstract class BaseModalFormComponent implements ModalFormComponent { + + private final String type; + + public BaseModalFormComponent(String type) { + this.type = type; + } + + @Override + public String getType() { + return type; + } + + @Override + public final JSONObject serializeToJson() { + JSONObject obj = new JSONObject(); + obj.put("type", type); + serializeData(obj); + return obj; + } +} diff --git a/src/main/java/org/dragonet/proxy/gui/CustomFormComponent.java b/src/main/java/org/dragonet/proxy/gui/CustomFormComponent.java new file mode 100644 index 000000000..d08227f61 --- /dev/null +++ b/src/main/java/org/dragonet/proxy/gui/CustomFormComponent.java @@ -0,0 +1,39 @@ +package org.dragonet.proxy.gui; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created on 2017/12/26. + */ +public class CustomFormComponent extends BaseModalFormComponent { + + private String title; + + private final List components = new ArrayList<>(); + + public CustomFormComponent(String title) { + super("custom_form"); + this.title = title; + } + + public CustomFormComponent addComponent(ModalFormComponent component){ + components.add(component); + return this; + } + + public List getComponents() { + return components; + } + + @Override + public void serializeData(JSONObject out) { + out.put("title", title); + JSONArray content = new JSONArray(); + components.forEach((c) -> content.put(c.serializeToJson())); + out.put("content", content); + } +} diff --git a/src/main/java/org/dragonet/proxy/gui/DropDownComponent.java b/src/main/java/org/dragonet/proxy/gui/DropDownComponent.java new file mode 100644 index 000000000..c75b8aed4 --- /dev/null +++ b/src/main/java/org/dragonet/proxy/gui/DropDownComponent.java @@ -0,0 +1,27 @@ +package org.dragonet.proxy.gui; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.List; + +/** + * Created on 2017/12/26. + */ +public class DropDownComponent extends BaseModalFormComponent { + + private String text; + private List options; + + public DropDownComponent(String text, List options) { + super("dropdown"); + this.text = text; + this.options = options; + } + + @Override + public void serializeData(JSONObject out) { + out.put("text", text); + out.put("options", new JSONArray(options)); + } +} diff --git a/src/main/java/org/dragonet/proxy/gui/LabelComponent.java b/src/main/java/org/dragonet/proxy/gui/LabelComponent.java new file mode 100644 index 000000000..a038ddf73 --- /dev/null +++ b/src/main/java/org/dragonet/proxy/gui/LabelComponent.java @@ -0,0 +1,25 @@ +package org.dragonet.proxy.gui; + +import org.json.JSONObject; + +/** + * Created on 2017/12/26. + */ +public class LabelComponent extends BaseModalFormComponent { + + private String text; + + public LabelComponent(String text) { + super("label"); + this.text = text; + } + + public String getText() { + return text; + } + + @Override + public void serializeData(JSONObject out) { + out.put("text", text); + } +} diff --git a/src/main/java/org/dragonet/proxy/gui/ModalFormComponent.java b/src/main/java/org/dragonet/proxy/gui/ModalFormComponent.java new file mode 100644 index 000000000..c224d3a71 --- /dev/null +++ b/src/main/java/org/dragonet/proxy/gui/ModalFormComponent.java @@ -0,0 +1,16 @@ +package org.dragonet.proxy.gui; + +import org.json.JSONObject; + +/** + * Created on 2017/12/26. + */ +public interface ModalFormComponent { + + String getType(); + + void serializeData(JSONObject out); + + JSONObject serializeToJson(); + +} diff --git a/src/main/java/org/dragonet/proxy/protocol/Protocol.java b/src/main/java/org/dragonet/proxy/protocol/Protocol.java index 33fba21d0..7d277b75c 100644 --- a/src/main/java/org/dragonet/proxy/protocol/Protocol.java +++ b/src/main/java/org/dragonet/proxy/protocol/Protocol.java @@ -73,6 +73,9 @@ public final class Protocol { packets.put(SET_ENTITY_LINK_PACKET, SetEntityLinkPacket.class); packets.put(PLAYER_INPUT_PACKET, PlayerInputPacket.class); + packets.put(MODAL_FORM_REQUEST_PACKET, ModalFormRequestPacket.class); + packets.put(MODAL_FORM_RESPONSE_PACKET, ModalFormResponsePacket.class); + packets.put(CONTAINER_OPEN_PACKET, ContainerOpenPacket.class); packets.put(CONTAINER_CLOSE_PACKET, ContainerClosePacket.class); packets.put(INVENTORY_CONTENT_PACKET, InventoryContentPacket.class); diff --git a/src/main/java/org/dragonet/proxy/protocol/packets/ModalFormRequestPacket.java b/src/main/java/org/dragonet/proxy/protocol/packets/ModalFormRequestPacket.java new file mode 100644 index 000000000..47f4c3b1c --- /dev/null +++ b/src/main/java/org/dragonet/proxy/protocol/packets/ModalFormRequestPacket.java @@ -0,0 +1,30 @@ +package org.dragonet.proxy.protocol.packets; + +import org.dragonet.proxy.protocol.PEPacket; +import org.dragonet.proxy.protocol.ProtocolInfo; + +/** + * Created on 2017/12/26. + */ +public class ModalFormRequestPacket extends PEPacket { + + public int formId; + public String formData; + + @Override + public int pid() { + return ProtocolInfo.MODAL_FORM_REQUEST_PACKET; + } + + @Override + public void encodePayload() { + putUnsignedVarInt(formId); + putString(formData); + } + + @Override + public void decodePayload() { + formId = (int) (getUnsignedVarInt() & 0xFFFFFFFF); + formData = getString(); + } +} diff --git a/src/main/java/org/dragonet/proxy/protocol/packets/ModalFormResponsePacket.java b/src/main/java/org/dragonet/proxy/protocol/packets/ModalFormResponsePacket.java new file mode 100644 index 000000000..3f442ea6d --- /dev/null +++ b/src/main/java/org/dragonet/proxy/protocol/packets/ModalFormResponsePacket.java @@ -0,0 +1,30 @@ +package org.dragonet.proxy.protocol.packets; + +import org.dragonet.proxy.protocol.PEPacket; +import org.dragonet.proxy.protocol.ProtocolInfo; + +/** + * Created on 2017/12/26. + */ +public class ModalFormResponsePacket extends PEPacket { + + private int formId; + private String formData; + + @Override + public int pid() { + return ProtocolInfo.MODAL_FORM_RESPONSE_PACKET; + } + + @Override + public void encodePayload() { + putUnsignedVarInt(formId); + putString(formData); + } + + @Override + public void decodePayload() { + formId = (int) (getUnsignedVarInt() & 0xFFFFFFFF); + formData = getString(); + } +}