Search in sources :

Example 1 with EntityDataMap

use of com.nukkitx.protocol.bedrock.data.entity.EntityDataMap in project Protocol by CloudburstMC.

the class BedrockPacketHelper_v361 method writeEntityData.

@Override
public void writeEntityData(ByteBuf buffer, EntityDataMap entityDataMap) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(entityDataMap, "entityDataDictionary");
    VarInts.writeUnsignedInt(buffer, entityDataMap.size());
    for (Map.Entry<EntityData, Object> entry : entityDataMap.entrySet()) {
        int index = buffer.writerIndex();
        VarInts.writeUnsignedInt(buffer, this.entityData.get(entry.getKey()));
        Object object = entry.getValue();
        EntityData.Type type = EntityData.Type.from(object);
        VarInts.writeUnsignedInt(buffer, this.entityDataTypes.get(type));
        switch(type) {
            case BYTE:
                buffer.writeByte((byte) object);
                break;
            case SHORT:
                buffer.writeShortLE((short) object);
                break;
            case INT:
                VarInts.writeInt(buffer, (int) object);
                break;
            case FLOAT:
                buffer.writeFloatLE((float) object);
                break;
            case STRING:
                writeString(buffer, (String) object);
                break;
            case NBT:
                NbtMap tag;
                if (object instanceof NbtMap) {
                    tag = (NbtMap) object;
                } else {
                    ItemData item = (ItemData) object;
                    tag = item.getTag();
                    if (tag == null) {
                        tag = NbtMap.EMPTY;
                    }
                }
                this.writeTag(buffer, tag);
                break;
            case VECTOR3I:
                writeVector3i(buffer, (Vector3i) object);
                break;
            case FLAGS:
                int flagsIndex = entry.getKey() == EntityData.FLAGS_2 ? 1 : 0;
                object = ((EntityFlags) object).get(flagsIndex, this.entityFlags);
            case LONG:
                VarInts.writeLong(buffer, (long) object);
                break;
            case VECTOR3F:
                writeVector3f(buffer, (Vector3f) object);
                break;
            default:
                buffer.writerIndex(index);
                break;
        }
    }
}
Also used : EntityData(com.nukkitx.protocol.bedrock.data.entity.EntityData) NbtMap(com.nukkitx.nbt.NbtMap) NbtMap(com.nukkitx.nbt.NbtMap) EntityDataMap(com.nukkitx.protocol.bedrock.data.entity.EntityDataMap) Map(java.util.Map) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Aggregations

NbtMap (com.nukkitx.nbt.NbtMap)1 EntityData (com.nukkitx.protocol.bedrock.data.entity.EntityData)1 EntityDataMap (com.nukkitx.protocol.bedrock.data.entity.EntityDataMap)1 ItemData (com.nukkitx.protocol.bedrock.data.inventory.ItemData)1 Map (java.util.Map)1