/calculator - (To be improved)

/calculator - (To be improved)

Postby Leeizazombie » 01 Feb 2014, 15:10

Hey guys, just got into a mathematical mood because of joppiesaus :lol:
Decided to make a calculator command, it clearly has much more work to be done, like double (decimal) and determine remainders ect.. and a better way of using the command, but here's a quick one I made for now:
/calculator [option] [number 1] [number 2]
Options:
add, subtract, multiply, add. You can also just use a short version: + - * /
Then use the numbers so /calc + 5 5 Equals 10

Code: Select all
using System;
using System.Threading;
namespace MCDzienny
{
    public class CmdCalculator : Command
    {
        public Boolean active;
        public override string name { get { return "calculate"; } }
        public override string shortcut { get { return "calc"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
        public override void Use(Player p, string message)
        {

            string option = message.Split(' ')[0];
            int first = Convert.ToInt32(message.Split(' ')[1]);
            int second = Convert.ToInt32(message.Split(' ')[2]);
            if (option == "add" || option == "plus" || option == "+")
            {
                try
                {
                    Player.SendMessage(p, "%bThe answer is: %7" + (first + second));
                }
                catch { Player.SendMessage(p, "Syntax Error."); return; }
               
            }
            if (option == "subtract" || option == "minus" || option == "-")
            {
                try
                {
                    Player.SendMessage(p, "%bThe answer is: %7" + (first - second));
                }
                catch { Player.SendMessage(p, "Syntax Error."); return; }
            }
            if (option == "multiply" || option == "times" || option == "*")
            {
                try
                {
                    Player.SendMessage(p, "%bThe answer is: %7" + (first * second));
                }
                catch { Player.SendMessage(p, "Syntax Error."); return; }
            }
            if (option == "divide" || option == "/")
            {
                try
                {
                    Player.SendMessage(p, "%bThe answer is: %7" + (first / second));
                }
                catch { Player.SendMessage(p, "Syntax Error."); return; }
            }
        }
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/calculator - A simple calculator made by LeeIzaZombie");
            Player.SendMessage(p, "It can add, multiply, subtract or divide two numbers.");
            Player.SendMessage(p, "Example - %b/calculator add 5 5");
            Player.SendMessage(p, "Shortcuts can be used: %b+ / * -");
            Player.SendMessage(p, "/calculator + 5 5");
            Player.SendMessage(p, "This is %aVersion 1.0");
        }
    }
}
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: /calculator - (To be improved)

Postby Conor » 02 Feb 2014, 01:58

Nice work :)

A little thing to help your reliability, when you parse your string type variables to an integer type variable, check that it is first possible!

Code: Select all
int first = Convert.ToInt32(message.Split(' ')[1]);
int second = Convert.ToInt32(message.Split(' ')[2]);


Can become
Code: Select all
// first and second variables will hold the parsed values if successful
int first = 0;
int second = 0;

if (!int.TryParse(message.Split(' ')[1], out first) || !int.TryParse(message.Split(' ')[2], out second))
{
   Player.SendMessage(p, "Invalid number.");
   return;
}
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor


Return to Custom Commands

Who is online

Users browsing this forum: No registered users and 8 guests

cron