Esempio n. 1
0
        public override void PlaceBlock(Level world, MiNET.Player player, BlockCoordinates blockCoordinates, BlockFace face, Vector3 faceCoords)
        {
            var coor = GetNewCoordinatesFromFace(blockCoordinates, face);

            ItemFrameBlockEntity itemFrameBlockEntity = new ItemFrameBlockEntity
            {
                Coordinates = coor
            };

            FullyLuminousItemFrame itemFrame = new FullyLuminousItemFrame(_frame, itemFrameBlockEntity, world)
            {
                Coordinates = coor,
            };

            if (!itemFrame.CanPlace(world, blockCoordinates, face))
            {
                return;
            }

            itemFrame.PlaceBlock(world, player, coor, face, faceCoords);

            // Then we create and set the block entity that has all the intersting data

            world.SetBlockEntity(itemFrameBlockEntity);
        }
Esempio n. 2
0
        /**
         * Credit to @gurun, as what is below is based on his work.
         */
        public static List <Entity> SpawnMapImage(string imageLocation, int width, int height, Level level, BlockCoordinates spawnLocation, MapDirection mapDirection = MapDirection.South)
        {
            var spawnedEntities = new List <Entity>();

            try
            {
                Bitmap    image;
                CachedMap cachedMap;

                if (CachedMaps.ContainsKey(imageLocation))
                {
                    cachedMap = CachedMaps[imageLocation];
                    image     = cachedMap.CachedImage;

                    //Dodgily ensure the building flag is disabled
                    cachedMap.IsBuilding = false;
                    //SkyUtil.log($"Using Cached Image for {imageLocation}");
                }
                else
                {
                    if (!File.Exists(imageLocation))
                    {
                        SkyUtil.log($"File doesn't exist for Map ({imageLocation})");
                        return(spawnedEntities);
                    }

                    image     = new Bitmap((Bitmap)Image.FromFile(imageLocation), width * 128, height * 128);
                    cachedMap = new CachedMap(image);

                    //SkyUtil.log($"Loading Image for {imageLocation}");
                }

                BlockCoordinates center = spawnLocation;

                for (int x = 0; x < width; x++)
                {
                    int xSpawnLoc = center.X + x;
                    for (int y = 0; y < height; y++)
                    {
                        byte[] bitmapToBytes;
                        if (cachedMap.IsBuilding)
                        {
                            var croppedImage = CropImage(image, new Rectangle(new Point(x * 128, y * 128), new Size(128, 128)));
                            bitmapToBytes = BitmapToBytes(croppedImage, true);

                            if (bitmapToBytes.Length != 128 * 128 * 4)
                            {
                                return(spawnedEntities);                                //TODO: Throw Exception/Alert Log?
                            }

                            cachedMap.CachedBitmaps.Add(new Tuple <int, int>(x, y), bitmapToBytes);
                        }
                        else
                        {
                            bitmapToBytes = cachedMap.CachedBitmaps[new Tuple <int, int>(x, y)];
                        }

                        MapEntity frame = new MapEntity(level);
                        frame.ImageProvider = new MapImageProvider {
                            Batch = CreateCachedPacket(frame.EntityId, bitmapToBytes)
                        };
                        frame.SpawnEntity();

                        AddMapIdToDictionary(level is GameLevel gameLevel ? gameLevel.GameId : level.LevelId, frame.EntityId);

                        BlockCoordinates     frambc = new BlockCoordinates(xSpawnLoc, center.Y + height - y - 2, center.Z);
                        ItemFrameBlockEntity itemFrameBlockEntity = new ItemFrameBlockEntity
                        {
                            Coordinates = frambc
                        };

                        var itemFrame = new FullyLuminousItemFrame(frame, itemFrameBlockEntity, level)
                        {
                            Coordinates = frambc,
                            Metadata    = (byte)mapDirection,
                        };
                        level.SetBlock(itemFrame, true, false);
                        level.SetBlockEntity(itemFrameBlockEntity);

                        spawnedEntities.Add(frame);
                    }
                }

                if (cachedMap.IsBuilding)
                {
                    CachedMaps.TryAdd(imageLocation, cachedMap);
                    cachedMap.IsBuilding = false;                     //Completely Cached
                }
            }
            catch (Exception e)
            {
                SkyUtil.log("Aborted image generation");
                BugSnagUtil.ReportBug(e);
            }

            return(spawnedEntities);
        }