C#:
using Newtonsoft.Json;
using Oxide.Core;
using System.Collections;
using System.Collections.Generic;
using ru = Oxide.Game.Rust;
using System.Linq;
namespace Oxide.Plugins
{
[Info("ZXCSmelter", "fermens#8767", "1.0.2")]
class ZXCSmelter : RustPlugin
{
#region Config
private PluginConfig config;
protected override void LoadDefaultConfig()
{
config = PluginConfig.DefaultConfig();
}
protected override void LoadConfig()
{
base.LoadConfig();
config = Config.ReadObject<PluginConfig>();
}
protected override void SaveConfig()
{
Config.WriteObject(config);
}
private class PluginConfig
{
[JsonProperty("Cooldown")]
public float cooldown;
[JsonProperty("Chat command")]
public string command;
[JsonProperty("Smelt wood?")]
public bool wood;
[JsonProperty("Special smelting rates")]
public Dictionary<string, int> ooo { get; set; } = new Dictionary<string, int>
{
{ "crude.oil", 3 }
};
public static PluginConfig DefaultConfig()
{
return new PluginConfig()
{
cooldown = 300f,
command = "smelt",
wood = true
};
}
}
#endregion
#region Lang
Dictionary<string, string> messagesRU = new Dictionary<string, string>
{
{"M.COOLDOWN", "<color=#00FF00>[ZXC Smelter]</color> Переплавка инвентаря находится на перезарядке подождите: {0} сек."},
{"M.PERM", "<color=#00FF00>[ZXC Smelter]</color> У вас нет разрешения на использование <color=#00FF00>Inventory Smelter</color>." },
{"M.INV", "<color=#00FF00>[ZXC Smelter]</color> Ваш инвентарь успешно переплавлен." }
};
Dictionary<string, string> messagesEN = new Dictionary<string, string>
{
{"M.COOLDOWN", "<color=#00FF00>[ZXC Smelter]</color> Inventory Smelter is on cooldown, please wait: {0} сек."},
{"M.PERM", "<color=#00FF00>[ZXC Smelter]</color> You do not have permission to use <color=#00FF00>Inventory Smelter</color>." },
{"M.INV", "<color=#00FF00>[ZXC Smelter]</color> Your inventory has been successfully smelted." }
};
#endregion
#region Initialize
const string perminsta = "zxcsmelter.instant";
const string permcommand = "zxcsmelter.command";
private Dictionary<ulong, float> cooldowns = new Dictionary<ulong, float>();
private ItemDefinition coal;
private void OnServerInitialized()
{
permission.RegisterPermission(perminsta, this);
permission.RegisterPermission(permcommand, this);
lang.RegisterMessages(messagesEN, this, "en");
lang.RegisterMessages(messagesRU, this, "ru");
Interface.Oxide.GetLibrary<ru.Libraries.Command>(null).AddChatCommand(config.command, this, "SmeltCmd");
if (config.wood) coal = ItemManager.FindItemDefinition(-1938052175);
}
#endregion
#region Gathering
private object OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
{
var player = entity.ToPlayer();
if (player == null || !permission.UserHasPermission(player.UserIDString, perminsta)) return null;
return SmeltIt(player, item);
}
private object OnDispenserBonus(ResourceDispenser dispenser, BasePlayer player, Item item)
{
if (!permission.UserHasPermission(player.UserIDString, perminsta)) return null;
return SmeltIt(player, item);
}
private void OnCollectiblePickup(Item item, BasePlayer player)
{
if (!permission.UserHasPermission(player.UserIDString, perminsta)) return;
SmeltIt(player, item);
}
#endregion
#region Command
private void SmeltCmd(BasePlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, permcommand))
{
SendReply(player, lang.GetMessage("M.PERM", this, player.UserIDString));
return;
}
float cooldown;
if (cooldowns.TryGetValue(player.userID, out cooldown) && cooldown > UnityEngine.Time.realtimeSinceStartup)
{
SendReply(player, lang.GetMessage("M.COOLDOWN", this, player.UserIDString), cooldowns[player.userID] - (int)UnityEngine.Time.realtimeSinceStartup);
return;
}
foreach (Item item in player.inventory.containerBelt.itemList.ToList()) SmeltIt(player, item);
foreach (Item item in player.inventory.containerMain.itemList.ToList()) SmeltIt(player, item);
SendReply(player, lang.GetMessage("M.INV", this, player.UserIDString));
cooldowns[player.userID] = (int)UnityEngine.Time.realtimeSinceStartup + config.cooldown;
}
#endregion
#region Smelt
private object SmeltIt(BaseEntity player, Item item)
{
if (item.info.itemid == -151838493 && config.wood) return GiveToPlayer(player, item, coal);
else
{
ItemModCookable cookable = item.info.GetComponent<ItemModCookable>();
if (cookable == null) return null;
return GiveToPlayer(player, item, cookable.becomeOnCooked);
}
}
private object GiveToPlayer(BaseEntity player, Item item, ItemDefinition def)
{
int x;
if (!config.ooo.TryGetValue(item.info.shortname, out x)) x = 1;
Item newItem = ItemManager.Create(def, item.amount * x);
if (newItem == null) return null;
item.Remove(0.0f);
player.GiveItem(newItem, BaseEntity.GiveItemReason.ResourceHarvested);
return true;
}
#endregion
}
}