Advertisement
isuelt

TrapFloors.cs

Apr 5th, 2022
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Oxide.Core;
  5. using Oxide.Core.Configuration;
  6.  
  7. using Rust;
  8. using UnityEngine;
  9.  
  10. using IEnumerator = System.Collections.IEnumerator;
  11.  
  12. namespace Oxide.Plugins
  13. {
  14.     [Info("TrapFloors", "Cheeze", "1.2.1", ResourceId = 2038)]
  15.     [Description("Allows players to make trap floors that collapse on non cupboard authorised players")]
  16.  
  17.     class TrapFloors : RustPlugin
  18.     {
  19.         static readonly DynamicConfigFile DataFile = Interface.Oxide.DataFileSystem.GetFile("TrapFloors");
  20.         static List<Floor> trapFloors = new List<Floor>();
  21.         const string permAdmin = "trapfloors.admin";
  22.         const string permPlayer = "trapfloors.use";
  23.         BaseEntity newTrapFloor;
  24.  
  25.         class Floor
  26.         {
  27.             public uint Id;
  28.             public string Location;
  29.             public ulong PlayerId;
  30.             internal static Floor GetTrap(uint id) => trapFloors.First(x => x.Id == id);
  31.         }
  32.  
  33.         protected override void LoadDefaultConfig()
  34.         {
  35.             Config["MaxFloors"] = 6;
  36.             Config["EnableFoundationTraps"] = true;
  37.             SaveConfig();
  38.         }
  39.  
  40.         void Init()
  41.         {
  42.             LoadDefaultMessages();
  43.             trapFloors = DataFile.ReadObject<List<Floor>>();
  44.             permission.RegisterPermission(permAdmin, this);
  45.             permission.RegisterPermission(permPlayer, this);
  46.         }
  47.  
  48.         void OnServerInitialized()
  49.         {
  50.             foreach (var ent in UnityEngine.Object.FindObjectsOfType<BaseEntity>())
  51.             {
  52.                 if (trapFloors.All(x => x.Id != ent.net.ID)) continue;
  53.                 newTrapFloor.gameObject.AddComponent<FloorTrap>();
  54.                 ent.gameObject.AddComponent<FloorTrap>();
  55.             }
  56.         }
  57.  
  58.         void LoadDefaultMessages()
  59.         {
  60.             var messages = new Dictionary<string, string>
  61.             {
  62.                 ["ADMIN_BAD_SYNTAX"] = "Invalid, use /trapfloor add/remove/list/wipe",
  63.                 ["ADMIN_ONLY"] = "That command is for admins only",
  64.                 ["BAD_SYNTAX"] = "Incorrect Syntax, /trapfloor add",
  65.                 ["END_LIST"] = "End of Trap Floor List",
  66.                 ["FLOOR_EXISTS"] = "There is already a floor stored with the same ID",
  67.                 ["FLOOR_LIST"] = "Floor ID: {0}, Floor Location: {1}, Player ID: {2}",
  68.                 ["FLOOR_SET"] = "Trap Floor set! You have set {0} out of {1} available trap floors",
  69.                 ["INSTRUCTIONS"] = "Use: /trapfloor remove <id> || Trap List: /trapfloor list ",
  70.                 ["LIST"] = "Trap Floor List",
  71.                 ["MAX_REACHED"] = "Cannot create TrapFloor, you have reached the maximum of {0} TrapFloors",
  72.                 ["NOT_FLOOR"] = "This is not a floor",
  73.                 ["NO_BUILD"] = "You are not authorised here",
  74.                 ["NO_FLOOR"] = "No floor detected",
  75.                 ["NO_PERMISSION"] = "Sorry, you are not authorised to use this",
  76.                 ["REMOVED"] = "Trap floor removed with floor ID: {0}",
  77.                 ["WIPED"] = "Trap floors wiped!",
  78.                 ["NO_FOUNDATION"] = "You cannot set foundations as traps"
  79.             };
  80.             lang.RegisterMessages(messages, this);
  81.         }
  82.  
  83.         void OnServerSave() => SaveData();
  84.  
  85.         void Unloaded()
  86.         {
  87.             foreach (var floor in UnityEngine.Object.FindObjectsOfType<FloorTrap>())
  88.                 UnityEngine.Object.Destroy(floor);
  89.         }
  90.  
  91.         string pName = "<color=orange>TrapFloors:</color> ";
  92.  
  93.         #region ColliderCheck
  94.  
  95.         class FloorTrap : MonoBehaviour
  96.         {
  97.             void Awake()
  98.             {
  99.                 gameObject.name = "FloorTrap";
  100.                 gameObject.layer = (int)Layer.Reserved1;
  101.  
  102.                 var rigidbody = gameObject.GetComponent<Rigidbody>() ?? gameObject.AddComponent<Rigidbody>();
  103.  
  104.                 rigidbody.useGravity = false;
  105.                 rigidbody.isKinematic = true;
  106.                 rigidbody.detectCollisions = true;
  107.                 rigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete;
  108.  
  109.                 UpdateCollider();
  110.             }
  111.  
  112.             void UpdateCollider()
  113.             {
  114.                 var collider = gameObject.GetComponent<BoxCollider>() ?? gameObject.AddComponent<BoxCollider>();
  115.                 collider.size = new Vector3(1.5f, 0.25f, 1.5f);
  116.                 collider.isTrigger = true;
  117.                 collider.enabled = true;
  118.             }
  119.  
  120.             void OnTriggerEnter(Collider col)
  121.             {
  122.                 if (!(col.gameObject.ToBaseEntity() is BasePlayer)) return;
  123.                 var player = (BasePlayer)col.gameObject.ToBaseEntity();
  124.  
  125.                 if (player.CanBuild()) return;
  126.  
  127.                 StartCoroutine(KillIt(gameObject.ToBaseEntity()));
  128.                 trapFloors.Remove(Floor.GetTrap(gameObject.ToBaseEntity().net.ID));
  129.                 SaveData();
  130.  
  131.                 Effect.server.Run("assets/bundled/prefabs/fx/build/repair_failed.prefab", player.transform.position, Vector3.zero, null, true);
  132.             }
  133.  
  134.             IEnumerator KillIt(BaseEntity ent)
  135.             {
  136.                 yield return new WaitForSeconds(0.4f);
  137.  
  138.                 if (!ent.IsDestroyed)
  139.                     ent.Kill(BaseNetworkable.DestroyMode.Gib);
  140.             }
  141.         }
  142.  
  143.         #endregion Collider Check
  144.  
  145.         void OnEntityDeath(BaseCombatEntity entity)
  146.         {
  147.             if (!(entity is BuildingBlock)) return;
  148.             if ((!entity.name.Contains("floor/floor") || (!entity.name.Contains("foundation")) && (trapFloors.All(x => x.Id != entity.net.ID)))) return;
  149.  
  150.             trapFloors.Remove(Floor.GetTrap(Convert.ToUInt32(entity.net.ID)));
  151.             SaveData();
  152.         }
  153.  
  154.         [ChatCommand("trapfloor")]
  155.         void cmdAddTrapFloor(BasePlayer player, string command, string[] args)
  156.         {
  157.             if (args.Length == 0)
  158.             {
  159.                 PrintToChat(player, pName + LangMsg("BAD_SYNTAX", player.UserIDString));
  160.                 return;
  161.             }
  162.  
  163.             if ((args[0] == "add") || (args[0] != "remove") || (args[0] != "wipe") || (args[0] != "list"))
  164.             {
  165.                 switch (args[0])
  166.                 {
  167.                     case "add":
  168.                         if (!HasPermission(player, permPlayer))
  169.                     {
  170.                         PrintToChat(player, pName + LangMsg("NO_PERMISSION", player.UserIDString));
  171.                         return;
  172.                     }
  173.  
  174.                     if (!player.CanBuild())
  175.                     {
  176.                         PrintToChat(player, pName + LangMsg("NO_BUILD", player.UserIDString));
  177.                         return;
  178.                     }
  179.  
  180.                     int amount = trapFloors.Count(x => x.PlayerId == player.userID);
  181.                     Int32 max = Convert.ToInt32(Config["MaxFloors"]);
  182.                     bool useFoundations = Convert.ToBoolean(Config["EnableFoundationTraps"]);
  183.                     if (amount < max)
  184.                     {
  185.                         RaycastHit hit;
  186.  
  187.                         if (Physics.Raycast(player.eyes.HeadRay(), out hit, Mathf.Infinity)) newTrapFloor = hit.GetTransform().gameObject.ToBaseEntity();
  188.  
  189.                         if (newTrapFloor == null)
  190.                         {
  191.                             PrintToChat(player, pName + LangMsg("NO_FLOOR", player.UserIDString));
  192.                             return;
  193.                         }
  194.  
  195.                         if (trapFloors.Any(x => x.Id == newTrapFloor.net.ID))
  196.                         {
  197.                             PrintToChat(player, pName + LangMsg("FLOOR_EXISTS", player.UserIDString));
  198.                             return;
  199.                         }
  200.  
  201.                         if ((!newTrapFloor.name.Contains("floor")) && (!newTrapFloor.name.Contains("foundation")))
  202.                         {
  203.                             PrintToChat(player, pName + LangMsg("NOT_FLOOR", player.UserIDString));
  204.                             return;
  205.                         }
  206.  
  207.                         if (newTrapFloor.name.Contains("foundation") && !useFoundations)
  208.                         {
  209.                             PrintToChat(player, pName + LangMsg("NO_FOUNDATION", player.UserIDString));
  210.                             return;
  211.                         }
  212.  
  213.                         var info = new Floor()
  214.                         {
  215.                             Id = newTrapFloor.net.ID,
  216.                                 Location = newTrapFloor.transform.position.ToString(),
  217.                                 PlayerId = player.userID
  218.                         };
  219.  
  220.                         trapFloors.Add(info);
  221.                         amount = trapFloors.Count(x => x.PlayerId == player.userID);
  222.                         PrintToChat(player, pName + LangMsg("FLOOR_SET", player.UserIDString, amount, max));
  223.                         newTrapFloor.gameObject.AddComponent<FloorTrap>();
  224.                     }
  225.                     else
  226.                     {
  227.                         PrintToChat(player, pName + LangMsg("MAX_REACHED", player.UserIDString, max));
  228.                     }
  229.  
  230.                     break;
  231.  
  232.                     case "remove":
  233.                         if (!HasPermission(player, permAdmin))
  234.                     {
  235.                         PrintToChat(player, pName + LangMsg("ADMIN_ONLY", player.UserIDString));
  236.                         return;
  237.                     }
  238.  
  239.                     if (args.Length < 2)
  240.                     {
  241.                         PrintToChat(player, pName + LangMsg("INSTRUCTIONS", player.UserIDString));
  242.                         return;
  243.                     }
  244.  
  245.                     if (Floor.GetTrap(Convert.ToUInt32(args[1])) != null)
  246.                         trapFloors.Remove(Floor.GetTrap(Convert.ToUInt32(args[1])));
  247.                     player.ChatMessage(LangMsg("REMOVED", player.UserIDString, args[1]));
  248.                     break;
  249.  
  250.                     case "list":
  251.                         if (!HasPermission(player, permAdmin))
  252.                     {
  253.                         PrintToChat(player, pName + LangMsg("ADMIN_ONLY", player.UserIDString));
  254.                         return;
  255.                     }
  256.  
  257.                     PrintToChat(player, pName + LangMsg("LIST", player.UserIDString));
  258.                     foreach (var floor in trapFloors)
  259.                         player.ChatMessage(LangMsg("FLOOR_LIST", player.UserIDString, floor.Id, floor.Location, floor.PlayerId));
  260.                     PrintToChat(player, pName + LangMsg("END_LIST", player.UserIDString));
  261.                     break;
  262.  
  263.                     case "wipe":
  264.                         if (!HasPermission(player, permAdmin))
  265.                     {
  266.                         PrintToChat(player, pName + LangMsg("ADMIN_ONLY", player.UserIDString));
  267.                         return;
  268.                     }
  269.  
  270.                     trapFloors.Clear();
  271.                     PrintToChat(player, pName + LangMsg("WIPED", player.UserIDString));
  272.                     break;
  273.  
  274.                     case "default":
  275.                         PrintToChat(player, pName + LangMsg("BAD_SYNTAX", player.UserIDString));
  276.                     break;
  277.                 }
  278.  
  279.                 SaveData();
  280.             }
  281.             else
  282.             {
  283.                 PrintToChat(player, LangMsg("ADMIN_BAD_SYNTAX", player.UserIDString));
  284.             }
  285.         }
  286.  
  287.         void Remove(string args, BasePlayer player)
  288.         {
  289.             if (Floor.GetTrap(Convert.ToUInt32(args[1])) != null)
  290.                 trapFloors.Remove(Floor.GetTrap(Convert.ToUInt32(args[1])));
  291.             player.ChatMessage(LangMsg("REMOVED", player.UserIDString, args[1]));
  292.         }
  293.  
  294.         bool HasPermission(BasePlayer player, string perm) => permission.UserHasPermission(player.UserIDString, perm);
  295.  
  296.         string LangMsg(string key, string uid = null, params object[] args) => string.Format(lang.GetMessage(key, this, uid), args);
  297.  
  298.         static void SaveData() => DataFile.WriteObject(trapFloors);
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement