Almost forgot response handler woops, also allow PacketWrapper to be modifier :)

This commit is contained in:
Myles 2016-03-14 12:48:05 +00:00
parent 33f1a8975c
commit 91ae5990b1
3 changed files with 45 additions and 3 deletions

View File

@ -31,6 +31,20 @@ public class PacketWrapper {
throw new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index);
}
public <T> void set(Type<T> type, int index, T value) {
int currentIndex = 0;
for (Pair<Type, Object> 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> T read(Type<T> 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:

View File

@ -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

View File

@ -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<X, Y> {
private final X key;
private final Y value;
private X key;
private Y value;
public Pair(X key, Y value){
this.key = key;