Tweak item spawning to combine item names without quotes

Retains ability to spawn items to characters, its unlikely that player names will match at the last half of an item - and the item must also not exist for this to be an issue.
This commit is contained in:
Nilanth Animosus
2018-07-18 11:06:49 +01:00
parent ad681d5a26
commit 2df7d6a464
@@ -249,7 +249,7 @@ namespace Barotrauma
}; };
})); }));
commands.Add(new Command("spawnitem", "spawnitem [itemname] [cursor/inventory/random/[name]]: Spawn an item at the position of the cursor, in the inventory of the controlled character, in the inventory of the client with the given name, or at a random spawnpoint if the last parameter is omitted or \"random\".", commands.Add(new Command("spawnitem", "spawnitem [itemname] [cursor/inventory/cargo/random/[name]]: Spawn an item at the position of the cursor, in the inventory of the controlled character, in the inventory of the client with the given name, or at a random spawnpoint if the last parameter is omitted or \"random\".",
(string[] args) => (string[] args) =>
{ {
SpawnItem(args, GameMain.GameScreen.Cam.ScreenToWorld(PlayerInput.MousePosition), Character.Controlled, out string errorMsg); SpawnItem(args, GameMain.GameScreen.Cam.ScreenToWorld(PlayerInput.MousePosition), Character.Controlled, out string errorMsg);
@@ -2139,57 +2139,55 @@ namespace Barotrauma
Vector2? spawnPos = null; Vector2? spawnPos = null;
Inventory spawnInventory = null; Inventory spawnInventory = null;
if (args.Length > 1) int extraParams = 0;
switch (args.Last().ToLowerInvariant())
{ {
switch (args[1]) case "cursor":
{ extraParams = 1;
case "cursor": spawnPos = cursorPos;
spawnPos = cursorPos; break;
break; case "inventory":
case "inventory": extraParams = 1;
spawnInventory = controlledCharacter?.Inventory; spawnInventory = controlledCharacter == null ? null : controlledCharacter.Inventory;
break; break;
default: case "cargo":
//Check if last arg matches the name of an in-game player var wp = WayPoint.GetRandom(SpawnType.Cargo, null, Submarine.MainSub);
if (GameMain.Server != null) spawnPos = wp == null ? Vector2.Zero : wp.WorldPosition;
{ break;
var client = GameMain.Server.ConnectedClients.Find(c => c.Name.ToLower() == args.Last().ToLower()); //Dont do a thing, random is basically Human points anyways - its in the help description.
if (client == null) case "random":
{ extraParams = 1;
NewMessage("No player found with the name \"" + args.Last() + "\". Spawning item at random location. If the player you want to give the item to has a space in their name, try surrounding their name with quotes (\").", Color.Red); return;
break; default:
} extraParams = 0;
else if (client.Character == null) break;
{
errorMsg = "The player \"" + args.Last() + "\" is connected, but hasn't spawned yet.";
return;
}
else
{
//If the last arg matches the name of an in-game player, set the destination to their inventory.
spawnInventory = client.Character.Inventory;
break;
}
}
else
{
var matchingCharacter = FindMatchingCharacter(args.Skip(1).ToArray());
if (matchingCharacter?.Inventory != null) spawnInventory = matchingCharacter.Inventory;
}
break;
}
} }
string itemName = args[0]; string itemName = string.Join(" ", args.Take(args.Length - extraParams)).ToLowerInvariant();
var itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab; ItemPrefab itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
if (itemPrefab == null && extraParams == 0)
{
if (GameMain.Server != null)
{
var client = GameMain.Server.ConnectedClients.Find(c => c.Name.ToLower() == args.Last().ToLower());
if (client != null)
{
extraParams += 1;
itemName = string.Join(" ", args.Take(args.Length - extraParams)).ToLowerInvariant();
if (client.Character != null && client.Character.Name == args.Last().ToLower()) spawnInventory = client.Character.Inventory;
itemPrefab = MapEntityPrefab.Find(itemName) as ItemPrefab;
}
}
}
//Check again if the item can be found again after having checked for a character
if (itemPrefab == null) if (itemPrefab == null)
{ {
errorMsg = "Item \"" + itemName + "\" not found!"; errorMsg = "Item \"" + itemName + "\" not found!";
return; return;
} }
if (spawnPos == null && spawnInventory == null) if ((spawnPos == null || spawnPos == Vector2.Zero) && spawnInventory == null)
{ {
var wp = WayPoint.GetRandom(SpawnType.Human, null, Submarine.MainSub); var wp = WayPoint.GetRandom(SpawnType.Human, null, Submarine.MainSub);
spawnPos = wp == null ? Vector2.Zero : wp.WorldPosition; spawnPos = wp == null ? Vector2.Zero : wp.WorldPosition;
@@ -2198,6 +2196,7 @@ namespace Barotrauma
if (spawnPos != null) if (spawnPos != null)
{ {
Entity.Spawner.AddToSpawnQueue(itemPrefab, (Vector2)spawnPos); Entity.Spawner.AddToSpawnQueue(itemPrefab, (Vector2)spawnPos);
} }
else if (spawnInventory != null) else if (spawnInventory != null)
{ {