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

The following examples show how to use us.myles.ViaVersion.api.minecraft.item.Item. These examples are extracted from open source projects. 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 Project: ViaRewind   Author: ViaVersion   File: GameProfileStorage.java    License: 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 #2
Source Project: ViaBackwards   Author: ViaVersion   File: BlockItemPackets1_11.java    License: 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 #3
Source Project: ViaBackwards   Author: ViaVersion   File: BlockItemPackets1_14.java    License: 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 #4
Source Project: ViaVersion   Author: ViaVersion   File: ItemTypeTest.java    License: 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 Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: 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 #6
Source Project: ViaBackwards   Author: ViaVersion   File: BlockItemPackets1_16.java    License: 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 Project: ViaBackwards   Author: ViaVersion   File: BlockItemPackets1_16.java    License: 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 Project: ViaVersion   Author: ViaVersion   File: InventoryPackets.java    License: 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 #9
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: 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 #10
Source Project: ViaVersion   Author: ViaVersion   File: InventoryPackets.java    License: 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 #11
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter1_16To1_15_2.java    License: 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 #12
Source Project: ViaRewind   Author: ViaVersion   File: ItemType.java    License: 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 #13
Source Project: ViaRewind   Author: ViaVersion   File: ItemType.java    License: MIT License 5 votes vote down vote up
@Override
public void write(ByteBuf buffer, Item item) throws Exception {
	if (item == null) {
		buffer.writeShort(-1);
	} else {
		buffer.writeShort(item.getIdentifier());
		buffer.writeByte(item.getAmount());
		buffer.writeShort(item.getData());
		(compressed ? Types1_7_6_10.COMPRESSED_NBT : Types1_7_6_10.NBT).write(buffer, item.getTag());
	}
}
 
Example #14
Source Project: ViaBackwards   Author: ViaVersion   File: ItemRewriterBase.java    License: 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 #15
Source Project: ViaRewind   Author: ViaVersion   File: Windows.java    License: MIT License 5 votes vote down vote up
public Item[] getBrewingItems(short windowId) {
	return brewingItems.computeIfAbsent(windowId, key -> new Item[] {
			new Item(),
			new Item(),
			new Item(),
			new Item()
	});
}
 
Example #16
Source Project: ViaBackwards   Author: ViaVersion   File: RecipeRewriter1_14.java    License: MIT License 5 votes vote down vote up
public void handleSmelting(PacketWrapper wrapper) throws Exception {
    wrapper.passthrough(Type.STRING); // Group
    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

    wrapper.passthrough(Type.FLOAT); // EXP
    wrapper.passthrough(Type.VAR_INT); // Cooking time
}
 
Example #17
Source Project: ViaVersion   Author: ViaVersion   File: SpongeViaLoader.java    License: MIT License 5 votes vote down vote up
@Override
public void load() {
    // Update Listener
    registerListener(new UpdateListener());

    /* 1.9 client to 1.8 server */
    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        try {
            Class.forName("org.spongepowered.api.event.entity.DisplaceEntityEvent");
            storeListener(new Sponge4ArmorListener()).register();
        } catch (ClassNotFoundException e) {
            storeListener(new Sponge5ArmorListener(plugin)).register();
        }
        storeListener(new DeathListener(plugin)).register();
        storeListener(new BlockListener(plugin)).register();

        if (plugin.getConf().isItemCache()) {
            tasks.add(Via.getPlatform().runRepeatingSync(new HandItemCache(), 2L)); // Updates players items :)
            HandItemCache.CACHE = true;
        }
    }

    /* Providers */
    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        Via.getManager().getProviders().use(BulkChunkTranslatorProvider.class, new SpongeViaBulkChunkTranslator());
        Via.getManager().getProviders().use(MovementTransmitterProvider.class, new SpongeViaMovementTransmitter());

        Via.getManager().getProviders().use(HandItemProvider.class, new HandItemProvider() {
            @Override
            public Item getHandItem(final UserConnection info) {
                if (HandItemCache.CACHE) {
                    return HandItemCache.getHandItem(info.getProtocolInfo().getUuid());
                } else {
                    return super.getHandItem(info);
                }
            }
        });
    }
}
 
Example #18
Source Project: ViaVersion   Author: ViaVersion   File: BlockRewriter.java    License: MIT License 5 votes vote down vote up
public void registerSpawnParticle(ClientboundPacketType packetType, int blockId, int fallingDustId, int itemId,
                                  @Nullable IdRewriteFunction particleRewriteFunction, ItemRewriter.RewriteFunction itemRewriteFunction, Type<Item> itemType, Type<?> coordType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Particle ID
            map(Type.BOOLEAN); // 1 - Long Distance
            map(coordType); // 2 - X
            map(coordType); // 3 - Y
            map(coordType); // 4 - Z
            map(Type.FLOAT); // 5 - Offset X
            map(Type.FLOAT); // 6 - Offset Y
            map(Type.FLOAT); // 7 - Offset Z
            map(Type.FLOAT); // 8 - Particle Data
            map(Type.INT); // 9 - Particle Count
            handler(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                if (id == -1) return;
                if (id == blockId || id == fallingDustId) {
                    int data = wrapper.passthrough(Type.VAR_INT);
                    wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(data));
                } else if (id == itemId) {
                    // Has to be like this, until we make *everything* object oriented inside of each protocol :(
                    itemRewriteFunction.rewrite(wrapper.passthrough(itemType));
                }

                if (particleRewriteFunction != null) {
                    int newId = particleRewriteFunction.rewrite(id);
                    if (newId != id) {
                        wrapper.set(Type.INT, 0, newId);
                    }
                }
            });
        }
    });
}
 
Example #19
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter1_12To1_11_1.java    License: MIT License 5 votes vote down vote up
@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) {
    if (metadata.getValue() instanceof Item) {
        // Apply rewrite
        BedRewriter.toClientItem((Item) metadata.getValue());
    }

    if (type == null) return;
    // Evocation Illager aggressive property became 13
    if (type == Entity1_12Types.EntityType.EVOCATION_ILLAGER) {
        if (metadata.getId() == 12) {
            metadata.setId(13);
        }
    }
}
 
Example #20
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerEntityEquipment(ClientboundPacketType packetType, Type<Item> type) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.VAR_INT); // 1 - Slot ID
            map(type); // 2 - Item

            handler(itemToClientHandler(type));
        }
    });
}
 
Example #21
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerCreativeInvAction(ServerboundPacketType packetType, Type<Item> type) {
    protocol.registerIncoming(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.SHORT); // 0 - Slot
            map(type); // 1 - Clicked Item

            handler(itemToServerHandler(type));
        }
    });
}
 
Example #22
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerClickWindow(ServerboundPacketType packetType, Type<Item> type) {
    protocol.registerIncoming(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.UNSIGNED_BYTE); // 0 - Window ID
            map(Type.SHORT); // 1 - Slot
            map(Type.BYTE); // 2 - Button
            map(Type.SHORT); // 3 - Action number
            map(Type.VAR_INT); // 4 - Mode
            map(type); // 5 - Clicked Item

            handler(itemToServerHandler(type));
        }
    });
}
 
Example #23
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: 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 #24
Source Project: ViaVersion   Author: ViaVersion   File: FlatItemType.java    License: 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 #25
Source Project: ViaVersion   Author: ViaVersion   File: FlatItemType.java    License: 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 #26
Source Project: ViaVersion   Author: ViaVersion   File: ItemType.java    License: 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 #27
Source Project: ViaVersion   Author: ViaVersion   File: ItemType.java    License: 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 #28
Source Project: ViaVersion   Author: ViaVersion   File: FlatVarIntItemType.java    License: 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 #29
Source Project: ViaVersion   Author: ViaVersion   File: FlatVarIntItemType.java    License: 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 #30
Source Project: ViaVersion   Author: ViaVersion   File: FlatItemArrayType.java    License: 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;
}