public static void InitTestBlock(Player p)
        {
            CustomBlock block = new CustomBlock
            {
                Name          = "TestingBlock",
                ID            = 233,
                Solidity      = CustomBlockSolidity.Solid,
                MovementSpeed = 2,
                Texture       = new CustomBlockTexture
                {
                    SideID   = 1,
                    BottomID = 5,
                    TopID    = 1
                },
                TransmitsLight = false,
                WalkSound      = CustomBlocks.WalkSound.Stone,
                FullBright     = true,
                Shape          = 16,
                FogDensity     = 0,
                FogB           = 255,
                FogR           = 255,
                FogG           = 255
            };

            p.Message("Initing custom block " + block.Name);
            Packet pcket = PacketWriter.MakeDefineBlock(block);

            p.Send(pcket);
            var writer = File.CreateText("Test.gcblock");

            writer.Write(JsonConvert.SerializeObject(block, Formatting.Indented));
            writer.Flush();
            writer.Close();
        }
        private static bool CheckForClashes(IEnumerable <CustomBlock> blocks, CustomBlock block)
        {
            if (blocks.Any(b => b.ID == block.ID))
            {
                return(true);
            }

            return(block.ID <= 84 || block.ID == 86 || (block.ID >= 240 && block.ID <= 249));
        }
 public CustomBlockException(string message, Exception inner, CustomBlock block) : base(message, inner)
 {
     Block = block;
 }
 public CustomBlockException(string message, CustomBlock block) : base(message)
 {
     Block = block;
 }
        public static List <CustomBlock> LoadBlocks()
        {
            Logger.Log(LogType.SystemActivity, "Loading Custom Blocks...");
            List <CustomBlock> blocks = new List <CustomBlock>();

            if (!Directory.Exists(Dir))
            {
                Logger.Log(LogType.Warning, "Custom Blocks Folder does not exist. Creating it...");
                Directory.CreateDirectory(Dir);
                return(blocks);
            }

            string[] files = Directory.GetFiles(Dir);
            if (files.Length == 0)
            {
                return(blocks);
            }
            foreach (string file in files)
            {
                if (!Path.HasExtension(file) || Path.GetExtension(file) != ".gcblock")
                {
                    continue;
                }
                try
                {
                    int    x   = new Random().Next();
                    string dir = $"Custom Blocks/Temp{x}/";
                    if (!Unzip(file, x, out Exception ex))
                    {
                        throw new CustomBlockException("Failure to load gcblock", ex);
                    }
                    string tempOTemp = $"{dir}block.json";
                    if (!File.Exists(tempOTemp))
                    {
                        throw new CustomBlockException($"Malformed custom block file: {file}. JSON file does not exist.");
                    }

                    CustomBlock block         = JsonConvert.DeserializeObject <CustomBlock>(File.ReadAllText(tempOTemp));
                    string      bottomTexture = dir + block.Texture.BottomFilePath;
                    string      sideTexture   = dir + block.Texture.SideFilePath;
                    string      topTexture    = dir + block.Texture.TopFilePath;

                    if (!File.Exists(bottomTexture) || !File.Exists(sideTexture) || !File.Exists(topTexture))
                    {
                        throw new CustomBlockException(
                                  $"Malformed custom block file: {file}. One ore more textures are missing.");
                    }
                    // Ensures there are not block ID clashes
                    if (CheckForClashes(blocks, block))
                    {
                        throw new CustomBlockException("Block with same id already exists", block);
                    }
                    // Loads the block texture images
                    Image bottomImg = Image.FromFile(bottomTexture);
                    Image sideImg   = Image.FromFile(sideTexture);
                    Image topImg    = Image.FromFile(topTexture);

                    List <Image> imagesNeeded = new List <Image>();
                    int          whichOne     = -1;

                    // Compares images, chcks to see if they are the same
                    if (CompareImg(bottomImg, sideImg) && CompareImg(bottomImg, topImg))
                    {
                        imagesNeeded.Add(bottomImg);
                        whichOne = 4;
                    }
                    else if (CompareImg(bottomImg, sideImg))
                    {
                        imagesNeeded.Add(bottomImg);
                        imagesNeeded.Add(topImg);
                        whichOne = 0;
                    }
                    else if (CompareImg(sideImg, topImg))
                    {
                        imagesNeeded.Add(sideImg);
                        imagesNeeded.Add(bottomImg);
                        whichOne = 1;
                    }
                    else if (CompareImg(bottomImg, topImg))
                    {
                        imagesNeeded.Add(bottomImg);
                        imagesNeeded.Add(sideImg);
                        whichOne = 2;
                    }
                    else // All images are different
                    {
                        imagesNeeded.Add(bottomImg);
                        imagesNeeded.Add(sideImg);
                        imagesNeeded.Add(topImg);
                        whichOne = 3;
                    }

                    List <ImageGeneratorData> data = (from img in imagesNeeded
                                                      let spot = DetermineNextInt()
                                                                 select new ImageGeneratorData
                    {
                        Spot = spot, Image = img
                    }).ToList();

                    switch (whichOne) // Sets terrain.png ID's of textures based on which images are the same and which are not
                    {
                    case 0:           // Cases 0 through 2 only 2 of the images are the same
                        block.Texture.BottomID = (byte)data[0].Spot;
                        block.Texture.SideID   = (byte)data[0].Spot;
                        block.Texture.TopID    = (byte)data[1].Spot;
                        break;

                    case 1:
                        block.Texture.BottomID = (byte)data[1].Spot;
                        block.Texture.SideID   = (byte)data[0].Spot;
                        block.Texture.TopID    = (byte)data[0].Spot;
                        break;

                    case 2:
                        block.Texture.BottomID = (byte)data[0].Spot;
                        block.Texture.SideID   = (byte)data[1].Spot;
                        block.Texture.TopID    = (byte)data[0].Spot;
                        break;

                    case 3:     // This case all images are different
                        block.Texture.BottomID = (byte)data[0].Spot;
                        block.Texture.SideID   = (byte)data[1].Spot;
                        block.Texture.TopID    = (byte)data[2].Spot;
                        break;

                    case 4:     // This case all images are the same
                        block.Texture.BottomID = (byte)data[0].Spot;
                        block.Texture.SideID   = (byte)data[0].Spot;
                        block.Texture.TopID    = (byte)data[0].Spot;
                        break;
                    }

                    TerrainGenerator.Generate(data);                 // Saves to output_terrain.png
                    Server.TexturePack.Terrain = Image.FromFile(TerrainGenerator.Output);
                    if (!Server.TexturePack.Upload(out Exception e)) // Sends to GemsCraft server and sends url to classicube
                    {
                        throw new CustomBlockException("Unable to upload texture pack with custom blocks added", e);
                    }

                    blocks.Add(block);
                }
                catch (JsonReaderException e)
                {
                    Logger.Log(LogType.Error, $"Malformed custom block file: {file}.");
                    Console.WriteLine(e);
                }
            }
            return(blocks);
        }