craztheking

Untitled

Aug 23rd, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using Newtonsoft.Json;
  6.  
  7. public class SafeZone
  8. {
  9. public string Name;
  10. public Vector3 Position;
  11. public float Radius;
  12. public bool BlockProtection;
  13. public bool PvpProtection;
  14. public Messages Messages;
  15. }
  16.  
  17. public class Messages
  18. {
  19. public string Enter;
  20. public string Exit;
  21. }
  22.  
  23. public class SafeZoneConfig
  24. {
  25. public List<SafeZone> SafeZones = new List<SafeZone>();
  26. }
  27.  
  28. public class SafeZoneManager : IModApi
  29. {
  30. private static SafeZoneConfig config;
  31. private static string configPath = Path.Combine(GamePrefs.GetString(EnumGamePrefs.SaveGameFolder), "SafeZoneConfig.json");
  32. private static Dictionary<int, string> playerZones = new Dictionary<int, string>();
  33.  
  34. public void InitMod(Mod mod)
  35. {
  36. LoadConfig();
  37.  
  38. ModEvents.PlayerDamage.RegisterHandler(OnPlayerDamage);
  39. ModEvents.PlayerMove.RegisterHandler(OnPlayerMove);
  40. ModEvents.PlayerPlaceBlock.RegisterHandler(OnBlockPlace);
  41. ModEvents.PlayerRemoveBlock.RegisterHandler(OnBlockRemove);
  42. }
  43.  
  44. private void LoadConfig()
  45. {
  46. if (!File.Exists(configPath))
  47. {
  48. string defaultConfig = JsonConvert.SerializeObject(new SafeZoneConfig(), Formatting.Indented);
  49. File.WriteAllText(configPath, defaultConfig);
  50. }
  51.  
  52. string json = File.ReadAllText(configPath);
  53. config = JsonConvert.DeserializeObject<SafeZoneConfig>(json);
  54. }
  55.  
  56. private static bool OnPlayerDamage(EntityAlive attacker, EntityAlive victim, DamageSource source, int dmg, bool critHit, bool bBlockDamage)
  57. {
  58. if (attacker == null || victim == null) return true;
  59.  
  60. Vector3 posAttacker = attacker.position;
  61. Vector3 posVictim = victim.position;
  62.  
  63. foreach (var zone in config.SafeZones)
  64. {
  65. bool attackerInZone = Vector3.Distance(posAttacker, zone.Position) <= zone.Radius;
  66. bool victimInZone = Vector3.Distance(posVictim, zone.Position) <= zone.Radius;
  67.  
  68. if (zone.PvpProtection && (attackerInZone || victimInZone))
  69. {
  70. if (attackerInZone || victimInZone)
  71. {
  72. attacker.ChatMessage($"PvP is disabled in {zone.Name} safezone!");
  73. return false; // Cancel damage
  74. }
  75. }
  76. }
  77. return true;
  78. }
  79.  
  80. private static void OnPlayerMove(ClientInfo cInfo, Vector3 newPos)
  81. {
  82. foreach (var zone in config.SafeZones)
  83. {
  84. bool inside = Vector3.Distance(newPos, zone.Position) <= zone.Radius;
  85. bool wasInside = playerZones.ContainsKey(cInfo.entityId) && playerZones[cInfo.entityId] == zone.Name;
  86.  
  87. if (inside && !wasInside)
  88. {
  89. cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, zone.Messages.Enter, "FF00FF", true));
  90. playerZones[cInfo.entityId] = zone.Name;
  91. }
  92. else if (!inside && wasInside)
  93. {
  94. cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, zone.Messages.Exit, "FF00FF", true));
  95. playerZones.Remove(cInfo.entityId);
  96. }
  97. }
  98. }
  99.  
  100. private static bool OnBlockPlace(ClientInfo cInfo, Block block, Vector3i pos)
  101. {
  102. foreach (var zone in config.SafeZones)
  103. {
  104. if (zone.BlockProtection && Vector3.Distance(pos.ToVector3(), zone.Position) <= zone.Radius)
  105. {
  106. cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, $"Block placement disabled in {zone.Name} safezone!", "FF0000", true));
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112.  
  113. private static bool OnBlockRemove(ClientInfo cInfo, Block block, Vector3i pos)
  114. {
  115. foreach (var zone in config.SafeZones)
  116. {
  117. if (zone.BlockProtection && Vector3.Distance(pos.ToVector3(), zone.Position) <= zone.Radius)
  118. {
  119. cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, $"Block removal disabled in {zone.Name} safezone!", "FF0000", true));
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment