player health, food and saturation, support for incoming plugin messages, fix for chat component

This commit is contained in:
bixilon 2020-06-04 22:02:57 +02:00
parent db4a970d23
commit 9b360bc500
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 41 additions and 16 deletions

View File

@ -1,13 +1,20 @@
package de.bixilon.minosoft.game.datatypes; package de.bixilon.minosoft.game.datatypes;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
public class ChatComponent { public class ChatComponent {
final JSONObject json; JSONObject json;
public ChatComponent(String raw) { public ChatComponent(String raw) {
this.json = new JSONObject(raw); try {
this.json = new JSONObject(raw);
} catch (JSONException e) {
// not a text component, is a legacy string
this.json = new JSONObject();
this.json.put("text", raw);
}
} }
public ChatComponent(JSONObject json) { public ChatComponent(JSONObject json) {
@ -16,10 +23,10 @@ public class ChatComponent {
//ToDo //ToDo
public String getRawMessage() { public String getRawMessage() {
if (json.getString("text").length() != 0) { if (json.has("text") && json.getString("text").length() != 0) {
return json.getString("text"); return json.getString("text");
} }
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
JSONArray arr = json.getJSONArray("extra"); JSONArray arr = json.getJSONArray("extra");
for (int i = 0; i < arr.length(); i++) { for (int i = 0; i < arr.length(); i++) {
buffer.append(arr.getJSONObject(i).getString("text")); buffer.append(arr.getJSONObject(i).getString("text"));

View File

@ -153,6 +153,10 @@ public class Network {
try { try {
ClientboundPacket packet = clazz.getConstructor().newInstance(); ClientboundPacket packet = clazz.getConstructor().newInstance();
packet.read(inPacketBuffer, connection.getVersion()); packet.read(inPacketBuffer, connection.getVersion());
if (inPacketBuffer.getBytesLeft() > 0) {
// warn not all data used
Log.protocol(String.format("[IN] Packet %s did not used all bytes sent", ((p != null) ? p.name() : "UNKNOWN")));
}
if (packet instanceof PacketLoginSuccess) { if (packet instanceof PacketLoginSuccess) {
// login was okay, setting play status to avoid miss timing issues // login was okay, setting play status to avoid miss timing issues

View File

@ -1,5 +1,6 @@
package de.bixilon.minosoft.protocol.packets.clientbound.login; package de.bixilon.minosoft.protocol.packets.clientbound.login;
import de.bixilon.minosoft.game.datatypes.ChatComponent;
import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket; import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer; import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
@ -7,17 +8,17 @@ import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketLoginDisconnect implements ClientboundPacket { public class PacketLoginDisconnect implements ClientboundPacket {
String reason; ChatComponent reason;
@Override @Override
public void read(InPacketBuffer buffer, ProtocolVersion v) { public void read(InPacketBuffer buffer, ProtocolVersion v) {
reason = buffer.readString(); reason = buffer.readChatComponent();
log(); log();
} }
@Override @Override
public void log() { public void log() {
Log.protocol(String.format("Receiving login disconnect packet (%s)", reason)); Log.protocol(String.format("Receiving login disconnect packet (%s)", reason.getRawMessage()));
} }
@Override @Override
@ -25,7 +26,7 @@ public class PacketLoginDisconnect implements ClientboundPacket {
h.handle(this); h.handle(this);
} }
public String getReason() { public ChatComponent getReason() {
return reason; return reason;
} }
} }

View File

@ -1,5 +1,6 @@
package de.bixilon.minosoft.protocol.packets.clientbound.play; package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.game.datatypes.ChatComponent;
import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket; import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer; import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
@ -7,14 +8,14 @@ import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketDisconnect implements ClientboundPacket { public class PacketDisconnect implements ClientboundPacket {
String reason; ChatComponent reason;
@Override @Override
public void read(InPacketBuffer buffer, ProtocolVersion v) { public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) { switch (v) {
case VERSION_1_7_10: case VERSION_1_7_10:
reason = buffer.readString(); reason = buffer.readChatComponent();
break; break;
} }
log(); log();
@ -22,10 +23,10 @@ public class PacketDisconnect implements ClientboundPacket {
@Override @Override
public void log() { public void log() {
Log.game(String.format("Disconnected: %s", reason)); Log.game(String.format("Disconnected: %s", reason.getRawMessage()));
} }
public String getReason() { public ChatComponent getReason() {
return reason; return reason;
} }

View File

@ -16,9 +16,9 @@ public class PacketUpdateHealth implements ClientboundPacket {
public void read(InPacketBuffer buffer, ProtocolVersion v) { public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) { switch (v) {
case VERSION_1_7_10: case VERSION_1_7_10:
health = buffer.readFloat(); health = (float) (Math.round(buffer.readFloat() * 10) / 10.0);
food = buffer.readShort(); food = buffer.readShort();
saturation = buffer.readFloat(); saturation = (float) (Math.round(buffer.readFloat() * 10) / 10.0);
break; break;
} }
log(); log();

View File

@ -143,6 +143,18 @@ public class InByteBuffer {
} }
public ChatComponent readChatComponent() { public ChatComponent readChatComponent() {
return new ChatComponent(readJson()); return new ChatComponent(readString());
}
public int getPos() {
return this.pos;
}
public int getLength() {
return bytes.length;
}
public int getBytesLeft() {
return bytes.length - pos;
} }
} }

View File

@ -40,7 +40,7 @@ public interface Protocol {
packetClassMapping.put(Packets.Clientbound.PLAY_KEEP_ALIVE, PacketKeepAlive.class); packetClassMapping.put(Packets.Clientbound.PLAY_KEEP_ALIVE, PacketKeepAlive.class);
packetClassMapping.put(Packets.Clientbound.PLAY_CHUNK_BULK, PacketChunkBulk.class); packetClassMapping.put(Packets.Clientbound.PLAY_CHUNK_BULK, PacketChunkBulk.class);
packetClassMapping.put(Packets.Clientbound.PLAY_UPDATE_HEALTH, PacketUpdateHealth.class); packetClassMapping.put(Packets.Clientbound.PLAY_UPDATE_HEALTH, PacketUpdateHealth.class);
packetClassMapping.put(Packets.Clientbound.PLAY_PLUGIN_MESSAGE, PacketUpdateHealth.class); packetClassMapping.put(Packets.Clientbound.PLAY_PLUGIN_MESSAGE, PacketPluginMessageReceived.class);
packetClassMapping.put(Packets.Clientbound.PLAY_SPAWN_POSITION, PacketSpawnLocation.class); packetClassMapping.put(Packets.Clientbound.PLAY_SPAWN_POSITION, PacketSpawnLocation.class);
packetClassMapping.put(Packets.Clientbound.PLAY_CHAT_MESSAGE, PacketChatMessage.class); packetClassMapping.put(Packets.Clientbound.PLAY_CHAT_MESSAGE, PacketChatMessage.class);
packetClassMapping.put(Packets.Clientbound.PLAY_DISCONNECT, PacketDisconnect.class); packetClassMapping.put(Packets.Clientbound.PLAY_DISCONNECT, PacketDisconnect.class);