Advertisement
Krythic

Egglayer

Jun 13th, 2024
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace Oxide.Plugins
  6. {
  7.     [Info("Egglayer", "Krythic", "1.0.0")]
  8.     [Description("Allows players to randomly lay eggs after consuming food.")]
  9.  
  10.     public class EggLayer : RustPlugin
  11.     {
  12.         #region Variables
  13.  
  14.         private int poopingProbability = 5;
  15.  
  16.         private const string horseDungShortname = "easter.goldegg";
  17.  
  18.         private const string ignorePermission = "egglayer.ignore";
  19.  
  20.         private const string canPoopPermission = "egglayer.canpoop";
  21.  
  22.         private const string splashEffect = "assets/bundled/prefabs/fx/water/midair_splash.prefab";
  23.  
  24.         private const string screamSound = "assets/bundled/prefabs/fx/player/beartrap_scream.prefab";
  25.  
  26.         private const string invisibleChairPrefab = "assets/bundled/prefabs/static/chair.invisible.static.prefab";
  27.  
  28.         private readonly List<BaseMountable> spawnedChairs = new List<BaseMountable> { };
  29.        
  30.         private List<EggEntry> eggEntries = new List<EggEntry>();
  31.  
  32.         #endregion
  33.  
  34.         #region Oxide Hooks
  35.  
  36.         private void Init()
  37.         {
  38.             if (config.probabilityOfPooping < 1 || config.probabilityOfPooping > 100)
  39.                 Puts("The probability of egg laying must be between 1 and 100! Loaded default value: 5!");
  40.  
  41.             else poopingProbability = config.probabilityOfPooping;
  42.  
  43.             permission.RegisterPermission(ignorePermission, this);
  44.             permission.RegisterPermission(canPoopPermission, this);
  45.             eggEntries.Add(new EggEntry("easter.goldegg","Feces Covered Golden Egg...gross",5));
  46.             eggEntries.Add(new EggEntry("easter.silveregg","Feces Covered Silver Egg...gross",10));
  47.             eggEntries.Add(new EggEntry("easter.bronzeegg","Feces Covered Bronze Egg...gross",100));
  48.            
  49.         }
  50.  
  51.         private void OnItemUse(Item item, int amount)
  52.         {
  53.             if (item == null || item.info.category != ItemCategory.Food || item.info.shortname.Contains("seed") || Random.Range(0, 100) > poopingProbability) return;
  54.  
  55.             ItemContainer container = item.GetRootContainer();
  56.  
  57.             if (container == null) return;
  58.  
  59.             BasePlayer player = container.GetOwnerPlayer();
  60.  
  61.             if (player == null || ((player.metabolism.calories.value != player.metabolism.calories.max) && config.maxFullBar) || permission.UserHasPermission(player.UserIDString, ignorePermission) || (config.requiresPermission && !permission.UserHasPermission(player.UserIDString, canPoopPermission))) return;
  62.            
  63.             EggEntry entry = GetNextEggEntry();
  64.            
  65.             if(entry != null){
  66.                 Item gameItem = ItemManager.CreateByName(entry.shortname);
  67.  
  68.                 if (gameItem == null) return;
  69.  
  70.                 gameItem.name = entry.itemText;
  71.  
  72.                 if (config.playScreamSound) Effect.server.Run(screamSound, player.transform.position);
  73.  
  74.                 gameItem.Drop(player.transform.position, player.GetDropVelocity());
  75.             }
  76.         }
  77.  
  78.         #endregion
  79.  
  80.         #region Methods
  81.        
  82.         private EggEntry GetNextEggEntry()
  83.         {
  84.             foreach(EggEntry entry in eggEntries)
  85.             {
  86.                 if(Random.Range(0,100) <= entry.probability)
  87.                 {
  88.                     return entry;
  89.                 }
  90.             }
  91.             return null;
  92.         }
  93.  
  94.         #endregion
  95.        
  96.         public class EggEntry
  97.         {
  98.             public string shortname;
  99.             public string itemText;
  100.             public int probability;
  101.            
  102.             public EggEntry(string shortname, string itemText, int probability)
  103.             {
  104.                 this.shortname = shortname;
  105.                 this.itemText = itemText;
  106.                 this.probability = probability;
  107.             }
  108.         }
  109.  
  110.         #region Config
  111.  
  112.         private ConfigData config = new ConfigData();
  113.  
  114.         private class ConfigData
  115.         {
  116.             [JsonProperty(PropertyName = "Requires Permission To Poop")]
  117.             public bool requiresPermission = false;
  118.  
  119.             [JsonProperty(PropertyName = "Probability Of Pooping (1-100)")]
  120.             public int probabilityOfPooping = 5;
  121.  
  122.             [JsonProperty(PropertyName = "Only Poop If The Food Bar Is Full")]
  123.             public bool maxFullBar = false;
  124.  
  125.             [JsonProperty(PropertyName = "Sit Player When Pooping (3 Seconds)")]
  126.             public bool sitPlayer = false;
  127.  
  128.             [JsonProperty(PropertyName = "Play Scream Sound When Pooping")]
  129.             public bool playScreamSound = false;
  130.  
  131.             [JsonProperty(PropertyName = "Splash Water Effect When Pooping If Raw Food")]
  132.             public bool splashWaterEffect = false;
  133.  
  134.             [JsonProperty(PropertyName = "Fertilize Planter Boxes When Pooped On Top")]
  135.             public bool fertilizePlanterBoxes = false;
  136.  
  137.             [JsonProperty(PropertyName = "Spawn Poop Entity When Planter Boxes Are Fertilized")]
  138.             public bool spawnPoopIfFertilized = false;
  139.         }
  140.  
  141.         protected override void LoadConfig()
  142.         {
  143.             base.LoadConfig();
  144.  
  145.             try
  146.             {
  147.                 config = Config.ReadObject<ConfigData>();
  148.  
  149.                 if (config == null) LoadDefaultConfig();
  150.             }
  151.             catch
  152.             {
  153.                 PrintError("Configuration file is corrupt, check your config file at https://jsonlint.com/!");
  154.                 LoadDefaultConfig();
  155.                 return;
  156.             }
  157.  
  158.             SaveConfig();
  159.         }
  160.  
  161.         protected override void LoadDefaultConfig() => config = new ConfigData();
  162.  
  163.         protected override void SaveConfig() => Config.WriteObject(config);
  164.  
  165.         #endregion
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement