using Newtonsoft.Json;
using Oxide.Core.Libraries.Covalence;
using System.Collections.Generic;
using System.Linq;
using WebSocketSharp;
/*
* This update 1.2.3
* Max Durability permission fixes
* other fixes/edits
*
* This update 1.3.0
* Added More Permissions
* Added More Config Options
*
* This update 1.4.0
* Partial Re-write
* Patched New Commands
* Added infinity ammo no reload required
* Added Weapon Names
* Added Drop Weapon on Death toggle/feature
*
* this update 1.4.1
* Fixed admin permission ops
* fixed inifinity issues
* fixed Drop Weapon on Death toggle/feature
*
* This update 1.4.2
* Added Skin Id Support on item crafted
* Added Ammo Type Support on item crafted
* Added Preloaded Magazine Size support on item crafted
* Updated Lang file correctly now
* Fixed command weapon name renaming issue
* More in the works.
*/
namespace Oxide.Plugins
{
[Info("Firearm Modifier", "Khan", "1.4.2")]
[Description("Allows you to change Magazine Size + Weapon Condition levels")]
public class FirearmModifier : CovalencePlugin
{
#region Fields
private string Admin = "firearmmodifier.admin";
private PluginConfig _config;
private static FirearmModifier _instance;
private List<string> Exclude = new List<string>
{
"bow_hunting.entity",
"compound_bow.entity",
"crossbow.entity"
};
#endregion
#region Config
protected override void LoadDefaultConfig() => _config = new PluginConfig();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<PluginConfig>();
if (_config == null)
{
throw new JsonException();
}
if (!_config.ToDictionary().Keys.SequenceEqual(Config.ToDictionary(x => x.Key, x => x.Value).Keys))
{
PrintWarning($"Configuration file {Name}.json was Updated");
SaveConfig();
}
}
catch
{
PrintError("Configuration file is corrupt! Loading Default Config");
PrintError(
"Your Configuration file is corrupt! Please verify your config file at
https://jsonlint.com/");
LoadDefaultConfig();
}
}
protected override void SaveConfig() => Config.WriteObject(_config, true);
private class PluginConfig
{
[JsonProperty("Weapon Options")]
public Dictionary<string, WeaponConfig> WeaponOptions = new Dictionary<string, WeaponConfig>();
public string ToJson() =>
JsonConvert.SerializeObject(this);
public Dictionary<string, object> ToDictionary() =>
JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson());
}
private class WeaponConfig
{
public Dictionary<string, PermissionSetting> PermissionSettings = new Dictionary<string, PermissionSetting>();
[JsonIgnore]
public string[] Permissions => PermissionSettings.Keys.ToArray();
public PermissionSetting FindPermissionSetting(string permission)
{
PermissionSetting permissionSetting;
return PermissionSettings.TryGetValue(permission, out permissionSetting) ? permissionSetting : null;
}
}
public class PermissionSetting
{
public ulong SkinId;
public int MagazineSize;
public int PreloadSize;
public float Condition;
public bool InfiniteCondition;
public bool InfiniteAmmo;
public bool DropOnDeath;
public string WeaponName = "";
public string AmmoType = "";
public void SetSkinId(IPlayer player, string displayName, string value)
{
ulong id;
if (!ulong.TryParse(value, out id))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
SkinId = id;
player.Reply(GetMessage("Success", player.Id, displayName, "skin", SkinId));
}
public void SetMagazineSize(IPlayer player, string displayName, string value)
{
int size;
if (!int.TryParse(value, out size))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
MagazineSize = size;
player.Reply(GetMessage("Success", player.Id, displayName, "magazinesize", MagazineSize));
}
public void SetPreloadSize(IPlayer player, string displayName, string value)
{
int size;
if (!int.TryParse(value, out size))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
PreloadSize = size;
player.Reply(GetMessage("Success", player.Id, displayName, "preloadsize", PreloadSize));
}
public void SetCondition(IPlayer player, string displayName, string value)
{
float size;
if (!float.TryParse(value, out size))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
Condition = size;
player.Reply(GetMessage("Success", player.Id, displayName, "condition", Condition));
}
public void SetMax(IPlayer player, string displayName, string value)
{
bool boolean;
if (!bool.TryParse(value, out boolean))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
InfiniteCondition = boolean;
player.Reply(GetMessage("Success", player.Id, displayName, "max", InfiniteCondition));
}
public void SetMaxAmmo(IPlayer player, string displayName, string value)
{
bool boolean;
if (!bool.TryParse(value, out boolean))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
InfiniteAmmo = boolean;
player.Reply(GetMessage("Success", player.Id, displayName, "maxammo", InfiniteAmmo));
}
public void SetDropOnDeath(IPlayer player, string displayName, string value)
{
bool boolean;
if (!bool.TryParse(value, out boolean))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
DropOnDeath = boolean;
player.Reply(GetMessage("Success", player.Id, displayName, "drop", DropOnDeath));
}
public void SetWeaponName(IPlayer player, string displayName, string value)
{
if (string.IsNullOrEmpty(value))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
WeaponName = value;
player.Reply(GetMessage("Success", player.Id, displayName, "name", WeaponName));
}
public void SetAmmoType(IPlayer player, string displayName, string value)
{
if (string.IsNullOrEmpty(value))
{
player.Message(GetMessage("Syntax", player.Id));
return;
}
AmmoType = value;
player.Reply(GetMessage("Success", player.Id, displayName, "ammotype", AmmoType));
}
}
#endregion
#region Oxide
private void OnServerInitialized()
{
foreach (var itemDefinition in ItemManager.itemList)
{
ItemModEntity itemModEntity = itemDefinition.GetComponent<ItemModEntity>();
if (itemModEntity == null) continue;
BaseProjectile baseProjectile = itemModEntity.entityPrefab?.Get()?.GetComponent<BaseProjectile>();
if (baseProjectile == null) continue;
if (Exclude.Contains(itemDefinition.shortname) || _config.WeaponOptions.ContainsKey(itemDefinition.shortname)) continue;
_config.WeaponOptions.Add(itemDefinition.shortname, new WeaponConfig
{
PermissionSettings = new Dictionary<string, PermissionSetting>
{
{
"firearmmodifier.1",
new PermissionSetting
{
MagazineSize = baseProjectile.primaryMagazine.definition.builtInSize,
PreloadSize = baseProjectile.primaryMagazine.contents,
Condition = itemDefinition.condition.max,
InfiniteCondition = false,
InfiniteAmmo = false,
DropOnDeath = false,
WeaponName = "",
AmmoType = "",
SkinId = 0
}
},
{
"firearmmodifier.2",
new PermissionSetting
{
MagazineSize = baseProjectile.primaryMagazine.definition.builtInSize,
PreloadSize = baseProjectile.primaryMagazine.contents,
Condition = itemDefinition.condition.max,
InfiniteCondition = false,
InfiniteAmmo = false,
DropOnDeath = false,
WeaponName = "",
AmmoType = "",
SkinId = 0
}
},
{
"firearmmodifier.3",
new PermissionSetting
{
MagazineSize = baseProjectile.primaryMagazine.definition.builtInSize,
PreloadSize = baseProjectile.primaryMagazine.contents,
Condition = itemDefinition.condition.max,
InfiniteCondition = false,
InfiniteAmmo = false,
DropOnDeath = false,
WeaponName = "",
AmmoType = "",
SkinId = 0
}
},
{
"firearmmodifier.4",
new PermissionSetting
{
MagazineSize = baseProjectile.primaryMagazine.definition.builtInSize,
PreloadSize = baseProjectile.primaryMagazine.contents,
Condition = itemDefinition.condition.max,
InfiniteCondition = false,
InfiniteAmmo = false,
DropOnDeath = false,
WeaponName = "",
AmmoType = "",
SkinId = 0
}
},
{
"firearmmodifier.5",
new PermissionSetting
{
MagazineSize = baseProjectile.primaryMagazine.definition.builtInSize,
PreloadSize = baseProjectile.primaryMagazine.contents,
Condition = itemDefinition.condition.max,
InfiniteCondition = false,
InfiniteAmmo = false,
DropOnDeath = false,
WeaponName = "",
AmmoType = "",
SkinId = 0
}
}
}
});
}
SaveConfig();
}
private void Init()
{
permission.RegisterPermission(Admin, this);
}
private void Loaded()
{
_instance = this;
foreach (var items in _config.WeaponOptions.Select(x => x.Value.PermissionSettings.Keys).ToArray())
{
foreach (var item in items)
{
if (permission.PermissionExists(item, this)) continue;
permission.RegisterPermission(item, this);
}
}
}
private void Unload()
{
_instance = null;
}
#endregion
#region Core
private WeaponConfig FindWeaponOption(string shortname)
{
WeaponConfig weaponConfig;
return _config.WeaponOptions.TryGetValue(shortname, out weaponConfig) ? weaponConfig : null;
}
private void OnReloadWeapon(BasePlayer player, BaseProjectile projectile)
{
Item item = projectile.GetItem();
if (item == null) return;
WeaponConfig weaponOptions = FindWeaponOption(item.info.shortname);
if (weaponOptions == null) return;
foreach (var perm in weaponOptions.Permissions)
{
if (!permission.UserHasPermission(player.UserIDString, perm)) continue;
projectile.primaryMagazine.definition.builtInSize = weaponOptions.PermissionSettings[perm].MagazineSize;
projectile.primaryMagazine.capacity = weaponOptions.PermissionSettings[perm].MagazineSize;
if (!weaponOptions.PermissionSettings[perm].AmmoType.IsNullOrEmpty())
{
var ammo = ItemManager.FindItemDefinition(weaponOptions.PermissionSettings[perm].AmmoType);
projectile.primaryMagazine.ammoType = ammo;
}
projectile.SendNetworkUpdateImmediate();
break;
}
}
private void OnItemCraftFinished(ItemCraftTask task, Item item)
{
BaseProjectile projectile = item?.GetHeldEntity() as BaseProjectile;
if (projectile == null) return;
WeaponConfig weaponOptions = FindWeaponOption(item.info.shortname);
if (weaponOptions == null) return;
foreach (var perm in weaponOptions.Permissions)
{
if (!permission.UserHasPermission(task.owner.UserIDString, perm)) continue;
item._maxCondition = weaponOptions.PermissionSettings[perm].Condition;
item.condition = weaponOptions.PermissionSettings[perm].Condition;
projectile.primaryMagazine.contents = weaponOptions.PermissionSettings[perm].PreloadSize;
if (!weaponOptions.PermissionSettings[perm].AmmoType.IsNullOrEmpty())
{
var ammo = ItemManager.FindItemDefinition(weaponOptions.PermissionSettings[perm].AmmoType);
projectile.primaryMagazine.ammoType = ammo;
}
item.skin = weaponOptions.PermissionSettings[perm].SkinId;
if (!weaponOptions.PermissionSettings[perm].WeaponName.IsNullOrEmpty())
{
item.name = weaponOptions.PermissionSettings[perm].WeaponName;
item.MarkDirty();
}
projectile.SendNetworkUpdateImmediate();
break;
}
}
private void OnLoseCondition(Item item)
{
BasePlayer player = item.GetOwnerPlayer();
if (player == null) return;
WeaponConfig weaponOptions = FindWeaponOption(item.info.shortname);
if (weaponOptions == null) return;
foreach (var perm in weaponOptions.Permissions)
{
if (!permission.UserHasPermission(player.UserIDString, perm)) continue;
if (permission.UserHasPermission(player.UserIDString, perm) && weaponOptions.PermissionSettings[perm].InfiniteCondition == false) continue;
item.condition = weaponOptions.PermissionSettings[perm].Condition;
break;
}
}
private void OnWeaponFired(BaseProjectile projectile, BasePlayer player)
{
Item item = projectile.GetItem();
if (item == null) return;
WeaponConfig weaponOptions = FindWeaponOption(item.info.shortname);
if (weaponOptions == null) return;
foreach (var perm in weaponOptions.Permissions)
{
if (!permission.UserHasPermission(player.UserIDString, perm)) continue;
if (permission.UserHasPermission(player.UserIDString, perm) && weaponOptions.PermissionSettings[perm].InfiniteAmmo == false) continue;
if (projectile.primaryMagazine.contents > 0) continue;
projectile.primaryMagazine.contents = projectile.primaryMagazine.capacity;
projectile.SendNetworkUpdateImmediate();
break;
}
}
private void OnItemAddedToContainer(ItemContainer container, Item item)
{
BasePlayer player = item.GetOwnerPlayer();
if (player == null) return;
WeaponConfig weaponOptions = FindWeaponOption(item.info.shortname);
if (weaponOptions == null) return;
foreach (var perm in weaponOptions.Permissions)
{
if (!permission.UserHasPermission(player.UserIDString, perm)) continue;
if (weaponOptions.PermissionSettings[perm].WeaponName.IsNullOrEmpty()) continue;
item.name = weaponOptions.PermissionSettings[perm].WeaponName;
item.MarkDirty();
break;
}
}
bool CanDropActiveItem(BasePlayer player)
{
try
{
Item item = player.GetActiveItem();
if (item == null) return false;
WeaponConfig weaponOptions = FindWeaponOption(item.info.shortname);
if (weaponOptions == null) return false;
if (player == null || player.IsNpc || player is NPCMurderer) return false;
foreach (var perm in weaponOptions.Permissions)
{
if (!permission.UserHasPermission(player.UserIDString, perm)) return true;
if (permission.UserHasPermission(player.UserIDString, perm) && !weaponOptions.PermissionSettings[perm].DropOnDeath) return true;
if (permission.UserHasPermission(player.UserIDString, perm) && weaponOptions.PermissionSettings[perm].DropOnDeath) return false;
}
}
catch (System.Exception ex)
{
Puts("CanDropActiveItem threw Exception: " + ex);
throw;
}
return false;
}
#endregion
#region Commands
[Command("modify")]
private void Cmdmodify(IPlayer player, string command, string[] args)
{
if (!player.HasPermission("firearmmodifier.admin"))
{
player.Message(GetMessage("NoPerm", player.Id));
return;
}
if (args.Length < 3)
{
player.Reply(GetMessage("Syntax", player.Id));
return;
}
WeaponConfig weaponoption = FindWeaponOption(args[0].ToLower());
if (weaponoption == null)
{
player.Reply(GetMessage("InvalidSelection", player.Id));
return;
}
ItemDefinition itemDefinition = ItemManager.FindItemDefinition(args[0]);
if (itemDefinition == null)
{
player.Reply(GetMessage("InvalidItem", player.Id));
return;
}
PermissionSetting permissionSetting = weaponoption.FindPermissionSetting(args[1]);
if (permissionSetting == null)
{
player.Reply(GetMessage("InvalidPermission", player.Id));
return;
}
switch (args[2].ToLower())
{
case "magazinesize":
permissionSetting.SetMagazineSize(player, itemDefinition.displayName.english, args[3]);
SaveConfig();
break;
case "preloadsize":
permissionSetting.SetPreloadSize(player, itemDefinition.displayName.english, args[3]);
SaveConfig();
break;
case "skin":
permissionSetting.SetSkinId(player, itemDefinition.displayName.english, args[3]);
SaveConfig();
break;
case "condition":
permissionSetting.SetCondition(player, itemDefinition.displayName.english, args[3]);
SaveConfig();
break;
case "max":
permissionSetting.SetMax(player, itemDefinition.displayName.english, args[3]);
SaveConfig();
break;
case "maxammo":
permissionSetting.SetMaxAmmo(player, itemDefinition.displayName.english, args[3]);
SaveConfig();
break;
case "drop":
permissionSetting.SetDropOnDeath(player, itemDefinition.displayName.english, args[3]);
break;
case "name":
permissionSetting.SetWeaponName(player, itemDefinition.displayName.english, string.Join(" ",args[3]));
SaveConfig();
break;
case "ammotype":
permissionSetting.SetAmmoType(player, itemDefinition.displayName.english, string.Join(" ",args[3]));
SaveConfig();
break;
default:
player.Message(GetMessage("Syntax", player.Id));
break;
}
}
#endregion
#region lang
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
{"InvalidSelection", "You've selected an invalid weapon to modify please type a valid weapon shortname"},
{"Syntax", "Invalid Params please do /modify shortname permission setting value"},
{"InvalidPermission", "Invalid Permission Selected please check config for current permission names"},
{"InvalidItem", "Invalid item. Please provide a valid item name"},
{"NoPerm", "Unknown Command: modify"},
{"Success", "You've successfully set {0} {1} to {2}"}
}, this);
}
private static string GetMessage(string key, string userId = null, params object[] args) => string.Format(_instance.lang.GetMessage(key, _instance, userId), args);
#endregion
}
}
FirearmModifier.cs(502,57): error CS0150: A constant value is expected