/// <summary> /// Try to convert the argument to byte. /// </summary> /// <param name="value">the converted value</param> /// <param name="error"> /// CommandResult.LangError("NUMBER_BETWEEN", byte.MinValue, byte.MaxValue) if out of range; // CommandResult.LangError("INVALID_NUMBER", src.ToString()) if invalid. /// </param> /// <returns>true if sucessfull, otherwise false</returns> public static bool TryConvertToByte(this ICommandArgument src, out byte value, out CommandResult error) { value = 0; error = null; try { value = byte.Parse(src.ToString()); return(true); } catch (OverflowException) { error = CommandResult.LangError("NUMBER_BETWEEN", byte.MinValue, byte.MaxValue); } catch (FormatException) { error = CommandResult.LangError("INVALID_NUMBER", src.ToString()); } return(false); }
/// <summary> /// Tries to find a player, if found then <paramref name="callback"/> will be called /// </summary> /// <param name="cmdArg"></param> /// <param name="callback"></param> public static bool TryGet(ICommandArgument cmdArg, Action <UPlayer> callback) { var player = From(cmdArg.ToString()); if (player != null) { callback(player); return(true); } return(false); }
public static void DropKit(ICommandSource src, ICommandArgument kitArg, Vector3 pos) { var kitManager = KitModule.Instance.KitManager; var kitName = kitArg.ToString(); if (!kitManager.Contains(kitName)) { EssLang.Send(src, "KIT_NOT_EXIST", kitName); } else { var kitItems = kitManager.GetByName(kitName).Items; kitItems.Where(i => i is KitItem).Cast <KitItem>().ForEach(i => ItemManager.dropItem(i.UnturnedItem, pos, true, true, true) ); } }
public static void DropKit(ICommandSource src, ICommandArgument kitArg, Vector3 pos) { var kitManager = KitModule.Instance.KitManager; var kitName = kitArg.ToString(); if (!kitManager.Contains(kitName)) { context.User.SendLocalizedMessage(Translations, "KIT_NOT_EXIST", kitName); return; } foreach (var item in kitManager.GetByName(kitName).Items) { if (item is KitItem kitItem) { ItemManager.dropItem(kitItem.UnturnedItem, pos, true, true, true); } if (item is KitItemVehicle vehicleItem) { VehicleManager.spawnVehicle(vehicleItem.Id, pos + (Vector3.up * 16), Quaternion.identity); } } }
private static void GiveItem(ICommandSource src, UPlayer target, ICommandArgument itemArg, ICommandArgument amountArg, bool allPlayers = false) { if (!src.HasPermission("essentials.command.item.other") && target != src) { EssLang.Send(src, "COMMAND_NO_PERMISSION"); return; } var optAsset = ItemUtil.GetItem(itemArg.ToString()); if (optAsset.IsAbsent) { EssLang.Send(src, "ITEM_NOT_FOUND", itemArg); return; } if (UEssentials.Config.GiveItemBlacklist.Contains(optAsset.Value.id) && !src.HasPermission("essentials.bypass.blacklist.item")) { EssLang.Send(src, "BLACKLISTED_ITEM", $"{optAsset.Value.itemName} ({optAsset.Value.id})"); return; } ushort amt = 1; if (amountArg != null) { if (!amountArg.IsShort) { EssLang.Send(src, "INVALID_NUMBER", amountArg); } else if (amountArg.ToShort <= 0) { EssLang.Send(src, "MUST_POSITIVE"); } else { amt = amountArg.ToUShort; goto give; } return; } give: var asset = optAsset.Value; var playersToReceive = new List <UPlayer>(); var item = new Item(asset.id, true); if (asset is ItemFuelAsset) { ItemUtil.Refuel(item); } if (!src.HasPermission("essentials.bypass.itemlimit") && amt > UEssentials.Config.ItemSpawnLimit) { amt = UEssentials.Config.ItemSpawnLimit; EssLang.Send(src, "ITEM_LIMIT", amt); } if (allPlayers) { UServer.Players.ForEach(playersToReceive.Add); EssLang.Send(src, "GIVEN_ITEM_ALL", amt, asset.itemName, asset.id); } else { playersToReceive.Add(target); if (!src.IsConsole && src.ToPlayer() == target) { goto give2; } EssLang.Send(src, "GIVEN_ITEM", amt, asset.itemName, asset.id, target.CharacterName); } give2: playersToReceive.ForEach(p => { var success = p.GiveItem(item, amt, true); EssLang.Send(p, "RECEIVED_ITEM", amt, asset.itemName, asset.id); if (!success) { EssLang.Send(p, "INVENTORY_FULL"); } }); }