Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
modal form things
Browse files Browse the repository at this point in the history
  • Loading branch information
DefinitlyEvil committed Dec 26, 2017
1 parent b517d6f commit 4bd478d
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/dragonet/proxy/gui/BaseModalFormComponent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions src/main/java/org/dragonet/proxy/gui/CustomFormComponent.java
Original file line number Diff line number Diff line change
@@ -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<ModalFormComponent> 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<ModalFormComponent> 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);
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/dragonet/proxy/gui/DropDownComponent.java
Original file line number Diff line number Diff line change
@@ -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<String> options;

public DropDownComponent(String text, List<String> 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));
}
}
25 changes: 25 additions & 0 deletions src/main/java/org/dragonet/proxy/gui/LabelComponent.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/dragonet/proxy/gui/ModalFormComponent.java
Original file line number Diff line number Diff line change
@@ -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();

}
3 changes: 3 additions & 0 deletions src/main/java/org/dragonet/proxy/protocol/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 4bd478d

Please sign in to comment.