using System.Collections.Generic;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("ResourceGenerator", "YourName", "1.0.0")]
[Description("Generates resources and components in a chest every second, while players are online")]
public class ResourceGenerator : RustPlugin
{
private readonly Dictionary<uint, Timer> timers = new Dictionary<uint, Timer>();
private void OnPlayerInit(BasePlayer player)
{
// Start generating resources for the player
StartResourceGeneration(player);
}
private void OnPlayerDisconnected(BasePlayer player, string reason)
{
// Stop generating resources when player goes offline
StopResourceGeneration(player);
}
private void StartResourceGeneration(BasePlayer player)
{
// Create a new timer for the player
Timer timer = timer.Once(1f, () => GenerateResources(player), this);
timers[player.userID] = timer;
}
private void StopResourceGeneration(BasePlayer player)
{
// Stop the timer for the player
if (timers.TryGetValue(player.userID, out Timer timer))
{
timer.Destroy();
timers.Remove(player.userID);
}
}
private void GenerateResources(BasePlayer player)
{
// Check if the player is still online
if (player.IsConnected)
{
// Get the player's chest with skinid 2778291400
BaseEntity chest = FindChest(player, 2778291400);
if (chest != null)
{
// Add resources and components to the chest
chest.inventory.AddItem(ResourceType.Wood, 1000);
chest.inventory.AddItem(ResourceType.Metal, 500);
chest.inventory.AddItem(ComponentType.Gear, 20);
// Remove 1 fuel from the chest
chest.inventory.Take(null, ItemType.Fuel, 1);
}
}
// Continue generating resources every second
StartResourceGeneration(player);
}
private BaseEntity FindChest(BasePlayer player, ulong skinId)
{
// Get all entities in the player's vicinity
foreach (var entity in BaseEntity.saveList)
{
// Check if the entity is a storage container
if (entity is StorageContainer storageContainer)
{
// Check if the storage container has the specified skinid
if (storageContainer.skinID == skinId)
{
// Check if the storage container is owned by the player
if (storageContainer.OwnerID == player.userID)
{
return storageContainer;
}
}
}
}
return null;
}
private class ResourceType
{
public static ItemDefinition Wood = ItemManager.FindItemDefinition("wood");
public static ItemDefinition Metal = ItemManager.FindItemDefinition("metal");
}
private class ComponentType
{
public static ItemDefinition Gear = ItemManager.FindItemDefinition("gear");
}
}
}