mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-12 00:47:26 -04:00
ChatComponent wip; chat, disconnect, spawn location packets, datatype location
This commit is contained in:
parent
b9e1d9ba5e
commit
db4a970d23
@ -0,0 +1,33 @@
|
||||
package de.bixilon.minosoft.game.datatypes;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class ChatComponent {
|
||||
final JSONObject json;
|
||||
|
||||
public ChatComponent(String raw) {
|
||||
this.json = new JSONObject(raw);
|
||||
}
|
||||
|
||||
public ChatComponent(JSONObject json) {
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
//ToDo
|
||||
public String getRawMessage() {
|
||||
if (json.getString("text").length() != 0) {
|
||||
return json.getString("text");
|
||||
}
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
JSONArray arr = json.getJSONArray("extra");
|
||||
for (int i = 0; i < arr.length(); i++) {
|
||||
buffer.append(arr.getJSONObject(i).getString("text"));
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public JSONObject getRaw() {
|
||||
return this.json;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package de.bixilon.minosoft.game.datatypes.player;
|
||||
|
||||
public class Location {
|
||||
private final double x;
|
||||
private final double y;
|
||||
private final double z;
|
||||
|
||||
public Location(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (super.equals(obj)) {
|
||||
return true;
|
||||
}
|
||||
Location that = (Location) obj;
|
||||
return that.getX() == getX() && that.getY() == getY() && that.getZ() == getZ();
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package de.bixilon.minosoft.objects;
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.player.Location;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class Player {
|
||||
@ -7,6 +9,7 @@ public class Player {
|
||||
float health;
|
||||
short food;
|
||||
float saturation;
|
||||
Location spawnLocation;
|
||||
|
||||
public Player(Account acc) {
|
||||
this.acc = acc;
|
||||
@ -44,4 +47,12 @@ public class Player {
|
||||
public void setSaturation(float saturation) {
|
||||
this.saturation = saturation;
|
||||
}
|
||||
|
||||
public Location getSpawnLocation() {
|
||||
return spawnLocation;
|
||||
}
|
||||
|
||||
public void setSpawnLocation(Location spawnLocation) {
|
||||
this.spawnLocation = spawnLocation;
|
||||
}
|
||||
}
|
||||
|
@ -5,20 +5,19 @@ import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class PacketLoginDisconnect implements ClientboundPacket {
|
||||
JSONObject reason;
|
||||
String reason;
|
||||
|
||||
@Override
|
||||
public void read(InPacketBuffer buffer, ProtocolVersion v) {
|
||||
reason = buffer.readJson();
|
||||
reason = buffer.readString();
|
||||
log();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log() {
|
||||
Log.protocol(String.format("Receiving login disconnect packet (%s)", reason.toString()));
|
||||
Log.protocol(String.format("Receiving login disconnect packet (%s)", reason));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -26,7 +25,7 @@ public class PacketLoginDisconnect implements ClientboundPacket {
|
||||
h.handle(this);
|
||||
}
|
||||
|
||||
public JSONObject getReason() {
|
||||
public String getReason() {
|
||||
return reason;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
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.protocol.packets.ClientboundPacket;
|
||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
public class PacketChatMessage implements ClientboundPacket {
|
||||
ChatComponent c;
|
||||
|
||||
|
||||
@Override
|
||||
public void read(InPacketBuffer buffer, ProtocolVersion v) {
|
||||
switch (v) {
|
||||
case VERSION_1_7_10:
|
||||
c = buffer.readChatComponent();
|
||||
break;
|
||||
}
|
||||
log();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log() {
|
||||
Log.game(String.format("[CHAT] %s", c.getRawMessage()));
|
||||
}
|
||||
|
||||
public ChatComponent getChatComponent() {
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handle(PacketHandler h) {
|
||||
h.handle(this);
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ import de.bixilon.minosoft.util.Util;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PlayChunkBulk implements ClientboundPacket {
|
||||
public class PacketChunkBulk implements ClientboundPacket {
|
||||
HashMap<ChunkLocation, Chunk> chunkMap = new HashMap<>();
|
||||
|
||||
|
@ -0,0 +1,37 @@
|
||||
package de.bixilon.minosoft.protocol.packets.clientbound.play;
|
||||
|
||||
import de.bixilon.minosoft.logging.Log;
|
||||
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
public class PacketDisconnect implements ClientboundPacket {
|
||||
String reason;
|
||||
|
||||
|
||||
@Override
|
||||
public void read(InPacketBuffer buffer, ProtocolVersion v) {
|
||||
switch (v) {
|
||||
case VERSION_1_7_10:
|
||||
reason = buffer.readString();
|
||||
break;
|
||||
}
|
||||
log();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log() {
|
||||
Log.game(String.format("Disconnected: %s", reason));
|
||||
}
|
||||
|
||||
public String getReason() {
|
||||
return reason;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handle(PacketHandler h) {
|
||||
h.handle(this);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package de.bixilon.minosoft.protocol.packets.clientbound.play;
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.player.Location;
|
||||
import de.bixilon.minosoft.logging.Log;
|
||||
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
public class PacketSpawnLocation implements ClientboundPacket {
|
||||
Location loc;
|
||||
|
||||
|
||||
@Override
|
||||
public void read(InPacketBuffer buffer, ProtocolVersion v) {
|
||||
switch (v) {
|
||||
case VERSION_1_7_10:
|
||||
int x = buffer.readInteger();
|
||||
int y = buffer.readInteger();
|
||||
int z = buffer.readInteger();
|
||||
loc = new Location(x, y, z);
|
||||
break;
|
||||
}
|
||||
log();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log() {
|
||||
Log.protocol(String.format("Received spawn location %s %s %s", loc.getX(), loc.getY(), loc.getZ()));
|
||||
}
|
||||
|
||||
public Location getSpawnLocation() {
|
||||
return loc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(PacketHandler h) {
|
||||
h.handle(this);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package de.bixilon.minosoft.protocol.protocol;
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.BlockPosition;
|
||||
import de.bixilon.minosoft.game.datatypes.ChatComponent;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@ -140,4 +141,8 @@ public class InByteBuffer {
|
||||
public String toString() {
|
||||
return "dataLen: " + bytes.length + "; pos: " + pos;
|
||||
}
|
||||
|
||||
public ChatComponent readChatComponent() {
|
||||
return new ChatComponent(readJson());
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.bixilon.minosoft.protocol.protocol;
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.BlockPosition;
|
||||
import de.bixilon.minosoft.game.datatypes.ChatComponent;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@ -133,4 +134,8 @@ public class OutByteBuffer {
|
||||
public void writeBlockPosition(BlockPosition pos) {
|
||||
writeLong((((long) pos.getX() & 0x3FFFFFF) << 38) | (((long) pos.getZ() & 0x3FFFFFF) << 12) | ((long) pos.getY() & 0xFFF));
|
||||
}
|
||||
|
||||
public void writeChatComponent(ChatComponent c) {
|
||||
writeJson(c.getRaw());
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ public class PacketHandler {
|
||||
connection.sendPacket(new PacketKeepAliveResponse(pkg.getId()));
|
||||
}
|
||||
|
||||
public void handle(PlayChunkBulk pkg) {
|
||||
public void handle(PacketChunkBulk pkg) {
|
||||
//ToDo
|
||||
}
|
||||
|
||||
@ -79,4 +79,16 @@ public class PacketHandler {
|
||||
|
||||
public void handle(PacketPluginMessageReceived pkg) {
|
||||
}
|
||||
|
||||
public void handle(PacketSpawnLocation pkg) {
|
||||
connection.getPlayer().setSpawnLocation(pkg.getSpawnLocation());
|
||||
}
|
||||
|
||||
public void handle(PacketChatMessage pkg) {
|
||||
}
|
||||
|
||||
public void handle(PacketDisconnect pkg) {
|
||||
// got kicked
|
||||
connection.setConnectionState(ConnectionState.DISCONNECTING);
|
||||
}
|
||||
}
|
||||
|
@ -38,8 +38,11 @@ public interface Protocol {
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_PLAYER_INFO, PacketPlayerInfo.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_TIME_UPDATE, PacketTimeUpdate.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_KEEP_ALIVE, PacketKeepAlive.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_CHUNK_BULK, PlayChunkBulk.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_CHUNK_BULK, PacketChunkBulk.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_UPDATE_HEALTH, PacketUpdateHealth.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_PLUGIN_MESSAGE, PacketUpdateHealth.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_SPAWN_POSITION, PacketSpawnLocation.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_CHAT_MESSAGE, PacketChatMessage.class);
|
||||
packetClassMapping.put(Packets.Clientbound.PLAY_DISCONNECT, PacketDisconnect.class);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user