Drawing command example

Drawing command example

Postby dzienny » 22 May 2013, 13:27

Below is a sample code that draws a cube. It contains comments to help you understand how it works. It also uses a new API elements that significantly simplifies the process of capturing player block changes.

It works only with MCDzienny version 10.1+

Code: Select all
using System;
using System.Collections.Generic;

namespace MCDzienny
{
    public class CmdSimpleCuboid : Command
    {
        public override string name { get { return "scuboid"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "build"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Builder; } }

        // Usually for building commands the console access should be set to false.
        public override bool ConsoleAccess { get { return false; } }

        public override void Use(Player p, string message)
        {
            byte block = Block.Zero;

            // Parse a block type if specified.
            if (message != "")
            {
                block = Block.Parse(message);

                // Check if the block was parsed correctly.
                if (block == Block.Zero)
                {
                    Player.SendMessage(p, "Unknown block type.");
                    return;
                }

                // Check if the player can place the given block type.
                if (!Block.canPlace(p, block))
                {
                    Player.SendMessage(p, "You can't place this block type.");
                    return;
                }
            }

            // Capture two block changes.
            Player.SendMessage(p, "Place two blocks to determine the edges.");
            BlockCatch.CaptureMultipleBlocks(p, 2, DrawCuboid, new BasicDrawArgs(block));
        }

        private void DrawCuboid(Player p, List<ChangeInfo> changes, BasicDrawArgs da)
        {
            // Set a block type to the block last placed by a player.
            byte block = changes[1].Type;

            // If the block type was specified earlier set it.
            if (da.Type1 != Block.Zero)
                block = da.Type1;

            // Find the start and the end points of the cube.
            // Mind that Y represents a height!
            int startX = Math.Min(changes[0].X, changes[1].X);
            int endX = Math.Max(changes[0].X, changes[1].X);
            int startY = Math.Min(changes[0].Y, changes[1].Y);
            int endY = Math.Max(changes[0].Y, changes[1].Y);
            int startZ = Math.Min(changes[0].Z, changes[1].Z);
            int endZ = Math.Max(changes[0].Z, changes[1].Z);

            // Add every block between two points to the block changes list.
            for (int x = startX; x <= endX; x++)
                for (int y = startY; y <= endY; y++)
                    for (int z = startZ; z <= endZ; z++)
                    {
                        p.BlockChanges.Add(x, y, z, block);
                    }

            // Check if the player can place that many blocks if not abort the changes.
            if (p.group.maxBlocks < p.BlockChanges.Count)
            {
                Player.SendMessage(p, string.Format("You can't place {0} blocks. Your limit is {1}."
                    , p.BlockChanges.Count, p.group.maxBlocks));
                p.BlockChanges.Abort();
                return;
            }

            // Send the block changes.
            Player.SendMessage(p, string.Format("You've built a simple cuboid that consists of {0} blocks."
                , p.BlockChanges.Count));
            p.BlockChanges.Commit();

            // If the static mode is on, repeat.
            if (p.staticCommands)
                BlockCatch.CaptureMultipleBlocks(p, 2, DrawCuboid, da);
        }

        public override void Help(Player p)
        {
            Player.SendMessage(p, "/scuboid <block> - draws a simple cuboid.");
            Player.SendMessage(p, "block - a block type e.g. water, it's optional.");
        }
    }
}
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Drawing command example

Postby Conor » 22 May 2013, 17:20

The new block change methods/events look really nice; improved and easy to understand.

Thanks.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Drawing command example

Postby Conor » 22 May 2013, 18:36

[Sorry for double post]

However, does this render all previous custom commands using the old blockchange event as unusable?

I am imagining you would still keep this functionality to save a lot of hassle, but just checking - you can still use the old way?
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Drawing command example

Postby dzienny » 22 May 2013, 18:51

It just a method that provides higher abstraction level. Under the hood it works pretty much the old way. The backward compatibility is kept, so no worries. I try hard not to break already existing custom commands.
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Drawing command example

Postby ismellike » 22 May 2013, 18:59

It won't compile for me.

It says that "these don't contain definitions for that"

Code: Select all
da.Type1;
Block.Parse(message);
p.BlockChanges


I have the reference for mcdzienny_ set to the latest version
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Drawing command example

Postby dzienny » 22 May 2013, 19:19

@ismellike

I know it's a bit confusing but the current version is 10.0.1, and the feature will be available in the version 10.1 :)
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27

Re: Drawing command example

Postby ismellike » 22 May 2013, 19:40

That would make sense lol, didn't see the mcdzienny 10.1+ part
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas


Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 1 guest

cron