C#:
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
#region Changelogs and ToDo
/**********************************************************************
*
* 1.0.0 : - Initial release
* 1.0.1 : - Coding cleanup
* - Added Static Craftingcost to info command
* - Added checks and full inventory routine
* - Added Chaticon
* 1.0.2 : - Fixed material cost cfg reverting back
*
**********************************************************************/
#endregion
namespace Oxide.Plugins
{
[Info("Poker Tables", "Krungh Crow", "1.0.2")]
[Description("craft and place a Poker table")]
class PokerTables : RustPlugin
{
#region Variables
const string Chat_Perm = "pokertables.use";
ulong chaticon = 76561199182744880;
string prefix;
bool Debug = false;
#endregion
#region Configuration
void Init()
{
if (!LoadConfigVariables())
{
Puts("Config file issue detected. Please delete file, or check syntax and fix.");
return;
}
permission.RegisterPermission(Chat_Perm, this);
Debug = configData.PlugCFG.Debug;
prefix = configData.PlugCFG.Prefix;
if (Debug) Puts($"[Debug] trigger for Debug is true");
}
private ConfigData configData;
class ConfigData
{
[JsonProperty(PropertyName = "Main config")]
public SettingsPlugin PlugCFG = new SettingsPlugin();
}
class SettingsPlugin
{
[JsonProperty(PropertyName = "Debug")]
public bool Debug = false;
[JsonProperty(PropertyName = "Chat Prefix")]
public string Prefix = "[<color=yellow>Poker Tables</color>] ";
[JsonProperty(PropertyName = "Crafting Costs")]
public Dictionary<string, int> cost;
}
private bool LoadConfigVariables()
{
try
{
configData = Config.ReadObject<ConfigData>();
}
catch
{
return false;
}
SaveConf();
return true;
}
protected override void LoadDefaultConfig()
{
Puts("Fresh install detected Creating a new config file.");
configData = new ConfigData();
configData.PlugCFG.cost = new Dictionary<string, int>
{
{"scrap", 100},
{"wood", 1000},
{"metal.fragments", 50},
{"tarp", 1}
};
SaveConf();
}
void SaveConf() => Config.WriteObject(configData, true);
#endregion
#region LanguageAPI
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["Craft"] = "You need more resources:\n{0}",
["CraftingCost"] = "<color=green>Materials needed to craft the Poker Table</color> :",
["Info"] = "\n<color=green>Available Commands</color>\n<color=green>/poker info</color> : Shows info on version/author and commands",
["InfoCraft"] = "<color=green>/poker craft</color> : To craft the Poker Table",
["InvalidInput"] = "<color=red>Please enter a valid command!</color>",
["InventoryFull"] = "You need at least 1 more slot in your inventory,canceling the craft",
["Succesfull"] = "You succesfully crafted a Poker Table for 6 players",
["Version"] = "Version : V",
["NoPermission"] = "<color=green>You do not have permission to use that command!</color>",
}, this);
}
#endregion
#region Commands
[ChatCommand("Poker")]
private void cmdPrimary(BasePlayer player, string command, string[] args)
{
if (!permission.UserHasPermission(player.UserIDString, Chat_Perm))
{
Player.Message(player, prefix + string.Format(msg("NoPermission", player.UserIDString)), chaticon);
if (Debug) Puts($"[Debug] {player} had no permission for using Commands");
return;
}
if (args.Length == 0)
{
Player.Message(player, prefix + string.Format(msg("InvalidInput", player.UserIDString)), chaticon);
}
else
{
if (args[0].ToLower() == "info")
{
string recipelist = "";
foreach (var item in configData.PlugCFG.cost)
{
recipelist += $" <color=green>></color> {item.Key} x{item.Value}\n";
}
Player.Message(player, prefix + string.Format(msg("Version", player.UserIDString)) + this.Version.ToString() + " By : " + this.Author.ToString()
+ msg("Info") + "\n"
+ msg("InfoCraft") + "\n\n"
+ msg("CraftingCost") + "\n"
+ (recipelist.ToString())
, chaticon);
return;
}
if (args[0].ToLower() == "craft")
{
if (CanCraftPokerTable(player))
{
if (Debug) Puts($"[Debug] {player} Crafted a Poker Table");
var item = ItemManager.CreateByName("cardtable", 1, 0);
item.name = "Poker Table for 6";
item.MarkDirty();
if (item != null) player.GiveItem(item);
Player.Message(player, prefix + string.Format(msg("Succesfull", player.UserIDString)), chaticon);
}
return;
}
else
{
Player.Message(player, prefix + string.Format(msg("InvalidInput", player.UserIDString)), chaticon);
}
}
}
#endregion
#region Message helper
private string msg(string key, string id = null) => lang.GetMessage(key, this, id);
#endregion
#region Core
private bool CanCraftPokerTable(BasePlayer player)
{
if (!permission.UserHasPermission(player.UserIDString, Chat_Perm))
{
Player.Message(player, prefix + string.Format(msg("NoPermission", player.UserIDString)), chaticon);
return false;
}
if (player.inventory.containerMain.IsFull())
{
if (Debug) Puts($"{player} has no inventory space");
Player.Message(player, prefix + string.Format(msg($"InventoryFull", player.UserIDString)), chaticon);
return false;
}
var recipe = configData.PlugCFG.cost;
var Costs = new Dictionary<string, int>();
foreach (var component in recipe)
{
var name = component.Key;
var Owned = player.inventory.GetAmount(ItemManager.FindItemDefinition(component.Key).itemid);
var Needed = component.Value;
if (Owned < component.Value)
{
if (!Costs.ContainsKey(name))
{
Costs.Add(name, 0);
}
Costs[name] += Needed;
}
}
if (Costs.Count == 0)
{
foreach (var item in recipe)
{
player.inventory.Take(null, ItemManager.FindItemDefinition(item.Key).itemid, item.Value);
}
return true;
}
else
{
var recipelist = "";
foreach (var item in Costs)
{
recipelist += $" > {item.Key} x{item.Value}\n";
}
Player.Message(player, prefix + string.Format(msg("Craft", player.UserIDString), recipelist), chaticon);
return false;
}
}
#endregion
}
}