use of com.nukkitx.protocol.bedrock.data.entity.EntityData 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;
}
}
}
use of com.nukkitx.protocol.bedrock.data.entity.EntityData in project Protocol by CloudburstMC.
the class BedrockPacketHelper_v361 method readEntityData.
@Override
public void readEntityData(ByteBuf buffer, EntityDataMap entityDataMap) {
Preconditions.checkNotNull(buffer, "buffer");
Preconditions.checkNotNull(entityDataMap, "entityDataDictionary");
int length = VarInts.readUnsignedInt(buffer);
for (int i = 0; i < length; i++) {
int metadataInt = VarInts.readUnsignedInt(buffer);
EntityData entityData = this.entityData.get(metadataInt);
EntityData.Type type = this.entityDataTypes.get(VarInts.readUnsignedInt(buffer));
if (entityData != null && entityData.isFlags()) {
if (type != EntityData.Type.LONG) {
throw new IllegalArgumentException("Expected long value for flags, got " + type.name());
}
type = EntityData.Type.FLAGS;
}
Object object;
switch(type) {
case BYTE:
object = buffer.readByte();
break;
case SHORT:
object = buffer.readShortLE();
break;
case INT:
object = VarInts.readInt(buffer);
break;
case FLOAT:
object = buffer.readFloatLE();
break;
case STRING:
object = readString(buffer);
break;
case NBT:
object = this.readTag(buffer);
break;
case VECTOR3I:
object = readVector3i(buffer);
break;
case FLAGS:
int index = entityData == EntityData.FLAGS_2 ? 1 : 0;
entityDataMap.getOrCreateFlags().set(VarInts.readLong(buffer), index, this.entityFlags);
continue;
case LONG:
object = VarInts.readLong(buffer);
break;
case VECTOR3F:
object = readVector3f(buffer);
break;
default:
throw new IllegalArgumentException("Unknown entity data type received");
}
if (entityData != null) {
entityDataMap.put(entityData, object);
} else {
log.debug("Unknown entity data: {} type {} value {}", metadataInt, type, object);
}
}
}
Aggregations