using System.Collections.Generic; using System.IO; using System.IO.Streams; using static TShockAPI.GetDataHandlers; namespace TShockAPI.Handlers.NetModules { /// /// Provides handling for the Creative Power net module. Checks permissions on all creative powers /// public class CreativePowerHandler : INetModuleHandler { /// /// The power type being activated /// public CreativePowerTypes PowerType { get; set; } /// /// Reads the power type from the stream /// /// public void Deserialize(MemoryStream data) { PowerType = (CreativePowerTypes)data.ReadInt16(); } /// /// Determines if the player has permission to use the power type /// /// /// public void HandlePacket(TSPlayer player, out bool rejectPacket) { if (!HasPermission(PowerType, player)) { rejectPacket = true; return; } rejectPacket = false; } /// /// Determines if a player has permission to use a specific creative power /// /// /// /// public static bool HasPermission(CreativePowerTypes powerType, TSPlayer player) { if (!PowerToPermissionMap.ContainsKey(powerType)) { TShock.Log.ConsoleDebug("CreativePowerHandler received permission check request for unknown creative power"); return false; } string permission = PowerToPermissionMap[powerType]; if (!player.HasPermission(permission)) { player.SendErrorMessage("You do not have permission to {0}.", PermissionToDescriptionMap[permission]); return false; } return true; } /// /// Maps creative powers to permission nodes /// public static Dictionary PowerToPermissionMap = new Dictionary { { CreativePowerTypes.FreezeTime, Permissions.journey_timefreeze }, { CreativePowerTypes.SetDawn, Permissions.journey_timeset }, { CreativePowerTypes.SetNoon, Permissions.journey_timeset }, { CreativePowerTypes.SetDusk, Permissions.journey_timeset }, { CreativePowerTypes.SetMidnight, Permissions.journey_timeset }, { CreativePowerTypes.Godmode, Permissions.journey_godmode }, { CreativePowerTypes.WindStrength, Permissions.journey_windstrength }, { CreativePowerTypes.RainStrength, Permissions.journey_rainstrength }, { CreativePowerTypes.TimeSpeed, Permissions.journey_timespeed }, { CreativePowerTypes.RainFreeze, Permissions.journey_rainfreeze }, { CreativePowerTypes.WindFreeze, Permissions.journey_windfreeze }, { CreativePowerTypes.IncreasePlacementRange, Permissions.journey_placementrange }, { CreativePowerTypes.WorldDifficulty, Permissions.journey_setdifficulty }, { CreativePowerTypes.BiomeSpreadFreeze, Permissions.journey_biomespreadfreeze }, { CreativePowerTypes.SetSpawnRate, Permissions.journey_setspawnrate }, }; /// /// Maps journey mode permission nodes to descriptions of what the permission allows /// public static Dictionary PermissionToDescriptionMap = new Dictionary { { Permissions.journey_timefreeze, "freeze the time of the server" }, { Permissions.journey_timeset, "modify the time of the server" }, { Permissions.journey_godmode, "toggle godmode" }, { Permissions.journey_windstrength, "modify the wind strength of the server" }, { Permissions.journey_rainstrength, "modify the rain strength of the server" }, { Permissions.journey_timespeed, "modify the time speed of the server" }, { Permissions.journey_rainfreeze, "freeze the rain strength of the server" }, { Permissions.journey_windfreeze, "freeze the wind strength of the server" }, { Permissions.journey_placementrange, "modify the tile placement range of your character" }, { Permissions.journey_setdifficulty, "modify the world difficulty of the server" }, { Permissions.journey_biomespreadfreeze, "freeze the biome spread of the server" }, { Permissions.journey_setspawnrate, "modify the NPC spawn rate of the server" }, }; } }