3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 08:10:09 +01:00

Fix JSON for title packet, #24. Also implement method for generic JSON fixing woo!

Dieser Commit ist enthalten in:
Myles 2016-03-01 19:20:54 +00:00
Ursprung 6b3fb6424c
Commit 8ceee31299

Datei anzeigen

@ -2,23 +2,20 @@ package us.myles.ViaVersion.transformers;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.spacehq.mc.protocol.data.game.chunk.Column; import org.spacehq.mc.protocol.data.game.chunk.Column;
import org.spacehq.mc.protocol.util.NetUtil; import org.spacehq.mc.protocol.util.NetUtil;
import us.myles.ViaVersion.*; import us.myles.ViaVersion.*;
import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.handlers.ViaVersionInitializer;
import us.myles.ViaVersion.metadata.MetaIndex; import us.myles.ViaVersion.metadata.MetaIndex;
import us.myles.ViaVersion.metadata.NewType; import us.myles.ViaVersion.metadata.NewType;
import us.myles.ViaVersion.sounds.SoundEffect;
import us.myles.ViaVersion.metadata.Type; import us.myles.ViaVersion.metadata.Type;
import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.PacketType;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.sounds.SoundEffect;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -64,8 +61,7 @@ public class OutgoingTransformer {
SoundEffect effect = SoundEffect.getByName(name); SoundEffect effect = SoundEffect.getByName(name);
int catid = 0; int catid = 0;
String newname = name; String newname = name;
if(effect != null) if (effect != null) {
{
catid = effect.getCategory().getId(); catid = effect.getCategory().getId();
newname = effect.getNewName(); newname = effect.getNewName();
} }
@ -77,22 +73,30 @@ public class OutgoingTransformer {
int passenger = input.readInt(); int passenger = input.readInt();
int vehicle = input.readInt(); int vehicle = input.readInt();
boolean lead = input.readBoolean(); boolean lead = input.readBoolean();
if (!lead){ if (!lead) {
output.clear(); output.clear();
writeVarInt(PacketType.PLAY_SET_PASSENGERS.getNewPacketID(),output); writeVarInt(PacketType.PLAY_SET_PASSENGERS.getNewPacketID(), output);
writeVarInt(vehicle,output); writeVarInt(vehicle, output);
writeVarIntArray(Collections.singletonList(passenger),output); writeVarIntArray(Collections.singletonList(passenger), output);
return; return;
} }
output.writeInt(passenger); output.writeInt(passenger);
output.writeInt(vehicle); output.writeInt(vehicle);
return; return;
} }
if (packet == PacketType.PLAY_DISCONNECT){ if (packet == PacketType.PLAY_DISCONNECT) {
String reason = readString(input); String reason = readString(input);
if (reason.startsWith("\"")) writeString(fixJson(reason), output);
reason = "{\"text\":" + reason + "}"; return;
writeString(reason,output); }
if (packet == PacketType.PLAY_TITLE) {
int action = PacketUtil.readVarInt(input);
PacketUtil.writeVarInt(action, output);
if (action == 0 || action == 1) {
String text = PacketUtil.readString(input);
PacketUtil.writeString(fixJson(text), output);
}
output.writeBytes(input);
return; return;
} }
if (packet == PacketType.PLAY_ENTITY_TELEPORT) { if (packet == PacketType.PLAY_ENTITY_TELEPORT) {
@ -263,17 +267,14 @@ public class OutgoingTransformer {
return; return;
} }
if (packet == PacketType.PLAY_OPEN_WINDOW){ if (packet == PacketType.PLAY_OPEN_WINDOW) {
int windowId = input.readUnsignedByte(); int windowId = input.readUnsignedByte();
String type = readString(input); String type = readString(input);
String windowTitle = readString(input); String windowTitle = readString(input);
if (windowTitle.startsWith("\""))
windowTitle = "{\"text\":" + windowTitle + "}";;
output.writeByte(windowId); output.writeByte(windowId);
writeString(type,output); writeString(type, output);
writeString(windowTitle,output); writeString(fixJson(windowTitle), output);
output.writeBytes(input); output.writeBytes(input);
return; return;
} }
@ -320,15 +321,7 @@ public class OutgoingTransformer {
output.writeLong(location); output.writeLong(location);
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
String line = PacketUtil.readString(input); String line = PacketUtil.readString(input);
if (line == null || line.equalsIgnoreCase("null")) { PacketUtil.writeString(fixJson(line), output);
line = "{\"text\":\"\"}";
} else {
if (!line.startsWith("\"") && !line.startsWith("{"))
line = "\"" + line + "\"";
if (line.startsWith("\""))
line = "{\"text\":" + line + "}";
}
PacketUtil.writeString(line, output);
} }
} }
if (packet == PacketType.PLAY_SPAWN_PLAYER) { if (packet == PacketType.PLAY_SPAWN_PLAYER) {
@ -362,7 +355,7 @@ public class OutgoingTransformer {
return; return;
} }
if(packet == PacketType.PLAY_MAP) { if (packet == PacketType.PLAY_MAP) {
int damage = PacketUtil.readVarInt(input); int damage = PacketUtil.readVarInt(input);
PacketUtil.writeVarInt(damage, output); PacketUtil.writeVarInt(damage, output);
byte scale = input.readByte(); byte scale = input.readByte();
@ -440,6 +433,18 @@ public class OutgoingTransformer {
output.writeBytes(input); output.writeBytes(input);
} }
private String fixJson(String line) {
if (line == null || line.equalsIgnoreCase("null")) {
line = "{\"text\":\"\"}";
} else {
if (!line.startsWith("\"") && !line.startsWith("{"))
line = "\"" + line + "\"";
if (line.startsWith("\""))
line = "{\"text\":" + line + "}";
}
return line;
}
private void transformMetadata(Object dw, ByteBuf output) { private void transformMetadata(Object dw, ByteBuf output) {
// get entity // get entity
try { try {