C#:
using System;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using Oxide.Core;
using Oxide.Core.Plugins;
#region Changelogs and ToDo
/**********************************************************************
*
* 1.0.0 : - Initial release
* 1.0.1 : - Reformatted Debug check
* 1.0.2 : - Added cfg option to put apples in the players inventory before tossing it on the ground.
* - Added cfg option to exclude certain tree prefabs (full path)
* Set debug true to log the tree that has been cut down helping to find the trees you dont want the apples to drop from.
*
**********************************************************************/
#endregion
namespace Oxide.Plugins
{
[Info("Apples", "Krungh Crow", "1.0.2")]
[Description("When the apple falls")]
class Apples : RustPlugin
{
#region Variables
string Drop_Perm = "apples.drop";
ulong chaticon = 0;
string prefix;
bool Debug = false;
#endregion
#region Configuration
void Init()
{
if (!LoadConfigVariables())
{
Puts("Config file issue detected. Please delete file, or check syntax and fix.");
return;
}
permission.RegisterPermission(Drop_Perm, this);
Debug = configData.PlugCFG.Debug;
prefix = configData.PlugCFG.Prefix;
chaticon = configData.PlugCFG.Chaticon;
if (Debug) Puts($"[Debug] The debug for [{this.Name}] is active if unintentional change cfg and reload");
}
private ConfigData configData;
class ConfigData
{
[JsonProperty(PropertyName = "Main config")]
public SettingsPlugin PlugCFG = new SettingsPlugin();
[JsonProperty(PropertyName = "Dropchance (0-1)")]
public float Dropchance = 0.5f;
[JsonProperty(PropertyName = "Min apples")]
public int Min = 1;
[JsonProperty(PropertyName = "Max apples")]
public int Max = 3;
[JsonProperty(PropertyName = "Put in Inventory (true/false)")]
public bool PutInInventory = true;
[JsonProperty(PropertyName = "Excluded Tree Prefabs (full paths)")]
public List<string> ExcludedTrees = new List<string>();
}
class SettingsPlugin
{
[JsonProperty(PropertyName = "Debug")]
public bool Debug = false;
[JsonProperty(PropertyName = "Chat Steam64ID")]
public ulong Chaticon = 0;
[JsonProperty(PropertyName = "Chat Prefix")]
public string Prefix = "[<color=orange>Apples</color>] : ";
}
private bool LoadConfigVariables()
{
try{ configData = Config.ReadObject<ConfigData>(); }
catch{ return false; }
SaveConf();
return true;
}
protected override void LoadDefaultConfig()
{
Puts("Fresh install detected. Creating a new config file.");
configData = new ConfigData
{
ExcludedTrees = new List<string>
{
"assets/bundled/prefabs/autospawn/resource/wood_log_pile/wood-pile.prefab"
}
};
SaveConf();
}
void SaveConf() => Config.WriteObject(configData, true);
#endregion
#region LanguageAPI
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["Applesdrop"] = "{0} Apple(s) fell from the tree",
}, this);
}
#endregion
#region Oxide Hooks
void OnDispenserBonus(ResourceDispenser dispenser , BasePlayer player , Item item)
{
if (player == null) return;
if (dispenser.gatherType == ResourceDispenser.GatherType.Tree)
{
string prefabPath = dispenser.gameObject.ToBaseEntity().PrefabName;
Debugmsg(prefabPath); //example
if (configData.ExcludedTrees.Contains(prefabPath))
{
Debugmsg($"Tree with prefab path '{prefabPath}' is in the exclusion list. No apples will drop.");
return;
}
if (!HasPerm(player , Drop_Perm))
{
Debugmsg($"{player.displayName} did not have the {Drop_Perm} permission assigned, skipping trigger");
return;
}
if (UnityEngine.Random.Range(0f , 1f) <= configData.Dropchance)
{
int amount = UnityEngine.Random.Range(configData.Min , configData.Max);
var apple = ItemManager.CreateByName("apple" , amount , 0);
if (apple != null)
{
Debugmsg($"A tree was cut down by {player.displayName} and {amount} apple(s) should be given.");
if (configData.PutInInventory)
{
if (player.inventory.GiveItem(apple))
{
Debugmsg($"{amount} apple(s) were added to {player.displayName}'s inventory");
Player.Message(player , msg("Applesdrop").Replace("{0}" , amount.ToString()) , chaticon);
}
else
{
Debugmsg($"{player.displayName} had no inventory space, dropping apple(s) instead");
apple.DropAndTossUpwards(player.transform.position + new Vector3(0f , 1.0f , 0f));
Player.Message(player , msg("Applesdrop").Replace("{0}" , amount.ToString()) , chaticon);
}
}
else
{
apple.DropAndTossUpwards(player.transform.position + new Vector3(0f , 1.0f , 0f));
Player.Message(player , msg("Applesdrop").Replace("{0}" , amount.ToString()) , chaticon);
}
return;
}
}
Debugmsg($"The tree did not drop apple(s) for {player.displayName}");
return;
}
Debugmsg("ResourceDispenser was not a tree");
}
#endregion
#region Message helpers
private string msg(string key, string id = null) => prefix + lang.GetMessage(key, this, id);
void Debugmsg(string key) { if (Debug) Puts($"[Debug] {key}"); }
#endregion
#region Helpers
bool HasPerm(BasePlayer player, string perm) { return permission.UserHasPermission(player.UserIDString, perm); }
#endregion
}
}