craztheking

Untitled

Aug 23rd, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.IO;
  5.  
  6. public class AdminSafezoneBuildMod : MonoBehaviour
  7. {
  8. private bool adminBuildMode = false;
  9. private Config config;
  10.  
  11. void Start()
  12. {
  13. LoadConfig();
  14. ChatManager.RegisterCommand(config.adminBuildCommand, OnAdminBuildCommand);
  15. GameManager.Instance.OnBlockPlaced += OnBlockAction;
  16. GameManager.Instance.OnBlockDestroyed += OnBlockAction;
  17. }
  18.  
  19. void LoadConfig()
  20. {
  21. string configPath = "Mods/AdminSafezoneBuild/Config/AdminSafezoneConfig.json";
  22. if (File.Exists(configPath))
  23. {
  24. string json = File.ReadAllText(configPath);
  25. config = JsonUtility.FromJson<Config>(json);
  26. }
  27. else
  28. {
  29. config = new Config();
  30. }
  31. }
  32.  
  33. void OnAdminBuildCommand(Player player, string command, string[] args)
  34. {
  35. if (!player.IsAdmin) return;
  36.  
  37. adminBuildMode = !adminBuildMode;
  38. if (config.notifyAdmin)
  39. {
  40. string msg = adminBuildMode ? config.enableMessage : config.disableMessage;
  41. GameManager.Instance.ChatMessageToPlayer(player, msg);
  42. }
  43. }
  44.  
  45. void OnBlockAction(Player player, Vector3 blockPos, BlockValue block, bool placing)
  46. {
  47. if (!IsInSafezone(blockPos)) return;
  48.  
  49. if (!player.IsAdmin) // block normal players
  50. {
  51. GameManager.Instance.ChatMessageToPlayer(player, "You cannot build or destroy blocks in safezones!");
  52. CancelBlockAction(player, blockPos, placing);
  53. }
  54. else if (!adminBuildMode) // block admin if they haven’t toggled
  55. {
  56. GameManager.Instance.ChatMessageToPlayer(player, "Safezone restrictions active. Toggle admin build mode to modify blocks.");
  57. CancelBlockAction(player, blockPos, placing);
  58. }
  59. // else: admin can build/destroy freely
  60. }
  61.  
  62. bool IsInSafezone(Vector3 position)
  63. {
  64. // Replace with your safezone check logic
  65. return SafezoneManager.IsPositionInSafezone(position);
  66. }
  67.  
  68. void CancelBlockAction(Player player, Vector3 blockPos, bool placing)
  69. {
  70. if (placing)
  71. player.CancelBlockPlacement(blockPos);
  72. else
  73. player.CancelBlockDestruction(blockPos);
  74. }
  75.  
  76. [Serializable]
  77. public class Config
  78. {
  79. public string adminBuildCommand = "#adminbuild";
  80. public bool notifyAdmin = true;
  81. public string enableMessage = "Admin Build Mode is now ENABLED. You can build in safezones.";
  82. public string disableMessage = "Admin Build Mode is now DISABLED. Safezone restrictions restored.";
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment