Пример #1
0
        public async Task <ItemStack> ReadSlotAsync()
        {
            var present = await this.ReadBooleanAsync();

            if (present)
            {
                var item = Registry.GetItem((short)await this.ReadVarIntAsync());

                var slot = new ItemStack(item.Type, await this.ReadByteAsync())
                {
                    Present = present
                };

                var reader = new NbtReader(this);

                while (reader.ReadToFollowing())
                {
                    var itemMetaBuilder = new ItemMetaBuilder();

                    if (reader.IsCompound)
                    {
                        var root = (NbtCompound)reader.ReadAsTag();

                        foreach (var tag in root)
                        {
                            switch (tag.Name.ToLower())
                            {
                            case "enchantments":
                            {
                                var enchantments = (NbtList)tag;

                                foreach (var enchant in enchantments)
                                {
                                    if (enchant is NbtCompound compound)
                                    {
                                        var id = compound.Get <NbtString>("id").Value;

                                        itemMetaBuilder.AddEnchantment(id.ToEnchantType(), compound.Get <NbtShort>("lvl").Value);
                                    }
                                }

                                break;
                            }

                            case "storedenchantments":
                            {
                                var enchantments = (NbtList)tag;

                                //Globals.PacketLogger.LogDebug($"List Type: {enchantments.ListType}");

                                foreach (var enchantment in enchantments)
                                {
                                    if (enchantment is NbtCompound compound)
                                    {
                                        var id = compound.Get <NbtString>("id").Value;

                                        itemMetaBuilder.AddStoredEnchantment(id.ToEnchantType(), compound.Get <NbtShort>("lvl").Value);
                                    }
                                }
                                break;
                            }

                            case "slot":
                            {
                                itemMetaBuilder.WithSlot(tag.ByteValue);
                                //Console.WriteLine($"Setting slot: {itemMetaBuilder.Slot}");
                                break;
                            }

                            case "damage":
                            {
                                itemMetaBuilder.WithDurability(tag.IntValue);
                                //Globals.PacketLogger.LogDebug($"Setting damage: {tag.IntValue}");
                                break;
                            }

                            case "display":
                            {
                                var display = (NbtCompound)tag;

                                foreach (var displayTag in display)
                                {
                                    if (displayTag.Name.EqualsIgnoreCase("name"))
                                    {
                                        itemMetaBuilder.WithName(displayTag.StringValue);
                                    }
                                    else if (displayTag.Name.EqualsIgnoreCase("lore"))
                                    {
                                        var loreTag = (NbtList)displayTag;

                                        foreach (var lore in loreTag)
                                        {
                                            itemMetaBuilder.AddLore(JsonConvert.DeserializeObject <ChatMessage>(lore.StringValue));
                                        }
                                    }
                                }
                                break;
                            }

                            default:
                                break;
                            }
                        }
                        //slot.ItemNbt.Slot = compound.Get<NbtByte>("Slot").Value;
                        //slot.ItemNbt.Count = compound.Get<NbtByte>("Count").Value;
                        //slot.ItemNbt.Id = compound.Get<NbtShort>("id").Value;
                        //slot.ItemNbt.Damage = compound.Get<NbtShort>("Damage").Value;
                        //slot.ItemNbt.RepairCost = compound.Get<NbtInt>("RepairCost").Value;
                    }

                    slot.ItemMeta = itemMetaBuilder.Build();
                }

                return(slot);
            }

            return(null);
        }
Пример #2
0
        public ItemStack ReadItemStack()
        {
            var present = ReadBoolean();

            if (present)
            {
                var item = Registry.GetItem((short)ReadVarInt());

                var slot = new ItemStack(item.Type, ReadUnsignedByte())
                {
                    Present = present
                };

                var reader = new NbtReader(this);

                while (reader.ReadToFollowing())
                {
                    var itemMetaBuilder = new ItemMetaBuilder();

                    if (reader.IsCompound)
                    {
                        var root = (NbtCompound)reader.ReadAsTag();

                        foreach (var tag in root)
                        {
                            switch (tag.Name.ToUpperInvariant())
                            {
                            case "ENCHANTMENTS":
                            {
                                var enchantments = (NbtList)tag;

                                foreach (var enchant in enchantments)
                                {
                                    if (enchant is NbtCompound compound)
                                    {
                                        var id = compound.Get <NbtString>("id").Value;

                                        itemMetaBuilder.AddEnchantment(id.ToEnchantType(), compound.Get <NbtShort>("lvl").Value);
                                    }
                                }

                                break;
                            }

                            case "STOREDENCHANTMENTS":
                            {
                                var enchantments = (NbtList)tag;

                                //Globals.PacketLogger.LogDebug($"List Type: {enchantments.ListType}");

                                foreach (var enchantment in enchantments)
                                {
                                    if (enchantment is NbtCompound compound)
                                    {
                                        var id = compound.Get <NbtString>("id").Value;

                                        itemMetaBuilder.AddStoredEnchantment(id.ToEnchantType(), compound.Get <NbtShort>("lvl").Value);
                                    }
                                }
                                break;
                            }

                            case "SLOT":
                            {
                                itemMetaBuilder.WithSlot(tag.ByteValue);
                                //Console.WriteLine($"Setting slot: {itemMetaBuilder.Slot}");
                                break;
                            }

                            case "DAMAGE":
                            {
                                itemMetaBuilder.WithDurability(tag.IntValue);
                                //Globals.PacketLogger.LogDebug($"Setting damage: {tag.IntValue}");
                                break;
                            }

                            case "DISPLAY":
                            {
                                var display = (NbtCompound)tag;

                                foreach (var displayTag in display)
                                {
                                    if (displayTag.Name.EqualsIgnoreCase("name"))
                                    {
                                        itemMetaBuilder.WithName(displayTag.StringValue);
                                    }
                                    else if (displayTag.Name.EqualsIgnoreCase("lore"))
                                    {
                                        var loreTag = (NbtList)displayTag;

                                        foreach (var lore in loreTag)
                                        {
                                            itemMetaBuilder.AddLore(JsonConvert.DeserializeObject <ChatMessage>(lore.StringValue));
                                        }
                                    }
                                }
                                break;
                            }
                            }
                        }
                    }

                    slot.ItemMeta = itemMetaBuilder.Build();
                }

                return(slot);
            }

            return(null);
        }
Пример #3
0
        public async Task <ItemStack> ReadSlotAsync()
        {
            var present = await this.ReadBooleanAsync();

            if (present)
            {
                var item = Registry.GetItem((short)await this.ReadVarIntAsync());

                var slot = new ItemStack(item.Type, await this.ReadByteAsync())
                {
                    Present = present
                };

                var reader = new NbtReader(this);

                INbtTag tag;
                while ((tag = reader.ReadNextTag()) != null)
                {
                    var itemMetaBuilder = new ItemMetaBuilder();

                    if (tag is NbtCompound root)
                    {
                        foreach (var(name, child) in root)
                        {
                            switch (name.ToLower())
                            {
                            case "enchantments":
                            {
                                var enchantments = (NbtList)child;

                                foreach (var enchant in enchantments)
                                {
                                    if (enchant is NbtCompound compound)
                                    {
                                        itemMetaBuilder.AddEnchantment(compound.GetString("id").ToEnchantType(), compound.GetShort("lvl"));
                                    }
                                }

                                break;
                            }

                            case "storedenchantments":
                            {
                                var enchantments = (NbtList)child;

                                //Globals.PacketLogger.LogDebug($"List Type: {enchantments.ListType}");

                                foreach (var enchantment in enchantments)
                                {
                                    if (enchantment is NbtCompound compound)
                                    {
                                        compound.TryGetTag("id", out var id);
                                        compound.TryGetTag("lvl", out var lvl);

                                        itemMetaBuilder.AddStoredEnchantment(compound.GetString("id").ToEnchantType(), compound.GetShort("lvl"));
                                    }
                                }
                                break;
                            }

                            case "slot":
                            {
                                var byteTag = (NbtTag <byte>)child;

                                itemMetaBuilder.WithSlot(byteTag.Value);
                                //Console.WriteLine($"Setting slot: {itemMetaBuilder.Slot}");
                                break;
                            }

                            case "damage":
                            {
                                var intTag = (NbtTag <int>)child;

                                itemMetaBuilder.WithDurability(intTag.Value);
                                //Globals.PacketLogger.LogDebug($"Setting damage: {tag.IntValue}");
                                break;
                            }

                            case "display":
                            {
                                var display = (NbtCompound)child;

                                foreach (var(displayTagName, displayTag) in display)
                                {
                                    if (displayTagName.EqualsIgnoreCase("name") && displayTag is NbtTag <string> stringTag)
                                    {
                                        var messages = stringTag.Value.FromJson <ChatMessage[]>();
                                        itemMetaBuilder.WithName(messages[0]);
                                    }
                                    else if (displayTag.Name.EqualsIgnoreCase("lore"))
                                    {
                                        var loreTag = (NbtList)displayTag;

                                        foreach (NbtTag <string> lore in loreTag)
                                        {
                                            itemMetaBuilder.AddLore(lore.Value.FromJson <ChatMessage>());
                                        }
                                    }
                                }
                                break;
                            }

                            default:
                                break;
                            }
                        }
                        //slot.ItemNbt.Slot = compound.Get<NbtByte>("Slot").Value;
                        //slot.ItemNbt.Count = compound.Get<NbtByte>("Count").Value;
                        //slot.ItemNbt.Id = compound.Get<NbtShort>("id").Value;
                        //slot.ItemNbt.Damage = compound.Get<NbtShort>("Damage").Value;
                        //slot.ItemNbt.RepairCost = compound.Get<NbtInt>("RepairCost").Value;
                    }

                    slot.ItemMeta = itemMetaBuilder.Build();
                }

                return(slot);
            }

            return(null);
        }
Пример #4
0
    public static ItemStack?ItemFromNbt(this NbtCompound?item)
    {
        if (item is null)
        {
            return(null);
        }

        var itemStack = Registry.Registry.GetSingleItem(item.GetString("id"));

        var itemMetaBuilder = new ItemMetaBuilder();

        foreach (var(name, child) in item)
        {
            switch (name.ToUpperInvariant())
            {
            case "ENCHANTMENTS":
            {
                var enchantments = (NbtList)child;

                foreach (var enchant in enchantments)
                {
                    if (enchant is NbtCompound compound)
                    {
                        itemMetaBuilder.AddEnchantment(compound.GetString("id").ToEnchantType(), compound.GetShort("lvl"));
                    }
                }

                break;
            }

            case "STOREDENCHANTMENTS":
            {
                var enchantments = (NbtList)child;

                foreach (var enchantment in enchantments)
                {
                    if (enchantment is NbtCompound compound)
                    {
                        compound.TryGetTag("id", out var id);
                        compound.TryGetTag("lvl", out var lvl);

                        itemMetaBuilder.AddStoredEnchantment(compound.GetString("id").ToEnchantType(), compound.GetShort("lvl"));
                    }
                }
                break;
            }

            case "SLOT":
            {
                var byteTag = (NbtTag <byte>)child;

                itemStack.Slot = byteTag.Value;
                break;
            }

            case "DAMAGE":
            {
                var intTag = (NbtTag <int>)child;

                itemMetaBuilder.WithDurability(intTag.Value);
                break;
            }

            case "DISPLAY":
            {
                var display = (NbtCompound)child;

                foreach (var(displayTagName, displayTag) in display)
                {
                    if (displayTagName.EqualsIgnoreCase("name") && displayTag is NbtTag <string> stringTag)
                    {
                        itemMetaBuilder.WithName(stringTag.Value);
                    }
                    else if (displayTag.Name.EqualsIgnoreCase("lore"))
                    {
                        var loreTag = (NbtList)displayTag;

                        foreach (NbtTag <string> lore in loreTag)
                        {
                            itemMetaBuilder.AddLore(lore.Value.FromJson <ChatMessage>());
                        }
                    }
                }
                break;
            }
            }
        }

        itemStack.ItemMeta = itemMetaBuilder.Build();

        return(itemStack);
    }
Пример #5
0
        public ItemStack ReadItemStack()
        {
            var present = ReadBoolean();

            if (present)
            {
                var item = Registry.GetItem((short)ReadVarInt());

                var slot = new ItemStack(item.Type, ReadUnsignedByte())
                {
                    Present = present
                };

                var reader = new NbtReader(this);

                INbtTag tag;
                while ((tag = reader.ReadNextTag()) != null)
                {
                    var itemMetaBuilder = new ItemMetaBuilder();

                    if (tag is NbtCompound root)
                    {
                        foreach (var(name, child) in root)
                        {
                            switch (name.ToUpperInvariant())
                            {
                            case "ENCHANTMENTS":
                            {
                                var enchantments = (NbtList)child;

                                foreach (var enchant in enchantments)
                                {
                                    if (enchant is NbtCompound compound)
                                    {
                                        itemMetaBuilder.AddEnchantment(compound.GetString("id").ToEnchantType(), compound.GetShort("lvl"));
                                    }
                                }

                                break;
                            }

                            case "STOREDENCHANTMENTS":
                            {
                                var enchantments = (NbtList)child;

                                //Globals.PacketLogger.LogDebug($"List Type: {enchantments.ListType}");

                                foreach (var enchantment in enchantments)
                                {
                                    if (enchantment is NbtCompound compound)
                                    {
                                        compound.TryGetTag("id", out var id);
                                        compound.TryGetTag("lvl", out var lvl);

                                        itemMetaBuilder.AddStoredEnchantment(compound.GetString("id").ToEnchantType(), compound.GetShort("lvl"));
                                    }
                                }
                                break;
                            }

                            case "SLOT":
                            {
                                var byteTag = (NbtTag <byte>)child;

                                itemMetaBuilder.WithSlot(byteTag.Value);
                                //Console.WriteLine($"Setting slot: {itemMetaBuilder.Slot}");
                                break;
                            }

                            case "DAMAGE":
                            {
                                var intTag = (NbtTag <int>)child;

                                itemMetaBuilder.WithDurability(intTag.Value);
                                //Globals.PacketLogger.LogDebug($"Setting damage: {tag.IntValue}");
                                break;
                            }

                            case "DISPLAY":
                            {
                                var display = (NbtCompound)child;

                                foreach (var(displayTagName, displayTag) in display)
                                {
                                    if (displayTagName.EqualsIgnoreCase("name") && displayTag is NbtTag <string> stringTag)
                                    {
                                        itemMetaBuilder.WithName(stringTag.Value);
                                    }
                                    else if (displayTag.Name.EqualsIgnoreCase("lore"))
                                    {
                                        var loreTag = (NbtList)displayTag;

                                        foreach (NbtTag <string> lore in loreTag)
                                        {
                                            itemMetaBuilder.AddLore(lore.Value.FromJson <ChatMessage>());
                                        }
                                    }
                                }
                                break;
                            }
                            }
                        }
                    }

                    slot.ItemMeta = itemMetaBuilder.Build();
                }

                return(slot);
            }

            return(null);
        }