Exemplo n.º 1
0
        public Entity(World world, CompoundTag tag)
        {
            this.EntityID = ++Entity.nextEntityId;

            this.SetDataProperty(new EntityDataLong(Entity.DATA_FLAGS, 0));
            this.SetDataProperty(new EntityDataShort(Entity.DATA_AIR, 400));
            this.SetDataProperty(new EntityDataShort(Entity.DATA_MAX_AIR, 400));
            this.SetDataProperty(new EntityDataString(Entity.DATA_NAMETAG, ""));
            this.SetDataProperty(new EntityDataLong(Entity.DATA_LEAD_HOLDER_EID, -1));
            this.SetDataProperty(new EntityDataFloat(Entity.DATA_SCALE, 1.0f));
            this.SetDataProperty(new EntityDataFloat(Entity.DATA_BOUNDING_BOX_WIDTH, this.WIDTH));
            this.SetDataProperty(new EntityDataFloat(Entity.DATA_BOUNDING_BOX_HEIGHT, this.HEIGHT));

            this.SetFlag(Entity.DATA_FLAGS, Entity.DATA_FLAG_HAS_COLLISION);
            this.SetFlag(Entity.DATA_FLAGS, Entity.DATA_FLAG_AFFECTED_BY_GRAVITY);

            if (!this.IsPlayer)
            {
                ListTag pos = tag.GetList("Pos");
                this.Vector3 = new Vector3(
                    pos.GetTag <FloatTag>(0).Data,
                    pos.GetTag <FloatTag>(1).Data,
                    pos.GetTag <FloatTag>(2).Data);
                ListTag rotation = tag.GetList("Rotation");
                this.Yaw   = pos.GetTag <FloatTag>(0).Data;
                this.Pitch = pos.GetTag <FloatTag>(1).Data;

                this.World = world;
                this.World.AddEntity(this);
            }

            this.EntityInit();
        }
Exemplo n.º 2
0
        public static Item ReadItem(CompoundTag nbt)
        {
            Item item = Item.Get(nbt.GetShort("id"), nbt.GetShort("damage"), nbt.GetByte("count"));

            if (nbt.Exist("tag"))
            {
                CompoundTag tag = (CompoundTag)nbt.GetCompound("tag").Clone();
                tag.Name = "";
                item.SetNamedTag(tag);
            }
            if (nbt.Exist("CanPlaceOn"))
            {
                ListTag list = nbt.GetList("CanPlaceOn");
                for (int i = 0; i < list.Count; ++i)
                {
                    item.AddCanPlaceOn(((StringTag)list[i]).Data);
                }
            }
            if (nbt.Exist("CanDestroy"))
            {
                ListTag list = nbt.GetList("CanDestroy");
                for (int i = 0; i < list.Count; ++i)
                {
                    item.AddCanDestroy(((StringTag)list[i]).Data);
                }
            }
            return(item);
        }
Exemplo n.º 3
0
        /// <summary>
        /// <see cref="Entity"/> の初期化をします。
        /// </summary>
        /// <param name="nbt"><see cref="Entity"/> のNBTデータ</param>
        protected virtual void EntityInit(CompoundTag nbt)
        {
            ListTag list = nbt.GetList("Pos");

            this.X       = ((FloatTag)list[0]).Data;
            this.Y       = ((FloatTag)list[1]).Data;
            this.Z       = ((FloatTag)list[2]).Data;
            list         = nbt.GetList("Motion");
            this.MotionX = ((FloatTag)list[0]).Data;
            this.MotionY = ((FloatTag)list[1]).Data;
            this.MotionZ = ((FloatTag)list[2]).Data;
            list         = nbt.GetList("Rotation");
            this.Yaw     = ((FloatTag)list[0]).Data;
            this.Pitch   = ((FloatTag)list[1]).Data;

            this.ResetLastMovements();
            this.RecalculateBoundingBox();

            this.DataProperties = new EntityMetadataManager(this.EntityID);
            this.SetDataProperty(new EntityDataLong(DATA_FLAGS, 0));
            this.SetDataProperty(new EntityDataShort(DATA_AIR, 400));
            this.SetDataProperty(new EntityDataShort(DATA_MAX_AIR, 400));
            this.SetDataProperty(new EntityDataString(DATA_NAMETAG, ""));
            this.SetDataProperty(new EntityDataLong(DATA_LEAD_HOLDER_EID, -1));
            this.SetDataProperty(new EntityDataFloat(DATA_SCALE, 1.0f));
            this.SetDataProperty(new EntityDataFloat(DATA_BOUNDING_BOX_WIDTH, this.Width));
            this.SetDataProperty(new EntityDataFloat(DATA_BOUNDING_BOX_HEIGHT, this.Height));

            this.SetFlag(DATA_FLAGS, DATA_FLAG_HAS_COLLISION);
            this.SetFlag(DATA_FLAGS, DATA_FLAG_GRAVITY);

            this.Attributes = new EntityAttributeDictionary(this.EntityID);
        }
Exemplo n.º 4
0
        public Chunk NBTDeserialize(CompoundTag tag)
        {
            CompoundTag level = (CompoundTag)tag["Level"];
            int         x     = level.GetInt("xPos");
            int         z     = level.GetInt("zPos");

            SubChunk[] subChunks = ArrayUtils.CreateArray <SubChunk>(16);
            ListTag    sections  = level.GetList("Sections");

            for (int i = 0; i < sections.Count; ++i)
            {
                CompoundTag section  = ((CompoundTag)sections[i]);
                SubChunk    subChunk = new SubChunk();
                byte        y        = section.GetByte("Y");
                subChunk.BlockDatas  = section.GetByteArray("Blocks");
                subChunk.MetaDatas   = new NibbleArray(section.GetByteArray("Data"));
                subChunk.SkyLights   = new NibbleArray(section.GetByteArray("SkyLight"));
                subChunk.BlockLigths = new NibbleArray(section.GetByteArray("BlockLight"));
                subChunks[y]         = subChunk;
            }

            byte[]  biomes    = level.GetByteArray("Biomes");
            short[] cast      = new short[256];
            int[]   heightMap = level.GetIntArray("HeightMap");
            heightMap.CopyTo(cast, 0);

            Chunk chunk = new Chunk(x, z, subChunks, biomes, cast, level.GetList("Entities"), level.GetList("TileEntities"));

            chunk.LastUpdate       = level.GetLong("LastUpdate");
            chunk.InhabitedTime    = level.GetLong("InhabitedTime");
            chunk.LightPopulated   = level.GetByte("LightPopulated") == 1;
            chunk.TerrainPopulated = level.GetByte("TerrainPopulated") == 1;

            return(chunk);
        }
Exemplo n.º 5
0
        public override Chunk DeserializeChunk(CompoundTag tag)
        {
            CompoundTag level = tag.GetCompound("").GetCompound("Level");

            int x = level.GetInt("xPos");
            int z = level.GetInt("zPos");

            byte[]  biomes    = level.GetByteArray("Biomes");
            short[] cast      = new short[256];
            int[]   heightMap = level.GetIntArray("HeightMap");
            for (int i = 0; i < 256; ++i)
            {
                cast[i] = (short)heightMap[i];
            }

            ListTag sections = level.GetList("Sections");

            SubChunk[] subChunks = new SubChunk[16];
            for (int i = 0; i < sections.Count; ++i)
            {
                CompoundTag section  = (CompoundTag)sections[i];
                SubChunk    subChunk = new SubChunk();
                byte        y        = section.GetByte("Y");
                byte[]      bytes    = section.GetByteArray("Blocks");
                int[]       blocks   = new int[4096];
                for (int j = 0; j < 4096; ++j)
                {
                    blocks[j] = bytes[j];
                }
                subChunk.BlockDatas = blocks;
                subChunk.MetaDatas  = new NibbleArray(section.GetByteArray("Data"));
                subChunk.BlockLight = section.GetByteArray("BlockLight");
                subChunk.SkyLight   = section.GetByteArray("SkyLight");
                subChunks[y]        = subChunk;
            }

            Chunk chunk = new Chunk(x, z, subChunks, level.GetList("Entities"), level.GetList("TileEntities"))
            {
                LastUpdate       = level.GetLong("LastUpdate"),
                LightPopulated   = level.GetByte("LightPopulated"),
                TerrainPopulated = level.GetByte("TerrainPopulated"),
                V             = level.GetByte("V"),
                InhabitedTime = level.GetLong("InhabitedTime"),
                Biomes        = biomes,
                HeightMap     = cast
            };

            return(chunk);
        }
Exemplo n.º 6
0
        public string[] GetLore()
        {
            if (!this.HasTags)
            {
                return(new string[0]);
            }

            CompoundTag tag = this.GetNamedTag();

            if (tag.Exist("display"))
            {
                return(new string[0]);
            }
            CompoundTag display = tag.GetCompound("display");

            if (!display.Exist("Lore"))
            {
                return(new string[0]);
            }
            ListTag lores = display.GetList("Lore");

            string[] data = new string[lores.Count];
            for (int i = 0; i < lores.Count; ++i)
            {
                data[i] = ((StringTag)lores[i]).Data;
            }
            return(data);
        }
Exemplo n.º 7
0
        public Item AddEnchantment(Enchantment enchantment)
        {
            CompoundTag tag = this.NamedTag;
            ListTag     list;

            if (tag.Exist("ench"))
            {
                list = tag.GetList("ench");
            }
            else
            {
                list = new ListTag("ench", NBTTagType.COMPOUND);
                tag.PutList(list);
            }
            for (int i = 0; i < list.Count; ++i)
            {
                if (((CompoundTag)list[i]).GetShort("id") == enchantment.ID)
                {
                    list[i] = new CompoundTag()
                              .PutShort("id", (short)enchantment.ID)
                              .PutShort("lvl", (short)enchantment.Level);
                    this.NamedTag = tag;
                    return(this);
                }
            }
            CompoundTag ench = new CompoundTag()
                               .PutShort("id", (short)enchantment.ID)
                               .PutShort("lvl", (short)enchantment.Level);

            list.Add(ench);
            this.NamedTag = tag;
            return(this);
        }
Exemplo n.º 8
0
        public void NBTIOTests_ReadGZFileTest()
        {
            CompoundTag tag = NBTIO.ReadGZIPFile(Environment.CurrentDirectory + "\\test2.nbt");

            Console.WriteLine(tag);
            Console.WriteLine(tag.GetList("list")[0]);
        }
        private void ConvertChunkData(ChunkData data)
        {
            CompoundTag oldTag = data.Data.GetCompound("").GetCompound("Level");
            CompoundTag newTag = (CompoundTag)oldTag.Clone();

            ListTag sections    = oldTag.GetList("Sections");
            ListTag newSections = ConvertSections(sections);

            if (newSections != null)
            {
                newTag.Remove("Sections");
                newTag.PutList(newSections);

                List <byte> biomes = new List <byte>();
                foreach (int b in oldTag.GetIntArray("Biomes"))
                {
                    biomes.Add((byte)b);
                }
                newTag.Remove("Biomes");
                newTag.PutByteArray("Biomes", biomes.ToArray());
            }

            CheckCancel();

            if (newTag.Exist("Heightmaps"))
            {
                newTag.Remove("Heightmaps");
            }
            else
            {
                newTag.Remove("HeightMap");
            }

            int[] map = new int[256];
            for (int i = 0; i < 256; i++)
            {
                map[i] = 0xff;
            }
            newTag.PutIntArray("HeightMap", map);

            newTag.PutList(new ListTag("TileTicks", NBTTagType.COMPOUND));

            newTag.PutList(new ListTag("Entities", NBTTagType.COMPOUND));
            newTag.PutList(new ListTag("TileEntities", NBTTagType.COMPOUND));

            newTag.PutBool("TerrainPopulated", true);
            newTag.PutBool("TerrainGenerated", true);

            newTag.Remove("CarvingMasks");
            newTag.Remove("Structures");

            data.Data = new CompoundTag("").PutCompound("Level", newTag);
        }
Exemplo n.º 10
0
        public void NBTIOTests_ReadRawFileTest()
        {
            CompoundTag tag = NBTIO.ReadRawFile(Environment.CurrentDirectory + "\\test.nbt");

            tag.GetBool("bool");
            tag.GetByte("byte");
            tag.GetByteArray("byteArray");
            tag.GetShort("short");
            tag.GetInt("int");
            tag.GetIntArray("intArray");
            tag.GetLong("long");
            tag.GetLongArray("longArray");
            tag.GetFloat("float");
            tag.GetDouble("double");
            tag.GetList("list");
            tag.GetCompound("com");
            Console.WriteLine(tag);
        }
Exemplo n.º 11
0
        public Enchantment GetEnchantment(int id)
        {
            if (!this.HasEnchantment(id))
            {
                return(null);
            }
            CompoundTag tag  = this.GetNamedTag();
            ListTag     list = tag.GetList("ench");

            for (int i = 0; i < list.Count; ++i)
            {
                CompoundTag ench = (CompoundTag)list[i];
                if (ench.GetShort("id") == id)
                {
                    return(Enchantment.GetEnchantment(id, ench.GetShort("lvl")));
                }
            }
            return(null);
        }
Exemplo n.º 12
0
        public Enchantment[] GetEnchantments()
        {
            CompoundTag tag = this.NamedTag;

            if (!tag.Exist("ench"))
            {
                return(new Enchantment[0]);
            }

            ListTag            list   = tag.GetList("ench");
            List <Enchantment> enches = new List <Enchantment>();

            for (int i = 0; i < list.Count; ++i)
            {
                CompoundTag ench = (CompoundTag)list[i];
                enches.Add(Enchantment.GetEnchantment(ench.GetShort("id"), ench.GetShort("lvl")));
            }
            return(enches.ToArray());
        }
Exemplo n.º 13
0
        public virtual void LoadNBT(CompoundTag nbt)
        {
            if (!nbt.Exist(this.Name))
            {
                ListTag list = new ListTag(this.Name, NBTTagType.COMPOUND);
                for (int i = 0; i < this.Size; ++i)
                {
                    list.Add(NBTIO.WriteItem(Item.Get(0, 0, 0)));
                }
                nbt.PutList(list);
            }

            ListTag items = nbt.GetList(this.Name);

            for (int i = 0; i < this.Size; ++i)
            {
                Item item = NBTIO.ReadItem((CompoundTag)items[i]);
                this.SetItem(i, item, false);
            }
        }
Exemplo n.º 14
0
        public Item RemoveEnchantment(Enchantment enchantment)
        {
            CompoundTag tag = this.NamedTag;

            if (!tag.Exist("ench"))
            {
                return(this);
            }
            ListTag list = tag.GetList("ench");

            for (int i = 0; i < list.Count; ++i)
            {
                if (((CompoundTag)list[i]).GetShort("id") == enchantment.ID)
                {
                    list.RemoveAt(i);
                }
            }
            this.NamedTag = tag;
            return(this);
        }
Exemplo n.º 15
0
        public bool HasEnchantment(int id)
        {
            CompoundTag tag = this.NamedTag;

            if (!tag.Exist("ench"))
            {
                return(false);
            }

            ListTag list = tag.GetList("ench");

            for (int i = 0; i < list.Count; ++i)
            {
                CompoundTag ench = (CompoundTag)list[i];
                if (ench.GetShort("id") == id)
                {
                    return(true);
                }
            }
            return(false);
        }
        private CompoundTag ConvertSection(Tag sectionTag)
        {
            byte[]      blockData = new byte[4096];
            NibbleArray metaData  = new NibbleArray(4096);

            CompoundTag section = (CompoundTag)sectionTag;

            long[]  states  = section.GetLongArray("BlockStates");
            ListTag palette = section.GetList("Palette");
            List <RuntimeTable.Table> indexs = new List <RuntimeTable.Table>();

            foreach (Tag paletteTag in palette.Tags)
            {
                if (paletteTag is CompoundTag)
                {
                    CompoundTag pt   = (CompoundTag)paletteTag;
                    string      name = pt.GetString("Name");
                    indexs.Add(RuntimeTable.GetNameToTable(name, pt.GetCompound("Properties").Tags));
                }
            }

            int         bits      = CheckMostBit(indexs.Count - 1);
            List <byte> fixStates = new List <byte>();

            foreach (long state in states)
            {
                fixStates.AddRange(BitConverter.GetBytes((ulong)state));
            }

            BitArray stateBits = new BitArray(fixStates.ToArray());

            for (int i = 0; i < 4096; i++)
            {
                int  bitOffset = i * bits;
                uint index     = stateBits.Get(bitOffset + bits - 1) ? 1u : 0u;
                for (int j = bits - 2; j >= 0; j--)
                {
                    index <<= 1;
                    index  |= stateBits.Get(bitOffset + j) ? 1u : 0u;
                }

                try
                {
                    RuntimeTable.Table table = indexs[(int)index];
                    blockData[i] = (byte)(table.Id & 0xff);
                    metaData[i]  = (byte)(table.Data & 0xf);
                }
                catch (Exception e)
                {
                    Logger.Info(indexs.Count + " = " + states[0] + " >>> " + states[1]);
                    throw e;
                }
            }

            var newSection = (CompoundTag)section.Clone();

            newSection.Remove("BlockStates");
            newSection.Remove("Palette");

            newSection.PutByteArray("Blocks", blockData);
            newSection.PutByteArray("Data", metaData.ArrayData);

            return(newSection);
        }