csharp Copy code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Facepunch;
using Facepunch.Rust;
using Network;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
using UnityEngine.Assertions;
namespace Oxide.Plugins
{
[Info("Aa", "Marte6", "1.0.0")]
[Description("Aa")]
class Aa : RustPlugin
{
#region Fields
private Configuration _config;
#endregion
#region Oxide Hooks
private void Init()
{
RegisterCommands();
}
object OnEntityKill(Recycler entity)
{
Puts("OnEntityKill works!: " + entity);
return null;
}
private void OnEntityBuilt(Planner plan, GameObject obj)
{
var player = plan?.GetOwnerPlayer();
var entity = obj.GetComponent<BaseEntity>();
if (player == null || entity == null || entity.ShortPrefabName != "generator.small" || entity.skinID != 3341430953)
{
return;
}
if (!DeployRecycler(player, entity))
{
player.ChatMessage("No Fundation");
timer.Once(1, () => GiveRecyclerItem(player));
}
NextTick(() => entity?.Kill());
}
#endregion
#region Commands
private void RegisterCommands()
{
foreach (var command in _config.Commands)
{
cmd.AddChatCommand(command, this, nameof(CmdTest));
}
}
private void CmdTest(BasePlayer player, string command, string[] args)
{
GiveRecyclerItem(player);
}
#endregion
#region Utility Methods
private bool DeployRecycler(BasePlayer player, BaseEntity entity)
{
var buildingBlock = GetBuildingBlock(entity);
if (buildingBlock == null)
{
return false;
}
var recycler = GameManager.server.CreateEntity("assets/bundled/prefabs/static/recycler_static.prefab");
recycler.OwnerID = player.userID;
recycler.SetParent(buildingBlock);
recycler.transform.position = entity.transform.position;
recycler.transform.rotation = entity.transform.rotation * Quaternion.Euler(0, 270, 0);
recycler.Spawn();
recycler.SendNetworkUpdateImmediate();
return true;
}
private BuildingBlock GetBuildingBlock(BaseEntity entity)
{
Vector3 origin = entity.transform.position + Vector3.up * 0.1f;
Ray ray = new(origin, Vector3.down);
if (Physics.Raycast(ray, out RaycastHit hitInfo, 4f, LayerMask.GetMask("Construction")))
{
var groundEntity = hitInfo.GetEntity();
if (groundEntity is BuildingBlock buildingBlock)
{
return buildingBlock;
}
}
return null;
}
private void GiveRecyclerItem(BasePlayer player)
{
Item item = ItemManager.CreateByName("electric.generator.small", 1, 3341430953);
item.name = "Recycler";
player.inventory.GiveItem(item);
}
#endregion
#region Configuration
private class Configuration
{
[JsonProperty("Commands")]
public List<string> Commands { get; set; }
[JsonProperty("Version")]
public VersionNumber Version { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>() ?? throw new Exception();
if (_config.Version < Version)
{
UpdateConfigValues();
}
SaveConfig();
}
catch
{
PrintError("There is an error in your configuration file. Default values will be used.");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig()
{
_config = CreateDefaultConfig();
}
protected override void SaveConfig()
{
Config.WriteObject(_config);
}
private void UpdateConfigValues()
{
PrintWarning("Configuration change detected! Updating settings.");
_config.Version = Version;
PrintWarning("Configuration update completed!");
}
private Configuration CreateDefaultConfig()
{
return new Configuration
{
Commands = new List<string> { "teste", "testee", "testeeee" },
};
}
#endregion
}
}