feat(spawner): Adds where for EditSpawner command (#663)

Adds `where` option for EditSpawner command

Closes #547
This commit is contained in:
Kamron Batman 2021-07-20 20:19:27 -07:00 committed by GitHub
parent c9ce597573
commit 26592f202e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,6 +20,7 @@ using static Server.Types;
namespace Server.Engines.Spawners
{
public class EditSpawnCommand : BaseCommand
{
public static void Initialize()
@ -33,7 +34,7 @@ namespace Server.Engines.Spawners
Supports = CommandSupport.Complex | CommandSupport.Simple;
Commands = new[] { "EditSpawner" };
ObjectTypes = ObjectTypes.Items;
Usage = "EditSpawner <type> <arguments> set <properties>";
Usage = "EditSpawner <type> <arguments> set <properties> where <properties>";
Description = "Modifies spawners arguments and properties for the given type";
ListOptimized = true;
}
@ -66,18 +67,29 @@ namespace Server.Engines.Spawners
var argSpan = e.ArgString.AsSpan(name.Length + 1);
var setIndex = argSpan.InsensitiveIndexOf("set ");
var where = argSpan.InsensitiveIndexOf("where ");
ReadOnlySpan<char> props = null;
ReadOnlySpan<char> props = null, findmatch = null;
if (setIndex > -1)
if (setIndex > -1 || where > -1)
{
var start = setIndex + 4;
props = argSpan[start..];
argSpan = argSpan[..setIndex];
var len = where > -1 ? where : argSpan.Length;
findmatch = argSpan.Slice(len < argSpan.Length ? len + 6 : argSpan.Length);
if (setIndex > -1)
{
props = argSpan[start..^len];
argSpan = argSpan[..setIndex];
}
else
{
argSpan = argSpan[..^len];
}
}
var argStr = argSpan.ToString().DefaultIfNullOrEmpty(null);
var propsStr = props.ToString().DefaultIfNullOrEmpty(null);
var whereStr = findmatch.ToString().DefaultIfNullOrEmpty(null);
e.Mobile.SendMessage("Updating spawners...");
@ -85,19 +97,29 @@ namespace Server.Engines.Spawners
{
if (obj is BaseSpawner spawner)
{
UpdateSpawner(spawner, name, argStr, propsStr);
UpdateSpawner(spawner, name, argStr, propsStr, whereStr);
}
}
e.Mobile.SendMessage("Update completed.");
}
public static void UpdateSpawner(BaseSpawner spawner, string name, string arguments, string properties)
public static void UpdateSpawner(BaseSpawner spawner, string name, string arguments, string properties, string find = null)
{
foreach (var entry in spawner.Entries)
{
// TODO: Should cache spawn type on the entry
if (entry.SpawnedName.InsensitiveEquals(name))
if (!entry.SpawnedName.InsensitiveEquals(name))
{
continue;
}
var found = find != null ? entry.Properties?.LastIndexOf(find, StringComparison.OrdinalIgnoreCase) : null;
var shouldUpdate = find == null || found > -1 &&
(found + find.Length == entry.Properties.Length ||
char.IsWhiteSpace(entry.Properties[(int)found + find.Length]));
if (shouldUpdate)
{
if (arguments != null)
{