Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 50 additions & 38 deletions StackSizeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@

namespace Oxide.Plugins
{
[Info("Stack Size Controller", "AnExiledDev", "4.1.1")]
[Info("Stack Size Controller", "AnExiledDev/optimized by WouayNote", "4.1.3")]
[Description("Allows configuration of most items max stack size.")]
class StackSizeController : CovalencePlugin
{
[PluginReference]
Plugin AirFuel, GetToDaChoppa, VehicleVendorOptions;

private const string _vanillaDefaultsUri = "https://raw.githubusercontent.com/AnExiledDev/StackSizeController/master/vanilla-defaults.json";
private const string _vanillaDefaultsUri = "https://raw.githubusercontent.com/WouayNote/StackSizeController/master/vanilla-defaults.json";

private Configuration _config;
private Dictionary<string, int> _vanillaDefaults;
private Dictionary<int, int> _computedStackSizesByItemId = new Dictionary<int, int>();

private readonly List<string> _ignoreList = new List<string>
{
Expand Down Expand Up @@ -266,7 +267,7 @@ private string GetMessage(string key, string playerId)

// Credit to WhiteThunder- https://github.com/AnExiledDev/StackSizeController/pull/7
// Fix initial fuel amount for vendor-spawned helis since they use 20% of max stack size of low grade.
private void OnEntitySpawned(MiniCopter heli)
private void OnEntitySpawned(Minicopter heli)
{
// Ignore if a known plugin is loaded that adjusts heli fuel.
if (AirFuel != null || GetToDaChoppa != null || VehicleVendorOptions != null)
Expand Down Expand Up @@ -391,8 +392,7 @@ private void ItemSearchCommand(IPlayer player, string command, string[] args)
{
if (args.Length != 1)
{
player.Reply(
string.Format(GetMessage("NotEnoughArguments", player.Id), 1));
player.Reply(string.Format(GetMessage("NotEnoughArguments", player.Id), 1));
}

List<ItemDefinition> itemDefinitions = ItemManager.itemList.Where(itemDefinition =>
Expand All @@ -407,8 +407,9 @@ private void ItemSearchCommand(IPlayer player, string command, string[] args)

foreach (ItemDefinition itemDefinition in itemDefinitions)
{
int vanillaStackSize = _vanillaDefaults.TryGetValue(itemDefinition.shortname, out vanillaStackSize) ? vanillaStackSize : itemDefinition.stackable;
output.AddRow(itemDefinition.itemid.ToString(), itemDefinition.shortname,
itemDefinition.category.ToString(), _vanillaDefaults[itemDefinition.shortname].ToString("N0"),
itemDefinition.category.ToString(), vanillaStackSize.ToString("N0"),
Mathf.Clamp(GetStackSize(itemDefinition), 0, int.MaxValue).ToString("N0"));
}

Expand Down Expand Up @@ -443,8 +444,9 @@ private void ListCategoryItemsCommand(IPlayer player, string command, string[] a
foreach (ItemDefinition itemDefinition in ItemManager.GetItemDefinitions()
.Where(itemDefinition => itemDefinition.category == itemCategory))
{
int vanillaStackSize = _vanillaDefaults.TryGetValue(itemDefinition.shortname, out vanillaStackSize) ? vanillaStackSize : itemDefinition.stackable;
output.AddRow(itemDefinition.itemid.ToString(), itemDefinition.shortname,
itemDefinition.category.ToString(), _vanillaDefaults[itemDefinition.shortname].ToString("N0"),
itemDefinition.category.ToString(), vanillaStackSize.ToString("N0"),
Mathf.Clamp(GetStackSize(itemDefinition), 0, int.MaxValue).ToString("N0"),
_config.CategoryStackMultipliers[itemDefinition.category.ToString()].ToString());
}
Expand Down Expand Up @@ -521,8 +523,8 @@ private void SetVanillaDefaults(int code, string response)

private int GetVanillaStackSize(ItemDefinition itemDefinition)
{
return _vanillaDefaults.ContainsKey(itemDefinition.shortname)
? _vanillaDefaults[itemDefinition.shortname]
return _vanillaDefaults.TryGetValue(itemDefinition.shortname, out int vanillaStackSize)
? vanillaStackSize
: itemDefinition.stackable;
}

Expand All @@ -535,45 +537,51 @@ private int GetStackSize(ItemDefinition itemDefinition)
{
try
{
if (_ignoreList.Contains(itemDefinition.shortname))
// Cached Stack Size
if (this._computedStackSizesByItemId.TryGetValue(itemDefinition.itemid, out int stackSize))
{
return GetVanillaStackSize(itemDefinition);
return stackSize;
}

int stackable = GetVanillaStackSize(itemDefinition);

// Individual Limit set by shortname
if (_config.IndividualItemStackSize.ContainsKey(itemDefinition.shortname))
{
stackable = _config.IndividualItemStackSize[itemDefinition.shortname];
}

// Individual Multiplier set by shortname
if (_config.IndividualItemStackMultipliers.ContainsKey(itemDefinition.shortname))
{
return Mathf.RoundToInt(stackable * _config.IndividualItemStackMultipliers[itemDefinition.shortname]);
}

// Individual Multiplier set by item id
if (_config.IndividualItemStackMultipliers.ContainsKey(itemDefinition.itemid.ToString()))
// Ignored Items
if (_ignoreList.Contains(itemDefinition.shortname))
{
return Mathf.RoundToInt(stackable * _config.IndividualItemStackMultipliers[itemDefinition.itemid.ToString()]);
stackSize = GetVanillaStackSize(itemDefinition);
}

// Category stack multiplier defined
if (_config.CategoryStackMultipliers.ContainsKey(itemDefinition.category.ToString()) &&
_config.CategoryStackMultipliers[itemDefinition.category.ToString()] > 1.0f)
else
{
return Mathf.RoundToInt(
stackable * _config.CategoryStackMultipliers[itemDefinition.category.ToString()]);
// Individual Limit
if (!_config.IndividualItemStackSize.TryGetValue(itemDefinition.shortname, out int stackable))
{
stackable = GetVanillaStackSize(itemDefinition);
}
// Individual Multiplier set by shortname
if (_config.IndividualItemStackMultipliers.TryGetValue(itemDefinition.shortname, out float stackMultiplierOfShortName))
{
stackSize = Mathf.RoundToInt(stackable * stackMultiplierOfShortName);
}
// Individual Multiplier set by item id
else if (_config.IndividualItemStackMultipliers.TryGetValue(itemDefinition.itemid.ToString(), out float stackMultiplierOfId))
{
stackSize = Mathf.RoundToInt(stackable * stackMultiplierOfId);
}
// Category stack multiplier defined
else if (_config.CategoryStackMultipliers.TryGetValue(itemDefinition.category.ToString(), out float stackMultiplierOfCategory) && stackMultiplierOfCategory > 1.0f)
{
stackSize = Mathf.RoundToInt(stackable * stackMultiplierOfCategory);
}
// Global stack multiplier
else
{
stackSize = Mathf.RoundToInt(stackable * _config.GlobalStackMultiplier);
}
}

return Mathf.RoundToInt(stackable * _config.GlobalStackMultiplier);
// Cache Computed Stack Size
this._computedStackSizesByItemId.Add(itemDefinition.itemid, stackSize);
return stackSize;
}
catch (Exception ex)
{
LogError("Exception encountered during GetStackSize. Item: " + itemDefinition.shortname + " Ex:" + ex.ToString());

return GetVanillaStackSize(itemDefinition);
}
}
Expand All @@ -596,6 +604,8 @@ private void SetStackSizes()

itemDefinition.stackable = Mathf.Clamp(GetStackSize(itemDefinition), 1, int.MaxValue);
}
// Invalidate Stack Size Cache
_computedStackSizesByItemId.Clear();
}

private void RevertStackSizes()
Expand All @@ -616,6 +626,8 @@ private void RevertStackSizes()

itemDefinition.stackable = Mathf.Clamp(GetVanillaStackSize(itemDefinition), 1, int.MaxValue);
}
// Invalidate Stack Size Cache
_computedStackSizesByItemId.Clear();
}

#endregion
Expand Down
Loading