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

The following examples show how to use us.myles.ViaVersion.api.minecraft.item.Item#setIdentifier() . 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 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 2
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 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;
    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 4
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 5
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 6
Source File: ItemRewriterBase.java    From ViaBackwards with MIT License 5 votes vote down vote up
@Nullable
public Item handleItemToClient(Item item) {
    if (item == null) return null;
    if (toClientRewriter != null) {
        item.setIdentifier(toClientRewriter.rewrite(item.getIdentifier()));
    }
    return item;
}
 
Example 7
Source File: InventoryPackets.java    From ViaVersion with MIT License 5 votes vote down vote up
public static void toServer(Item item) {
    if (item == null) return;
    item.setIdentifier(getOldItemId(item.getIdentifier()));

    CompoundTag tag;
    if ((tag = item.getTag()) != null) {
        // Display Name 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;
                ListTag via = display.get(NBT_TAG_NAME + "|Lore");
                if (via != null) {
                    display.put(ConverterRegistry.convertToTag("Lore", ConverterRegistry.convertToValue(via)));
                } else {
                    for (Tag loreEntry : lore) {
                        if (loreEntry instanceof StringTag) {
                            ((StringTag) loreEntry).setValue(
                                    ChatRewriter.jsonTextToLegacy(
                                            ((StringTag) loreEntry).getValue()
                                    )
                            );
                        }
                    }
                }
                display.remove(NBT_TAG_NAME + "|Lore");
            }
        }
    }
}
 
Example 8
Source File: FlatVarIntItemType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public Item read(ByteBuf buffer) throws Exception {
    boolean present = buffer.readBoolean();
    if (!present) {
        return null;
    } else {
        Item item = new Item();
        item.setIdentifier(Type.VAR_INT.readPrimitive(buffer));
        item.setAmount(buffer.readByte());
        item.setTag(Type.NBT.read(buffer));
        return item;
    }
}
 
Example 9
Source File: ItemType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public Item read(ByteBuf buffer) throws Exception {
    short id = buffer.readShort();
    if (id < 0) {
        return null;
    } else {
        Item item = new Item();
        item.setIdentifier(id);
        item.setAmount(buffer.readByte());
        item.setData(buffer.readShort());
        item.setTag(Type.NBT.read(buffer));
        return item;
    }
}
 
Example 10
Source File: FlatItemType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public Item read(ByteBuf buffer) throws Exception {
    short id = buffer.readShort();
    if (id < 0) {
        return null;
    } else {
        Item item = new Item();
        item.setIdentifier(id);
        item.setAmount(buffer.readByte());
        item.setTag(Type.NBT.read(buffer));
        return item;
    }
}
 
Example 11
Source File: ItemType.java    From ViaRewind with MIT License 5 votes vote down vote up
@Override
public Item read(ByteBuf buffer) throws Exception {
	int readerIndex = buffer.readerIndex();
	short id = buffer.readShort();
	if (id < 0) {
		return null;
	}
	Item item = new Item();
	item.setIdentifier(id);
	item.setAmount(buffer.readByte());
	item.setData(buffer.readShort());
	item.setTag((compressed ? Types1_7_6_10.COMPRESSED_NBT : Types1_7_6_10.NBT).read(buffer));
	return item;
}
 
Example 12
Source File: InventoryPackets.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void toClient(Item item) {
    if (item == null) return;
    item.setIdentifier(getNewItemId(item.getIdentifier()));
}
 
Example 13
Source File: InventoryPackets.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void toServer(Item item) {
    if (item == null) return;
    item.setIdentifier(getOldItemId(item.getIdentifier()));
}
 
Example 14
Source File: InventoryPackets.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void toServer(Item item) {
    if (item == null) return;
    item.setIdentifier(getOldItemId(item.getIdentifier()));
}
 
Example 15
Source File: InventoryPackets.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void toClient(Item item) {
    if (item == null) return;
    item.setIdentifier(getNewItemId(item.getIdentifier()));
}
 
Example 16
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 17
Source File: InventoryPackets1_13_1.java    From ViaBackwards with MIT License 4 votes vote down vote up
public static void toClient(Item item) {
    if (item == null) return;
    item.setIdentifier(getOldItemId(item.getIdentifier()));
}
 
Example 18
Source File: InventoryPackets1_13_1.java    From ViaBackwards with MIT License 4 votes vote down vote up
public static void toServer(Item item) {
    if (item == null) return;
    item.setIdentifier(getNewItemId(item.getIdentifier()));
}
 
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;
}