From a2a1486ea1f694304fa3600b4acc729e4f8ff575 Mon Sep 17 00:00:00 2001 From: Deathmax Date: Thu, 15 Dec 2011 21:28:46 +0800 Subject: [PATCH] Add support for prefixes in /item and /give --- TShockAPI/Commands.cs | 42 +++++++++++++++++++++----------------- TShockAPI/TSPlayer.cs | 4 ++-- TShockAPI/Utils.cs | 47 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 968e59d4..66e01272 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -2497,7 +2497,7 @@ namespace TShockAPI { if (args.Parameters.Count < 1) { - args.Player.SendMessage("Invalid syntax! Proper syntax: /item [item amount]", Color.Red); + args.Player.SendMessage("Invalid syntax! Proper syntax: /item [item amount] [prefix name/id]", Color.Red); return; } if (args.Parameters[0].Length == 0) @@ -2506,7 +2506,16 @@ namespace TShockAPI return; } int itemAmount = 0; - int.TryParse(args.Parameters[args.Parameters.Count - 1], out itemAmount); + int prefix = 0; + if (args.Parameters.Count == 2) + int.TryParse(args.Parameters[1], out itemAmount); + else if (args.Parameters.Count == 3) + { + int.TryParse(args.Parameters[1], out itemAmount); + var found = TShock.Utils.GetPrefixByIdOrName(args.Parameters[2]); + if (found.Count == 1) + prefix = found[0]; + } var items = TShock.Utils.GetItemByIdOrName(args.Parameters[0]); if (items.Count == 0) { @@ -2525,7 +2534,7 @@ namespace TShockAPI { if (itemAmount == 0 || itemAmount > item.maxStack) itemAmount = item.maxStack; - args.Player.GiveItem(item.type, item.name, item.width, item.height, itemAmount); + args.Player.GiveItem(item.type, item.name, item.width, item.height, itemAmount, prefix); args.Player.SendMessage(string.Format("Gave {0} {1}(s).", itemAmount, item.name)); } else @@ -2544,7 +2553,7 @@ namespace TShockAPI { if (args.Parameters.Count < 2) { - args.Player.SendMessage("Invalid syntax! Proper syntax: /give [item amount]", Color.Red); + args.Player.SendMessage("Invalid syntax! Proper syntax: /give [item amount] [prefix id/name]", Color.Red); return; } if (args.Parameters[0].Length == 0) @@ -2558,13 +2567,20 @@ namespace TShockAPI return; } int itemAmount = 0; + int prefix = 0; var items = TShock.Utils.GetItemByIdOrName(args.Parameters[0]); args.Parameters.RemoveAt(0); string plStr = args.Parameters[0]; args.Parameters.RemoveAt(0); - if (args.Parameters.Count > 0) - int.TryParse(args.Parameters[args.Parameters.Count - 1], out itemAmount); - + if (args.Parameters.Count == 1) + int.TryParse(args.Parameters[0], out itemAmount); + else if (args.Parameters.Count == 2) + { + int.TryParse(args.Parameters[0], out itemAmount); + var found = TShock.Utils.GetPrefixByIdOrName(args.Parameters[1]); + if (found.Count == 1) + prefix = found[0]; + } if (items.Count == 0) { @@ -2595,7 +2611,7 @@ namespace TShockAPI { if (itemAmount == 0 || itemAmount > item.maxStack) itemAmount = item.maxStack; - plr.GiveItem(item.type, item.name, item.width, item.height, itemAmount); + plr.GiveItem(item.type, item.name, item.width, item.height, itemAmount, prefix); args.Player.SendMessage(string.Format("Gave {0} {1} {2}(s).", plr.Name, itemAmount, item.name)); plr.SendMessage(string.Format("{0} gave you {1} {2}(s).", args.Player.Name, itemAmount, item.name)); } @@ -2618,39 +2634,29 @@ namespace TShockAPI int radius = 50; if (args.Parameters.Count > 0) { - if (args.Parameters[0].ToLower() == "all") { - radius = Int32.MaxValue / 16; - } else { - try { - radius = Convert.ToInt32(args.Parameters[0]); - } catch (Exception) { args.Player.SendMessage("Please either enter the keyword \"all\", or the block radius you wish to delete all items from.", Color.Red); return; } - } } int count = 0; for (int i = 0; i < 200; i++) { - if ((Math.Sqrt(Math.Pow(Main.item[i].position.X - args.Player.X, 2) + Math.Pow(Main.item[i].position.Y - args.Player.Y, 2)) < radius * 16) && (Main.item[i].active)) { - Main.item[i].active = false; NetMessage.SendData(0x15, -1, -1, "", i, 0f, 0f, 0f, 0); count++; } - } args.Player.SendMessage("All " + count.ToString() + " items within a radius of " + radius.ToString() + " have been deleted."); diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 1cda0126..3eb3a154 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -268,9 +268,9 @@ namespace TShockAPI return false; } - public virtual void GiveItem(int type, string name, int width, int height, int stack) + public virtual void GiveItem(int type, string name, int width, int height, int stack, int prefix = 0) { - int itemid = Item.NewItem((int)X, (int)Y, width, height, type, stack, true); + int itemid = Item.NewItem((int)X, (int)Y, width, height, type, stack, true, prefix); // This is for special pickaxe/hammers/swords etc Main.item[itemid].SetDefaults(name); // The set default overrides the wet and stack set by NewItem diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 311deee2..f4b8959a 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -309,10 +309,12 @@ namespace TShockAPI { return (id > 0 && id < Main.maxBuffs) ? Main.buffName[id] : "null"; } + public string GetBuffDescription(int id) { return (id > 0 && id < Main.maxBuffs) ? Main.buffTip[id] : "null"; } + public List GetBuffByName(string name) { for (int i = 1; i < Main.maxBuffs; i++) @@ -329,6 +331,51 @@ namespace TShockAPI return found; } + public string GetPrefixById(int id) + { + var item = new Item(); + item.SetDefaults(0); + item.prefix = (byte)id; + item.AffixName(); + return item.name.Trim(); + } + + public List GetPrefixByName(string name) + { + Item item = new Item(); + for (int i = 1; i < 83; i++) + { + item.prefix = (byte) i; + item.AffixName(); + if (item.name.Trim() == name) + return new List { i }; + } + var found = new List(); + for (int i = 1; i < 83; i++) + { + try + { + item.prefix = (byte) i; + if (item.name.Trim().ToLower() == name.ToLower()) + return new List { i }; + if (item.name.Trim().ToLower().StartsWith(name.ToLower())) + found.Add(i); + } + catch { } + } + return found; + } + + public List GetPrefixByIdOrName(string idOrName) + { + int type = -1; + if (int.TryParse(idOrName, out type) && type > 0 && type < 84) + { + return new List {type}; + } + return GetPrefixByName(idOrName); + } + /// /// Kicks all player from the server without checking for immunetokick permission. ///