us.myles.ViaVersion.api.minecraft.item.Item Java Examples

The following examples show how to use us.myles.ViaVersion.api.minecraft.item.Item. 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: ItemRewriter.java    From ViaVersion with MIT License 6 votes vote down vote up
public void registerEntityEquipmentArray(ClientboundPacketType packetType, Type<Item> type) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID

            handler(wrapper -> {
                byte slot;
                do {
                    slot = wrapper.passthrough(Type.BYTE);
                     // & 0x7F into an extra variable if slot is needed
                    toClient.rewrite(wrapper.passthrough(type));
                } while ((slot & 0xFFFFFF80) != 0);
            });
        }
    });
}
 
Example #2
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 #3
Source File: MetadataRewriter1_16To1_15_2.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
public void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
    if (metadata.getMetaType() == MetaType1_14.Slot) {
        InventoryPackets.toClient((Item) metadata.getValue());
    } else if (metadata.getMetaType() == MetaType1_14.BlockID) {
        int data = (int) metadata.getValue();
        metadata.setValue(Protocol1_16To1_15_2.getNewBlockStateId(data));
    }

    if (type == null) return;

    if (type == Entity1_16Types.EntityType.AREA_EFFECT_CLOUD) {
        if (metadata.getId() == 10) {
            Particle particle = (Particle) metadata.getValue();
            particle.setId(WorldPackets.getNewParticleId(particle.getId()));
        }
    } else if (type.isOrHasParent(Entity1_16Types.EntityType.ABSTRACT_ARROW)) {
        if (metadata.getId() == 8) {
            metadatas.remove(metadata);
        } else if (metadata.getId() > 8) {
            metadata.setId(metadata.getId() - 1);
        }
    }
}
 
Example #4
Source File: ItemTypeTest.java    From ViaVersion with MIT License 6 votes vote down vote up
@Test
public void testNormalItemWrite() throws Exception {
    ByteBuf buf = Unpooled.buffer();

    // Test item write
    Type.ITEM.write(buf, new Item((int) Short.MAX_VALUE, (byte) -128, (short) 257, null));
    Assertions.assertArrayEquals(toBytes(buf), new byte[]{
            127, -1,
            -128,
            1, 1,
            0
    });
    Type.FLAT_ITEM.write(buf, new Item(420, (byte) 53, (short) 0, null));
    Assertions.assertArrayEquals(toBytes(buf), new byte[]{
            1, (byte) 164,
            53,
            0
    });
    Type.FLAT_VAR_INT_ITEM.write(buf, new Item(268435456, (byte) 127, (short) 0, null));
    Assertions.assertArrayEquals(toBytes(buf), new byte[]{
            1,
            -128, -128, -128, -128, 1,
            127,
            0
    });
}
 
Example #5
Source File: GameProfileStorage.java    From ViaRewind with MIT License 6 votes vote down vote up
public Item getSkull() {
	CompoundTag tag = new CompoundTag("");
	CompoundTag ownerTag = new CompoundTag("SkullOwner");
	tag.put(ownerTag);
	ownerTag.put(new StringTag("Id", uuid.toString()));
	CompoundTag properties = new CompoundTag("Properties");
	ownerTag.put(properties);
	ListTag textures = new ListTag("textures", CompoundTag.class);
	properties.put(textures);
	for (GameProfileStorage.Property property : this.properties) {
		if (property.name.equals("textures")) {
			CompoundTag textureTag = new CompoundTag("");
			textureTag.put(new StringTag("Value", property.value));
			if (property.signature!=null) {
				textureTag.put(new StringTag("Signature", property.signature));
			}
			textures.add(textureTag);
		}
	}

	return new Item((short) 397, (byte) 1, (short) 3, tag);
}
 
Example #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: ItemArrayType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf buffer, Item[] object) throws Exception {
    Type.SHORT.writePrimitive(buffer, (short) object.length);
    for (Item o : object) {
        Type.ITEM.write(buffer, o);
    }
}
 
Example #13
Source File: BlockItemPackets1_11.java    From ViaBackwards with MIT License 5 votes vote down vote up
private Item getNewItem(ChestedHorseStorage storage, int slotId, Item current) {
    int strength = storage.isChested() ? storage.getLiamaStrength() : 0;
    int startNonExistingFormula = 2 + 3 * strength;
    int endNonExistingFormula = 2 + 3 * (storage.isChested() ? 5 : 0);

    if (slotId >= startNonExistingFormula && slotId < endNonExistingFormula)
        return new Item(166, (byte) 1, (short) 0, getNamedTag(ChatColor.RED + "SLOT DISABLED"));
    if (slotId == 1)
        return null;
    return current;
}
 
Example #14
Source File: RecipeRewriter1_15.java    From ViaBackwards with MIT License 5 votes vote down vote up
public void handleStonecutting(PacketWrapper wrapper) throws Exception {
    wrapper.passthrough(Type.STRING);
    Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
    for (Item item : items) {
        rewriter.handleItemToClient(item);
    }

    rewriter.handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
 
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: EntityIdRewriter.java    From ViaVersion with MIT License 5 votes vote down vote up
public static void toServerItem(Item item, boolean backwards) {
    if (!hasEntityTag(item)) return;

    CompoundTag entityTag = item.getTag().get("EntityTag");
    Tag idTag = entityTag.get("id");
    if (idTag instanceof StringTag) {
        StringTag id = (StringTag) idTag;
        String newName = backwards ? oldToNewNames.get(id.getValue()) : oldToNewNames.inverse().get(id.getValue());
        if (newName != null) {
            id.setValue(newName);
        }
    }
}
 
Example #17
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 #18
Source File: EntityTracker1_9.java    From ViaVersion with MIT License 5 votes vote down vote up
public void setSecondHand(int entityID, Item item) {
    PacketWrapper wrapper = new PacketWrapper(0x3C, null, getUser());
    wrapper.write(Type.VAR_INT, entityID);
    wrapper.write(Type.VAR_INT, 1); // slot
    wrapper.write(Type.ITEM, item);
    try {
        wrapper.send(Protocol1_9To1_8.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #19
Source File: FlatVarIntItemArrayType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf buffer, Item[] object) throws Exception {
    Type.SHORT.writePrimitive(buffer, (short) object.length);
    for (Item o : object) {
        Type.FLAT_VAR_INT_ITEM.write(buffer, o);
    }
}
 
Example #20
Source File: FlatItemArrayType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf buffer, Item[] object) throws Exception {
    Type.SHORT.writePrimitive(buffer, (short) object.length);
    for (Item o : object) {
        Type.FLAT_ITEM.write(buffer, o);
    }
}
 
Example #21
Source File: ItemRewriter.java    From ViaVersion with MIT License 5 votes vote down vote up
public PacketHandler itemArrayHandler(Type<Item[]> type) {
    return wrapper -> {
        Item[] items = wrapper.get(type, 0);
        for (Item item : items) {
            toClient.rewrite(item);
        }
    };
}
 
Example #22
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 #23
Source File: FlatItemArrayType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public Item[] read(ByteBuf buffer) throws Exception {
    int amount = Type.SHORT.readPrimitive(buffer);
    Item[] array = new Item[amount];
    for (int i = 0; i < amount; i++) {
        array[i] = Type.FLAT_ITEM.read(buffer);
    }
    return array;
}
 
Example #24
Source File: FlatVarIntItemType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf buffer, Item object) throws Exception {
    if (object == null) {
        buffer.writeBoolean(false);
    } else {
        buffer.writeBoolean(true);
        Type.VAR_INT.writePrimitive(buffer, object.getIdentifier());
        buffer.writeByte(object.getAmount());
        Type.NBT.write(buffer, object.getTag());
    }
}
 
Example #25
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 #26
Source File: ItemType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf buffer, Item object) throws Exception {
    if (object == null) {
        buffer.writeShort(-1);
    } else {
        buffer.writeShort(object.getIdentifier());
        buffer.writeByte(object.getAmount());
        buffer.writeShort(object.getData());
        Type.NBT.write(buffer, object.getTag());
    }
}
 
Example #27
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 #28
Source File: FlatItemType.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf buffer, Item object) throws Exception {
    if (object == null) {
        buffer.writeShort(-1);
    } else {
        buffer.writeShort(object.getIdentifier());
        buffer.writeByte(object.getAmount());
        Type.NBT.write(buffer, object.getTag());
    }
}
 
Example #29
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 #30
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;
}