Esempio n. 1
0
        public static async Task MainAsync(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine(@"Minecraft.Scratch - a console app using Minecraft.Client

Modify the source if you want to make it do anything useful.

usage:

minecraft.client <Minecraft instance ip>");
                return;
            }

            try
            {
                using (var world = JavaWorld.Connect(args[0]))
                {
                    await world.PostToChatAsync("Hello from C# and .NET Core!");

                    var    player = world.Player;
                    Bridge bridge = null;

                    DisplayAvailableCommands();

                    while (true)
                    {
                        Vector3   playerPosition = new Vector3();
                        Direction?direction;

                        var cmd = Console.ReadKey(true);
                        Console.WriteLine();
                        var shift = cmd.Modifiers.HasFlag(ConsoleModifiers.Shift);
                        var ctrl  = cmd.Modifiers.HasFlag(ConsoleModifiers.Control);
                        var alt   = cmd.Modifiers.HasFlag(ConsoleModifiers.Alt);
                        switch (cmd.Key)
                        {
                        case ConsoleKey.Escape:
                            return;

                        case ConsoleKey.P:
                            if (ctrl)
                            {
                                player.Moved -= _onPlayerMoved;
                                Console.WriteLine("Stopped detecting player movement.");
                            }
                            else
                            {
                                player.Moved += _onPlayerMoved;
                                Console.WriteLine("Started detecting player movement.");
                            }
                            break;

                        case ConsoleKey.T:
                            Console.WriteLine("Where do you want to go? Enter X,Y,Z coordinates and press Enter:");
                            var destCoord = Console.ReadLine().ParseCoordinates();
                            await player.SetPositionAsync(destCoord);

                            playerPosition = await player.GetTilePositionAsync();

                            Console.WriteLine($"Player is now at {playerPosition}");
                            break;

                        case ConsoleKey.M:
                            direction = GetDirection();
                            if (direction.HasValue)
                            {
                                playerPosition = await player.GetTilePositionAsync();

                                await player.SetPositionAsync(playerPosition.Towards(direction.Value));

                                playerPosition = await player.GetTilePositionAsync();

                                Console.WriteLine($"Player moved {direction} to {playerPosition}");
                            }
                            break;

                        case ConsoleKey.D:
                            Console.WriteLine(@"

Please choose what to draw:
    * C is for castle
    * I is for image
    * S is for snowy block
    * B is for Borromean rings
    * D is for a Dragon Egg

");
                            var whatToDraw = Console.ReadKey(true);
                            switch (whatToDraw.Key)
                            {
                            case ConsoleKey.C:
                                direction = GetDirection();
                                if (direction.HasValue)
                                {
                                    playerPosition = await player.GetTilePositionAsync();

                                    var castlePosition = playerPosition.Towards(direction.Value, 70);
                                    Console.WriteLine($"Building a castle at {castlePosition}.");
                                    new Castle(world, castlePosition).Build();
                                }
                                break;

                            case ConsoleKey.I:
                                direction = GetDirection();
                                if (direction.HasValue)
                                {
                                    RenderIcon(world, direction);
                                }
                                break;

                            case ConsoleKey.S:
                                direction = GetDirection();
                                if (direction.HasValue)
                                {
                                    Console.WriteLine(@"

It's starting to snow on this particular block...");
                                    Snow(world, direction);
                                }
                                break;

                            case ConsoleKey.B:
                                playerPosition = await player.GetTilePositionAsync();

                                new Borromean(world, playerPosition + new Vector3(0, 35, 0)).Build();
                                break;

                            case ConsoleKey.D:
                                direction = GetDirection();
                                if (direction.HasValue)
                                {
                                    Console.WriteLine(@"

Here comes the egg...");
                                    LayDragonEgg(world, direction);
                                }
                                break;
                            }
                            break;

                        case ConsoleKey.H:
                            var height = world.GetHeight(playerPosition);
                            Console.WriteLine($"Height under the player is {height}");
                            break;

                        case ConsoleKey.E:
                            if (ctrl)
                            {
                                world.PostedToChat -= _onChatMessage;
                                Console.WriteLine("Stopped listening to chat messages.");
                            }
                            else
                            {
                                world.PostedToChat += _onChatMessage;
                                Console.WriteLine(@"Started listening to chat messages.

Available commands:

    * `Explode` to explode the tile under the player
    * `Snow` to create a block north of the player that will get snowed on.
    * `Castle` to build a castle north of the player
    * `Image` to render an image north of the player
");
                            }
                            break;

                        case ConsoleKey.X:
                            if (ctrl)
                            {
                                world.BlockHit -= _onBlockHitExplode;
                                Console.WriteLine("Stopped exploding blocks when hit.");
                            }
                            else
                            {
                                world.BlockHit += _onBlockHitExplode;
                                Console.WriteLine("Started exploding blocks when hit.");
                            }
                            break;

                        case ConsoleKey.B:
                            if (ctrl)
                            {
                                if (bridge != null)
                                {
                                    bridge.Dispose();
                                    bridge = null;
                                    Console.WriteLine("Stopped building a bridge under the player.");
                                }
                            }
                            else if (bridge == null)
                            {
                                bridge = new Bridge(world, player);
                                Console.WriteLine("Started building a bridge under the player.");
                            }
                            break;

                        case ConsoleKey.I:
                            if (ctrl)
                            {
                                world.BlockHit -= _onBlockHitIdentify;
                                Console.WriteLine("Stopped block identification mode.");
                            }
                            else
                            {
                                world.BlockHit += _onBlockHitIdentify;
                                Console.WriteLine("Started blocks identification mode.");
                            }
                            break;

                        default:
                            DisplayAvailableCommands();
                            break;
                        }
                    }
                }
            }
            catch (FailedToConnectToMinecraftEngine e)
            {
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 2
0
        public static async Task MainAsync(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine(@"Minecraft.Scratch - a console app using Minecraft.Client

Modify the source if you want to make it do anything useful.

usage:

minecraft.client <raspberry pi ip>");
                return;
            }

            try
            {
                using (var world = JavaWorld.Connect(args[0]))
                {
                    await world.PostToChatAsync("Hello from C# and .NET Core!");

                    var player = world.Player;

                    while (true)
                    {
                        Console.WriteLine(@"
Available commands:

P = Get current position
T = Transport to a given position
M = Move towards north/south/east/west
C = Build a castle
I = Render a picture
H = Height under the player
S = Add snow

Press ESC to quit

");

                        Vector3   playerPosition = new Vector3();
                        Direction?direction;

                        var cmd = Console.ReadKey();
                        Console.WriteLine();
                        switch (cmd.Key)
                        {
                        case ConsoleKey.Escape:
                            return;

                        case ConsoleKey.P:
                            playerPosition = await player.GetTilePositionAsync();

                            Console.WriteLine($"Player is on {playerPosition}.");
                            break;

                        case ConsoleKey.T:
                            Console.WriteLine("Where do you want to go? Enter X,Y,Z coordinates and press Enter:");
                            var destCoord = Console.ReadLine().ParseCoordinates();
                            await player.SetPositionAsync(destCoord);

                            playerPosition = await player.GetTilePositionAsync();

                            Console.WriteLine($"Player is now at {playerPosition}");
                            break;

                        case ConsoleKey.M:
                            direction = GetDirection();
                            if (direction.HasValue)
                            {
                                playerPosition = await player.GetTilePositionAsync();

                                await player.SetPositionAsync(playerPosition.Towards(direction.Value));

                                playerPosition = await player.GetTilePositionAsync();

                                Console.WriteLine($"Player moved {direction} to {playerPosition}");
                            }
                            break;

                        case ConsoleKey.C:
                            direction = GetDirection();
                            if (direction.HasValue)
                            {
                                playerPosition = await player.GetTilePositionAsync();

                                new Castle(world, playerPosition.Towards(direction.Value, 30)).Build();
                            }
                            break;

                        case ConsoleKey.I:
                            direction = GetDirection();
                            if (direction.HasValue)
                            {
                                playerPosition = await player.GetTilePositionAsync();

                                var imageBuilder = new ImageBuilder(world);
                                imageBuilder.DrawImage(
                                    Path.Combine(".", "Media", "Minecraft.gif"),
                                    playerPosition.Towards(direction.Value, 20));
                            }
                            break;

                        case ConsoleKey.H:
                            var height = world.GetHeight(playerPosition);
                            Console.WriteLine($"Height under the player is {height}");
                            break;

                        case ConsoleKey.S:
                            playerPosition = await player.GetTilePositionAsync();

                            // Create grass and put a snowy layer on top of it
                            // to create a snowy grass block.
                            var grass = new Grass();
                            world.SetBlock(grass, playerPosition + new Vector3(0, 0, 3));
                            var snowLayer = new Snow();
                            world.SetBlock(snowLayer, playerPosition + new Vector3(0, 1, 3));
                            Console.WriteLine("Snow added");
                            break;
                        }
                    }
                }
            }
            catch (FailedToConnectToMinecraftEngine e)
            {
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 3
0
 public void Setup()
 {
     World = JavaWorld.Connect();
     Rnd   = new Random();
 }