use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.
the class EnchantedBookTranslator method translateToBedrock.
@Override
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemMapping mapping) {
if (!itemTag.contains("StoredEnchantments")) {
return;
}
Tag enchTag = itemTag.get("StoredEnchantments");
if (enchTag instanceof ListTag) {
enchTag = new ListTag("Enchantments", ((ListTag) enchTag).getValue());
itemTag.remove("StoredEnchantments");
itemTag.put(enchTag);
}
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.
the class FireworkRocketTranslator method translateToBedrock.
@Override
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemMapping mapping) {
CompoundTag fireworks = itemTag.get("Fireworks");
if (fireworks == null) {
return;
}
if (fireworks.get("Flight") != null) {
fireworks.put(new ByteTag("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue())));
}
ListTag explosions = fireworks.get("Explosions");
if (explosions == null) {
return;
}
for (Tag effect : explosions.getValue()) {
CompoundTag effectData = (CompoundTag) effect;
CompoundTag newEffectData = translateExplosionToBedrock(effectData, "");
explosions.remove(effectData);
explosions.add(newEffectData);
}
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.
the class LecternInventoryTranslator method updateBook.
/**
* Translate the data of the book in the lectern into a block entity tag.
*/
private void updateBook(GeyserSession session, Inventory inventory, GeyserItemStack book) {
LecternContainer lecternContainer = (LecternContainer) inventory;
if (session.isDroppingLecternBook()) {
// We have to enter the inventory GUI to eject the book
ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getId(), 3);
session.sendDownstreamPacket(packet);
session.setDroppingLecternBook(false);
InventoryUtils.closeInventory(session, inventory.getId(), false);
} else if (lecternContainer.getBlockEntityTag() == null) {
CompoundTag tag = book.getNbt();
// Position has to be the last interacted position... right?
Vector3i position = session.getLastInteractionBlockPosition();
// If shouldExpectLecternHandled returns true, this is already handled for us
// shouldRefresh means that we should boot out the client on our side because their lectern GUI isn't updated yet
boolean shouldRefresh = !session.getGeyser().getWorldManager().shouldExpectLecternHandled() && !session.getLecternCache().contains(position);
NbtMap blockEntityTag;
if (tag != null) {
int pagesSize = ((ListTag) tag.get("pages")).size();
ItemData itemData = book.getItemData(session);
NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), pagesSize);
lecternTag.putCompound("book", NbtMap.builder().putByte("Count", (byte) itemData.getCount()).putShort("Damage", (short) 0).putString("Name", "minecraft:written_book").putCompound("tag", itemData.getTag()).build());
lecternTag.putInt("page", lecternContainer.getCurrentBedrockPage());
blockEntityTag = lecternTag.build();
} else {
// There is *a* book here, but... no NBT.
NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 1);
NbtMapBuilder bookTag = NbtMap.builder().putByte("Count", (byte) 1).putShort("Damage", (short) 0).putString("Name", "minecraft:writable_book").putCompound("tag", NbtMap.builder().putList("pages", NbtType.COMPOUND, Collections.singletonList(NbtMap.builder().putString("photoname", "").putString("text", "").build())).build());
blockEntityTag = lecternTag.putCompound("book", bookTag.build()).build();
}
// Even with serverside access to lecterns, we don't easily know which lectern this is, so we need to rebuild
// the block entity tag
lecternContainer.setBlockEntityTag(blockEntityTag);
lecternContainer.setPosition(position);
if (shouldRefresh) {
// Update the lectern because it's not updated client-side
BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
session.getLecternCache().add(position);
// Close the window - we will reopen it once the client has this data synced
ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getId());
session.sendDownstreamPacket(closeWindowPacket);
InventoryUtils.closeInventory(session, inventory.getId(), false);
}
}
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.
the class FireworkEntity method setFireworkItem.
public void setFireworkItem(EntityMetadata<ItemStack, ?> entityMetadata) {
ItemStack item = entityMetadata.getValue();
if (item == null) {
return;
}
CompoundTag tag = item.getNbt();
if (tag == null) {
return;
}
// https://bugs.mojang.com/browse/MCPE-89115
if (session.getClientData().getDeviceOs() == DeviceOs.XBOX || session.getClientData().getDeviceOs() == DeviceOs.PS4) {
return;
}
CompoundTag fireworks = tag.get("Fireworks");
if (fireworks == null) {
// Thank you Mineplex very cool
return;
}
NbtMapBuilder fireworksBuilder = NbtMap.builder();
if (fireworks.get("Flight") != null) {
fireworksBuilder.putByte("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue()));
}
List<NbtMap> explosions = new ArrayList<>();
if (fireworks.get("Explosions") != null) {
for (Tag effect : ((ListTag) fireworks.get("Explosions")).getValue()) {
CompoundTag effectData = (CompoundTag) effect;
NbtMapBuilder effectBuilder = NbtMap.builder();
if (effectData.get("Type") != null) {
effectBuilder.putByte("FireworkType", MathUtils.getNbtByte(effectData.get("Type").getValue()));
}
if (effectData.get("Colors") != null) {
int[] oldColors = (int[]) effectData.get("Colors").getValue();
byte[] colors = new byte[oldColors.length];
int i = 0;
for (int color : oldColors) {
colors[i++] = FireworkColor.fromJavaRGB(color);
}
effectBuilder.putByteArray("FireworkColor", colors);
}
if (effectData.get("FadeColors") != null) {
int[] oldColors = (int[]) effectData.get("FadeColors").getValue();
byte[] colors = new byte[oldColors.length];
int i = 0;
for (int color : oldColors) {
colors[i++] = FireworkColor.fromJavaRGB(color);
}
effectBuilder.putByteArray("FireworkFade", colors);
}
if (effectData.get("Trail") != null) {
effectBuilder.putByte("FireworkTrail", MathUtils.getNbtByte(effectData.get("Trail").getValue()));
}
if (effectData.get("Flicker") != null) {
effectBuilder.putByte("FireworkFlicker", MathUtils.getNbtByte(effectData.get("Flicker").getValue()));
}
explosions.add(effectBuilder.build());
}
}
fireworksBuilder.putList("Explosions", NbtType.COMPOUND, explosions);
NbtMapBuilder builder = NbtMap.builder();
builder.put("Fireworks", fireworksBuilder.build());
dirtyMetadata.put(EntityData.DISPLAY_ITEM, builder.build());
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.
the class InventoryPackets method handleItemToServer.
@Override
public Item handleItemToServer(Item item) {
if (item == null)
return null;
Integer rawId = null;
boolean gotRawIdFromTag = false;
CompoundTag tag = item.tag();
// Use tag to get original ID and data
if (tag != null) {
// Check for valid tag
if (tag.get(NBT_TAG_NAME) instanceof IntTag) {
rawId = ((NumberTag) tag.get(NBT_TAG_NAME)).asInt();
// Remove the tag
tag.remove(NBT_TAG_NAME);
gotRawIdFromTag = true;
}
}
if (rawId == null) {
int oldId = Protocol1_13To1_12_2.MAPPINGS.getItemMappings().inverse().get(item.identifier());
if (oldId != -1) {
// Handle spawn eggs
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
if (eggEntityId.isPresent()) {
rawId = 383 << 16;
if (tag == null)
item.setTag(tag = new CompoundTag());
if (!tag.contains("EntityTag")) {
CompoundTag entityTag = new CompoundTag();
entityTag.put("id", new StringTag(eggEntityId.get()));
tag.put("EntityTag", entityTag);
}
} else {
rawId = (oldId >> 4) << 16 | oldId & 0xF;
}
}
}
if (rawId == null) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Failed to get 1.12 item for " + item.identifier());
}
// Stone
rawId = 0x10000;
}
item.setIdentifier((short) (rawId >> 16));
item.setData((short) (rawId & 0xFFFF));
// NBT changes
if (tag != null) {
if (isDamageable(item.identifier())) {
if (tag.get("Damage") instanceof IntTag) {
if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("Damage").getValue());
}
tag.remove("Damage");
}
}
if (item.identifier() == 358) {
// map
if (tag.get("map") instanceof IntTag) {
if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("map").getValue());
}
tag.remove("map");
}
}
if (item.identifier() == 442 || item.identifier() == 425) {
// shield / banner
if (tag.get("BlockEntityTag") instanceof CompoundTag) {
CompoundTag blockEntityTag = tag.get("BlockEntityTag");
if (blockEntityTag.get("Base") instanceof IntTag) {
IntTag base = blockEntityTag.get("Base");
// invert color id
base.setValue(15 - base.asInt());
}
if (blockEntityTag.get("Patterns") instanceof ListTag) {
for (Tag pattern : (ListTag) blockEntityTag.get("Patterns")) {
if (pattern instanceof CompoundTag) {
IntTag c = ((CompoundTag) pattern).get("Color");
// Invert color id
c.setValue(15 - c.asInt());
}
}
}
}
}
// Display Name now uses JSON
if (tag.get("display") instanceof CompoundTag) {
CompoundTag display = tag.get("display");
if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name");
StringTag via = display.remove(NBT_TAG_NAME + "|Name");
name.setValue(via != null ? via.getValue() : ChatRewriter.jsonToLegacyText(name.getValue()));
}
}
// ench is now Enchantments and now uses identifiers
if (tag.get("Enchantments") instanceof ListTag) {
ListTag enchantments = tag.get("Enchantments");
ListTag ench = new ListTag(CompoundTag.class);
for (Tag enchantmentEntry : enchantments) {
if (enchantmentEntry instanceof CompoundTag) {
CompoundTag enchEntry = new CompoundTag();
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
if (oldId != null) {
enchEntry.put("id", new ShortTag(oldId));
enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
ench.add(enchEntry);
}
}
}
tag.remove("Enchantments");
tag.put("ench", ench);
}
if (tag.get("StoredEnchantments") instanceof ListTag) {
ListTag storedEnch = tag.get("StoredEnchantments");
ListTag newStoredEnch = new ListTag(CompoundTag.class);
for (Tag enchantmentEntry : storedEnch) {
if (enchantmentEntry instanceof CompoundTag) {
CompoundTag enchEntry = new CompoundTag();
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
if (oldId != null) {
enchEntry.put("id", new ShortTag(oldId));
enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
newStoredEnch.add(enchEntry);
}
}
}
tag.remove("StoredEnchantments");
tag.put("StoredEnchantments", newStoredEnch);
}
if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) {
tag.put("CanPlaceOn", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanPlaceOn"))));
tag.remove(NBT_TAG_NAME + "|CanPlaceOn");
} else if (tag.get("CanPlaceOn") instanceof ListTag) {
ListTag old = tag.get("CanPlaceOn");
ListTag newCanPlaceOn = new ListTag(StringTag.class);
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
if (newValues != null) {
for (String newValue : newValues) {
newCanPlaceOn.add(new StringTag(newValue));
}
} else {
newCanPlaceOn.add(oldTag);
}
}
tag.put("CanPlaceOn", newCanPlaceOn);
}
if (tag.get(NBT_TAG_NAME + "|CanDestroy") instanceof ListTag) {
tag.put("CanDestroy", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanDestroy"))));
tag.remove(NBT_TAG_NAME + "|CanDestroy");
} else if (tag.get("CanDestroy") instanceof ListTag) {
ListTag old = tag.get("CanDestroy");
ListTag newCanDestroy = new ListTag(StringTag.class);
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
if (newValues != null) {
for (String newValue : newValues) {
newCanDestroy.add(new StringTag(newValue));
}
} else {
newCanDestroy.add(oldTag);
}
}
tag.put("CanDestroy", newCanDestroy);
}
}
return item;
}
Aggregations