craztheking

Untitled

Aug 23rd, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using Newtonsoft.Json;
  5. using UnityEngine;
  6.  
  7. public class LevelPvPLockConfig
  8. {
  9. public int MinLevelForPvP = 5;
  10. public bool NotifyPlayers = true;
  11. public Messages Messages = new Messages
  12. {
  13. TooLowLevel = "You cannot attack other players until you reach level {level}.",
  14. PvPEnabled = "Congratulations! PvP is now enabled for you at level {level}.",
  15. ConfigReloaded = "LevelPvPLock configuration reloaded successfully.",
  16. NotAdmin = "You do not have permission to execute this command."
  17. };
  18. }
  19.  
  20. public class Messages
  21. {
  22. public string TooLowLevel;
  23. public string PvPEnabled;
  24. public string ConfigReloaded;
  25. public string NotAdmin;
  26. }
  27.  
  28. public class LevelPvPLockMod : IModApi
  29. {
  30. private static LevelPvPLockConfig config;
  31. private static string configPath = Path.Combine(GamePrefs.GetString(EnumGamePrefs.SaveGameFolder), "LevelPvPLock", "Config", "level_pvp_lock.json");
  32. private static HashSet<string> unlockedPlayers = new HashSet<string>();
  33.  
  34. public void InitMod(Mod mod)
  35. {
  36. LoadConfig();
  37. ModEvents.PlayerDamage.RegisterHandler(OnPlayerDamage);
  38. ModEvents.ServerCommand.RegisterHandler(OnServerCommand);
  39. }
  40.  
  41. private static void LoadConfig()
  42. {
  43. if (!File.Exists(configPath))
  44. {
  45. Directory.CreateDirectory(Path.GetDirectoryName(configPath));
  46. var defaultJson = JsonConvert.SerializeObject(new LevelPvPLockConfig(), Formatting.Indented);
  47. File.WriteAllText(configPath, defaultJson);
  48. }
  49.  
  50. config = JsonConvert.DeserializeObject<LevelPvPLockConfig>(File.ReadAllText(configPath));
  51. unlockedPlayers.Clear();
  52. Log.Out($"[LevelPvPLock] Loaded config. Minimum PvP level: {config.MinLevelForPvP}");
  53. }
  54.  
  55. private static bool OnPlayerDamage(EntityAlive attacker, EntityAlive victim, DamageSource source, int dmg, bool critHit, bool bBlockDamage)
  56. {
  57. if (attacker == null || victim == null) return true;
  58. if (!attacker.IsPlayer() || !victim.IsPlayer()) return true; // Only affect PvP
  59.  
  60. int attackerLevel = attacker.GetLevel();
  61. int victimLevel = victim.GetLevel();
  62.  
  63. // Prevent PvP if either is below minimum level
  64. if (attackerLevel < config.MinLevelForPvP || victimLevel < config.MinLevelForPvP)
  65. {
  66. if (config.NotifyPlayers && attacker != null)
  67. {
  68. attacker.ChatMessage(config.Messages.TooLowLevel.Replace("{level}", config.MinLevelForPvP.ToString()));
  69. }
  70. return false; // Cancel damage
  71. }
  72.  
  73. // Notify player once when reaching PvP level
  74. if (!unlockedPlayers.Contains(attacker.EntityName) && attackerLevel >= config.MinLevelForPvP)
  75. {
  76. if (config.NotifyPlayers)
  77. {
  78. attacker.ChatMessage(config.Messages.PvPEnabled.Replace("{level}", config.MinLevelForPvP.ToString()));
  79. }
  80. unlockedPlayers.Add(attacker.EntityName);
  81. }
  82.  
  83. return true;
  84. }
  85.  
  86. private static void OnServerCommand(ClientInfo cInfo, string command, string[] args)
  87. {
  88. if (!command.Equals("#reloadlevelpvp", StringComparison.OrdinalIgnoreCase)) return;
  89.  
  90. if (!GameManager.Instance.AdminTools.IsPlayerAdmin(cInfo))
  91. {
  92. cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>()
  93. .Setup(EnumGameMessages.Chat, config.Messages.NotAdmin, "FF0000", true));
  94. return;
  95. }
  96.  
  97. LoadConfig();
  98. cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>()
  99. .Setup(EnumGameMessages.Chat, config.Messages.ConfigReloaded, "00FF00", true));
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment