com.github.steveice10.opennbt.tag.builtin.CompoundTag Java Examples
The following examples show how to use
com.github.steveice10.opennbt.tag.builtin.CompoundTag.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CrossbowTranslator.java From Geyser with MIT License | 6 votes |
@Override public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { if (itemTag.get("ChargedProjectiles") != null) { ListTag chargedProjectiles = itemTag.get("ChargedProjectiles"); if (!chargedProjectiles.getValue().isEmpty()) { CompoundTag projectile = (CompoundTag) chargedProjectiles.getValue().get(0); CompoundTag newProjectile = new CompoundTag("chargedItem"); newProjectile.put(new ByteTag("Count", (byte) projectile.get("Count").getValue())); newProjectile.put(new StringTag("Name", (String) projectile.get("id").getValue())); // Not sure what this is for newProjectile.put(new ByteTag("Damage", (byte) 0)); itemTag.put(newProjectile); } } }
Example #2
Source File: BasicItemTranslator.java From Geyser with MIT License | 6 votes |
@Override public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { if (!itemTag.contains("display")) { return; } CompoundTag displayTag = itemTag.get("display"); if (displayTag.contains("Name")) { StringTag nameTag = displayTag.get("Name"); displayTag.put(new StringTag("Name", toJavaMessage(nameTag))); } if (displayTag.contains("Lore")) { ListTag loreTag = displayTag.get("Lore"); List<Tag> lore = new ArrayList<>(); for (Tag tag : loreTag.getValue()) { if (!(tag instanceof StringTag)) return; lore.add(new StringTag("", "§r" + toJavaMessage((StringTag) tag))); } displayTag.put(new ListTag("Lore", lore)); } }
Example #3
Source File: SignBlockEntityTranslator.java From Geyser with MIT License | 6 votes |
@Override public List<Tag<?>> translateTag(CompoundTag tag, BlockState blockState) { List<Tag<?>> tags = new ArrayList<>(); StringBuilder signText = new StringBuilder(); for(int i = 0; i < 4; i++) { int currentLine = i+1; String signLine = getOrDefault(tag.getValue().get("Text" + currentLine), ""); signLine = MessageUtils.getBedrockMessage(Message.fromString(signLine)); //Java allows up to 16+ characters on certain symbols. if(signLine.length() >= 15 && (signLine.contains("-") || signLine.contains("="))) { signLine = signLine.substring(0, 14); } signText.append(signLine); signText.append("\n"); } tags.add(new StringTag("Text", MessageUtils.getBedrockMessage(Message.fromString(signText.toString())))); return tags; }
Example #4
Source File: BannerTranslator.java From Geyser with MIT License | 6 votes |
@Override public ItemData translateToBedrock(ItemStack itemStack, ItemEntry itemEntry) { if (itemStack.getNbt() == null) return super.translateToBedrock(itemStack, itemEntry); ItemData itemData = super.translateToBedrock(itemStack, itemEntry); CompoundTag blockEntityTag = itemStack.getNbt().get("BlockEntityTag"); if (blockEntityTag.contains("Patterns")) { ListTag patterns = blockEntityTag.get("Patterns"); CompoundTagBuilder builder = itemData.getTag().toBuilder(); builder.tag(convertBannerPattern(patterns)); itemData = ItemData.of(itemData.getId(), itemData.getDamage(), itemData.getCount(), builder.buildRootTag()); } return itemData; }
Example #5
Source File: BookPagesTranslator.java From Geyser with MIT License | 6 votes |
@Override public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { if (!itemTag.contains("pages")) { return; } List<Tag> pages = new ArrayList<>(); ListTag pagesTag = itemTag.get("pages"); for (Tag tag : pagesTag.getValue()) { if (!(tag instanceof StringTag)) continue; StringTag textTag = (StringTag) tag; CompoundTag pageTag = new CompoundTag(""); pageTag.put(new StringTag("photoname", "")); pageTag.put(new StringTag("text", MessageUtils.getBedrockMessageLenient(textTag.getValue()))); pages.add(pageTag); } itemTag.remove("pages"); itemTag.put(new ListTag("pages", pages)); }
Example #6
Source File: ItemRewriter.java From ViaVersion with MIT License | 6 votes |
public static void rewriteBookToServer(Item item) { int id = item.getIdentifier(); if (id != 387) { return; } CompoundTag tag = item.getTag(); ListTag pages = tag.get("pages"); if (pages == null) { // is this even possible? return; } for (int i = 0; i < pages.size(); i++) { Tag pageTag = pages.get(i); if (!(pageTag instanceof StringTag)) { continue; } StringTag stag = (StringTag) pageTag; String value = stag.getValue(); if (value.replaceAll(" ", "").isEmpty()) { value = "\"" + fixBookSpaceChars(value) + "\""; } else { value = fixBookSpaceChars(value); } stag.setValue(value); } }
Example #7
Source File: BlockEntityProvider.java From ViaVersion with MIT License | 6 votes |
/** * Transforms the BlockEntities to blocks! * * @param user UserConnection instance * @param position Block Position - WARNING: Position is null when called from a chunk * @param tag BlockEntity NBT * @param sendUpdate send a block change update * @return new block id * @throws Exception Gotta throw that exception */ public int transform(UserConnection user, Position position, CompoundTag tag, boolean sendUpdate) throws Exception { Tag idTag = tag.get("id"); if (idTag == null) return -1; String id = (String) idTag.getValue(); BlockEntityHandler handler = handlers.get(id); if (handler == null) { if (Via.getManager().isDebug()) { Via.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag); } return -1; } int newBlock = handler.transform(user, tag); if (sendUpdate && newBlock != -1) { sendBlockChange(user, position, newBlock); } return newBlock; }
Example #8
Source File: CommandBlockStorage.java From ViaVersion with MIT License | 6 votes |
public Optional<CompoundTag> getCommandBlock(Position position) { Pair<Integer, Integer> chunkCoords = getChunkCoords(position); Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkCoords); if (blocks == null) return Optional.empty(); CompoundTag tag = blocks.get(position); if (tag == null) return Optional.empty(); tag = tag.clone(); tag.put(new ByteTag("powered", (byte) 0)); tag.put(new ByteTag("auto", (byte) 0)); tag.put(new ByteTag("conditionMet", (byte) 0)); return Optional.of(tag); }
Example #9
Source File: EntityPackets.java From ViaVersion with MIT License | 6 votes |
private static CompoundTag createEndEntry() { CompoundTag tag = new CompoundTag(""); tag.put(new ByteTag("piglin_safe", (byte) 0)); tag.put(new ByteTag("natural", (byte) 0)); tag.put(new FloatTag("ambient_light", 0)); tag.put(new StringTag("infiniburn", "minecraft:infiniburn_end")); tag.put(new ByteTag("respawn_anchor_works", (byte) 0)); tag.put(new ByteTag("has_skylight", (byte) 0)); tag.put(new ByteTag("bed_works", (byte) 0)); tag.put(new LongTag("fixed_time", 6000)); tag.put(new ByteTag("has_raids", (byte) 1)); tag.put(new StringTag("name", "minecraft:the_end")); tag.put(new IntTag("logical_height", 256)); tag.put(new ByteTag("shrunk", (byte) 0)); tag.put(new ByteTag("ultrawarm", (byte) 0)); tag.put(new ByteTag("has_ceiling", (byte) 0)); return tag; }
Example #10
Source File: InventoryPackets.java From ViaVersion with MIT License | 6 votes |
public static void toClient(Item item) { if (item == null) return; if (item.getIdentifier() == 771 && item.getTag() != null) { CompoundTag tag = item.getTag(); Tag ownerTag = tag.get("SkullOwner"); if (ownerTag instanceof CompoundTag) { CompoundTag ownerCompundTag = (CompoundTag) ownerTag; Tag idTag = ownerCompundTag.get("Id"); if (idTag instanceof StringTag) { UUID id = UUID.fromString((String) idTag.getValue()); ownerCompundTag.put(new IntArrayTag("Id", UUIDIntArrayType.uuidToIntArray(id))); } } } item.setIdentifier(getNewItemId(item.getIdentifier())); }
Example #11
Source File: InventoryPackets.java From ViaVersion with MIT License | 6 votes |
public static void toClient(Item item) { if (item == null) return; item.setIdentifier(getNewItemId(item.getIdentifier())); CompoundTag tag; if ((tag = item.getTag()) != null) { // Display Lore now uses JSON Tag displayTag = tag.get("display"); if (displayTag instanceof CompoundTag) { CompoundTag display = (CompoundTag) displayTag; Tag loreTag = display.get("Lore"); if (loreTag instanceof ListTag) { ListTag lore = (ListTag) loreTag; display.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|Lore", ConverterRegistry.convertToValue(lore))); for (Tag loreEntry : lore) { if (loreEntry instanceof StringTag) { ((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJson(((StringTag) loreEntry).getValue()).toString()); } } } } } }
Example #12
Source File: AnvilInventoryTranslator.java From Geyser with MIT License | 6 votes |
@Override public void updateSlot(GeyserSession session, Inventory inventory, int slot) { if (slot >= 0 && slot <= 2) { ItemStack item = inventory.getItem(slot); if (item != null) { String rename; CompoundTag tag = item.getNbt(); if (tag != null) { CompoundTag displayTag = tag.get("display"); if (displayTag != null) { String itemName = displayTag.get("Name").getValue().toString(); Message message = Message.fromString(itemName); rename = message.getText(); } else { rename = ""; } } else { rename = ""; } ClientRenameItemPacket renameItemPacket = new ClientRenameItemPacket(rename); session.sendDownstreamPacket(renameItemPacket); } } super.updateSlot(session, inventory, slot); }
Example #13
Source File: BedHandler.java From ViaVersion with MIT License | 6 votes |
@Override public int transform(UserConnection user, CompoundTag tag) { BlockStorage storage = user.get(BlockStorage.class); Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z"))); if (!storage.contains(position)) { Via.getPlatform().getLogger().warning("Received an bed color update packet, but there is no bed! O_o " + tag); return -1; } // RED_BED + FIRST_BED int blockId = storage.get(position).getOriginal() - 972 + 748; Tag color = tag.get("color"); if (color != null) { blockId += (((Number) color.getValue()).intValue() * 16); } return blockId; }
Example #14
Source File: InventoryPackets.java From ViaVersion with MIT License | 6 votes |
public static void toServer(Item item) { if (item == null) return; item.setIdentifier(getOldItemId(item.getIdentifier())); if (item.getIdentifier() == 771 && item.getTag() != null) { CompoundTag tag = item.getTag(); Tag ownerTag = tag.get("SkullOwner"); if (ownerTag instanceof CompoundTag) { CompoundTag ownerCompundTag = (CompoundTag) ownerTag; Tag idTag = ownerCompundTag.get("Id"); if (idTag instanceof IntArrayTag) { UUID id = UUIDIntArrayType.uuidFromIntArray((int[]) idTag.getValue()); ownerCompundTag.put(new StringTag("Id", id.toString())); } } } }
Example #15
Source File: BannerTranslator.java From Geyser with MIT License | 5 votes |
/** * Convert the Bedrock edition banner pattern nbt to Java edition * * @param pattern Bedorck edition pattern nbt * @return The Java edition format pattern nbt */ public static CompoundTag getJavaBannerPattern(com.nukkitx.nbt.tag.CompoundTag pattern) { Map<String, Tag> tags = new HashMap<>(); tags.put("Color", new IntTag("Color", 15 - pattern.getInt("Color"))); tags.put("Pattern", new StringTag("Pattern", pattern.getString("Pattern"))); return new CompoundTag("", tags); }
Example #16
Source File: ItemRewriter_1_11_TO_1_10.java From ChatItem with GNU General Public License v3.0 | 5 votes |
static void toClient(Item item) { if (hasEntityTag(item)) { CompoundTag entityTag = item.getTag().get("EntityTag"); if (entityTag.get("id") instanceof StringTag) { StringTag id = entityTag.get("id"); if (oldToNewNames.containsKey(id.getValue())) { id.setValue(oldToNewNames.get(id.getValue())); } } } }
Example #17
Source File: BannerTranslator.java From Geyser with MIT License | 5 votes |
/** * Convert a list of patterns from Bedrock nbt to Java nbt * * @param patterns The patterns to convert * @return The new converted patterns */ public static ListTag convertBannerPattern(com.nukkitx.nbt.tag.ListTag<?> patterns) { List<Tag> tagsList = new ArrayList<>(); for (Object patternTag : patterns.getValue()) { CompoundTag newPatternTag = getJavaBannerPattern((com.nukkitx.nbt.tag.CompoundTag) patternTag); tagsList.add(newPatternTag); } return new ListTag("Patterns", tagsList); }
Example #18
Source File: EntityIdRewriter.java From ViaVersion with MIT License | 5 votes |
private static boolean hasEntityTag(Item item) { if (item == null || item.getIdentifier() != 383) return false; // Monster Egg CompoundTag tag = item.getTag(); if (tag == null) return false; Tag entityTag = tag.get("EntityTag"); return entityTag instanceof CompoundTag && ((CompoundTag) entityTag).get("id") instanceof StringTag; }
Example #19
Source File: MapItemTranslator.java From Geyser with MIT License | 5 votes |
@Override public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) { IntTag mapId = itemTag.get("map"); if (mapId != null) { itemTag.put(new StringTag("map_uuid", mapId.getValue().toString())); itemTag.put(new IntTag("map_name_index", mapId.getValue())); itemTag.put(new ByteTag("map_display_players", (byte) 1)); itemTag.remove("map"); } }
Example #20
Source File: WorldPackets.java From ViaVersion with MIT License | 5 votes |
private static void handleBlockEntity(CompoundTag compoundTag) { StringTag idTag = compoundTag.get("id"); if (idTag == null) return; String id = idTag.getValue(); if (id.equals("minecraft:conduit")) { Tag targetUuidTag = compoundTag.remove("target_uuid"); if (!(targetUuidTag instanceof StringTag)) return; // target_uuid -> Target UUID targetUuid = UUID.fromString((String) targetUuidTag.getValue()); compoundTag.put(new IntArrayTag("Target", UUIDIntArrayType.uuidToIntArray(targetUuid))); } else if (id.equals("minecraft:skull") && compoundTag.get("Owner") instanceof CompoundTag) { CompoundTag ownerTag = compoundTag.remove("Owner"); StringTag ownerUuidTag = ownerTag.remove("Id"); if (ownerUuidTag != null) { UUID ownerUuid = UUID.fromString(ownerUuidTag.getValue()); ownerTag.put(new IntArrayTag("Id", UUIDIntArrayType.uuidToIntArray(ownerUuid))); } // Owner -> SkullOwner CompoundTag skullOwnerTag = new CompoundTag("SkullOwner"); for (Tag tag : ownerTag) { skullOwnerTag.put(tag); } compoundTag.put(skullOwnerTag); } }
Example #21
Source File: EnchantedBookTranslator.java From Geyser with MIT License | 5 votes |
@Override public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { if (!itemTag.contains("Enchantments")) { return; } Tag enchTag = itemTag.get("Enchantments"); if (enchTag instanceof ListTag) { enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue()); itemTag.remove("Enchantments"); itemTag.put(enchTag); } }
Example #22
Source File: BinaryTagIO.java From ViaVersion with MIT License | 5 votes |
/** * Writes a compound tag to a {@link String}. * * @param tag the compound tag * @return the string * @throws IOException if an exception was encountered while writing the compound tag */ @NotNull public static String writeString(final @NotNull CompoundTag tag) throws IOException { final StringBuilder sb = new StringBuilder(); try (final TagStringWriter emit = new TagStringWriter(sb)) { emit.writeTag(tag); } return sb.toString(); }
Example #23
Source File: EntityPackets.java From ViaVersion with MIT License | 5 votes |
private static void addSharedOverwaldEntries(CompoundTag tag) { tag.put(new ByteTag("piglin_safe", (byte) 0)); tag.put(new ByteTag("natural", (byte) 1)); tag.put(new FloatTag("ambient_light", 0)); tag.put(new StringTag("infiniburn", "minecraft:infiniburn_overworld")); tag.put(new ByteTag("respawn_anchor_works", (byte) 0)); tag.put(new ByteTag("has_skylight", (byte) 1)); tag.put(new ByteTag("bed_works", (byte) 1)); tag.put(new ByteTag("has_raids", (byte) 1)); tag.put(new IntTag("logical_height", 256)); tag.put(new ByteTag("shrunk", (byte) 0)); tag.put(new ByteTag("ultrawarm", (byte) 0)); }
Example #24
Source File: AnvilInventoryTranslator.java From Geyser with MIT License | 5 votes |
@Override public void translateActions(GeyserSession session, Inventory inventory, List<InventoryActionData> actions) { InventoryActionData anvilResult = null; InventoryActionData anvilInput = null; for (InventoryActionData action : actions) { if (action.getSource().getContainerId() == ContainerId.ANVIL_MATERIAL) { //useless packet return; } else if (action.getSource().getContainerId() == ContainerId.ANVIL_RESULT) { anvilResult = action; } else if (bedrockSlotToJava(action) == 0) { anvilInput = action; } } ItemData itemName = null; if (anvilResult != null) { itemName = anvilResult.getFromItem(); } else if (anvilInput != null) { itemName = anvilInput.getToItem(); } if (itemName != null) { String rename; com.nukkitx.nbt.tag.CompoundTag tag = itemName.getTag(); if (tag != null) { rename = tag.getCompound("display").getString("Name"); } else { rename = ""; } ClientRenameItemPacket renameItemPacket = new ClientRenameItemPacket(rename); session.sendDownstreamPacket(renameItemPacket); } if (anvilResult != null) { //client will send another packet to grab anvil output return; } super.translateActions(session, inventory, actions); }
Example #25
Source File: FlowerPotHandler.java From ViaVersion with MIT License | 5 votes |
@Override public int transform(UserConnection user, CompoundTag tag) { Object item = tag.contains("Item") ? tag.get("Item").getValue() : null; Object data = tag.contains("Data") ? tag.get("Data").getValue() : null; // Convert item to String without namespace or to Byte if (item instanceof String) { item = ((String) item).replace("minecraft:", ""); } else if (item instanceof Number) { item = ((Number) item).byteValue(); } else { item = (byte) 0; } // Convert data to Byte if (data instanceof Number) { data = ((Number) data).byteValue(); } else { data = (byte) 0; } Integer flower = flowers.get(new Pair<>(item, (byte) data)); if (flower != null) return flower; flower = flowers.get(new Pair<>(item, (byte) 0)); if (flower != null) return flower; return 5265; // Fallback to empty pot }
Example #26
Source File: BannerTranslator.java From Geyser with MIT License | 5 votes |
/** * Convert the Java edition banner pattern nbt to Bedrock edition, null if the pattern doesn't exist * * @param pattern Java edition pattern nbt * @return The Bedrock edition format pattern nbt */ public static com.nukkitx.nbt.tag.CompoundTag getBedrockBannerPattern(CompoundTag pattern) { String patternName = (String) pattern.get("Pattern").getValue(); // Return null if its the globe pattern as it doesn't exist on bedrock if (patternName.equals("glb")) { return null; } return CompoundTagBuilder.builder() .intTag("Color", 15 - (int) pattern.get("Color").getValue()) .stringTag("Pattern", (String) pattern.get("Pattern").getValue()) .stringTag("Pattern", patternName) .buildRootTag(); }
Example #27
Source File: CrossbowTranslator.java From Geyser with MIT License | 5 votes |
@Override public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) { if (itemTag.get("chargedItem") != null) { CompoundTag chargedItem = itemTag.get("chargedItem"); CompoundTag newProjectile = new CompoundTag(""); newProjectile.put(new ByteTag("Count", (byte) chargedItem.get("Count").getValue())); newProjectile.put(new StringTag("id", (String) chargedItem.get("Name").getValue())); ListTag chargedProjectiles = new ListTag("ChargedProjectiles"); chargedProjectiles.add(newProjectile); itemTag.put(chargedProjectiles); } }
Example #28
Source File: Chunk1_13Type.java From ViaVersion with MIT License | 5 votes |
@Override public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception { output.writeInt(chunk.getX()); output.writeInt(chunk.getZ()); output.writeBoolean(chunk.isFullChunk()); Type.VAR_INT.writePrimitive(output, chunk.getBitmask()); ByteBuf buf = output.alloc().buffer(); try { for (int i = 0; i < 16; i++) { ChunkSection section = chunk.getSections()[i]; if (section == null) continue; // Section not set Types1_13.CHUNK_SECTION.write(buf, section); section.writeBlockLight(buf); if (!section.hasSkyLight()) continue; // No sky light, we're done here. section.writeSkyLight(buf); } buf.readerIndex(0); Type.VAR_INT.writePrimitive(output, buf.readableBytes() + (chunk.isBiomeData() ? 1024 : 0)); output.writeBytes(buf); } finally { buf.release(); // release buffer } // Write biome data if (chunk.isBiomeData()) { for (int value : chunk.getBiomeData()) { output.writeInt(value); } } // Write Block Entities Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(new CompoundTag[0])); }
Example #29
Source File: CampfireBlockEntityTranslator.java From Geyser with MIT License | 5 votes |
@Override public com.nukkitx.nbt.tag.CompoundTag getDefaultBedrockTag(String bedrockId, int x, int y, int z) { CompoundTagBuilder tagBuilder = getConstantBedrockTag(bedrockId, x, y, z).toBuilder(); tagBuilder.tag(new com.nukkitx.nbt.tag.CompoundTag("Item1", new HashMap<>())); tagBuilder.tag(new com.nukkitx.nbt.tag.CompoundTag("Item2", new HashMap<>())); tagBuilder.tag(new com.nukkitx.nbt.tag.CompoundTag("Item3", new HashMap<>())); tagBuilder.tag(new com.nukkitx.nbt.tag.CompoundTag("Item4", new HashMap<>())); return tagBuilder.buildRootTag(); }
Example #30
Source File: SpawnerHandler.java From ViaVersion with MIT License | 5 votes |
@Override public int transform(UserConnection user, CompoundTag tag) { if (tag.contains("SpawnData") && tag.get("SpawnData") instanceof CompoundTag) { CompoundTag data = tag.get("SpawnData"); if (data.contains("id") && data.get("id") instanceof StringTag) { StringTag s = data.get("id"); s.setValue(EntityNameRewriter.rewrite(s.getValue())); } } // Always return -1 because the block is still the same id return -1; }