im trying to make a command to craft a length rope on usage with cloth and plant fibers, iv made this same reletive script work before with placeable objects but it doesn't seem to wanna work when i replace the previous item with rope, iv been banging my head against it for the last day now with no results so i figured maybe a 2nd pair of eyes could see what im doing wrong.
Edit: NVM Just figured it out myself.
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Rope Crafting", "Judess69er", "1.0.0")]
[Description("Craft Rope From Cloth & Plant Fiber")]
public class ExtendedRope : RustPlugin
{
#region Vars
private const ulong skinID = 1414245522;
private static ExtendedRope plugin;
private const string permUse = "ExtendedRope.use";
#endregion
#region Config
private static ConfigData config;
private class ConfigData
{
[JsonProperty(PropertyName = "2. Craft settings:")]
public OCraft craft;
public class OCraft
{
[JsonProperty(PropertyName = "1. Enabled")]
public bool enabled;
[JsonProperty(PropertyName = "2. Cost (shortname - amount):")]
public Dictionary<string, int> cost;
}
}
private ConfigData GetDefaultConfig()
{
return new ConfigData
{
craft = new ConfigData.OCraft
{
enabled = true,
cost = new Dictionary<string, int>
{
{"cloth", 30},
{"plantfiber", 15}
}
},
};
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<ConfigData>();
}
catch
{
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
PrintError("Configuration file is corrupt(or not exists), creating new one!");
config = GetDefaultConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(config);
}
#endregion
#region Language
private Dictionary<string, string> EN = new Dictionary<string, string>
{
{"Name", "Rope"},
{"Receive", "You received rope!"},
{"NoCraft", "Craft disabled!"},
{"Craft", "For craft you need more resources:\n{0}"},
{"Permission", "You need permission to do that!"}
};
private void message(BasePlayer player, string key, params object[] args)
{
if (player == null)
{
return;
}
var message = string.Format(lang.GetMessage(key, this, player.UserIDString), args);
player.ChatMessage(message);
}
#endregion
#region Oxide Hooks
private void OnEntityBuilt(Planner plan, GameObject go)
{
CheckDeploy(go.ToBaseEntity());
}
private void OnServerInitialized()
{
plugin = this;
lang.RegisterMessages(EN, this);
permission.RegisterPermission(permUse, this);
CheckRopes();
}
#endregion
#region Core
private void CheckRopes()
{
foreach (var rope in UnityEngine.Object.FindObjectsOfType<Rope>())
{
if (rope.OwnerID != 0 && rope.GetComponent<ExtendedRopeComponent>() == null)
{
rope.gameObject.AddComponent<CExtendedRopeComponent>();
}
}
};
private void GiveRope(BasePlayer player, bool pickup = false)
{
var item = CreateItem();
if (item != null && player != null)
{
player.GiveItem(item);
}
}
private Item CreateItem();
{
var item = ItemManager.CreateByName("rope", 10, skinID);
if (item != null)
{
item.name = plugin?.GetRopeName();
}
return item;
}
[ChatCommand("rope.craft")]
private void Craft(BasePlayer player)
{
if (CanCraft(player))
{
GiveRope(player);
}
}
private bool CanCraft(BasePlayer player)
{
if (!config.craft.enabled)
{
message(player, "NoCraft");
return false;
}
if (!permission.UserHasPermission(player.UserIDString, permUse))
{
message(player, "Permission");
return false;
}
var recipe = config.craft.cost;
var more = new Dictionary<string, int>();
foreach (var component in recipe)
{
var name = component.Key;
var has = player.inventory.GetAmount(ItemManager.FindItemDefinition(component.Key).itemid);
var need = component.Value;
if (has < component.Value)
{
if (!more.ContainsKey(name))
{
more.Add(name, 0);
}
more[name] += need;
}
}
if (more.Count == 0)
{
foreach (var item in recipe)
{
player.inventory.Take(null, ItemManager.FindItemDefinition(item.Key).itemid, item.Value);
}
return true;
}
else
{
var text = "";
foreach (var item in more)
{
text += $" * {item.Key} x{item.Value}\n";
}
player.ChatMessage(string.Format(lang.GetMessage("Craft", this), text));
return false;
}
}
#endregion
#region Helpers
private string GetRopeName()
{
return lang.GetMessage("Name", this);
}
private bool IsRope(ulong skin)
{
return skin != 0 && skin == skinID;
}
#endregion
#region Command
[ConsoleCommand("rope.give")]
private void Cmd(ConsoleSystem.Arg arg)
{
if (arg.IsAdmin && arg.Args?.Length > 0)
{
var player = BasePlayer.Find(arg.Args[0]) ?? BasePlayer.FindSleeping(arg.Args[0]);
if (player == null)
{
PrintWarning($"We can't find player with that name/ID! {arg.Args[0]}");
return;
}
GiveRope(player);
}
}
}
}
#endregion