Skip to content

[Mod] Give item command

lioncash edited this page Jul 4, 2011 · 15 revisions

Giving yourself items based on their name

In Minecraft this is a basic Multiplayer command you can use. You type for example: "/give diedy 23 64". This will give player diedy 64 times the item with ID 23. On Terraria it wont be possible to give a player name as we ain't got a custom server and admin functionality yet.

###Example Use: Giving yourself 10 units of wood:

/give wood 10

Using Chat-Hook to give a player an item

Open your ModClass.cs and add following code into The onChat method:

       try
        {
            if (text.Substring(0, 1) == "/")
            {
                var command = text.Substring(1, text.IndexOf(' ') - 1);
                var parameter = text.Substring(text.IndexOf(' ') + 1);
                var count = 1;
                if (text.IndexOf(' ') != text.LastIndexOf(' '))
                {
                    count = int.Parse(text.Substring(text.LastIndexOf(' ') + 1));
                    if (count != 0)
                    {
                        parameter = parameter.Substring(0, parameter.LastIndexOf(' '));
                    }
                    else
                    {
                        count = 1;
                    }  
                }

                switch (command)
                {
                    case "give":
                        var newItem = new Item();
                        newItem.SetDefaults(parameter);
                        newItem.stack = count;

                        var chat = "Giving " + Main.player[Main.myPlayer].name + " " + count + "x " + parameter + ".";
                        NetMessage.SendData(0x19, -1, -1, chat, Main.myPlayer, 0f, 0f, 0f);

                        //While loop used to make sure you dont have a stack that exceeds the stack limit
                        while (count > newItem.maxStack)
                        {
                            newItem.stack = newItem.maxStack;
                            Main.player[Main.myPlayer].GetItem(Main.myPlayer, newItem);
                            count = count - newItem.maxStack;
                            newItem = new Item();
                            newItem.SetDefaults(parameter);
                        }
                         newItem.stack = count;
                         Main.player[Main.myPlayer].GetItem(Main.myPlayer, newItem);
                        break;
                    default:
                        break;
                }
                return false;
            }
        }
        catch (Exception)
        {
            return false;
        }


        return true;

##Revised /Give Command with Auto-Capitalization

Since we're lazy, this code eliminates the use of having to hit the Shift key at all.

Instead of the above code, use the code below (this assumes you have NOTHING else in your ModClass file, if you do happen to have something in there, I provided the full code so it is easier to view and modify):

using System;

namespace Terraria
{
internal static class ModClass
{
    static string Capitalize(String inStr) //Responsible for the Auto-Capitalization
    {
        var length = inStr.Length;
        char[] cArray = inStr.ToCharArray();
        for (int i = 0; i < length; i++)
        {
            if (cArray[i] == ' ')
                cArray[i + 1] = Char.ToUpper(cArray[i + 1]);
            else if (i == 0)
                cArray[i] = Char.ToUpper(cArray[i]);

        }
        return new String(cArray);
    }

    //Hooks the chat. Add to this if you want specific / codes to work
    public static bool OnChat(String text)
    {

        try
        {
            if (text.Substring(0, 1) == "/")
            {
                var command = text.Substring(1, text.IndexOf(' ') - 1);
                var parameter = text.Substring(text.IndexOf(' ') + 1);
                parameter = Capitalize(parameter); //Implements the Auto-Capitalization

                var count = 1;
                if (text.IndexOf(' ') != text.LastIndexOf(' '))
                {
                    count = int.Parse(text.Substring(text.LastIndexOf(' ') + 1));
                    if (count != 0)
                    {
                        parameter = parameter.Substring(0, parameter.LastIndexOf(' '));
                    }
                    else
                    {
                        count = 1;
                    }
                }

                switch (command)
                {
                    case "give": //Give item mode
                        var newItem =  new Item {stack = count};
                        newItem.SetDefaults(parameter);
                        if (newItem.stack > newItem.maxStack)
                        {
                            newItem.stack = newItem.maxStack;
                        }
                        Main.player[Main.myPlayer].GetItem(Main.myPlayer, newItem);
                        break;
                }
                return false;
            }
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
}
}

Now, when you use the give command. You can simply type "/give wood" instead of "/give Wood", or "/give gold pickaxe 1" instead of "/give Gold Pickaxe 1"

===NOTE===

If you use the auto-capitalization /give item code, then you will need to set the activators for everything else to start with a capital.

ie. For the debug code, instead of it being:

case "debug": Main.debugMode = parameter == "true";
it will have to be changed to:
case "debug": Main.debugMode = parameter == "True";

-Added by Lioncash. Code borrowed from CloudHopper(revised case command), and tuldok89(help with the auto-capitalization)

Clone this wiki locally