C#:
using Oxide.Core;
using System.Collections.Generic;
namespace Oxide.Plugins {
[Info("SmeltOnGather", "Death", "1.0.7")]
class SmeltOnGather : RustPlugin {
#region Declarations
const string perm = "smeltongather.use";
#endregion
#region Hooks
void OnServerInitialized() {
LoadConfig();
LoadData();
if (!options.SaveWithServer) {
Unsubscribe("OnServerSave");
}
permission.RegisterPermission(perm, this);
}
void Unload() {
SaveData();
}
void OnServerSave() {
SaveData();
}
void OnPlayerConnected(BasePlayer player) {
if (options.OnByDefault && permission.UserHasPermission(player.UserIDString, perm) && !data.Preferences.ContainsKey(player.userID)) {
data.Preferences.Add(player.userID, new Preferences {
CharcoalEnabled = true, OreEnabled = true, PickupEnabled = true
}
);
SendReply(player, "Auto smelting on gather automatically enabled. Use /smelt to disable it!");
}
}
object OnDispenserGather(ResourceDispenser dispenser, BasePlayer player, Item item) {
if (player == null || item == null || item.info == null) {
return null;
}
if (!data.Preferences.TryGetValue(player.userID, out preference)) {
return null;
}
var cookComponent = item.info.GetComponent<ItemModCookable>();
var amountMultiplied = item.amount * options.Multiplier;
// Extra null check to handle charcoal.
if (cookComponent == null && preference.CharcoalEnabled && item.info.GetComponent<ItemModBurnable>() != null) {
player.GiveItem(ItemManager.CreateByItemID(-1938052175, amountMultiplied));
item.Remove(0f);
return true;
}
if (cookComponent == null) {
// Giving item sent from OnCollectiblePickup if no component was found.
if (dispenser == null) {
player.GiveItem(item);
}
return null;
}
if (dispenser != null && !preference.OreEnabled) {
return null;
}
player.GiveItem(ItemManager.Create(cookComponent.becomeOnCooked, amountMultiplied));
item.Remove(0f);
return true;
}
object OnCollectiblePickup(CollectibleEntity entity, BasePlayer player) {
// Potential fix for rare NREs.
if (player == null || entity == null) {
return null;
}
if (!data.Preferences.TryGetValue(player.userID, out preference) || !preference.PickupEnabled || options.Blacklist.Contains(entity.ShortPrefabName)) {
return null;
}
foreach (var item in entity.itemList) {
OnDispenserGather(null, player, ItemManager.Create(item.itemDef, (int)item.amount));
}
// Hook returns before world entity is cleaned up. This is a continuation of the native code to finish the job.
if (entity.pickupEffect.isValid) {
Effect.server.Run(entity.pickupEffect.resourcePath, entity.transform.position);
}
// Adding just in case
var randomItemDispenser = PrefabAttribute.server.Find<RandomItemDispenser>(entity.prefabID);
if (randomItemDispenser != null) {
randomItemDispenser.DistributeItems(player, entity.transform.position);
}
entity.itemList = null;
entity.Kill();
return true;
}
object OnDispenserBonus(ResourceDispenser dispenser, BasePlayer player, Item item) {
OnDispenserGather(dispenser, player, item);
return true;
}
#endregion
#region Functions
#region Commands
[ChatCommand("smelt")]
void ChatCommand(BasePlayer player, string command, string[] args) {
if (!permission.UserHasPermission(player.UserIDString, perm)) {
SendReply(player, $"Unknown command: {command}");
return;
}
if (!data.Preferences.TryGetValue(player.userID, out preference)) {
data.Preferences.Add(player.userID, new Preferences());
preference = data.Preferences[player.userID];
}
if (args == null || args.Length == 0) {
if (options.ShowStatus) {
SendReply(player, $"<size=20>Auto Smelting</size>\nPickup [{(preference.PickupEnabled ? "Enabled" : "Disabled")}] - Toggles for collectables.\nOre [{(preference.OreEnabled ? "Enabled" : "Disabled")}] - Toggles for nodes.\nCharcoal [{(preference.CharcoalEnabled ? "Enabled" : "Disabled")}] - Toggles for wood.");
}
else {
SendReply(player, $"<size=20>Auto Smelting</size>\nPickup - Toggles for collectables.\nOre - Toggles for nodes.\nCharcoal - Toggles for wood.");
}
return;
}
switch (args[0]) {
case "pickup" : preference.PickupEnabled = !preference.PickupEnabled;
SendReply(player, $"You've {(preference.PickupEnabled ? "enabled" : "disabled")} auto smelt for collectables.");
break;
case "ore" : preference.OreEnabled = !preference.OreEnabled;
SendReply(player, $"You've {(preference.OreEnabled ? "enabled" : "disabled")} auto smelt for ore.");
break;
case "charcoal" : preference.CharcoalEnabled = !preference.CharcoalEnabled;
SendReply(player, $"You've {(preference.CharcoalEnabled ? "enabled" : "disabled")} auto smelt for charcoal.");
break;
default : SendReply(player, $"Entry not recognized: {args[0]}");
break;
}
}
#endregion
#region Config
ConfigFile options;
class ConfigFile {
public bool SaveWithServer = false;
public bool OnByDefault = false;
public int Multiplier = 1;
public bool ShowStatus = true;
public List<string> Blacklist;
}
void LoadDefaultConfig() {
options = new ConfigFile();
PopulateDefaultData();
}
void LoadConfig() {
options = Config.ReadObject<ConfigFile>();
if (options.Blacklist == null) {
PopulateDefaultData();
}
SaveConfig(options);
}
void SaveConfig(ConfigFile config) {
Config.WriteObject(config, true);
}
void PopulateDefaultData() {
options.Blacklist = new List<string> {
"hemp-collectable", "berry-black-collectable", "berry-blue-collectable", "berry-green-collectable", "berry-red-collectable", "berry-white-collectable", "berry-yellow-collectable"
}
;
Puts("Default config has been added. Configure in /data/SmeltOnGather.json");
}
#endregion
#region Data
Data data;
Preferences preference;
class Data {
public Dictionary<ulong, Preferences> Preferences = new Dictionary<ulong, Preferences>();
}
class Preferences {
public bool PickupEnabled;
public bool OreEnabled;
public bool CharcoalEnabled;
}
void LoadData() {
data = Interface.Oxide.DataFileSystem.ReadObject<Data>("SmeltOnGather");
if (data == null) {
data = new Data();
}
}
void SaveData() {
Interface.Oxide.DataFileSystem.WriteObject("SmeltOnGather", data);
}
#endregion
#endregion
}
}