C#:
using Oxide.Core;
using Facepunch;
using Oxide.Core.Libraries.Covalence;
using UnityEngine;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Oxide.Plugins
{
[Info("Bush Trimmer", "Snaplatack", "1.0.2")]
[Description("Remove bushes with a command")]
class BushTrimmer : RustPlugin
{
private HashSet<string> adminPlayers = new();
#region Initialize
private void Init()
{
AddCovalenceCommand(config.commandPerm.trimBushCommand, nameof(ClearBushCmd), config.commandPerm.allowTrim);
}
#endregion
#region Commands
private void ClearBushCmd(BasePlayer player, string command, string[] args)
{
if (player == null || player.IsNpc) return;
if (!permission.UserHasPermission(player.UserIDString, config.commandPerm.allowTrim)) return;
float radius;
if (args.Length > 0)
float.TryParse(args[0], out radius);
else
radius = config.settings.radiusToDetect;
try
{
if (!player.IsAdmin)
{
player.SetPlayerFlag(BasePlayer.PlayerFlags.IsAdmin, true);
player.SendNetworkUpdateImmediate();
}
else adminPlayers.Add(player.UserIDString);
RemoveBush(player, radius);
}
catch // in case anything above errors, we ensure the player doesnt have admin
{
player.SetPlayerFlag(BasePlayer.PlayerFlags.IsAdmin, false);
player.SendNetworkUpdateImmediate();
if (adminPlayers.Contains(player.UserIDString)) adminPlayers.Remove(player.UserIDString);
}
}
#endregion
#region Methods
private void RemoveBush(BasePlayer player, float radius)
{
player.SendConsoleCommand("clear_bushes", radius);
Player.Message(player, string.Format(config.settings.msgResponse, radius));
if (!adminPlayers.Contains(player.UserIDString))
{
player.SetPlayerFlag(BasePlayer.PlayerFlags.IsAdmin, false);
player.SendNetworkUpdate();
return;
}
adminPlayers.Remove(player.UserIDString);
}
#endregion
#region Configuration
private Configuration config;
private class Configuration
{
[JsonProperty(PropertyName = "General Settings")]
public PluginSettings settings = new PluginSettings
{
radiusToDetect = 1.2f,
msgResponse = "Removing bushes within {0}m of your position..."
};
[JsonProperty(PropertyName = "Permissions & Commands")]
public PermsCommands commandPerm = new PermsCommands
{
allowTrim = "bushtrimmer.allow",
trimBushCommand = new string[] { "trimbush", "bt" }
};
public Core.VersionNumber Version = new Core.VersionNumber(0, 0, 0);
}
protected override void LoadDefaultConfig() => config = new Configuration();
protected override void LoadConfig()
{
try
{
base.LoadConfig();
config = Config.ReadObject<Configuration>();
if (config == null)
{
LoadDefaultConfig();
}
if (config.Version != Version)
{
UpdateConfig();
}
}
catch
{
Puts($"\n////////////////////////\n\n\n// Configuration file {Name}.json is invalid!\n// Resetting the config now!\n\n\n////////////////////////");
LoadDefaultConfig();
SaveConfig();
}
}
protected override void SaveConfig()
{
Puts($"Configuration changes saved to {Name}.json");
Config.WriteObject(config, true);
}
private void UpdateConfig()
{
if (config.Version >= Version) return;
if (config.Version < new VersionNumber(1, 0, 2))
{
LoadDefaultConfig();
var configUpdateStr = "[CONFIG UPDATE] Updating to Version {0}";
PrintWarning(string.Format(configUpdateStr, Version));
config.Version = this.Version;
SaveConfig();
}
}
#endregion
#region Definitions
private class PluginSettings
{
[JsonProperty(PropertyName = "Radius to detect bushes from players position [1.2 - 1.5 Recommended]")]
public float radiusToDetect;
[JsonProperty(PropertyName = "Response message [0 = radius]")]
public string msgResponse;
}
private class PermsCommands
{
[JsonProperty(PropertyName = "Permission to allow trimming bushes")]
public string allowTrim;
[JsonProperty(PropertyName = "Command to trim bushes")]
public string[] trimBushCommand;
}
#endregion
}
}