From 374d296f3cb53ba2a1653f567ea083aa7fcf4b52 Mon Sep 17 00:00:00 2001 From: Deathmax Date: Wed, 1 Jun 2011 18:51:22 +0800 Subject: [PATCH 1/6] Updated /item so that now it edits the player's inventory directly. Added /give. --- TShockAPI/TShock.cs | 61 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 6d6c453a..f2dcc532 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -317,7 +317,6 @@ namespace TShockAPI } } } - //ATM it just drops the item. Trying to find out can I update the player's inventory directly. if (msg.StartsWith("/item") && msg.Split(' ').Length == 2) { var args = msg.Split(' ')[1]; @@ -326,13 +325,47 @@ namespace TShockAPI { if (type >= 1 && type <= 235) { - int id = Item.NewItem(0, 0, 0, 0, type, 1, true); - Main.item[id].position.X = (float)x; - Main.item[id].position.Y = (float)y; - Main.item[id].stack = Main.item[id].maxStack; - NetMessage.SendData(21, -1, -1, "", id, 0f, 0f, 0f); - Tools.SendMessage(ply, "Spawned " + Main.item[id].name + "."); - handler.Handled = true; + for (int i = 0; i < 40; i++) + { + if (!Main.player[ply].inventory[i].active) + { + Main.player[ply].inventory[i].SetDefaults(type); + Main.player[ply].inventory[i].stack = Main.player[ply].inventory[i].maxStack; + Tools.SendMessage(ply, "Got some " + Main.player[ply].inventory[i].name + "."); + UpdateInventories(); + handler.Handled = true; + break; + } + } + } + } + } + if (msg.StartsWith("/give") && msg.Split(' ').Length == 3) + { + var args = msg.Split(' '); + int type = 0; + int player = -1; + if (int.TryParse(args[1], out type)) + { + if (type >= 1 && type <= 235) + { + player = Tools.FindPlayer(args[2]); + if (player != -1) + { + for (int i = 0; i < 40; i++) + { + if (!Main.player[player].inventory[i].active) + { + Main.player[player].inventory[i].SetDefaults(type); + Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; + Tools.SendMessage(ply, string.Format("Gave {0} some {1}.", args[2], Main.player[player].inventory[i].name)); + Tools.SendMessage(player, string.Format("{0} gave you some {1}.", Tools.FindPlayer(ply), Main.player[player].inventory[i].name)); + UpdateInventories(); + handler.Handled = true; + break; + } + } + } } } } @@ -508,5 +541,17 @@ namespace TShockAPI } } } + + public static void UpdateInventories() + { + for (int i = 0; i < Main.player.Length; i++) + { + for (int j = 0; j < 44; j++) + { + for (int h = 0; h < Main.player.Length; h++) + NetMessage.SendData(5, h, i, Main.player[i].inventory[j].name, i, (float)j, 0f, 0f); + } + } + } } } \ No newline at end of file From e9461f49d78866368a51324fdad74bf50c57d884 Mon Sep 17 00:00:00 2001 From: Deathmax Date: Wed, 1 Jun 2011 19:57:03 +0800 Subject: [PATCH 2/6] /spawnmob now has another argument that specifies the number of NPCs to spawn. /spawnmob, /item and /give now supports name for item/mob type. --- TShockAPI/TShock.cs | 191 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 153 insertions(+), 38 deletions(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index f2dcc532..b8d42f19 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -5,6 +5,7 @@ using Microsoft.Xna.Framework; using Terraria; using TerrariaAPI; using TerrariaAPI.Hooks; +using System.Text.RegularExpressions; namespace TShockAPI { @@ -303,66 +304,142 @@ namespace TShockAPI handler.Handled = true; } } - if (msg.Length > 9 && msg.Substring(0, 9) == "/spawnmob") + if (msg.StartsWith("/spawnmob")) { - string args = msg.Remove(0, 9).Trim(); - int type = 0; - if (int.TryParse(args, out type)) + var args = Regex.Split(msg, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); + if (args.Length >= 2 && args.Length <= 3) { - if (type >= 1 && type <= 43) + for (int i = 1; i < args.Length; i++) + args[i] = ((args[i].TrimEnd('"')).TrimStart('"')); + string inputtype = ""; + int amount = 1; + int npcid = -1; + int type = -1; + inputtype = args[1]; + if (args.Length == 3) + int.TryParse(args[2], out amount); + + if (int.TryParse(inputtype, out type)) { - var npcid = NPC.NewNPC(x, y, type, 0); - Tools.Broadcast("NPC " + type.ToString() + " spawned with ID " + npcid.ToString()); - handler.Handled = true; + if (type >= 1 && type <= 43) + { + for (int i = 0; i < amount; i++) + npcid = NPC.NewNPC(x, y, type, 0); + Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", Main.npc[npcid].name, amount)); + handler.Handled = true; + } + } + else + { + type = GetNPCID(inputtype); + if (type > 0) + { + for (int i = 0; i < amount; i++) + npcid = NPC.NewNPC(x, y, type, 0); + Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", Main.npc[npcid].name, amount)); + handler.Handled = true; + } } } } - if (msg.StartsWith("/item") && msg.Split(' ').Length == 2) + if (msg.StartsWith("/item")) { - var args = msg.Split(' ')[1]; - int type = 0; - if (int.TryParse(args, out type)) + var args = Regex.Split(msg, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")[1]; + if (args.Length >= 2) { - if (type >= 1 && type <= 235) + args = ((args.TrimEnd('"')).TrimStart('"')); + int type = 0; + if (int.TryParse(args, out type)) { - for (int i = 0; i < 40; i++) + if (type >= 1 && type <= 235) { - if (!Main.player[ply].inventory[i].active) + for (int i = 0; i < 40; i++) { - Main.player[ply].inventory[i].SetDefaults(type); - Main.player[ply].inventory[i].stack = Main.player[ply].inventory[i].maxStack; - Tools.SendMessage(ply, "Got some " + Main.player[ply].inventory[i].name + "."); - UpdateInventories(); - handler.Handled = true; - break; + if (!Main.player[ply].inventory[i].active) + { + Main.player[ply].inventory[i].SetDefaults(type); + Main.player[ply].inventory[i].stack = Main.player[ply].inventory[i].maxStack; + Tools.SendMessage(ply, "Got some " + Main.player[ply].inventory[i].name + "."); + UpdateInventories(); + handler.Handled = true; + break; + } + } + } + } + else + { + type = GetItemID(args); + if (type != -1) + { + for (int i = 0; i < 40; i++) + { + if (!Main.player[ply].inventory[i].active) + { + Main.player[ply].inventory[i].SetDefaults(type); + Main.player[ply].inventory[i].stack = Main.player[ply].inventory[i].maxStack; + Tools.SendMessage(ply, "Got some " + Main.player[ply].inventory[i].name + "."); + UpdateInventories(); + handler.Handled = true; + break; + } } } } } } - if (msg.StartsWith("/give") && msg.Split(' ').Length == 3) + if (msg.StartsWith("/give")) { - var args = msg.Split(' '); - int type = 0; - int player = -1; - if (int.TryParse(args[1], out type)) + var args = Regex.Split(msg, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); + if (args.Length == 3) { - if (type >= 1 && type <= 235) + for (int i = 1; i < args.Length; i++) + args[i] = ((args[i].TrimEnd('"')).TrimStart('"')); + int type = 0; + int player = -1; + if (int.TryParse(args[1], out type)) { - player = Tools.FindPlayer(args[2]); - if (player != -1) + if (type >= 1 && type <= 235) { - for (int i = 0; i < 40; i++) + player = Tools.FindPlayer(args[2]); + if (player != -1) { - if (!Main.player[player].inventory[i].active) + for (int i = 0; i < 40; i++) { - Main.player[player].inventory[i].SetDefaults(type); - Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; - Tools.SendMessage(ply, string.Format("Gave {0} some {1}.", args[2], Main.player[player].inventory[i].name)); - Tools.SendMessage(player, string.Format("{0} gave you some {1}.", Tools.FindPlayer(ply), Main.player[player].inventory[i].name)); - UpdateInventories(); - handler.Handled = true; - break; + if (!Main.player[player].inventory[i].active) + { + Main.player[player].inventory[i].SetDefaults(type); + Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; + Tools.SendMessage(ply, string.Format("Gave {0} some {1}.", args[2], Main.player[player].inventory[i].name)); + Tools.SendMessage(player, string.Format("{0} gave you some {1}.", Tools.FindPlayer(ply), Main.player[player].inventory[i].name)); + UpdateInventories(); + handler.Handled = true; + break; + } + } + } + } + } + else + { + type = GetItemID(args[1]); + if (type != -1) + { + player = Tools.FindPlayer(args[2]); + if (player != -1) + { + for (int i = 0; i < 40; i++) + { + if (!Main.player[player].inventory[i].active) + { + Main.player[player].inventory[i].SetDefaults(type); + Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; + Tools.SendMessage(ply, string.Format("Gave {0} some {1}.", args[2], Main.player[player].inventory[i].name)); + Tools.SendMessage(player, string.Format("{0} gave you some {1}.", Tools.FindPlayer(ply), Main.player[player].inventory[i].name)); + UpdateInventories(); + handler.Handled = true; + break; + } } } } @@ -553,5 +630,43 @@ namespace TShockAPI } } } + + //TODO : Notify the player if there is more than one match. (or do we want a First() kinda thing?) + public static int GetNPCID(string name, bool exact = false) + { + NPC npc = new NPC(); + for (int i = 1; i <= 43; i++) + { + if (exact) + { + //Method #1 - must be exact match, allows support for different coloured slimes + npc.SetDefaults(name); + if (npc.name == name) + return i; + } + else + { + //Method #2 - allows impartial matching + name = name.ToLower(); + npc.SetDefaults(i); + if (npc.name.ToLower().StartsWith(name)) + return i; + } + } + return -1; + } + + public static int GetItemID(string name) + { + Item item = new Item(); + name = name.ToLower(); + for (int i = 1; i <= 235; i++) + { + item.SetDefaults(i); + if (item.name.ToLower().StartsWith(name)) + return i; + } + return -1; + } } } \ No newline at end of file From 3ab1c9e906cd31e9e7b0c6b92f02e13aec86f5de Mon Sep 17 00:00:00 2001 From: Deathmax Date: Wed, 1 Jun 2011 20:13:15 +0800 Subject: [PATCH 3/6] Cleaned up some redundant code. --- TShockAPI/TShock.cs | 126 ++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 91 deletions(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index b8d42f19..0be03b2f 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -319,70 +319,38 @@ namespace TShockAPI if (args.Length == 3) int.TryParse(args[2], out amount); - if (int.TryParse(inputtype, out type)) - { - if (type >= 1 && type <= 43) - { - for (int i = 0; i < amount; i++) - npcid = NPC.NewNPC(x, y, type, 0); - Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", Main.npc[npcid].name, amount)); - handler.Handled = true; - } - } - else - { + if (!int.TryParse(inputtype, out type)) type = GetNPCID(inputtype); - if (type > 0) - { - for (int i = 0; i < amount; i++) - npcid = NPC.NewNPC(x, y, type, 0); - Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", Main.npc[npcid].name, amount)); - handler.Handled = true; - } + if (type >= 1 && type <= 43) + { + for (int i = 0; i < amount; i++) + npcid = NPC.NewNPC(x, y, type, 0); + Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", Main.npc[npcid].name, amount)); + handler.Handled = true; } } } - if (msg.StartsWith("/item")) + if (msg.StartsWith("/item")) { var args = Regex.Split(msg, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")[1]; if (args.Length >= 2) { args = ((args.TrimEnd('"')).TrimStart('"')); int type = 0; - if (int.TryParse(args, out type)) - { - if (type >= 1 && type <= 235) - { - for (int i = 0; i < 40; i++) - { - if (!Main.player[ply].inventory[i].active) - { - Main.player[ply].inventory[i].SetDefaults(type); - Main.player[ply].inventory[i].stack = Main.player[ply].inventory[i].maxStack; - Tools.SendMessage(ply, "Got some " + Main.player[ply].inventory[i].name + "."); - UpdateInventories(); - handler.Handled = true; - break; - } - } - } - } - else - { + if (!int.TryParse(args, out type)) type = GetItemID(args); - if (type != -1) + if (type >= 1 && type <= 235) + { + for (int i = 0; i < 40; i++) { - for (int i = 0; i < 40; i++) + if (!Main.player[ply].inventory[i].active) { - if (!Main.player[ply].inventory[i].active) - { - Main.player[ply].inventory[i].SetDefaults(type); - Main.player[ply].inventory[i].stack = Main.player[ply].inventory[i].maxStack; - Tools.SendMessage(ply, "Got some " + Main.player[ply].inventory[i].name + "."); - UpdateInventories(); - handler.Handled = true; - break; - } + Main.player[ply].inventory[i].SetDefaults(type); + Main.player[ply].inventory[i].stack = Main.player[ply].inventory[i].maxStack; + Tools.SendMessage(ply, "Got some " + Main.player[ply].inventory[i].name + "."); + UpdateInventories(); + handler.Handled = true; + break; } } } @@ -397,49 +365,24 @@ namespace TShockAPI args[i] = ((args[i].TrimEnd('"')).TrimStart('"')); int type = 0; int player = -1; - if (int.TryParse(args[1], out type)) - { - if (type >= 1 && type <= 235) - { - player = Tools.FindPlayer(args[2]); - if (player != -1) - { - for (int i = 0; i < 40; i++) - { - if (!Main.player[player].inventory[i].active) - { - Main.player[player].inventory[i].SetDefaults(type); - Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; - Tools.SendMessage(ply, string.Format("Gave {0} some {1}.", args[2], Main.player[player].inventory[i].name)); - Tools.SendMessage(player, string.Format("{0} gave you some {1}.", Tools.FindPlayer(ply), Main.player[player].inventory[i].name)); - UpdateInventories(); - handler.Handled = true; - break; - } - } - } - } - } - else - { + if (!int.TryParse(args[1], out type)) type = GetItemID(args[1]); - if (type != -1) + if (type >= 1 && type <= 235) + { + player = Tools.FindPlayer(args[2]); + if (player != -1) { - player = Tools.FindPlayer(args[2]); - if (player != -1) + for (int i = 0; i < 40; i++) { - for (int i = 0; i < 40; i++) + if (!Main.player[player].inventory[i].active) { - if (!Main.player[player].inventory[i].active) - { - Main.player[player].inventory[i].SetDefaults(type); - Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; - Tools.SendMessage(ply, string.Format("Gave {0} some {1}.", args[2], Main.player[player].inventory[i].name)); - Tools.SendMessage(player, string.Format("{0} gave you some {1}.", Tools.FindPlayer(ply), Main.player[player].inventory[i].name)); - UpdateInventories(); - handler.Handled = true; - break; - } + Main.player[player].inventory[i].SetDefaults(type); + Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; + Tools.SendMessage(ply, string.Format("Gave {0} some {1}.", args[2], Main.player[player].inventory[i].name)); + Tools.SendMessage(player, string.Format("{0} gave you some {1}.", Tools.FindPlayer(ply), Main.player[player].inventory[i].name)); + UpdateInventories(); + handler.Handled = true; + break; } } } @@ -451,7 +394,8 @@ namespace TShockAPI { Tools.SendMessage(ply, "TShock Commands:"); Tools.SendMessage(ply, "/kick, /ban, /reload, /off, /dropmeteor, /invade"); - Tools.SendMessage(ply, "/star, /skeletron, /eye, /eater, /hardcore"); + Tools.SendMessage(ply, "/star, /skeletron, /eye, /eater, /hardcore, /give"); + Tools.SendMessage(ply, "/password, /save, /item, /spawnmob, /tp, /tphere"); Tools.SendMessage(ply, "Terraria commands:"); Tools.SendMessage(ply, "/playing, /p, /me"); handler.Handled = true; From 7a809846cb17e60e7415246e39d267b4b81767d5 Mon Sep 17 00:00:00 2001 From: Deathmax Date: Wed, 1 Jun 2011 22:46:16 +0800 Subject: [PATCH 4/6] Implemented /heal --- TShockAPI/TShock.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 0be03b2f..ef21063c 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -389,6 +389,28 @@ namespace TShockAPI } } } + if (msg.StartsWith("/heal")) + { + var args = Regex.Split(msg, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); + int player = ply; + if (args.Length == 2) + player = Tools.FindPlayer((args[1].TrimEnd('"')).TrimStart('"')); + if (player != ply) + { + Tools.SendMessage(ply, string.Format("You just healed {0}", (args[1].TrimEnd('"')).TrimStart('"'))); + Tools.SendMessage(player, string.Format("{0} just healed you!", Tools.FindPlayer(ply))); + } + else + Tools.SendMessage(ply, "You just got healed!"); + for (int i = 0; i < 20; i++) + { + int itemid = Item.NewItem(1, 1, 1, 1, 58); + Main.item[itemid].position.X = (float)x; + Main.item[itemid].position.Y = (float)y; + NetMessage.SendData(21, -1, -1, "", itemid, 0f, 0f, 0f); + } + handler.Handled = true; + } } if (msg == "/help") { From 4feb9300209d4c82cc0ff33231ccd83496f353cd Mon Sep 17 00:00:00 2001 From: Deathmax Date: Wed, 1 Jun 2011 23:02:42 +0800 Subject: [PATCH 5/6] /butcher - kill all non-town NPCs - still glitchy, client doesn't properly update until the NPC is damaged --- TShockAPI/TShock.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index ef21063c..4b299602 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -411,6 +411,23 @@ namespace TShockAPI } handler.Handled = true; } + if (msg == "/butcher") + { + int killcount = 0; + for (int i = 0; i < Main.npc.Length; i++) + { + if (Main.npc[i].townNPC || !Main.npc[i].active) + continue; + else + { + Main.npc[i].StrikeNPC(99999, 90f, 1); + NetMessage.SendData(28, -1, -1, "", ply, (float)99999, 90f, 1); + killcount++; + } + } + Tools.Broadcast("Killed " + killcount.ToString() + " NPCs."); + handler.Handled = true; + } } if (msg == "/help") { From 1728112caafe7b23ba9b8d556c356654dbe1220b Mon Sep 17 00:00:00 2001 From: Deathmax Date: Wed, 1 Jun 2011 23:10:24 +0800 Subject: [PATCH 6/6] One line fix for /butcher --- TShockAPI/TShock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 4b299602..b50cda5e 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -421,7 +421,7 @@ namespace TShockAPI else { Main.npc[i].StrikeNPC(99999, 90f, 1); - NetMessage.SendData(28, -1, -1, "", ply, (float)99999, 90f, 1); + NetMessage.SendData(28, -1, -1, "", i, (float)99999, 90f, 1); killcount++; } }