You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
8.0 KiB
185 lines
8.0 KiB
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System;
|
|
|
|
namespace Oxide.Plugins
|
|
{
|
|
[Info("EasyFurnace", "oskar3123", "1.1.2", ResourceId = 1191)]
|
|
class EasyFurnace : RustPlugin
|
|
{
|
|
class Cfg
|
|
{
|
|
public static int
|
|
furnaceMetalOres,
|
|
furnaceMetalWood,
|
|
furnaceMetalOutput,
|
|
furnaceSulfurOres,
|
|
furnaceSulfurWood,
|
|
furnaceSulfurOutput,
|
|
largeFurnaceMetalOres,
|
|
largeFurnaceMetalWood,
|
|
largeFurnaceMetalOutput,
|
|
largeFurnaceSulfurOres,
|
|
largeFurnaceSulfurWood,
|
|
largeFurnaceSulfurOutput;
|
|
}
|
|
|
|
protected override void LoadDefaultConfig()
|
|
{
|
|
PrintWarning("Creating a new configuration file");
|
|
Config.Clear();
|
|
Config["Furnace", "Metal", "Ores"] = 4;
|
|
Config["Furnace", "Metal", "Wood"] = 1;
|
|
Config["Furnace", "Metal", "Output"] = 1;
|
|
Config["Furnace", "Sulfur", "Ores"] = 4;
|
|
Config["Furnace", "Sulfur", "Wood"] = 1;
|
|
Config["Furnace", "Sulfur", "Output"] = 1;
|
|
Config["LargeFurnace", "Metal", "Ores"] = 12;
|
|
Config["LargeFurnace", "Metal", "Wood"] = 5;
|
|
Config["LargeFurnace", "Metal", "Output"] = 1;
|
|
Config["LargeFurnace", "Sulfur", "Ores"] = 12;
|
|
Config["LargeFurnace", "Sulfur", "Wood"] = 3;
|
|
Config["LargeFurnace", "Sulfur", "Output"] = 3;
|
|
SaveConfig();
|
|
}
|
|
|
|
protected override void LoadConfig()
|
|
{
|
|
base.LoadConfig();
|
|
Cfg.furnaceMetalOres = (int)Config["Furnace", "Metal", "Ores"];
|
|
Cfg.furnaceMetalWood = (int)Config["Furnace", "Metal", "Wood"];
|
|
Cfg.furnaceMetalOutput = (int)Config["Furnace", "Metal", "Output"];
|
|
Cfg.furnaceSulfurOres = (int)Config["Furnace", "Sulfur", "Ores"];
|
|
Cfg.furnaceSulfurWood = (int)Config["Furnace", "Sulfur", "Wood"];
|
|
Cfg.furnaceSulfurOutput = (int)Config["Furnace", "Sulfur", "Output"];
|
|
Cfg.largeFurnaceMetalOres = (int)Config["LargeFurnace", "Metal", "Ores"];
|
|
Cfg.largeFurnaceMetalWood = (int)Config["LargeFurnace", "Metal", "Wood"];
|
|
Cfg.largeFurnaceMetalOutput = (int)Config["LargeFurnace", "Metal", "Output"];
|
|
Cfg.largeFurnaceSulfurOres = (int)Config["LargeFurnace", "Sulfur", "Ores"];
|
|
Cfg.largeFurnaceSulfurWood = (int)Config["LargeFurnace", "Sulfur", "Wood"];
|
|
Cfg.largeFurnaceSulfurOutput = (int)Config["LargeFurnace", "Sulfur", "Output"];
|
|
}
|
|
|
|
int GetStackSize(string shortname) { return ItemManager.FindItemDefinition(shortname).stackable; }
|
|
|
|
Dictionary<BaseOven, BasePlayer> furnaceCache = new Dictionary<BaseOven, BasePlayer>();
|
|
|
|
void OnLootEntity(BasePlayer player, BaseEntity entity)
|
|
{
|
|
BaseOven furnace = entity as BaseOven;
|
|
if (!furnace) return;
|
|
|
|
furnaceCache[furnace] = player;
|
|
}
|
|
|
|
int RemoveItemsFromInventory(BasePlayer player, string shortname, int amount)
|
|
{
|
|
ItemDefinition itemToRemove = ItemManager.FindItemDefinition(shortname);
|
|
List<Item> foundItems = player.inventory.FindItemIDs(itemToRemove.itemid);
|
|
int numberFound = foundItems == null ? 0 : foundItems.Sum(item => item.amount);
|
|
if (numberFound < amount) amount = numberFound;
|
|
int numberRemoved = player.inventory.Take(foundItems, itemToRemove.itemid, amount);
|
|
return numberRemoved;
|
|
}
|
|
|
|
void OnItemAddedToContainer(ItemContainer container, Item item)
|
|
{
|
|
if (item.info.shortname != "metal.ore" && item.info.shortname != "sulfur.ore") return;
|
|
|
|
if (container.itemList.Count() > 1) return;
|
|
|
|
int cap = container.capacity;
|
|
if (cap != 6 && cap != 18) return;
|
|
|
|
if (item.amount < cap) return;
|
|
|
|
int oresize = cap == 6 ?
|
|
(item.info.shortname == "metal.ore" ? Cfg.furnaceMetalOres : Cfg.furnaceSulfurOres) :
|
|
(item.info.shortname == "metal.ore" ? Cfg.largeFurnaceMetalOres : Cfg.largeFurnaceSulfurOres);
|
|
int woodsize = cap == 6 ?
|
|
(item.info.shortname == "metal.ore" ? Cfg.furnaceMetalWood : Cfg.furnaceSulfurWood) :
|
|
(item.info.shortname == "metal.ore" ? Cfg.largeFurnaceMetalWood : Cfg.largeFurnaceSulfurWood);
|
|
int outputsize = cap == 6 ?
|
|
(item.info.shortname == "metal.ore" ? Cfg.furnaceMetalOutput : Cfg.furnaceSulfurOutput) :
|
|
(item.info.shortname == "metal.ore" ? Cfg.largeFurnaceMetalOutput : Cfg.largeFurnaceSulfurOutput);
|
|
double woodfactor = item.info.shortname == "metal.ore" ? 5D : 2.5D;
|
|
string outputname = item.info.shortname == "metal.ore" ? "metal.fragments" : "sulfur";
|
|
|
|
if (oresize + woodsize + outputsize > cap) return;
|
|
|
|
BaseOven furnace = null;
|
|
foreach (BaseOven key in furnaceCache.Keys)
|
|
if (key.inventory == container)
|
|
{
|
|
furnace = key;
|
|
break;
|
|
}
|
|
if (!furnace) return;
|
|
|
|
BasePlayer player;
|
|
if (!furnaceCache.TryGetValue(furnace, out player) || !player) return;
|
|
|
|
int orecount = 0;
|
|
Item[] items = player.inventory.AllItems();
|
|
foreach (Item itm in items)
|
|
if (itm.info.shortname == item.info.shortname)
|
|
orecount += itm.amount;
|
|
orecount += item.amount;
|
|
if (orecount > oresize * GetStackSize(outputname))
|
|
orecount = oresize * GetStackSize(outputname);
|
|
|
|
int woodToRetain = (int)Math.Ceiling((orecount / oresize) * woodfactor);
|
|
int woodMaxStack = GetStackSize("wood");
|
|
if (woodToRetain > woodMaxStack * woodsize)
|
|
woodToRetain = woodMaxStack * woodsize;
|
|
|
|
int retainedWood = RemoveItemsFromInventory(player, "wood", woodToRetain);
|
|
if (retainedWood < woodToRetain)
|
|
{
|
|
ItemManager.Create(ItemManager.FindItemDefinition("wood"), retainedWood).MoveToContainer(player.inventory.containerMain);
|
|
return;
|
|
}
|
|
|
|
int retainedAmount;
|
|
retainedAmount = RemoveItemsFromInventory(player, outputname, outputsize);
|
|
if (retainedAmount < outputsize)
|
|
{
|
|
ItemManager.Create(ItemManager.FindItemDefinition("wood"), retainedWood).MoveToContainer(player.inventory.containerMain);
|
|
return;
|
|
}
|
|
|
|
item.MoveToContainer(player.inventory.containerMain, -1, false);
|
|
|
|
int extraWood = retainedWood % woodsize;
|
|
int perstack = (int)Math.Floor((double)retainedWood / woodsize);
|
|
for (int i = 0; i < woodsize; i++)
|
|
{
|
|
ItemManager.Create(ItemManager.FindItemDefinition("wood"), perstack + (extraWood > 0 ? 1 : 0)).MoveToContainer(container, -1, false);
|
|
extraWood--;
|
|
}
|
|
|
|
for (int i = 0; i < outputsize; i++)
|
|
ItemManager.Create(ItemManager.FindItemDefinition(outputname), 1).MoveToContainer(container, -1, false);
|
|
|
|
RemoveItemsFromInventory(player, item.info.shortname, orecount);
|
|
|
|
int amountPerStack = orecount / oresize;
|
|
Item[] oresToAdd = new Item[oresize];
|
|
int extras = orecount % oresize;
|
|
for (int i = 0; i < oresize; i++)
|
|
{
|
|
int tmpCnt = 0;
|
|
if (extras > 0)
|
|
tmpCnt++;
|
|
tmpCnt += amountPerStack;
|
|
extras--;
|
|
oresToAdd[i] = ItemManager.Create(ItemManager.FindItemDefinition(item.info.shortname), tmpCnt);
|
|
}
|
|
|
|
foreach (Item oreToAdd in oresToAdd)
|
|
oreToAdd.MoveToContainer(container, -1, false);
|
|
|
|
furnace.Invoke("StartCooking", 0);
|
|
}
|
|
}
|
|
} |