use of com.nukkitx.nbt.NbtMap in project Geyser by GeyserMC.
the class FlowerPotBlockEntityTranslator method getTag.
/**
* Get the Nukkit CompoundTag of the flower pot.
*
* @param blockState Java block state of flower pot.
* @param position Bedrock position of flower pot.
* @return Bedrock tag of flower pot.
*/
public static NbtMap getTag(GeyserSession session, int blockState, Vector3i position) {
NbtMapBuilder tagBuilder = NbtMap.builder().putInt("x", position.getX()).putInt("y", position.getY()).putInt("z", position.getZ()).putByte("isMovable", (byte) 1).putString("id", "FlowerPot");
// Get the Java name of the plant inside. e.g. minecraft:oak_sapling
String name = BlockStateValues.getFlowerPotValues().get(blockState);
if (name != null) {
// Get the Bedrock CompoundTag of the block.
// This is where we need to store the *Java* name because Bedrock has six minecraft:sapling blocks with different block states.
NbtMap plant = session.getBlockMappings().getFlowerPotBlocks().get(name);
if (plant != null) {
tagBuilder.put("PlantBlock", plant.toBuilder().build());
}
}
return tagBuilder.build();
}
use of com.nukkitx.nbt.NbtMap in project Geyser by GeyserMC.
the class JavaHorseScreenOpenTranslator method translate.
@Override
public void translate(GeyserSession session, ClientboundHorseScreenOpenPacket packet) {
Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
if (entity == null) {
return;
}
UpdateEquipPacket updateEquipPacket = new UpdateEquipPacket();
updateEquipPacket.setWindowId((short) packet.getContainerId());
updateEquipPacket.setWindowType((short) ContainerType.HORSE.getId());
updateEquipPacket.setUniqueEntityId(entity.getGeyserId());
NbtMapBuilder builder = NbtMap.builder();
List<NbtMap> slots = new ArrayList<>();
InventoryTranslator inventoryTranslator;
if (entity instanceof LlamaEntity) {
inventoryTranslator = new LlamaInventoryTranslator(packet.getNumberOfSlots());
slots.add(CARPET_SLOT);
} else if (entity instanceof ChestedHorseEntity) {
inventoryTranslator = new DonkeyInventoryTranslator(packet.getNumberOfSlots());
slots.add(SADDLE_SLOT);
} else {
inventoryTranslator = new HorseInventoryTranslator(packet.getNumberOfSlots());
slots.add(SADDLE_SLOT);
slots.add(ARMOR_SLOT);
}
// Build the NbtMap that sets the icons for Bedrock (e.g. sets the saddle outline on the saddle slot)
builder.putList("slots", NbtType.COMPOUND, slots);
updateEquipPacket.setTag(builder.build());
session.sendUpstreamPacket(updateEquipPacket);
session.setInventoryTranslator(inventoryTranslator);
InventoryUtils.openInventory(session, new Container(entity.getNametag(), packet.getContainerId(), packet.getNumberOfSlots(), null, session.getPlayerInventory()));
}
use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.
the class BedrockPacketHelper_v332 method readItem.
@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
int id = VarInts.readInt(buffer);
if (id == 0) {
// We don't need to read anything extra.
return ItemData.AIR;
}
int aux = VarInts.readInt(buffer);
int damage = (short) (aux >> 8);
if (damage == Short.MAX_VALUE)
damage = -1;
int count = aux & 0xff;
int nbtSize = buffer.readShortLE();
NbtMap compoundTag = null;
if (nbtSize > 0) {
try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
compoundTag = (NbtMap) reader.readTag();
} catch (IOException e) {
throw new IllegalStateException("Unable to load NBT data", e);
}
} else if (nbtSize == -1) {
int tagCount = buffer.readUnsignedByte();
if (tagCount != 1)
throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
compoundTag = (NbtMap) reader.readTag();
} catch (IOException e) {
throw new IllegalStateException("Unable to load NBT data", e);
}
}
String[] canPlace = new String[VarInts.readInt(buffer)];
for (int i = 0; i < canPlace.length; i++) {
canPlace[i] = this.readString(buffer);
}
String[] canBreak = new String[VarInts.readInt(buffer)];
for (int i = 0; i < canBreak.length; i++) {
canBreak[i] = this.readString(buffer);
}
return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).build();
}
use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.
the class BedrockPacketHelper_v340 method readItem.
@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
int id = VarInts.readInt(buffer);
if (id == 0) {
// We don't need to read anything extra.
return ItemData.AIR;
}
int aux = VarInts.readInt(buffer);
int damage = (short) (aux >> 8);
if (damage == Short.MAX_VALUE)
damage = -1;
int count = aux & 0xff;
int nbtSize = buffer.readShortLE();
NbtMap compoundTag = null;
if (nbtSize > 0) {
try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
compoundTag = (NbtMap) reader.readTag();
} catch (IOException e) {
throw new IllegalStateException("Unable to load NBT data", e);
}
} else if (nbtSize == -1) {
int tagCount = buffer.readUnsignedByte();
if (tagCount != 1)
throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
compoundTag = (NbtMap) reader.readTag();
} catch (IOException e) {
throw new IllegalStateException("Unable to load NBT data", e);
}
}
String[] canPlace = new String[VarInts.readInt(buffer)];
for (int i = 0; i < canPlace.length; i++) {
canPlace[i] = this.readString(buffer);
}
String[] canBreak = new String[VarInts.readInt(buffer)];
for (int i = 0; i < canBreak.length; i++) {
canBreak[i] = this.readString(buffer);
}
long blockingTicks = 0;
if (this.isBlockingItem(id, session)) {
blockingTicks = VarInts.readLong(buffer);
}
return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).blockingTicks(blockingTicks).build();
}
use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.
the class BedrockPacketHelper_v291 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:
ItemData item;
if (object instanceof NbtMap) {
item = ItemData.builder().id(1).damage(0).count(1).tag((NbtMap) object).build();
} else {
item = (ItemData) object;
}
this.writeItem(buffer, item, null);
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;
}
}
}
Aggregations