C#:
using HarmonyLib;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using System.Collections.Generic;
using System.Linq;
namespace Oxide.Plugins
{
[Info("DrugsAPI", "Yaki", "1.0.0")]
[Description("Centralized API for drug item definitions and creation across plugins.")]
class DrugsAPI : RustPlugin
{
#region Variables
private Dictionary<string, ItemConfig> _itemConfigByDisplayName;
private Dictionary<string, ItemConfig.DrugItemConfig> _drugItemConfigByDisplayName;
private Dictionary<ulong, ItemConfig> _itemConfigBySkinID;
private Dictionary<ulong, ItemConfig> _itemConfigByCloneSkinID;
private Dictionary<ulong, ItemConfig.DrugItemConfig> _drugItemConfigBySkinID;
#endregion Variables
#region Hooks
private void OnServerInitialized()
{
BuildItemConfigLookups();
}
#endregion Hooks
#region Patches
[AutoPatch]
[HarmonyPatch(typeof(Item), "SplitItem", typeof(int))]
internal class Item_SplitItem
{
static void Postfix(ref Item __result, ref Item __instance)
{
if (__result != null && !string.IsNullOrEmpty(__instance.name))
{
__result.name = __instance.name;
}
}
}
#endregion Patches
#region API
[HookMethod("API_GetAllItemConfigs")]
public List<Dictionary<string, object>> API_GetAllItemConfigs()
{
return _config.Drugs.Select(d => ItemConfigToDict(d)).ToList();
}
[HookMethod("API_GetItemConfig")]
public Dictionary<string, object> API_GetItemConfig(string displayName)
{
if (_itemConfigByDisplayName.TryGetValue(displayName, out var cfg))
return ItemConfigToDict(cfg);
return null;
}
[HookMethod("API_GetItemConfigBySkinID")]
public Dictionary<string, object> API_GetItemConfigBySkinID(ulong skinID)
{
if (_itemConfigBySkinID.TryGetValue(skinID, out var cfg))
return ItemConfigToDict(cfg);
if (_itemConfigByCloneSkinID.TryGetValue(skinID, out cfg))
return ItemConfigToDict(cfg);
return null;
}
[HookMethod("API_CreateDrugSeed")]
public Item API_CreateDrugSeed(string displayName, int amount)
{
if (!_itemConfigByDisplayName.TryGetValue(displayName, out var cfg))
return null;
var item = ItemManager.CreateByName(cfg.ItemShortName, amount, cfg.SkinID);
if (item != null)
item.name = cfg.DisplayName;
return item;
}
[HookMethod("API_CreateDrug")]
public Item API_CreateDrug(string displayName, int amount)
{
if (!_drugItemConfigByDisplayName.TryGetValue(displayName, out var cfg))
return null;
var item = ItemManager.CreateByName(cfg.DrugItemShortName, amount, cfg.DrugSkinID);
if (item != null)
item.name = cfg.DrugDisplayName;
return item;
}
[HookMethod("API_CreateDrugClone")]
public Item API_CreateDrugClone(string displayName, int amount)
{
var cloneCfg = _config.Drugs
.Select(d => d.DrugItemClone)
.FirstOrDefault(c => c != null && c.DrugItemCloneDisplayName == displayName);
if (cloneCfg == null)
return null;
var item = ItemManager.CreateByName(cloneCfg.DrugItemCloneShortName, amount, cloneCfg.DrugItemCloneSkinID);
if (item != null)
item.name = cloneCfg.DrugItemCloneDisplayName;
return item;
}
[HookMethod("API_CalculateHarvestAmount")]
public int API_CalculateHarvestAmount(int yCount, int gCount, int hCount, int wCount, int xCount)
{
var g = _config.DrugGeneticsSettings;
return yCount * g.Y + gCount * g.G + hCount * g.H + wCount * g.W + xCount * g.X;
}
#endregion API
#region Methods
private void BuildItemConfigLookups()
{
_itemConfigByDisplayName = new Dictionary<string, ItemConfig>(_config.Drugs.Count);
_drugItemConfigByDisplayName = new Dictionary<string, ItemConfig.DrugItemConfig>(_config.Drugs.Count);
_itemConfigBySkinID = new Dictionary<ulong, ItemConfig>(_config.Drugs.Count);
_itemConfigByCloneSkinID = new Dictionary<ulong, ItemConfig>(_config.Drugs.Count);
_drugItemConfigBySkinID = new Dictionary<ulong, ItemConfig.DrugItemConfig>(_config.Drugs.Count);
foreach (var cfg in _config.Drugs)
{
_itemConfigByDisplayName[cfg.DisplayName] = cfg;
if (cfg.DrugItem != null)
{
_drugItemConfigByDisplayName[cfg.DrugItem.DrugDisplayName] = cfg.DrugItem;
if (cfg.DrugItem.DrugSkinID != 0)
_drugItemConfigBySkinID[cfg.DrugItem.DrugSkinID] = cfg.DrugItem;
}
if (cfg.SkinID != 0)
_itemConfigBySkinID[cfg.SkinID] = cfg;
if (cfg.DrugItemClone != null && cfg.DrugItemClone.DrugItemCloneSkinID != 0)
_itemConfigByCloneSkinID[cfg.DrugItemClone.DrugItemCloneSkinID] = cfg;
}
}
private Dictionary<string, object> ItemConfigToDict(ItemConfig cfg)
{
var drugs = new Dictionary<string, object>
{
["DisplayName"] = cfg.DisplayName,
["ItemShortName"] = cfg.ItemShortName,
["SkinID"] = cfg.SkinID,
["ItemAmount"] = cfg.ItemAmount,
["ItemImageName"] = cfg.ItemImageName ?? string.Empty,
["ItemDescription"] = cfg.ItemDescription ?? string.Empty
};
if (cfg.DrugItem != null)
{
drugs["DrugDisplayName"] = cfg.DrugItem.DrugDisplayName;
drugs["DrugItemShortName"] = cfg.DrugItem.DrugItemShortName;
drugs["DrugSkinID"] = cfg.DrugItem.DrugSkinID;
drugs["DrugItemImageName"] = cfg.DrugItem.DrugItemImageName ?? string.Empty;
drugs["DrugItemDescription"] = cfg.DrugItem.DrugItemDescription ?? string.Empty;
}
if (cfg.DrugItemClone != null)
{
drugs["DrugItemCloneShortName"] = cfg.DrugItemClone.DrugItemCloneShortName;
drugs["DrugItemCloneDisplayName"] = cfg.DrugItemClone.DrugItemCloneDisplayName;
drugs["DrugItemCloneSkinID"] = cfg.DrugItemClone.DrugItemCloneSkinID;
drugs["DrugItemCloneImageName"] = cfg.DrugItemClone.DrugItemCloneImageName ?? string.Empty;
drugs["DrugItemCloneAmount"] = cfg.DrugItemClone.DrugItemCloneAmount;
}
return drugs;
}
#endregion Methods
#region Config
private PluginConfig _config;
protected override void LoadDefaultConfig()
{
Puts("Creating a default config...");
_config = PluginConfig.DefaultConfig();
_config.PluginVersion = Version;
SaveConfig();
Puts("Creation of the default config completed!");
}
protected override void LoadConfig()
{
base.LoadConfig();
_config = Config.ReadObject<PluginConfig>();
if (_config.PluginVersion < Version) UpdateConfigValues();
}
private void UpdateConfigValues()
{
Puts("Config update detected! Updating config values...");
if (_config.PluginVersion < new VersionNumber(1, 0, 0))
{
}
_config.PluginVersion = Version;
SaveConfig();
Puts("Config update to version {0} completed!", Version);
}
protected override void SaveConfig() => Config.WriteObject(_config);
private class ItemConfig
{
[JsonProperty(PropertyName = "Item Shortname")] public string ItemShortName { get; set; }
[JsonProperty(PropertyName = "Item Amount")] public int ItemAmount { get; set; }
[JsonProperty(PropertyName = "Skin ID")] public ulong SkinID { get; set; }
[JsonProperty(PropertyName = "Display Name")] public string DisplayName { get; set; }
[JsonProperty(PropertyName = "Item Description")] public string ItemDescription { get; set; }
[JsonProperty(PropertyName = "Item Image Name")] public string ItemImageName { get; set; }
[JsonProperty(PropertyName = "Drug Item")] public DrugItemConfig DrugItem { get; set; }
[JsonProperty(PropertyName = "Drug Item Clone")] public DrugItemCloneConfig DrugItemClone { get; set; }
public class DrugItemConfig
{
[JsonProperty(PropertyName = "Drug Item Shortname")] public string DrugItemShortName { get; set; }
[JsonProperty(PropertyName = "Drug Item Skin ID")] public ulong DrugSkinID { get; set; }
[JsonProperty(PropertyName = "Drug Item Display Name")] public string DrugDisplayName { get; set; }
[JsonProperty(PropertyName = "Drug Item Description")] public string DrugItemDescription { get; set; }
[JsonProperty(PropertyName = "Drug Item Image Name")] public string DrugItemImageName { get; set; }
}
public class DrugItemCloneConfig
{
[JsonProperty(PropertyName = "Drug Item Clone Shortname")] public string DrugItemCloneShortName { get; set; }
[JsonProperty(PropertyName = "Drug Item Clone Display Name")] public string DrugItemCloneDisplayName { get; set; }
[JsonProperty(PropertyName = "Drug Item Clone Image Name")] public string DrugItemCloneImageName { get; set; }
[JsonProperty(PropertyName = "Drug Item Clone Skin ID")] public ulong DrugItemCloneSkinID { get; set; }
[JsonProperty(PropertyName = "Drug Item Clones To Give When Cloning")] public int DrugItemCloneAmount { get; set; }
}
}
private class DrugGeneticsConfig
{
[JsonProperty(PropertyName = "Drug Genetic G")] public int G { get; set; }
[JsonProperty(PropertyName = "Drug Genetic Y")] public int Y { get; set; }
[JsonProperty(PropertyName = "Drug Genetic H")] public int H { get; set; }
[JsonProperty(PropertyName = "Drug Genetic W")] public int W { get; set; }
[JsonProperty(PropertyName = "Drug Genetic X")] public int X { get; set; }
}
private class PluginConfig
{
[JsonProperty(PropertyName = "Drugs")] public List<ItemConfig> Drugs { get; set; }
[JsonProperty(PropertyName = "Drug Genetics (sum of values for genes is # drugs player gets when harvesting)")] public DrugGeneticsConfig DrugGeneticsSettings { get; set; }
[JsonProperty("Configuration version")] public VersionNumber PluginVersion { get; set; }
public static PluginConfig DefaultConfig()
{
return new PluginConfig
{
Drugs = new List<ItemConfig>
{
new ItemConfig
{
ItemShortName = "seed.hemp",
ItemAmount = 1,
SkinID = 3354230033,
DisplayName = "Weed Seeds",
ItemImageName = "weedseeds",
ItemDescription = "Grows into a Weed Leaf",
DrugItem = new ItemConfig.DrugItemConfig
{
DrugItemShortName = "cloth",
DrugSkinID = 3354227129,
DrugDisplayName = "Weed Leaf",
DrugItemDescription = "Can be sold for currency.",
DrugItemImageName = "weedleaf"
},
DrugItemClone = new ItemConfig.DrugItemCloneConfig
{
DrugItemCloneShortName = "clone.hemp",
DrugItemCloneDisplayName = "Weed Clone",
DrugItemCloneImageName = "weedseeds",
DrugItemCloneSkinID = 3354230033,
DrugItemCloneAmount = 2
}
},
new ItemConfig
{
ItemShortName = "seed.hemp",
ItemAmount = 1,
SkinID = 3354230311,
DisplayName = "Coca Seeds",
ItemImageName = "cocaseeds",
ItemDescription = "Grows into a Cocaine Leaf",
DrugItem = new ItemConfig.DrugItemConfig
{
DrugItemShortName = "cloth",
DrugSkinID = 3354228232,
DrugDisplayName = "Cocaine Leaf",
DrugItemDescription = "Can be sold for currency.",
DrugItemImageName = "cocaleaf"
},
DrugItemClone = new ItemConfig.DrugItemCloneConfig
{
DrugItemCloneShortName = "clone.hemp",
DrugItemCloneDisplayName = "Cocaine Clone",
DrugItemCloneImageName = "cocaseeds",
DrugItemCloneSkinID = 3354230311,
DrugItemCloneAmount = 2
}
},
new ItemConfig
{
ItemShortName = "seed.hemp",
ItemAmount = 1,
SkinID = 3354229737,
DisplayName = "Tobacco Seeds",
ItemImageName = "tobaccoseeds",
ItemDescription = "Grows into a Tobacco Leaf",
DrugItem = new ItemConfig.DrugItemConfig
{
DrugItemShortName = "cloth",
DrugSkinID = 3354228677,
DrugDisplayName = "Tobacco Leaf",
DrugItemDescription = "Can be sold for currency.",
DrugItemImageName = "tobaccoleaf"
},
DrugItemClone = new ItemConfig.DrugItemCloneConfig
{
DrugItemCloneShortName = "clone.hemp",
DrugItemCloneDisplayName = "Tobacco Clone",
DrugItemCloneImageName = "tobaccoseeds",
DrugItemCloneSkinID = 3354229737,
DrugItemCloneAmount = 2
}
},
new ItemConfig
{
ItemShortName = "seed.hemp",
ItemAmount = 1,
SkinID = 3354229356,
DisplayName = "Poppy Seeds",
ItemImageName = "poppyseeds",
ItemDescription = "Grows into an Opium Leaf",
DrugItem = new ItemConfig.DrugItemConfig
{
DrugItemShortName = "cloth",
DrugSkinID = 3354228480,
DrugDisplayName = "Opium Leaf",
DrugItemDescription = "Can be sold for currency.",
DrugItemImageName = "poppyleaf"
},
DrugItemClone = new ItemConfig.DrugItemCloneConfig
{
DrugItemCloneShortName = "clone.hemp",
DrugItemCloneDisplayName = "Opium Clone",
DrugItemCloneImageName = "poppyseeds",
DrugItemCloneSkinID = 3354229356,
DrugItemCloneAmount = 2
}
}
},
DrugGeneticsSettings = new DrugGeneticsConfig
{
G = 3,
Y = 5,
H = 2,
W = 1,
X = 1
},
PluginVersion = new VersionNumber()
};
}
}
#endregion Config
}
}