Example #1
0
 private static TerrariaChest Adapt(Chest chest) =>
 new TerrariaChest
 {
     x    = chest.Position.X,
     y    = chest.Position.Y,
     name = chest.Name,
     item = chest.Items.Select(Adapt).ToArray()
 };
Example #2
0
        private bool AddChest(Chest chest)
        {
            var index = -1;

            lock (_chestLock)
            {
                for (var i = 0; i < _terrariaChests.Length; ++i)
                {
                    var terrariaChest = _terrariaChests[i];
                    if (terrariaChest == null)
                    {
                        index = i;
                        break;
                    }

                    var type = _terrariaTiles[terrariaChest.x, terrariaChest.y].type;
                    if (type != TileID.Containers && type != TileID.Dressers && type != TileID.Containers2)
                    {
                        index = i;
                        break;
                    }
                }
                if (index < 0)
                {
                    return(false);
                }

                _terrariaChests[index] = Adapt(chest);
            }
            var x      = chest.Position.X;
            var y      = chest.Position.Y;
            var tile   = _terrariaTiles[x, y];
            var frameX = tile.frameX;
            var type2  = tile.type;

            if (type2 == TileID.Containers)
            {
                NetMessage.SendData(34, -1, -1, null, 0, x, y, (short)(frameX / 36), index);
            }
            else if (type2 == TileID.Dressers)
            {
                NetMessage.SendData(34, -1, -1, null, 2, x, y, (short)((frameX - 18) / 54), index);
            }
            else if (type2 == TileID.Containers2)
            {
                NetMessage.SendData(34, -1, -1, null, 4, x, y, (short)(frameX / 36), index);
            }
            return(true);
        }
Example #3
0
        private bool RemoveChest(Chest chest)
        {
            var index = -1;

            lock (_chestLock)
            {
                for (var i = _terrariaChests.Length - 1; i >= 0; --i)
                {
                    var terrariaChest = _terrariaChests[i];
                    if (terrariaChest != null && terrariaChest.x == chest.Position.X &&
                        terrariaChest.y == chest.Position.Y)
                    {
                        index = i;
                        break;
                    }
                }
                if (index < 0)
                {
                    return(false);
                }

                _terrariaChests[index] = null;
            }
            var x    = chest.Position.X;
            var y    = chest.Position.Y;
            var type = _terrariaTiles[x, y].type;

            if (type == TileID.Containers)
            {
                NetMessage.SendData(34, -1, -1, null, 1, x, y, 0, index);
            }
            else if (type == TileID.Dressers)
            {
                NetMessage.SendData(34, -1, -1, null, 3, x, y, 0, index);
            }
            else if (type == TileID.Containers2)
            {
                NetMessage.SendData(34, -1, -1, null, 5, x, y, 0, index);
            }
            return(true);
        }
        public void WriteRead()
        {
            var tiles = new Tile?[10, 10];

            Main.tileFrameImportant[1] = true;
            var tile = new Tile {
                IsActive = true, BlockColor = 1, WallColor = 1, BlockId = 1, WallId = 1, Liquid = 255
            };

            for (var x = 0; x < 10; ++x)
            {
                for (var y = 0; y < 10; ++y)
                {
                    tiles[x, y] = tile;
                }
            }
            var clipboard = new Clipboard(tiles);
            var items     = new Item[40];

            items[0] = new Item(1, 2, 3);
            var chest         = new Chest(new Vector(0, 1), "Test", new Item[40]);
            var itemFrame     = new ItemFrame(new Vector(3, 4), new Item(1, 2, 3));
            var logicSensor   = new LogicSensor(new Vector(2, 5), LogicSensorType.Day, true, 5);
            var sign          = new Sign(new Vector(1, 0), "Test2");
            var trainingDummy = new TrainingDummy(new Vector(4, 5), 21);

            clipboard.AddTileEntity(chest);
            clipboard.AddTileEntity(itemFrame);
            clipboard.AddTileEntity(logicSensor);
            clipboard.AddTileEntity(sign);
            clipboard.AddTileEntity(trainingDummy);
            var schematicFormat = new DefaultSchematicFormat();

            Clipboard clipboard2;

            using (var stream = new MemoryStream())
            {
                schematicFormat.Write(clipboard, stream);
                stream.Position = 0;
                clipboard2      = schematicFormat.Read(stream);

                Assert.That(clipboard2, Is.Not.Null);
            }

            for (var x = 0; x < 10; ++x)
            {
                for (var y = 0; y < 10; ++y)
                {
                    Assert.That(clipboard2.GetTile(new Vector(x, y)), Is.EqualTo(tile));
                }
            }
            foreach (var entity in clipboard2.GetTileEntities())
            {
                if (entity is Chest chest2)
                {
                    Assert.That(chest2.Position, Is.EqualTo(chest.Position));
                    Assert.That(chest2.Name, Is.EqualTo(chest.Name));
                    Assert.That(chest2.Items, Is.EquivalentTo(chest2.Items));
                }
                else if (entity is ItemFrame itemFrame2)
                {
                    Assert.That(itemFrame2.Position, Is.EqualTo(itemFrame.Position));
                    Assert.That(itemFrame2.Item, Is.EqualTo(itemFrame.Item));
                }
                else if (entity is LogicSensor logicSensor2)
                {
                    Assert.That(logicSensor2.Position, Is.EqualTo(logicSensor.Position));
                    Assert.That(logicSensor2.Type, Is.EqualTo(logicSensor.Type));
                    Assert.That(logicSensor2.IsEnabled, Is.EqualTo(logicSensor.IsEnabled));
                    Assert.That(logicSensor2.Data, Is.EqualTo(logicSensor.Data));
                }
                else if (entity is Sign sign2)
                {
                    Assert.That(sign2.Position, Is.EqualTo(sign.Position));
                    Assert.That(sign2.Text, Is.EqualTo(sign.Text));
                }
                else if (entity is TrainingDummy trainingDummy2)
                {
                    Assert.That(trainingDummy2.Position, Is.EqualTo(trainingDummy.Position));
                    Assert.That(trainingDummy2.NpcIndex, Is.EqualTo(trainingDummy.NpcIndex));
                }
            }
        }