use of net.glowstone.entity.meta.MetadataMap.Entry in project Glowstone by GlowstoneMC.
the class GlowBufUtils method writeMetadata.
/**
* Write a list of mob metadata entries to the buffer.
*
* @param buf The buffer.
* @param entries The metadata.
* @throws IOException if the buffer could not be written to
*/
public static void writeMetadata(ByteBuf buf, List<Entry> entries) throws IOException {
for (Entry entry : entries) {
MetadataIndex index = entry.index;
Object value = entry.value;
int type = index.getType().getId();
int id = index.getIndex();
buf.writeByte(id);
buf.writeByte(type);
if (!index.getType().isOptional() && value == null) {
continue;
}
if (index.getType().isOptional()) {
buf.writeBoolean(value != null);
if (value == null) {
continue;
}
}
writeValue(buf, value, index.getType());
}
buf.writeByte(0xff);
}
use of net.glowstone.entity.meta.MetadataMap.Entry in project Glowstone by GlowstoneMC.
the class GlowEntity method createUpdateMessage.
/**
* Creates a {@link Message} which can be sent to a client to update this entity.
*
* @param session Session to update this entity for
* @return A message which can update this entity.
*/
public List<Message> createUpdateMessage(GlowSession session) {
double x = location.getX();
double y = location.getY();
double z = location.getZ();
double dx = x * 32 - previousLocation.getX() * 32;
double dy = y * 32 - previousLocation.getY() * 32;
double dz = z * 32 - previousLocation.getZ() * 32;
dx *= 128;
dy *= 128;
dz *= 128;
boolean teleport = dx > Short.MAX_VALUE || dy > Short.MAX_VALUE || dz > Short.MAX_VALUE || dx < Short.MIN_VALUE || dy < Short.MIN_VALUE || dz < Short.MIN_VALUE;
List<Message> result = new LinkedList<>();
boolean moved = hasMoved();
boolean rotated = hasRotated();
if (teleported || moved && teleport) {
result.add(new EntityTeleportMessage(entityId, location));
} else if (rotated) {
int yaw = Position.getIntYaw(location);
int pitch = Position.getIntPitch(location);
if (moved) {
result.add(new RelativeEntityPositionRotationMessage(entityId, (short) dx, (short) dy, (short) dz, yaw, pitch));
} else {
result.add(new EntityRotationMessage(entityId, yaw, pitch));
}
} else if (moved) {
result.add(new RelativeEntityPositionMessage(entityId, (short) dx, (short) dy, (short) dz));
}
// send changed metadata
List<Entry> changes = metadata.getChanges();
if (!changes.isEmpty()) {
result.add(new EntityMetadataMessage(entityId, changes));
}
// send velocity if needed
if (velocityChanged) {
result.add(new EntityVelocityMessage(entityId, velocity));
}
if (passengerChanged) {
// A player can be a passenger of any arbitrary entity, e.g. a boat
// In case the current session belongs to this player passenger
// We need to send the self_id
List<Integer> passengerIds = new ArrayList<>();
getPassengers().forEach(e -> passengerIds.add(e.getEntityId()));
result.add(new SetPassengerMessage(getEntityId(), passengerIds.stream().mapToInt(Integer::intValue).toArray()));
passengerChanged = false;
}
if (leashHolderChanged) {
int attached = isLeashed() && session.getPlayer().getEntityId() == leashHolder.getEntityId() ? 0 : this.getEntityId();
int holder = !isLeashed() ? -1 : leashHolder.getEntityId();
// createAfterSpawnMessage()
if (!isLeashed() || session.getPlayer().canSeeEntity(leashHolder)) {
result.add(new AttachEntityMessage(attached, holder));
}
}
return result;
}
use of net.glowstone.entity.meta.MetadataMap.Entry in project Glowstone by GlowstoneMC.
the class GlowBufUtils method readMetadata.
/**
* Read a list of mob metadata entries from the buffer.
*
* @param buf The buffer.
* @return The metadata.
* @throws IOException if the buffer could not be read
*/
public static List<Entry> readMetadata(ByteBuf buf) throws IOException {
List<Entry> entries = new ArrayList<>();
byte item;
while ((item = buf.readByte()) != -1) {
MetadataType type = MetadataType.byId(buf.readByte());
MetadataIndex index = MetadataIndex.getIndex((int) item, type);
switch(type) {
case BYTE:
entries.add(new Entry(index, buf.readByte()));
break;
case INT:
entries.add(new Entry(index, ByteBufUtils.readVarInt(buf)));
break;
case FLOAT:
entries.add(new Entry(index, buf.readFloat()));
break;
case STRING:
entries.add(new Entry(index, ByteBufUtils.readUTF8(buf)));
break;
case ITEM:
entries.add(new Entry(index, readSlot(buf)));
break;
case BOOLEAN:
entries.add(new Entry(index, buf.readBoolean()));
break;
case VECTOR:
float x = buf.readFloat();
float y = buf.readFloat();
float z = buf.readFloat();
entries.add(new MetadataMap.Entry(index, new EulerAngle(x, y, z)));
break;
case POSITION:
case OPTPOSITION:
entries.add(new Entry(index, Position.getPosition(buf.readLong())));
break;
case DIRECTION:
entries.add(new Entry(index, ByteBufUtils.readVarInt(buf)));
break;
case OPTUUID:
if (buf.readBoolean()) {
entries.add(new Entry(index, readUuid(buf)));
}
break;
case BLOCKID:
entries.add(new Entry(index, ByteBufUtils.readVarInt(buf)));
break;
case NBTTAG:
entries.add(new Entry(index, readCompound(buf)));
break;
default:
}
}
return entries;
}
use of net.glowstone.entity.meta.MetadataMap.Entry in project Glowstone by GlowstoneMC.
the class SpawnPlayerCodec method decode.
@Override
public SpawnPlayerMessage decode(ByteBuf buf) throws IOException {
int id = ByteBufUtils.readVarInt(buf);
UUID uuid = GlowBufUtils.readUuid(buf);
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
int rotation = buf.readByte();
int pitch = buf.readByte();
List<Entry> list = GlowBufUtils.readMetadata(buf);
return new SpawnPlayerMessage(id, uuid, x, y, z, rotation, pitch, list);
}
use of net.glowstone.entity.meta.MetadataMap.Entry in project Glowstone by GlowstoneMC.
the class SpawnMobCodec method decode.
@Override
public SpawnMobMessage decode(ByteBuf buf) throws IOException {
int id = ByteBufUtils.readVarInt(buf);
UUID uuid = GlowBufUtils.readUuid(buf);
int type = ByteBufUtils.readVarInt(buf);
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
int headPitch = buf.readByte();
int pitch = buf.readByte();
int rotation = buf.readByte();
int velX = buf.readShort();
int velY = buf.readShort();
int velZ = buf.readShort();
List<Entry> list = GlowBufUtils.readMetadata(buf);
return new SpawnMobMessage(id, uuid, type, x, y, z, rotation, pitch, headPitch, velX, velY, velZ, list);
}
Aggregations