Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using Newtonsoft.Json;
- public class SafeZone
- {
- public string Name;
- public Vector3 Position;
- public float Radius;
- public bool BlockProtection;
- public bool PvpProtection;
- public Messages Messages;
- }
- public class Messages
- {
- public string Enter;
- public string Exit;
- }
- public class SafeZoneConfig
- {
- public List<SafeZone> SafeZones = new List<SafeZone>();
- }
- public class SafeZoneManager : IModApi
- {
- private static SafeZoneConfig config;
- private static string configPath = Path.Combine(GamePrefs.GetString(EnumGamePrefs.SaveGameFolder), "SafeZoneConfig.json");
- private static Dictionary<int, string> playerZones = new Dictionary<int, string>();
- public void InitMod(Mod mod)
- {
- LoadConfig();
- ModEvents.PlayerDamage.RegisterHandler(OnPlayerDamage);
- ModEvents.PlayerMove.RegisterHandler(OnPlayerMove);
- ModEvents.PlayerPlaceBlock.RegisterHandler(OnBlockPlace);
- ModEvents.PlayerRemoveBlock.RegisterHandler(OnBlockRemove);
- }
- private void LoadConfig()
- {
- if (!File.Exists(configPath))
- {
- string defaultConfig = JsonConvert.SerializeObject(new SafeZoneConfig(), Formatting.Indented);
- File.WriteAllText(configPath, defaultConfig);
- }
- string json = File.ReadAllText(configPath);
- config = JsonConvert.DeserializeObject<SafeZoneConfig>(json);
- }
- private static bool OnPlayerDamage(EntityAlive attacker, EntityAlive victim, DamageSource source, int dmg, bool critHit, bool bBlockDamage)
- {
- if (attacker == null || victim == null) return true;
- Vector3 posAttacker = attacker.position;
- Vector3 posVictim = victim.position;
- foreach (var zone in config.SafeZones)
- {
- bool attackerInZone = Vector3.Distance(posAttacker, zone.Position) <= zone.Radius;
- bool victimInZone = Vector3.Distance(posVictim, zone.Position) <= zone.Radius;
- if (zone.PvpProtection && (attackerInZone || victimInZone))
- {
- if (attackerInZone || victimInZone)
- {
- attacker.ChatMessage($"PvP is disabled in {zone.Name} safezone!");
- return false; // Cancel damage
- }
- }
- }
- return true;
- }
- private static void OnPlayerMove(ClientInfo cInfo, Vector3 newPos)
- {
- foreach (var zone in config.SafeZones)
- {
- bool inside = Vector3.Distance(newPos, zone.Position) <= zone.Radius;
- bool wasInside = playerZones.ContainsKey(cInfo.entityId) && playerZones[cInfo.entityId] == zone.Name;
- if (inside && !wasInside)
- {
- cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, zone.Messages.Enter, "FF00FF", true));
- playerZones[cInfo.entityId] = zone.Name;
- }
- else if (!inside && wasInside)
- {
- cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, zone.Messages.Exit, "FF00FF", true));
- playerZones.Remove(cInfo.entityId);
- }
- }
- }
- private static bool OnBlockPlace(ClientInfo cInfo, Block block, Vector3i pos)
- {
- foreach (var zone in config.SafeZones)
- {
- if (zone.BlockProtection && Vector3.Distance(pos.ToVector3(), zone.Position) <= zone.Radius)
- {
- cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, $"Block placement disabled in {zone.Name} safezone!", "FF0000", true));
- return false;
- }
- }
- return true;
- }
- private static bool OnBlockRemove(ClientInfo cInfo, Block block, Vector3i pos)
- {
- foreach (var zone in config.SafeZones)
- {
- if (zone.BlockProtection && Vector3.Distance(pos.ToVector3(), zone.Position) <= zone.Radius)
- {
- cInfo.SendPackage(NetPackageManager.GetPackage<NetPackageGameMessage>().Setup(EnumGameMessages.Chat, $"Block removal disabled in {zone.Name} safezone!", "FF0000", true));
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment