summaryrefslogtreecommitdiff
path: root/src/TrainerMod/Framework/Commands/Player/AddWeaponCommand.cs
blob: c4ea3d6f80f284a3dea98b78a0ad141850135445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Collections.Generic;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Tools;

namespace TrainerMod.Framework.Commands.Player
{
    /// <summary>A command which adds a weapon to the player inventory.</summary>
    internal class AddWeaponCommand : TrainerCommand
    {
        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        public AddWeaponCommand()
            : base("player_addweapon", "Gives the player a weapon.\n\nUsage: player_addweapon <item>\n- item: the weapon ID (use the 'list_items' command to see a list).") { }

        /// <summary>Handle the command.</summary>
        /// <param name="monitor">Writes messages to the console and log file.</param>
        /// <param name="command">The command name.</param>
        /// <param name="args">The command arguments.</param>
        public override void Handle(IMonitor monitor, string command, ArgumentParser args)
        {
            // parse arguments
            if (!args.TryGetInt(0, "weapon ID", out int weaponID, min: 0))
                return;

            // get raw weapon data
            if (!Game1.content.Load<Dictionary<int, string>>("Data\\weapons").TryGetValue(weaponID, out string data))
            {
                monitor.Log("There is no such weapon ID.", LogLevel.Error);
                return;
            }

            // get raw weapon type
            int type;
            {
                string[] fields = data.Split('/');
                string typeStr = fields.Length > 8 ? fields[8] : null;
                if (!int.TryParse(typeStr, out type))
                {
                    monitor.Log("Could not parse the data for the weapon with that ID.", LogLevel.Error);
                    return;
                }
            }

            // get weapon
            Tool weapon;
            switch (type)
            {
                case MeleeWeapon.stabbingSword:
                case MeleeWeapon.dagger:
                case MeleeWeapon.club:
                case MeleeWeapon.defenseSword:
                    weapon = new MeleeWeapon(weaponID);
                    break;

                case 4:
                    weapon = new Slingshot(weaponID);
                    break;

                default:
                    monitor.Log($"The specified weapon has unknown type '{type}' in the game data.", LogLevel.Error);
                    return;
            }

            // validate weapon
            if (weapon.Name == null)
            {
                monitor.Log("That weapon doesn't seem to be valid.", LogLevel.Error);
                return;
            }

            // add weapon
            Game1.player.addItemByMenuIfNecessary(weapon);
            monitor.Log($"OK, added {weapon.Name} to your inventory.", LogLevel.Info);
        }
    }
}