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

The following examples show how to use us.myles.ViaVersion.api.minecraft.item.Item#getIdentifier() . 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: Windows.java    From ViaRewind with MIT License 6 votes vote down vote up
public static void updateBrewingStand(UserConnection user, Item blazePowder, short windowId) {
	if (blazePowder != null && blazePowder.getIdentifier() != 377) return;
	int amount = blazePowder == null ? 0 : blazePowder.getAmount();
	PacketWrapper openWindow = new PacketWrapper(0x2D, null, user);
	openWindow.write(Type.UNSIGNED_BYTE, windowId);
	openWindow.write(Type.STRING, "minecraft:brewing_stand");
	openWindow.write(Type.STRING, "[{\"translate\":\"container.brewing\"},{\"text\":\": \",\"color\":\"dark_gray\"},{\"text\":\"§4" + amount + " \",\"color\":\"dark_red\"},{\"translate\":\"item.blazePowder.name\",\"color\":\"dark_red\"}]");
	openWindow.write(Type.UNSIGNED_BYTE, (short) 420);
	PacketUtil.sendPacket(openWindow, Protocol1_8TO1_9.class);

	Item[] items = user.get(Windows.class).getBrewingItems(windowId);
	for (int i = 0; i < items.length; i++) {
		PacketWrapper setSlot = new PacketWrapper(0x2F, null, user);
		setSlot.write(Type.BYTE, (byte) windowId);
		setSlot.write(Type.SHORT, (short) i);
		setSlot.write(Type.ITEM, items[i]);
		PacketUtil.sendPacket(setSlot, Protocol1_8TO1_9.class);
	}
}
 
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_13.java    From ViaBackwards with MIT License 6 votes vote down vote up
private void invertShieldAndBannerId(Item item, CompoundTag tag) {
    if (item.getIdentifier() != 442 && item.getIdentifier() != 425) return;

    Tag blockEntityTag = tag.get("BlockEntityTag");
    if (!(blockEntityTag instanceof CompoundTag)) return;

    CompoundTag blockEntityCompoundTag = (CompoundTag) blockEntityTag;
    Tag base = blockEntityCompoundTag.get("Base");
    if (base instanceof IntTag) {
        IntTag baseTag = (IntTag) base;
        baseTag.setValue(15 - baseTag.getValue()); // invert color id
    }

    Tag patterns = blockEntityCompoundTag.get("Patterns");
    if (patterns instanceof ListTag) {
        ListTag patternsTag = (ListTag) patterns;
        for (Tag pattern : patternsTag) {
            if (!(pattern instanceof CompoundTag)) continue;

            IntTag colorTag = ((CompoundTag) pattern).get("Color");
            colorTag.setValue(15 - colorTag.getValue()); // Invert color id
        }
    }
}
 
Example 6
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 7
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 8
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 9
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 10
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;
}
 
Example 11
Source File: BedRewriter.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void toClientItem(Item item) {
    if (item == null) return;
    if (item.getIdentifier() == 355 && item.getData() == 0) {
        item.setData((short) 14);
    }
}
 
Example 12
Source File: BedRewriter.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void toServerItem(Item item) {
    if (item == null) return;
    if (item.getIdentifier() == 355 && item.getData() == 14) {
        item.setData((short) 0);
    }
}
 
Example 13
Source File: ParticleMapping.java    From ViaBackwards with MIT License 4 votes vote down vote up
private int[] rewrite(Protocol1_12_2To1_13 protocol, Item newItem) {
    Item item = protocol.getBlockItemPackets().handleItemToClient(newItem);
    return new int[]{item.getIdentifier(), item.getData()};
}
 
Example 14
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;
}