To implement configurable delay-based permissions for item recycling in the Recyclerbox plugin, you'll need to create new permissions and modify the recycling process to introduce delays based on these permissions. We'll add two new configurable options in the plugin configuration for the delay durations and apply these delays when a player with the appropriate permission recycles items.
Here's how you can update the plugin to include these features:
using System.Collections.Generic;
using System.Linq;
using Facepunch;
using Newtonsoft.Json;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using System.Collections;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Recyclerbox", "Marte6", "2.4.0")]
[Description("Turn a large wooden or small wooden box into a Recycler with configurable delays.")]
public class Recyclerbox : CovalencePlugin
{
#region Fields
private Configuration config;
private const string UiOverlayName = "RecyclerOverlay";
private Item.Flag itemScrapedFlag = (Item.Flag)(1 << 78);
private readonly Dictionary<ulong, (StorageContainer recyclerContainer, BasePlayer player)> activeRecyclers = new();
private readonly Dictionary<ulong, (DroppedItemContainer droppedContainer, BasePlayer player)> droppedItemContainers = new();
private const string LargeBoxPrefab = "assets/prefabs/deployable/large wood storage/box.wooden.large.prefab";
private const string SmallBoxPrefab = "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab";
private const string DroppedItemPrefab = "assets/prefabs/misc/item drop/item_drop.prefab";
private readonly HashSet<(ulong, ulong)> notifiedPlayers = new();
#endregion
#region Permissions
private const string PermissionGetRecycler = "recyclerbox.get";
private const string PermissionTransformRecycler = "recyclerbox.transform";
private const string PermissionUseVirtualRecycler = "recyclerbox.virtual";
private const string PermissionRecyclerDelay1 = "recyclerbox.delay1";
private const string PermissionRecyclerDelay2 = "recyclerbox.delay2";
private readonly List<string> pluginPermissions = new List<string>
{
PermissionGetRecycler,
PermissionTransformRecycler,
PermissionUseVirtualRecycler,
PermissionRecyclerDelay1,
PermissionRecyclerDelay2
};
#endregion
#region Initialization
private void OnServerInitialized()
{
foreach (var permission in pluginPermissions)
this.permission.RegisterPermission(permission, this);
AddCovalenceCommand(config.GetRecyclerCommand, nameof(CommandGetRecyclerbox));
AddCovalenceCommand(config.TransformRecyclerCommand, nameof(CommandTransformRecyclerbox));
AddCovalenceCommand(config.UseVirtualRecyclerCommand, nameof(CommandUseVirtualRecycler));
}
private void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, UiOverlayName);
TerminateVirtualRecycler(player);
}
RemoveAllDroppedItemContainers();
}
#endregion
#region NoEscape
[PluginReference]
private Plugin NoEscape;
private bool IsPlayerRaidBlocked(BasePlayer player) => NoEscape?.Call<bool>("IsRaidBlocked", player) ?? false;
#endregion
#region Configuration
private class Configuration
{
[JsonProperty("Get Recyclerbox Command")]
public string[] GetRecyclerCommand = { "getrec", "getrecycler", "recyclerbox" };
[JsonProperty("Transform Recyclerbox Command")]
public string[] TransformRecyclerCommand = { "trarec", "boxtorecycler", "transformrecycler", "transforbox" };
[JsonProperty("Virtual Recyclerbox Command")]
public string[] UseVirtualRecyclerCommand = { "rec", "vrec", "virtualrecycler" };
[JsonProperty("Large Box SkinId Recycler")]
public ulong LargeBoxSkinIdRecycler = 2994391115;
[JsonProperty("Small Box SkinId Recycler")]
public ulong SmallBoxSkinIdRecycler = 2988594458;
[JsonProperty("RecyclerLimit")]
public int RecyclerLimit = 3;
[JsonProperty("Recycler Efficiency")]
public float RecyclerEfficiency = 0.6f;
[JsonProperty("Ratio Scrap")]
public float ScrapRatio = 1.2f;
[JsonProperty("Enable Prefab Fx")]
public bool EnablePrefabEffects = true;
[JsonProperty("Lock Recycler While In Use")]
public bool LockRecyclerDuringUse = false;
[JsonProperty("Disable During Raid Block (Requires No Escape plugin)")]
public bool DisableDuringRaidBlock = true;
[JsonProperty("Prefab Fx Open Recycler")]
public string PrefabEffectOnOpen = "assets/bundled/prefabs/fx/build/promote_toptier.prefab";
[JsonProperty("Prefab Fx Close Recycler")]
public string PrefabEffectOnClose = "assets/bundled/prefabs/fx/build/promote_toptier.prefab";
[JsonProperty("Transfer Virtual Recycled Items to Inventory")]
public bool TransferRecycledItemsToInventory = false;
[JsonProperty("Blacklisted Items")]
public string[] BlacklistedItems = { "lmg.m249" };
[JsonProperty("Blacklisted Skins")]
public List<ulong> BlacklistedSkins = new() { 2994391115, 2988594458 };
[JsonProperty("Check Player Alive")]
public bool CheckPlayerAlive = true;
[JsonProperty("Check Player Wounded")]
public bool CheckPlayerWounded = true;
[JsonProperty("Check Building Privilege")]
public bool CheckBuildingPrivilege = true;
[JsonProperty("Check Swimming")]
public bool CheckSwimming = true;
[JsonProperty("Check Falling")]
public bool CheckFalling = true;
[JsonProperty("Check Mounted")]
public bool CheckMounted = true;
[JsonProperty("Check Cargo Ship")]
public bool CheckCargoShip = true;
[JsonProperty("Check Balloon")]
public bool CheckBalloon = true;
[JsonProperty("Check Elevator")]
public bool CheckElevator = true;
[JsonProperty("Check Safe Zone")]
public bool CheckSafeZone = true;
// New properties for delay
[JsonProperty("Delay for Permission 1 (seconds)")]
public float DelayForPermission1 = 1.0f;
[JsonProperty("Delay for Permission 2 (seconds)")]
public float DelayForPermission2 = 2.0f;
public string ToJson() => JsonConvert.SerializeObject(this);
public Dictionary<string, object> ToDictionary() => JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson());
}
protected override void LoadDefaultConfig() => config = new Configuration();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<Configuration>() ?? throw new JsonException();
ValidateConfig();
SaveConfig();
}
catch
{
Puts($"Configuration file {Name}.json is invalid; using defaults");
LoadDefaultConfig();
}
}
protected override void SaveConfig()
{
Puts($"Configuration changes saved to {Name}.json");
Config.WriteObject(config, true);
}
private void ValidateConfig()
{
var currentConfigDict = config.ToDictionary();
var defaultConfigDict = new Configuration().ToDictionary();
bool updated = false;
foreach (var key in defaultConfigDict.Keys)
{
if (!currentConfigDict.ContainsKey(key))
{
currentConfigDict[key] = defaultConfigDict[key];
updated = true;
}
}
config.BlacklistedSkins = new HashSet<ulong>(config.BlacklistedSkins).ToList();
if (updated)
{
config = JsonConvert.DeserializeObject<Configuration>(JsonConvert.SerializeObject(currentConfigDict));
Puts("Configuration updated with missing default values.");
}
}
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(
new Dictionary<string, string>
{
["RecyclerTitle"] = "RECYCLER BOX",
["RecyclerSubtitle"] = "Items are instantly recycled",
["RecyclerItemMissingMsg"] = "Recyclerbox: You aren't holding a Large or Small Wooden Box.",
["RecyclerCreatedMsg"] = "Recyclerbox: The Box has been converted into a Recycler.",
["RecyclerAlreadyMsg"] = "Recyclerbox: This Box is currently a Recycler.",
["NotAllowed"] = "You do not have permission to use that command.",
["LimitReached"] = "Recycler Limit Reached.",
["GotRecyclerbox"] = "Recyclerbox sent to inventory.",
["Denied Privilege"] = "You cannot recycle within someone's building privilege",
["Denied Swimming"] = "You cannot recycle while swimming",
["Denied Falling"] = "You cannot recycle while falling",
["Denied Mounted"] = "You cannot recycle while mounted",
["Denied Wounded"] = "You cannot recycle while wounded",
["Denied Ship"] = "You cannot recycle while on a ship",
["Denied Elevator"] = "You cannot recycle while on an elevator",
["Denied Balloon"] = "You cannot recycle while on a balloon",
["Denied Hook Denied"] = "You can't recycle right now",
["Denied Safe Zone"] = "You cannot recycle while in a safe zone",
["RecycleDropped"] = "You left items in the recycler and dropped them on the ground!",
["ItemProhibited"] = "This item is prohibited from being recycled.",
},
this
);
lang.RegisterMessages(
new Dictionary<string, string>
{
["RecyclerTitle"] = "CAIXA RECICLADORA",
["RecyclerSubtitle"] = "Itens são reciclados instantaneamente",
["RecyclerItemMissingMsg"] = "Recyclerbox: Você não está segurando uma Caixa Grande ou Pequena de Madeira.",
["RecyclerCreatedMsg"] = "Recyclerbox: Você transformou a Caixa em um Reciclador.",
["RecyclerAlreadyMsg"] = "Recyclerbox: Esta Caixa já é um Reciclador.",
["NotAllowed"] = "Você não tem permissão para usar esse comando.",
["LimitReached"] = "Limite de Recicladores Atingido.",
["GotRecyclerbox"] = "Recyclerbox enviada para o inventário.",
["Denied Privilege"] = "Você não pode reciclar dentro do privilégio de construção de outro player",
["Denied Swimming"] = "Você não pode reciclar enquanto nada",
["Denied Falling"] = "Você não pode reciclar enquanto cai",
["Denied Mounted"] = "Você não pode reciclar enquanto estiver montado",
["Denied Wounded"] = "Você não pode reciclar enquanto estiver ferido",
["Denied Ship"] = "Você não pode reciclar enquanto estiver em um navio",
["Denied Elevator"] = "Você não pode reciclar enquanto estiver em um elevador",
["Denied Balloon"] = "Você não pode reciclar enquanto estiver em um balão",
["Denied Hook Denied"] = "Você não pode reciclar agora",
["Denied Safe Zone"] = "Você não pode reciclar enquanto estiver em uma zona segura",
["RecycleDropped"] = "Você deixou itens no reciclador e os deixou cair no chão!",
["ItemProhibited"] = "Este item é proibido de ser reciclado.",
},
this,
"pt-BR"
);
lang.RegisterMessages(
new Dictionary<string, string>
{
["RecyclerTitle"] = "RECYCLERBOX",
["RecyclerSubtitle"] = "Gegenstände werden sofort recycelt",
["RecyclerItemMissingMsg"] = "Recyclerbox: Du hast keine große oder kleine Holzkiste in der Hand.",
["RecyclerCreatedMsg"] = "Recyclerbox: Die Kiste wurde in ein Recyclerbox umgewandelt.",
["RecyclerAlreadyMsg"] = "Recyclerbox: Diese Kiste ist bereits eine Recyclerbox.",
["NotAllowed"] = "Du hast keine Berechtigung, diesen Befehl zu benutzen.",
["LimitReached"] = "Recycler-Limit erreicht.",
["GotRecyclerbox"] = "Die Recyclerbox ist deinem Inventar gutgeschrieben worden.",
["Denied Privilege"] = "Du kannst momentan nicht recyceln, du befindest dich in einem fremden Bauradius.",
["Denied Swimming"] = "Du kannst momentan nicht recyceln, während Du schwimmst.",
["Denied Falling"] = "Du kannst momentan nicht recyceln, während Du im freienfall bist.",
["Denied Mounted"] = "Du kannst momentan nicht recyceln, während Du auf einem Fahrzeug sitzt.",
["Denied Wounded"] = "Du kannst momentan nicht recyceln, während Du verletzt bist.",
["Denied Ship"] = "Du kannst momentan nicht recyceln, da Du dich auf einem Schiff befindest.",
["Denied Elevator"] = "Du kannst momentan nicht recyceln, da Du dich im Aufzug befindest.",
["Denied Balloon"] = "Du kannst momentan nicht recyceln, da Du dich im Ballon befindest.",
["Denied Hook Denied"] = "Du kannst momentan nicht recyceln.",
["Denied Safe Zone"] = "Du kannst momentan nicht recyceln, da Du dich in einer sicheren Zone befindest.",
["RecycleDropped"] = "Du hast Gegenstände im Recycler gelassen und auf den Boden fallen lassen!",
["ItemProhibited"] = "Dieser Gegenstand darf nicht recycelt werden.",
},
this,
"de"
);
lang.RegisterMessages(
new Dictionary<string, string>
{
["RecyclerTitle"] = "CAJA RECICLADORA",
["RecyclerSubtitle"] = "Los elementos son reciclados instantáneamente",
["RecyclerItemMissingMsg"] = "Recyclerbox: No tienes en la mano una caja grande o pequeña de madera.",
["RecyclerCreatedMsg"] = "Recyclerbox: La caja se ha convertido en una recicladora.",
["RecyclerAlreadyMsg"] = "Recyclerbox: Esta caja ya es una recicladora.",
["NotAllowed"] = "No tienes permiso para usar ese comando.",
["LimitReached"] = "Límite de recicladores alcanzado.",
["GotRecyclerbox"] = "Caja recicladora enviada al inventario.",
["Denied Privilege"] = "No puedes reciclar dentro del privilegio de construcción de alguien más",
["Denied Swimming"] = "No puedes reciclar mientras nadas",
["Denied Falling"] = "No puedes reciclar mientras caes",
["Denied Mounted"] = "No puedes reciclar mientras estás montado",
["Denied Wounded"] = "No puedes reciclar mientras estás herido",
["Denied Ship"] = "No puedes reciclar mientras estás en un barco",
["Denied Elevator"] = "No puedes reciclar mientras estás en un ascensor",
["Denied Balloon"] = "No puedes reciclar mientras estás en un globo",
["Denied Hook Denied"] = "No puedes reciclar ahora",
["Denied Safe Zone"] = "No puedes reciclar mientras estás en una zona segura",
["RecycleDropped"] = "Dejaste elementos en el reciclador y los dejaste caer en el suelo!",
["ItemProhibited"] = "Este elemento está prohibido de ser reciclado.",
},
this,
"es"
);
lang.RegisterMessages(
new Dictionary<string, string>
{
["RecyclerTitle"] = "РЕЦИКЛЕРНАЯ КОРОБКА",
["RecyclerSubtitle"] = "Предметы перерабатываются мгновенно",
["RecyclerItemMissingMsg"] = "Recyclerbox: У вас нет в руках большой или маленькой деревянной коробки.",
["RecyclerCreatedMsg"] = "Recyclerbox: Коробка была преобразована в переработчик.",
["RecyclerAlreadyMsg"] = "Recyclerbox: Эта коробка уже переработчик.",
["NotAllowed"] = "У вас нет прав на использование этой команды.",
["LimitReached"] = "Достигнут предел переработчиков.",
["GotRecyclerbox"] = "Рециклерная коробка отправлена в инвентарь.",
["Denied Privilege"] = "Вы не можете перерабатывать в пределах строительной привилегии другого игрока",
["Denied Swimming"] = "Вы не можете перерабатывать, будучи в плавании",
["Denied Falling"] = "Вы не можете перерабатывать в свободном падении",
["Denied Mounted"] = "Вы не можете перерабатывать, находясь верхом",
["Denied Wounded"] = "Вы не можете перерабатывать, пока ранены",
["Denied Ship"] = "Вы не можете перерабатывать, находясь на судне",
["Denied Elevator"] = "Вы не можете перерабатывать, находясь в лифте",
["Denied Balloon"] = "Вы не можете перерабатывать, находясь в шаре",
["Denied Hook Denied"] = "Вы не можете перерабатывать сейчас",
["Denied Safe Zone"] = "Вы не можете перерабатывать, находясь в безопасной зоне",
["RecycleDropped"] = "Вы оставили предметы в переработчике, и они упали на землю!",
["ItemProhibited"] = "Этот предмет запрещен для переработки.",
},
this,
"ru"
);
}
#endregion
#region Hooks
private void OnLootEntityEnd(BasePlayer player, StorageContainer container)
{
if ((container.skinID != config.LargeBoxSkinIdRecycler && container.skinID != config.SmallBoxSkinIdRecycler) || player == null)
return;
TerminateVirtualRecycler(player);
}
private void OnPlayerDisconnected(BasePlayer player, string reason)
{
if (player == null)
return;
TerminateVirtualRecycler(player);
if (droppedItemContainers.Count == 0)
return;
var temporaryContainerList = droppedItemContainers.ToList();
foreach (var (uid, (container, target)) in temporaryContainerList)
{
if (!IsValid(container) || !IsValid(target) || target.userID == player.userID)
{
if (IsValid(container))
container.Kill();
droppedItemContainers.Remove(uid);
}
}
}
private void CleanupDroppedContainers()
{
if (droppedItemContainers.Count == 0)
return;
var temporaryContainerList = new List<KeyValuePair<ulong, (DroppedItemContainer container, BasePlayer player)>>(droppedItemContainers);
foreach (var (uid, (container, _)) in temporaryContainerList)
{
if (IsValid(container))
container.Kill();
droppedItemContainers.Remove(uid);
}
}
private void OnLootEntity(BasePlayer player, StorageContainer container)
{
if (container.skinID != config.LargeBoxSkinIdRecycler && container.skinID != config.SmallBoxSkinIdRecycler)
return;
container.inventory.capacity = 42;
if (config.EnablePrefabEffects)
Effect.server.Run(config.PrefabEffectOnOpen, player.GetNetworkPosition());
if (config.LockRecyclerDuringUse)
container.SetFlag(BaseEntity.Flags.Locked, true);
CreateUi(player);
}
private void OnItemRemovedFromContainer(ItemContainer container, Item item)
{
if (IsValid(container?.entityOwner) && (container.entityOwner.skinID == config.LargeBoxSkinIdRecycler || container.entityOwner.skinID == config.SmallBoxSkinIdRecycler))
{
notifiedPlayers.Remove((container.entityOwner.OwnerID, item.uid.Value));
item.SetFlag(itemScrapedFlag, false);
}
}
private void OnItemAddedToContainer(ItemContainer container, Item item)
{
if (IsValid(container?.entityOwner)
&& (container.entityOwner.skinID == config.LargeBoxSkinIdRecycler || container.entityOwner.skinID == config.SmallBoxSkinIdRecycler)
&& !item.flags.HasFlag(itemScrapedFlag))
{
var player = BasePlayer.FindByID(container.entityOwner.OwnerID);
if (player != null && config.DisableDuringRaidBlock && IsPlayerRaidBlocked(player))
return;
float delay = GetPlayerRecyclerDelay(player);
// Schedule item recycling with delay
timer.Once(delay, () => RecycleItem(container, item));
}
}
private float GetPlayerRecyclerDelay(BasePlayer player)
{
if (permission.UserHasPermission(player.UserIDString, PermissionRecyclerDelay1))
{
return config.DelayForPermission1;
}
if (permission.UserHasPermission(player.UserIDString, PermissionRecyclerDelay2))
{
return config.DelayForPermission2;
}
return 0f; // No delay if no relevant permission
}
private void OnPlayerLootEnd(PlayerLoot playerLoot)
{
BasePlayer player = playerLoot.GetComponentInParent<BasePlayer>();
if (player == null)
return;
foreach (var container in playerLoot.containers)
{
if (IsValid(container?.entityOwner) && (container.entityOwner.skinID == config.LargeBoxSkinIdRecycler || container.entityOwner.skinID == config.SmallBoxSkinIdRecycler))
{
CuiHelper.DestroyUi(player, UiOverlayName);
if (!container.entityOwner.IsBusy())
{
if (config.EnablePrefabEffects)
Effect.server.Run(config.PrefabEffectOnClose, player.GetNetworkPosition());
if (config.LockRecyclerDuringUse)
container.entityOwner.SetFlag(BaseEntity.Flags.Locked, false);
}
}
}
}
private object CanLootEntity(BasePlayer player, DroppedItemContainer container)
{
return container.IsValid() && droppedItemContainers.TryGetValue(container.net.ID.Value, out var data) && data.player.userID != player.userID ? true : null;
}
private void OnEntityKill(DroppedItemContainer container)
{
if (IsValid(container))
droppedItemContainers.Remove(container.net.ID.Value);
}
private object CanAcceptItem(ItemContainer container, Item item, int targetPos)
{
if (IsValid(container?.entityOwner) && (container.entityOwner.skinID == config.LargeBoxSkinIdRecycler || container.entityOwner.skinID == config.SmallBoxSkinIdRecycler))
{
if (item.flags.HasFlag(itemScrapedFlag))
{
return null;
}
if (config.BlacklistedItems.Contains(item.info.shortname) || config.BlacklistedSkins.Contains(item.skin))
{
var player = BasePlayer.FindByID(container.entityOwner.OwnerID);
if (player != null && !notifiedPlayers.Contains((player.userID, item.uid.Value)))
{
player.ChatMessage(lang.GetMessage("ItemProhibited", this, player.UserIDString));
notifiedPlayers.Add((player.userID, item.uid.Value));
}
return ItemContainer.CanAcceptResult.CannotAcceptRightNow;
}
}
return null;
}
private void OnEntityBuilt(Planner planner, GameObject builtObject)
{
var player = planner?.GetOwnerPlayer();
var entity = builtObject.GetComponent<BaseEntity>();
if (player == null || entity == null || (entity.skinID != config.LargeBoxSkinIdRecycler && entity.skinID != config.SmallBoxSkinIdRecycler))
return;
var count = CountRecyclers(player.userID);
if (count > config.RecyclerLimit)
{
player.ChatMessage(lang.GetMessage("LimitReached", this, player.UserIDString));
string itemName = entity.ShortPrefabName == "box.wooden.large" ? "box.wooden.large" : "box.wooden";
GiveItemBackToPlayer(player, itemName, entity.skinID);
entity?.Kill();
return;
}
}
private void GiveItemBackToPlayer(BasePlayer player, string itemName, ulong skinID)
{
Item item = ItemManager.CreateByName(itemName, 1, skinID);
if (!player.inventory.GiveItem(item))
{
Vector3 dropPosition = player.transform.position + Vector3.up * 0.5f;
item.Drop(dropPosition, Vector3.zero);
}
}
#endregion
#region Functions
void RecycleItem(ItemContainer container, Item item)
{
if (!IsValid(container?.entityOwner) || item.flags.HasFlag(itemScrapedFlag))
return;
bool moveBackToPlayerInventory = false;
BasePlayer player = null;
if (config.TransferRecycledItemsToInventory && container.entityOwner.name == "Virtual")
{
moveBackToPlayerInventory = true;
player = BasePlayer.FindByID(container.entityOwner.OwnerID);
container = player.inventory.containerMain;
}
if (item.info.Blueprint == null)
{
AttemptToReturnItem(item, container, player, moveBackToPlayerInventory);
return;
}
float efficiency = config.RecyclerEfficiency;
if (item.hasCondition)
efficiency *= Mathf.Clamp(item.conditionNormalized * item.maxConditionNormalized, 0.1f, 1f);
int itemAmount = item.amount > 1 ? item.amount : 1;
if (item.info.Blueprint.scrapFromRecycle > 0)
{
AttemptToProduceScrap(item, itemAmount, container, player, moveBackToPlayerInventory, efficiency);
}
item.UseItem(itemAmount);
ProduceRecycleIngredients(item, itemAmount, container, moveBackToPlayerInventory, player, efficiency);
}
private void AttemptToReturnItem(Item item, ItemContainer container, BasePlayer player, bool returnItem)
{
if (returnItem)
{
if (!item.MoveToContainer(container))
{
item.Drop(player.GetCenter(), player.GetDropVelocity());
}
}
}
private void AttemptToProduceScrap(Item item, int itemAmount, ItemContainer container, BasePlayer player, bool returnItem, float efficiency)
{
float scrapRatio = config.ScrapRatio;
int scrapAmount = 0;
try
{
float calculatedScrap = item.info.Blueprint.scrapFromRecycle * itemAmount * scrapRatio;
scrapAmount = (int)calculatedScrap;
if (calculatedScrap < 1f && UnityEngine.Random.Range(0f, 1f) >= scrapRatio)
return;
scrapAmount = scrapAmount > 0 ? scrapAmount : 1;
Item newItem = ItemManager.CreateByName("scrap", scrapAmount, 0);
if (!returnItem)
newItem.SetFlag(itemScrapedFlag, true);
if (!newItem.MoveToContainer(container) && player != null)
{
newItem.Drop(player.GetCenter(), player.GetDropVelocity());
}
}
catch
{
Puts($"Error recycling {item.info.shortname} for scrap {scrapAmount}", this);
}
}
private void ProduceRecycleIngredients(Item item, int itemAmount, ItemContainer container, bool returnItem, BasePlayer player, float efficiency)
{
foreach (ItemAmount ingredient in item.info.Blueprint.ingredients)
{
if (ingredient.itemDef.shortname != "scrap")
{
int ingredientsToProduce = Mathf.FloorToInt(itemAmount * efficiency * ingredient.amount / item.info.Blueprint.amountToCreate);
if (ingredientsToProduce < 1 && UnityEngine.Random.Range(0f, 1f) > 0.5f)
ingredientsToProduce = 1;
if (ingredientsToProduce > 0)
{
for (int i = 0; i < ingredientsToProduce; i += ingredient.itemDef.stackable)
{
int quantityToCreate = Mathf.Min(ingredientsToProduce - i, ingredient.itemDef.stackable);
try
{
Item newItem = ItemManager.Create(ingredient.itemDef, quantityToCreate, 0);
if (!returnItem)
newItem.SetFlag(itemScrapedFlag, true);
if (!newItem.MoveToContainer(container) && player != null)
{
newItem.Drop(player.GetCenter(), player.GetDropVelocity());
}
}
catch
{
Puts($"Error recycling {item.info.shortname} to ingredient {ingredient.itemDef.shortname}", this);
}
}
}
}
}
}
private void CreateUi(BasePlayer player)
{
var uiElements = new CuiElementContainer();
var mainConfigPanel = uiElements.Add(
new CuiPanel
{
Image = { Color = "1 0.96 0.88 0.15" },
RectTransform =
{
AnchorMin = "0.5 0",
AnchorMax = "0.5 0",
OffsetMin = "192.5 610",
OffsetMax = "572.5 633",
},
CursorEnabled = false,
},
"Overlay",
UiOverlayName
);
uiElements.Add(
new CuiLabel
{
Text =
{
Text = lang.GetMessage("RecyclerTitle", this, player.UserIDString),
Align = TextAnchor.MiddleCenter,
FontSize = 18,
Color = "1 1 1 1",
Font = "robotocondensed-bold.ttf",
},
RectTransform = { AnchorMin = "0 0", AnchorMax = "1 3" },
},
mainConfigPanel
);
uiElements.Add(
new CuiLabel
{
Text =
{
Text = lang.GetMessage("RecyclerSubtitle", this, player.UserIDString),
Align = TextAnchor.MiddleCenter,
FontSize = 14,
Color = "1 1 1 1",
Font = "robotocondensed-regular.ttf",
},
RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" },
},
mainConfigPanel
);
CuiHelper.AddUi(player, uiElements);
}
public bool IsValid(BaseNetworkable entity) => entity.IsValid() && !entity.IsDestroyed;
private StorageContainer CreateVirtualRecycler(BasePlayer player)
{
var virtualRecyclerContainer = GameManager.server.CreateEntity(LargeBoxPrefab, player.transform.position.WithY(-5f)) as StorageContainer;
virtualRecyclerContainer.skinID = config.LargeBoxSkinIdRecycler;
if (virtualRecyclerContainer == null)
return null;
virtualRecyclerContainer.enableSaving = false;
virtualRecyclerContainer.Spawn();
if (!IsValid(virtualRecyclerContainer))
return null;
virtualRecyclerContainer.name = "Virtual";
virtualRecyclerContainer.OwnerID = player.userID;
virtualRecyclerContainer.SetFlag(BaseEntity.Flags.Locked, true);
virtualRecyclerContainer.UpdateNetworkGroup();
virtualRecyclerContainer.gameObject.layer = 0;
virtualRecyclerContainer.SendNetworkUpdateImmediate(true);
OpenPlayerContainer(player, virtualRecyclerContainer);
activeRecyclers[virtualRecyclerContainer.net.ID.Value] = (virtualRecyclerContainer, player);
return virtualRecyclerContainer;
}
private void OpenPlayerContainer(BasePlayer player, StorageContainer container)
{
player.Invoke(
() =>
{
if (!IsValid(container))
return;
player.EndLooting();
if (!player.inventory.loot.StartLootingEntity(container, false))
return;
player.inventory.loot.AddContainer(container.inventory);
player.inventory.loot.SendImmediate();
player.ClientRPC(RpcTarget.Player("RPC_OpenLootPanel", player), container.panelName);
player.SendNetworkUpdate();
},
0.2f
);
}
private StorageContainer GetPlayerVirtualRecycler(ulong userId)
{
foreach (var (recycler, player) in activeRecyclers.Values)
{
if (player?.userID == userId)
return recycler;
}
return null;
}
private void EmptyRecyclerContents(StorageContainer recyclerBox, BasePlayer player)
{
if (player == null || player.inventory?.containerMain == null || recyclerBox?.inventory?.itemList.IsNullOrEmpty() != false)
return;
var itemList = Pool.Get<List<Item>>();
itemList.AddRange(recyclerBox.inventory.itemList);
if (itemList.Count == 0)
{
Pool.FreeUnmanaged(ref itemList);
return;
}
player.ChatMessage(lang.GetMessage("RecycleDropped", this, player.UserIDString));
var droppedContainer = GameManager.server.CreateEntity(DroppedItemPrefab, player.transform.position + Vector3.up) as DroppedItemContainer;
if (droppedContainer == null)
return;
droppedContainer.enableSaving = false;
droppedContainer.lootPanelName = "generic_resizable";
droppedContainer.playerSteamID = player.userID;
droppedContainer.TakeFrom(new[] { recyclerBox.inventory });
droppedContainer.Spawn();
if (IsValid(droppedContainer))
droppedItemContainers[droppedContainer.net.ID.Value] = (droppedContainer, player);
Pool.FreeUnmanaged(ref itemList);
}
private void TerminateVirtualRecycler(BasePlayer player)
{
StorageContainer virtualRecycler = GetPlayerVirtualRecycler(player.userID);
if (IsValid(virtualRecycler) && activeRecyclers.Remove(virtualRecycler.net.ID.Value))
{
EmptyRecyclerContents(virtualRecycler, player);
virtualRecycler.Kill();
}
}
private void RemoveAllDroppedItemContainers()
{
if (droppedItemContainers.Count == 0)
return;
var temporaryContainerList = Pool.Get<List<KeyValuePair<ulong, (DroppedItemContainer container, BasePlayer player)>>>();
temporaryContainerList.AddRange(droppedItemContainers);
foreach (var (uid, (container, _)) in temporaryContainerList)
{
if (IsValid(container))
container.Kill();
droppedItemContainers.Remove(uid);
}
Pool.FreeUnmanaged(ref temporaryContainerList);
}
private void OpenVirtualRecycler(BasePlayer player)
{
if (player == null || !CanPlayerAccessRecycler(player))
return;
TerminateVirtualRecycler(player);
CreateVirtualRecycler(player);
}
private bool CanPlayerAccessRecycler(BasePlayer player)
{
if (config.CheckPlayerAlive && (player == null || !player.IsAlive()))
{
player.ChatMessage(lang.GetMessage("Denied Hook Denied", this, player.UserIDString));
}
else if (config.CheckPlayerWounded && player.IsWounded())
{
player.ChatMessage(lang.GetMessage("Denied Wounded", this, player.UserIDString));
}
else if (config.CheckBuildingPrivilege && !player.CanBuild())
{
player.ChatMessage(lang.GetMessage("Denied Privilege", this, player.UserIDString));
}
else if (config.CheckSwimming && player.IsSwimming())
{
player.ChatMessage(lang.GetMessage("Denied Swimming", this, player.UserIDString));
}
else if (config.CheckFalling && (!player.IsOnGround() || player.IsFlying || player.isInAir))
{
player.ChatMessage(lang.GetMessage("Denied Falling", this, player.UserIDString));
}
else if (config.CheckMounted && (player.isMounted || player.GetParentEntity() is BaseMountable))
{
player.ChatMessage(lang.GetMessage("Denied Mounted", this, player.UserIDString));
}
else if (config.CheckCargoShip && player.GetComponentInParent<CargoShip>())
{
player.ChatMessage(lang.GetMessage("Denied Ship", this, player.UserIDString));
}
else if (config.CheckBalloon && player.GetComponentInParent<HotAirBalloon>())
{
player.ChatMessage(lang.GetMessage("Denied Balloon", this, player.UserIDString));
}
else if (config.CheckElevator && player.GetComponentInParent<Lift>())
{
player.ChatMessage(lang.GetMessage("Denied Elevator", this, player.UserIDString));
}
else if (config.CheckSafeZone && player.InSafeZone())
{
player.ChatMessage(lang.GetMessage("Denied Safe Zone", this, player.UserIDString));
}
else
{
return true;
}
return false;
}
#endregion
#region Commands
private void CommandGetRecyclerbox(IPlayer iplayer, string command, string[] args)
{
if (!(iplayer.Object is BasePlayer player))
return;
if (!iplayer.HasPermission(PermissionGetRecycler))
{
player.ChatMessage(lang.GetMessage("NotAllowed", this, player.UserIDString));
return;
}
var count = CountRecyclers(player.userID);
player.ChatMessage("count: " + count);
if (count >= config.RecyclerLimit)
{
player.ChatMessage(lang.GetMessage("LimitReached", this, player.UserIDString));
return;
}
Item recyclerBox = ItemManager.CreateByName("box.wooden.large");
recyclerBox.skin = config.LargeBoxSkinIdRecycler;
if (args.Length > 0 && args[0] == "small")
{
recyclerBox = ItemManager.CreateByName("box.wooden");
recyclerBox.skin = config.SmallBoxSkinIdRecycler;
}
else
{
if (args.Length > 0)
{
player.ChatMessage(lang.GetMessage("NotAllowed", this, player.UserIDString));
return;
}
}
player.GiveItem(recyclerBox, BaseEntity.GiveItemReason.PickedUp);
player.ChatMessage(lang.GetMessage("GotRecyclerbox", this, player.UserIDString));
}
private int CountRecyclers(ulong playerId)
{
return UnityEngine.Object.FindObjectsOfType<StorageContainer>()
.Count(container =>
container.OwnerID == playerId
&& (
(container.skinID == config.LargeBoxSkinIdRecycler && container.PrefabName == LargeBoxPrefab)
|| (container.skinID == config.SmallBoxSkinIdRecycler && container.PrefabName == SmallBoxPrefab)
)
);
}
private void CommandTransformRecyclerbox(IPlayer iplayer, string command, string[] args)
{
if (!(iplayer.Object is BasePlayer player))
return;
if (!iplayer.HasPermission(PermissionTransformRecycler))
{
player.ChatMessage(lang.GetMessage("NotAllowed", this, player.UserIDString));
return;
}
var count = CountRecyclers(player.userID);
player.ChatMessage("count: " + count);
if (count >= config.RecyclerLimit)
{
player.ChatMessage(lang.GetMessage("LimitReached", this, player.UserIDString));
return;
}
Item activeItem = player.GetActiveItem();
if (activeItem == null || (activeItem.info.shortname != "box.wooden.large" && activeItem.info.shortname != "box.wooden"))
{
player.ChatMessage(lang.GetMessage("RecyclerItemMissingMsg", this, player.UserIDString));
return;
}
if (activeItem.skin == config.LargeBoxSkinIdRecycler || activeItem.skin == config.SmallBoxSkinIdRecycler)
{
player.ChatMessage(lang.GetMessage("RecyclerAlreadyMsg", this, player.UserIDString));
return;
}
player.ChatMessage(lang.GetMessage("RecyclerCreatedMsg", this, player.UserIDString));
activeItem.skin = activeItem.info.shortname == "box.wooden.large" ? config.LargeBoxSkinIdRecycler : config.SmallBoxSkinIdRecycler;
}
private void CommandUseVirtualRecycler(IPlayer iplayer, string command, string[] args)
{
if (!(iplayer.Object is BasePlayer player))
return;
if (!iplayer.HasPermission(PermissionUseVirtualRecycler))
{
player.ChatMessage(lang.GetMessage("NotAllowed", this, player.UserIDString));
return;
}
OpenVirtualRecycler(player);
}
#endregion
}
}
Explanation of Changes:
-
Added New Permissions:
recyclerbox.delay1 and recyclerbox.delay2 for determining delay durations when recycling.
-
Configuration:
- Introduced two new configuration options,
DelayForPermission1 and DelayForPermission2, which indicate the recycling delay durations in seconds for players with permissions recyclerbox.delay1 and recyclerbox.delay2, respectively.
-
Delay Handling:
- The
GetPlayerRecyclerDelay method calculates the applicable delay based on the player's permissions.
- In
OnItemAddedToContainer, a delay is introduced via the timer.Once method before calling RecycleItem.
These changes enable configurable item recycling delays based on player permissions. Adjust configuration values and assign permissions to control the delays for different player groups.