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

Last change on this file since 340 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 2.7 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) {
[224]27 try {
[250]28 if (_params.Count != 3 && _params.Count != 4) {
[325]29 SdtdConsole.Instance.Output ("Wrong number of arguments, expected 3 or 4, found " + _params.Count +
30 ".");
[224]31 return;
32 }
33
[230]34 ClientInfo ci = ConsoleHelper.ParseParamIdOrName (_params [0]);
[224]35
36 if (ci == null) {
[230]37 SdtdConsole.Instance.Output ("Playername or entity id not found.");
[224]38 return;
39 }
40
[306]41 ItemValue iv = ItemClass.GetItem (_params [1], true);
42 if (iv.type == ItemValue.None.type) {
[230]43 SdtdConsole.Instance.Output ("Item not found.");
[224]44 return;
45 }
46
[306]47 iv = new ItemValue (iv.type, true);
48
[326]49 int n;
[224]50 if (!int.TryParse (_params [2], out n) || n <= 0) {
[230]51 SdtdConsole.Instance.Output ("Amount is not an integer or not greater than zero.");
[224]52 return;
53 }
54
[324]55 int quality = Constants.cItemMaxQuality;
56
[250]57 if (_params.Count == 4) {
[325]58 if (!int.TryParse (_params [1], out quality) || quality <= 0) {
[324]59 SdtdConsole.Instance.Output ("Quality is not an integer or not greater than zero.");
[250]60 return;
61 }
[324]62 }
[250]63
[324]64 if (ItemClass.list [iv.type].HasSubItems) {
65 for (int i = 0; i < iv.Modifications.Length; i++) {
[325]66 ItemValue tmp = iv.Modifications [i];
[324]67 tmp.Quality = quality;
[325]68 iv.Modifications [i] = tmp;
[250]69 }
[324]70 } else if (ItemClass.list [iv.type].HasQuality) {
[250]71 iv.Quality = quality;
72 }
73
[230]74 EntityPlayer p = GameManager.Instance.World.Players.dict [ci.entityId];
[224]75
[238]76 ItemStack invField = new ItemStack (iv, n);
[224]77
[306]78 GameManager.Instance.ItemDropServer (invField, p.GetPosition (), Vector3.zero);
[224]79
[230]80 SdtdConsole.Instance.Output ("Dropped item");
[224]81 } catch (Exception e) {
82 Log.Out ("Error in Give.Run: " + e);
83 }
84 }
85 }
[325]86}
Note: See TracBrowser for help on using the repository browser.