source: binary-improvements/AllocsCommands/Commands/Give.cs@ 377

Last change on this file since 377 was 359, checked in by alloc, 5 years ago

Removed unnecessary try-catch-blocks from commands (command handler catches exceptions anyway and provides more detailed output)

File size: 2.6 KB
RevLine 
[224]1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
[325]5namespace AllocsFixes.CustomCommands {
[250]6 public class Give : ConsoleCmdAbstract {
7 public override string GetDescription () {
[224]8 return "give an item to a player (entity id or name)";
9 }
10
[238]11 public override string GetHelp () {
12 return "Give an item to a player by dropping it in front of that player\n" +
[325]13 "Usage:\n" +
14 " give <name / entity id> <item name> <amount>\n" +
15 " give <name / entity id> <item name> <amount> <quality>\n" +
16 "Either pass the full name of a player or his entity id (given by e.g. \"lpi\").\n" +
17 "Item name has to be the exact name of an item as listed by \"listitems\".\n" +
18 "Amount is the number of instances of this item to drop (as a single stack).\n" +
19 "Quality is the quality of the dropped items for items that have a quality.";
[238]20 }
21
[250]22 public override string[] GetCommands () {
[325]23 return new[] {"give", string.Empty};
[224]24 }
25
[250]26 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
[359]27 if (_params.Count != 3 && _params.Count != 4) {
28 SdtdConsole.Instance.Output ("Wrong number of arguments, expected 3 or 4, found " + _params.Count +
29 ".");
30 return;
31 }
[224]32
[359]33 ClientInfo ci = ConsoleHelper.ParseParamIdOrName (_params [0]);
[224]34
[359]35 if (ci == null) {
36 SdtdConsole.Instance.Output ("Playername or entity id not found.");
37 return;
38 }
[224]39
[359]40 ItemValue iv = ItemClass.GetItem (_params [1], true);
41 if (iv.type == ItemValue.None.type) {
42 SdtdConsole.Instance.Output ("Item not found.");
43 return;
44 }
[224]45
[359]46 iv = new ItemValue (iv.type, true);
[306]47
[359]48 int n;
49 if (!int.TryParse (_params [2], out n) || n <= 0) {
50 SdtdConsole.Instance.Output ("Amount is not an integer or not greater than zero.");
51 return;
52 }
[224]53
[359]54 int quality = Constants.cItemMaxQuality;
[324]55
[359]56 if (_params.Count == 4) {
57 if (!int.TryParse (_params [3], out quality) || quality <= 0) {
58 SdtdConsole.Instance.Output ("Quality is not an integer or not greater than zero.");
59 return;
[324]60 }
[359]61 }
[250]62
[359]63 if (ItemClass.list [iv.type].HasSubItems) {
64 for (int i = 0; i < iv.Modifications.Length; i++) {
65 ItemValue tmp = iv.Modifications [i];
66 tmp.Quality = quality;
67 iv.Modifications [i] = tmp;
[250]68 }
[359]69 } else if (ItemClass.list [iv.type].HasQuality) {
70 iv.Quality = quality;
71 }
[250]72
[359]73 EntityPlayer p = GameManager.Instance.World.Players.dict [ci.entityId];
[224]74
[359]75 ItemStack invField = new ItemStack (iv, n);
[224]76
[359]77 GameManager.Instance.ItemDropServer (invField, p.GetPosition (), Vector3.zero);
[224]78
[359]79 SdtdConsole.Instance.Output ("Dropped item");
[224]80 }
81 }
[325]82}
Note: See TracBrowser for help on using the repository browser.