From a8a08caa4bd2a54602f79ef0150abaa711633714 Mon Sep 17 00:00:00 2001 From: White Date: Thu, 10 Jul 2014 17:58:20 +0930 Subject: [PATCH] Add /tpnpc to allow teleportation to active npcs --- TShockAPI/Commands.cs | 42 ++++++++++++++++++++++++++++++++++++++++ TShockAPI/Permissions.cs | 3 +++ 2 files changed, 45 insertions(+) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 529df43f..f5191b9a 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -398,6 +398,11 @@ namespace TShockAPI AllowServer = false, HelpText = "Teleports a player to yourself." }); + add(new Command(Permissions.tpnpcs, TPNpc, "tpnpc") + { + AllowServer = false, + HelpText = "Teleports you to an npc" + }); add(new Command(Permissions.tppos, TPPos, "tppos") { AllowServer = false, @@ -2067,6 +2072,43 @@ namespace TShockAPI } } + private static void TPNpc(CommandArgs args) + { + if (args.Parameters.Count < 1) + { + args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /tpnpc "); + return; + } + + var npcStr = string.Join(" ", args.Parameters); + var matches = new List(); + foreach (var npc in Main.npc.Where(npc => npc.active)) + { + if (string.Equals(npc.name, npcStr, StringComparison.CurrentCultureIgnoreCase)) + { + matches = new List { npc }; + break; + } + if (npc.name.ToLower().StartsWith(npcStr.ToLower())) + matches.Add(npc); + } + + if (matches.Count > 1) + { + TShock.Utils.SendMultipleMatchError(args.Player, matches.Select(n => n.name)); + return; + } + if (matches.Count == 0) + { + args.Player.SendErrorMessage("Invalid Npc!"); + return; + } + + var target = matches[0]; + args.Player.Teleport(target.position.X, target.position.Y); + args.Player.SendSuccessMessage("Teleported to the {0}", target.name); + } + private static void TPPos(CommandArgs args) { if (args.Parameters.Count != 2) diff --git a/TShockAPI/Permissions.cs b/TShockAPI/Permissions.cs index 01e30d55..a67382c1 100644 --- a/TShockAPI/Permissions.cs +++ b/TShockAPI/Permissions.cs @@ -213,6 +213,9 @@ namespace TShockAPI [Description("User can teleport to tile positions.")] public static readonly string tppos = "tshock.tp.pos"; + [Description("User can teleport to npcs.")] + public static readonly string tpnpcs = "tshock.tp.npcs"; + [Description("Users can stop people from teleporting.")] public static readonly string tpallow = "tshock.tp.block";