From 91ae5990b1d424d68804e87a8622c21b260080af Mon Sep 17 00:00:00 2001 From: Myles Date: Mon, 14 Mar 2016 12:48:05 +0000 Subject: [PATCH] Almost forgot response handler woops, also allow PacketWrapper to be modifier :) --- .../myles/ViaVersion2/api/PacketWrapper.java | 14 ++++++++++ .../api/protocol/base/BaseProtocol.java | 28 ++++++++++++++++++- .../us/myles/ViaVersion2/api/util/Pair.java | 6 ++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java b/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java index 835562fc8..cd8178e8e 100644 --- a/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java +++ b/src/main/java/us/myles/ViaVersion2/api/PacketWrapper.java @@ -31,6 +31,20 @@ public class PacketWrapper { throw new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index); } + public void set(Type type, int index, T value) { + int currentIndex = 0; + for (Pair packetValue : packetValues) { + if (packetValue.getKey() == type) { // Ref check + if (currentIndex == index) { + packetValue.setValue(value); + return; + } + currentIndex++; + } + } + throw new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index); + } + public T read(Type type) { System.out.println("Reading: " + type.getTypeName()); // We could in the future log input read values, but honestly for things like bulk maps, mem waste D: diff --git a/src/main/java/us/myles/ViaVersion2/api/protocol/base/BaseProtocol.java b/src/main/java/us/myles/ViaVersion2/api/protocol/base/BaseProtocol.java index 61ceae54e..8697450b4 100644 --- a/src/main/java/us/myles/ViaVersion2/api/protocol/base/BaseProtocol.java +++ b/src/main/java/us/myles/ViaVersion2/api/protocol/base/BaseProtocol.java @@ -1,6 +1,10 @@ package us.myles.ViaVersion2.api.protocol.base; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.util.PacketUtil; import us.myles.ViaVersion2.api.PacketWrapper; import us.myles.ViaVersion2.api.data.UserConnection; import us.myles.ViaVersion2.api.protocol.Protocol; @@ -15,7 +19,29 @@ public class BaseProtocol extends Protocol { @Override protected void registerPackets() { /* Outgoing Packets */ - registerOutgoing(State.STATUS, 0x00, 0x00); // Status Response Packet + registerOutgoing(State.STATUS, 0x00, 0x00, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.STRING); + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) { + // TODO: Actually make this show compatible versions + ProtocolInfo info = wrapper.user().get(ProtocolInfo.class); + String originalStatus = wrapper.get(Type.STRING, 0); + try { + JSONObject json = (JSONObject) new JSONParser().parse(originalStatus); + JSONObject version = (JSONObject) json.get("version"); + version.put("protocol", info.getProtocolVersion()); + + wrapper.set(Type.STRING, 0, json.toJSONString()); // Update value + } catch (ParseException e) { + e.printStackTrace(); + } + } + }); + } + }); // Status Response Packet registerOutgoing(State.STATUS, 0x01, 0x01); // Status Pong Packet registerOutgoing(State.LOGIN, 0x00, 0x00); // Login Disconnect Packet diff --git a/src/main/java/us/myles/ViaVersion2/api/util/Pair.java b/src/main/java/us/myles/ViaVersion2/api/util/Pair.java index ed6b35e0b..163ff4f5f 100644 --- a/src/main/java/us/myles/ViaVersion2/api/util/Pair.java +++ b/src/main/java/us/myles/ViaVersion2/api/util/Pair.java @@ -2,12 +2,14 @@ package us.myles.ViaVersion2.api.util; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; @Getter +@Setter @EqualsAndHashCode public class Pair { - private final X key; - private final Y value; + private X key; + private Y value; public Pair(X key, Y value){ this.key = key;