Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using UnityEngine;
- public class LevelPvPLockConfig
- {
- public int MinLevelForPvP = 5;
- public bool NotifyPlayers = true;
- public Messages Messages = new Messages
- {
- TooLowLevel = "You cannot attack other players until you reach level {level}.",
- PvPEnabled = "Congratulations! PvP is now enabled for you at level {level}.",
- ConfigReloaded = "LevelPvPLock configuration reloaded successfully.",
- NotAdmin = "You do not have permission to execute this command."
- };
- }
- public class Messages
- {
- public string TooLowLevel;
- public string PvPEnabled;
- public string ConfigReloaded;
- public string NotAdmin;
- }
- public class LevelPvPLockMod : IModApi
- {
- private static LevelPvPLockConfig config;
- private static string configPath = Path.Combine(GamePrefs.GetString(EnumGamePrefs.SaveGameFolder), "LevelPvPLock", "Config", "level_pvp_lock.json");
- private static HashSet<string> unlockedPlayers = new HashSet<string>();
- public void InitMod(Mod mod)
- {
- LoadConfig();
- ModEvents.PlayerDamage.RegisterHandler(OnPlayerDamage);
- ModEvents.ServerCommand.RegisterHandler(OnServerCommand);
- }
- private static void LoadConfig()
- {
- if (!File.Exists(configPath))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(configPath));
- var defaultJson = JsonConvert.SerializeObject(new LevelPvPLockConfig(), Formatting.Indented);
- File.WriteAllText(configPath, defaultJson);
- }
- config = JsonConvert.DeserializeObject<LevelPvPLockConfig>(File.ReadAllText(configPath));
- unlockedPlayers.Clear();
- Log.Out($"[LevelPvPLock] Loaded config. Minimum PvP level: {config.MinLevelForPvP}");
- }
- private static bool OnPlayerDamage(EntityAlive attacker, EntityAlive victim, DamageSource source, int dmg, bool critHit, bool bBlockDamage)
- {
- if (attacker == null || victim == null) return true;
- if (!attacker.IsPlayer() || !victim.IsPlayer()) return true; // Only affect PvP
- int attackerLevel = attacker.GetLevel();
- int victimLevel = victim.GetLevel();
- // Prevent PvP if either is below minimum level
- if (attackerLevel < config.MinLevelForPvP || victimLevel < config.MinLevelForPvP)
- {
- if (config.NotifyPlayers && attacker != null)
- {
- attacker.ChatMessage(config.Messages.TooLowLevel.Replace("{level}", config.MinLevelForPvP.ToString()));
- }
- return false; // Cancel damage
- }
- // Notify player once when reaching PvP level
- if (!unlockedPlayers.Contains(attacker.EntityName) && attackerLevel >= config.MinLevelForPvP)
- {
- if (config.NotifyPlayers)
- {
- attacker.ChatMessage(config.Messages.PvPEnabled.Replace("{level}", config.MinLevelForPvP.ToString()));
- }
- unlockedPlayers.Add(attacker.EntityName);
- }
- return true;
- }
- private static void OnServerCommand(ClientInfo cInfo, string command, string[] args)
- {
- if (!command.Equals("#reloadlevelpvp", StringComparison.OrdinalIgnoreCase)) return;
- if (!GameManager.Instance.AdminTools.IsPlayerAdmin(cInfo))
- {
- cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>()
- .Setup(EnumGameMessages.Chat, config.Messages.NotAdmin, "FF0000", true));
- return;
- }
- LoadConfig();
- cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>()
- .Setup(EnumGameMessages.Chat, config.Messages.ConfigReloaded, "00FF00", true));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment