Java Code Examples for us.myles.ViaVersion.api.minecraft.item.Item#getTag()

The following examples show how to use us.myles.ViaVersion.api.minecraft.item.Item#getTag() . 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: InventoryPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
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 2
Source File: ItemRewriter.java    From ViaVersion with MIT License 6 votes vote down vote up
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 3
Source File: InventoryPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
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 4
Source File: InventoryPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
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 5
Source File: BlockItemPackets1_14.java    From ViaBackwards with MIT License 6 votes vote down vote up
@Override
public Item handleItemToServer(Item item) {
    if (item == null) return null;
    super.handleItemToServer(item);

    CompoundTag tag = item.getTag();
    if (tag != null) {
        // Display Lore now uses JSON
        if (tag.get("display") instanceof CompoundTag) {
            CompoundTag display = tag.get("display");
            if (display.get("Lore") instanceof ListTag) {
                ListTag lore = display.get("Lore");
                display.put(ConverterRegistry.convertToTag(nbtTagName + "|Lore", ConverterRegistry.convertToValue(lore)));
                for (Tag loreEntry : lore) {
                    if (loreEntry instanceof StringTag) {
                        ((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJson(((StringTag) loreEntry).getValue()).toString());
                    }
                }
            }
        }

        enchantmentRewriter.handleToServer(item);
    }
    return item;
}
 
Example 6
Source File: BlockItemPackets1_11.java    From ViaBackwards with MIT License 6 votes vote down vote up
@Override
public Item handleItemToServer(final Item item) {
    if (item == null) return null;
    super.handleItemToServer(item);

    CompoundTag tag = item.getTag();
    if (tag == null) return item;

    // Rewrite spawn eggs (id checks are done in the method itself)
    EntityIdRewriter.toServerItem(item, true);

    if (tag.contains(nbtTagName + "|ench")) {
        enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
    }
    if (tag.contains(nbtTagName + "|StoredEnchantments")) {
        enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
    }
    return item;
}
 
Example 7
Source File: BlockItemPackets1_16.java    From ViaBackwards with MIT License 6 votes vote down vote up
@Override
public Item handleItemToClient(Item item) {
    if (item == null) return null;

    super.handleItemToClient(item);

    CompoundTag tag = item.getTag();
    if (item.getIdentifier() == 771 && tag != null) {
        Tag ownerTag = tag.get("SkullOwner");
        if (ownerTag instanceof CompoundTag) {
            CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
            Tag idTag = ownerCompundTag.get("Id");
            if (idTag instanceof IntArrayTag) {
                UUID ownerUuid = UUIDIntArrayType.uuidFromIntArray((int[]) idTag.getValue());
                ownerCompundTag.put(new StringTag("Id", ownerUuid.toString()));
            }
        }
    }

    enchantmentRewriter.handleToClient(item);
    return item;
}
 
Example 8
Source File: BlockItemPackets1_16.java    From ViaBackwards with MIT License 6 votes vote down vote up
@Override
public Item handleItemToServer(Item item) {
    if (item == null) return null;

    int identifier = item.getIdentifier();
    super.handleItemToServer(item);

    CompoundTag tag = item.getTag();
    if (identifier == 771 && tag != null) {
        Tag ownerTag = tag.get("SkullOwner");
        if (ownerTag instanceof CompoundTag) {
            CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
            Tag idTag = ownerCompundTag.get("Id");
            if (idTag instanceof StringTag) {
                UUID ownerUuid = UUID.fromString((String) idTag.getValue());
                ownerCompundTag.put(new IntArrayTag("Id", UUIDIntArrayType.uuidToIntArray(ownerUuid)));
            }
        }
    }

    enchantmentRewriter.handleToServer(item);
    return item;
}
 
Example 9
Source File: ItemPackets1_11_1.java    From ViaBackwards with MIT License 6 votes vote down vote up
@Override
public Item handleItemToClient(final Item item) {
    if (item == null) return null;
    super.handleItemToClient(item);

    CompoundTag tag = item.getTag();
    if (tag == null) return item;

    if (tag.get("ench") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
    }
    if (tag.get("StoredEnchantments") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
    }
    return item;
}
 
Example 10
Source File: ItemPackets1_11_1.java    From ViaBackwards with MIT License 6 votes vote down vote up
@Override
public Item handleItemToServer(final Item item) {
    if (item == null) return null;
    super.handleItemToServer(item);

    CompoundTag tag = item.getTag();
    if (tag == null) return item;

    if (tag.contains(nbtTagName + "|ench")) {
        enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
    }
    if (tag.contains(nbtTagName + "|StoredEnchantments")) {
        enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
    }
    return item;
}
 
Example 11
Source File: BlockItemPackets1_11.java    From ViaBackwards with MIT License 6 votes vote down vote up
@Override
public Item handleItemToClient(Item item) {
    if (item == null) return null;
    super.handleItemToClient(item);

    CompoundTag tag = item.getTag();
    if (tag == null) return item;

    // Rewrite spawn eggs (id checks are done in the method itself)
    EntityIdRewriter.toClientItem(item, true);

    if (tag.get("ench") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
    }
    if (tag.get("StoredEnchantments") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
    }
    return item;
}
 
Example 12
Source File: EnchantmentRewriter.java    From ViaBackwards with MIT License 5 votes vote down vote up
public void handleToClient(Item item) {
    CompoundTag tag = item.getTag();
    if (tag == null) return;

    if (tag.get("Enchantments") instanceof ListTag) {
        rewriteEnchantmentsToClient(tag, false);
    }
    if (tag.get("StoredEnchantments") instanceof ListTag) {
        rewriteEnchantmentsToClient(tag, true);
    }
}
 
Example 13
Source File: ItemRewriterBase.java    From ViaBackwards with MIT License 5 votes vote down vote up
protected CompoundTag createViaNBT(Item item) {
    CompoundTag tag = new CompoundTag(nbtTagName);
    tag.put(new ShortTag("id", (short) item.getIdentifier()));
    if (item.getAmount() != 1) {
        tag.put(new ByteTag("amount", item.getAmount()));
    }
    if (item.getData() != 0) {
        tag.put(new ShortTag("data", item.getData()));
    }
    if (item.getTag() != null) {
        tag.put(CONVERTER.convert("extras", CONVERTER.convert(item.getTag())));
    }
    return tag;
}
 
Example 14
Source File: ItemRewriterBase.java    From ViaBackwards with MIT License 5 votes vote down vote up
@Nullable
public Item handleItemToServer(Item item) {
    if (item == null) return null;

    CompoundTag tag = item.getTag();
    if (tag == null) {
        if (toServerRewriter != null) {
            item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier()));
        }
        return item;
    }

    CompoundTag viaTag = tag.remove(nbtTagName);
    if (viaTag != null) {
        short id = (short) viaTag.get("id").getValue();
        item.setIdentifier(id);

        Tag dataTag = viaTag.get("data");
        short data = dataTag != null ? (short) dataTag.getValue() : 0;
        item.setData(data);

        Tag amountTag = viaTag.get("amount");
        byte amount = amountTag != null ? (byte) amountTag.getValue() : 1;
        item.setAmount(amount);

        CompoundTag extras = viaTag.get("extras");
        if (extras != null) {
            item.setTag(CONVERTER.convert("", CONVERTER.convert(extras)));
        }
    } else {
        // Rewrite id normally
        if (toServerRewriter != null) {
            item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier()));
        }
    }
    return item;
}
 
Example 15
Source File: EntityIdRewriter.java    From ViaVersion with MIT License 5 votes vote down vote up
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 16
Source File: EnchantmentRewriter.java    From ViaBackwards with MIT License 5 votes vote down vote up
public void handleToServer(Item item) {
    CompoundTag tag = item.getTag();
    if (tag == null) return;

    if (tag.contains(nbtTagName + "|Enchantments")) {
        rewriteEnchantmentsToServer(tag, false);
    }
    if (tag.contains(nbtTagName + "|StoredEnchantments")) {
        rewriteEnchantmentsToServer(tag, true);
    }
}
 
Example 17
Source File: ItemRewriter.java    From ViaRewind with MIT License 5 votes vote down vote up
public static Item toServer(Item item) {
	if (item==null) return null;

	CompoundTag tag = item.getTag();

	if (tag==null || !item.getTag().contains("ViaRewind1_7_6_10to1_8")) return item;

	CompoundTag viaVersionTag = tag.remove("ViaRewind1_7_6_10to1_8");

	item.setIdentifier((short) viaVersionTag.get("id").getValue());
	item.setData((Short) viaVersionTag.get("data").getValue());

	if (viaVersionTag.contains("noDisplay")) tag.remove("display");

	if (viaVersionTag.contains("displayName")) {
		CompoundTag display = tag.get("display");
		if (display==null) tag.put(display = new CompoundTag("display"));
		StringTag name = display.get("Name");
		if (name==null) display.put(new StringTag("Name", (String) viaVersionTag.get("displayName").getValue()));
		else name.setValue((String) viaVersionTag.get("displayName").getValue());
	} else if (tag.contains("display")) {
		((CompoundTag)tag.get("display")).remove("Name");
	}

	if (item.getIdentifier()==387) {
		ListTag oldPages = viaVersionTag.get("pages");
		tag.remove("pages");
		tag.put(oldPages);
	}

	return item;
}
 
Example 18
Source File: BlockItemPackets1_13.java    From ViaBackwards with MIT License 4 votes vote down vote up
@Override
public Item handleItemToClient(Item item) {
    if (item == null) return null;

    // Custom mappings/super call moved down
    int originalId = item.getIdentifier();

    Integer rawId = null;
    boolean gotRawIdFromTag = false;

    CompoundTag tag = item.getTag();

    // Use tag to get original ID and data
    Tag originalIdTag;
    if (tag != null && (originalIdTag = tag.remove(extraNbtTag)) != null) {
        rawId = (Integer) originalIdTag.getValue();
        gotRawIdFromTag = true;
    }

    if (rawId == null) {
        // Look for custom mappings
        super.handleItemToClient(item);

        // No custom mapping found, look at VV mappings
        if (item.getIdentifier() == originalId) {
            int oldId = MappingData.oldToNewItems.inverse().get(item.getIdentifier());
            if (oldId != -1) {
                rawId = itemIdToRaw(oldId, item, tag);
            } else if (item.getIdentifier() == 362) { // base/colorless shulker box
                rawId = 0xe50000; // purple shulker box
            } else {
                if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                    ViaBackwards.getPlatform().getLogger().warning("Failed to get 1.12 item for " + originalId);
                }

                rawId = 0x10000;
            }
        } else {  // Use the found custom mapping
            // Take the newly added tag
            if (tag == null) {
                tag = item.getTag();
            }

            rawId = itemIdToRaw(item.getIdentifier(), item, tag);
        }
    }

    item.setIdentifier(rawId >> 16);
    item.setData((short) (rawId & 0xFFFF));

    // NBT changes
    if (tag != null) {
        if (isDamageable(item.getIdentifier())) {
            Tag damageTag = tag.remove("Damage");
            if (!gotRawIdFromTag && damageTag instanceof IntTag) {
                item.setData((short) (int) damageTag.getValue());
            }
        }

        if (item.getIdentifier() == 358) { // map
            Tag mapTag = tag.remove("map");
            if (!gotRawIdFromTag && mapTag instanceof IntTag) {
                item.setData((short) (int) mapTag.getValue());
            }
        }

        // Shield and banner
        invertShieldAndBannerId(item, tag);

        // Display Name now uses JSON
        CompoundTag display = tag.get("display");
        if (display != null) {
            StringTag name = display.get("Name");
            if (name instanceof StringTag) {
                StringTag via = display.remove(extraNbtTag + "|Name");
                name.setValue(via != null ? via.getValue() : ChatRewriter.jsonTextToLegacy(name.getValue()));
            }
        }

        // ench is now Enchantments and now uses identifiers
        rewriteEnchantmentsToClient(tag, false);
        rewriteEnchantmentsToClient(tag, true);

        rewriteCanPlaceToClient(tag, "CanPlaceOn");
        rewriteCanPlaceToClient(tag, "CanDestroy");
    }
    return item;
}
 
Example 19
Source File: LegacyBlockItemRewriter.java    From ViaBackwards with MIT License 4 votes vote down vote up
@Override
@Nullable
public Item handleItemToClient(Item item) {
    if (item == null) return null;

    MappedLegacyBlockItem data = replacementData.get(item.getIdentifier());
    if (data == null) {
        // Just rewrite the id
        return super.handleItemToClient(item);
    }

    if (item.getTag() == null) {
        item.setTag(new CompoundTag(""));
    }

    // Backup data for toServer
    short originalData = item.getData();
    item.getTag().put(createViaNBT(item));

    item.setIdentifier(data.getId());
    // Keep original data if mapped data is set to -1
    if (data.getData() != -1) {
        item.setData(data.getData());
    }

    // Set display name
    if (data.getName() != null) {
        CompoundTag tag = item.getTag().get("display");
        if (tag == null) {
            item.getTag().put(tag = new CompoundTag("display"));
        }
        StringTag nameTag = tag.get("Name");
        if (nameTag == null) {
            tag.put(nameTag = new StringTag("Name", data.getName()));
        }

        // Handle colors
        String value = nameTag.getValue();
        if (value.contains("%vb_color%")) {
            tag.put(new StringTag("Name", value.replace("%vb_color%", BlockColors.get(originalData))));
        }
    }
    return item;
}
 
Example 20
Source File: ItemRewriter.java    From ViaRewind with MIT License 4 votes vote down vote up
public static Item toClient(Item item) {
	if (item==null) return null;

	CompoundTag tag = item.getTag();
	if (tag==null) item.setTag(tag = new CompoundTag(""));

	CompoundTag viaVersionTag = new CompoundTag("ViaRewind1_7_6_10to1_8");
	tag.put(viaVersionTag);

	viaVersionTag.put(new ShortTag("id", (short) item.getIdentifier()));
	viaVersionTag.put(new ShortTag("data", item.getData()));

	CompoundTag display = tag.get("display");
	if (display!=null && display.contains("Name")) {
		viaVersionTag.put(new StringTag("displayName", (String) display.get("Name").getValue()));
	}

	if (display!=null && display.contains("Lore")) {
		viaVersionTag.put(new ListTag("lore", ((ListTag)display.get("Lore")).getValue()));
	}

	if (tag.contains("ench") || tag.contains("StoredEnchantments")) {
		ListTag enchTag = tag.contains("ench") ? tag.get("ench") : tag.get("StoredEnchantments");
		List<Tag> enchants = enchTag.getValue();
		List<Tag> lore = new ArrayList<>();
		for (Tag ench : enchants) {
			short id = (short) ((CompoundTag)ench).get("id").getValue();
			short lvl = (short) ((CompoundTag)ench).get("lvl").getValue();
			String s;
			if (id==8) {
				s  = "§r§7Depth Strider ";
			} else {
				continue;
			}
			enchTag.remove(ench);
			s += Enchantments.ENCHANTMENTS.getOrDefault(lvl, "enchantment.level." + lvl);
			lore.add(new StringTag("", s));
		}
		if (!lore.isEmpty()) {
			if (display==null) {
				tag.put(display = new CompoundTag("display"));
				viaVersionTag.put(new ByteTag("noDisplay"));
			}
			ListTag loreTag = display.get("Lore");
			if (loreTag==null) display.put(loreTag = new ListTag("Lore", StringTag.class));
			lore.addAll(loreTag.getValue());
			loreTag.setValue(lore);
		}
	}

	if (item.getIdentifier()==387 && tag.contains("pages")) {
		ListTag pages = tag.get("pages");
		ListTag oldPages = new ListTag("pages", StringTag.class);
		viaVersionTag.put(oldPages);

		for (int i = 0; i<pages.size(); i++) {
			StringTag page = pages.get(i);
			String value = page.getValue();
			oldPages.add(new StringTag(page.getName(), value));
			value = ChatUtil.jsonToLegacy(value);
			page.setValue(value);
		}
	}

	ReplacementRegistry1_7_6_10to1_8.replace(item);

	if (viaVersionTag.size()==2 && (short)viaVersionTag.get("id").getValue()==item.getIdentifier() && (short)viaVersionTag.get("data").getValue()==item.getData()) {
		item.getTag().remove("ViaRewind1_7_6_10to1_8");
		if (item.getTag().isEmpty()) item.setTag(null);
	}

	return item;
}