using System; using System.Collections.Generic; using static TShockAPI.GetDataHandlers; namespace TShockAPI.Handlers.NetModules { /// /// Handles packet 82 - Load Net Module packets /// public class NetModulePacketHandler : IPacketHandler { /// /// Maps net module types to handlers for the net module type. Add to or edit this dictionary to customise handling /// public static Dictionary NetModulesToHandlersMap = new Dictionary { { NetModuleType.CreativePowers, typeof(CreativePowerHandler) }, { NetModuleType.CreativeUnlocksPlayerReport, typeof(CreativeUnlocksHandler) }, { NetModuleType.TeleportPylon, typeof(PylonHandler) }, { NetModuleType.Liquid, typeof(LiquidHandler) }, { NetModuleType.Bestiary, typeof(BestiaryHandler) }, { NetModuleType.Ambience, typeof(AmbienceHandler) } }; /// /// Invoked when a load net module packet is received. This method picks a based on the /// net module type being loaded, then forwards the data to the chosen handler to process /// /// /// public void OnReceive(object sender, ReadNetModuleEventArgs args) { INetModuleHandler handler; if (NetModulesToHandlersMap.TryGetValue(args.ModuleType, out Type type)) { handler = (INetModuleHandler)Activator.CreateInstance(type); } else { // We don't have handlers for NetModuleType.Ping and NetModuleType.Particles. // These net modules are fairly innocuous and can be processed normally by the game args.Handled = false; return; } handler.Deserialize(args.Data); handler.HandlePacket(args.Player, out bool rejectPacket); args.Handled = rejectPacket; } } }