Clean up NetModulePacketHandler a little

This commit is contained in:
Chris 2020-06-02 14:18:22 +09:30
parent 0af69e2bf2
commit 961c6cd9bc
3 changed files with 47 additions and 55 deletions

View file

@ -1,5 +1,6 @@
using System.IO; using System.IO;
using System.IO.Streams; using System.IO.Streams;
using Terraria;
using Terraria.GameContent.NetModules; using Terraria.GameContent.NetModules;
using Terraria.Net; using Terraria.Net;
@ -45,6 +46,17 @@ namespace TShockAPI.Handlers.NetModules
/// <param name="rejectPacket"></param> /// <param name="rejectPacket"></param>
public void HandlePacket(TSPlayer player, out bool rejectPacket) public void HandlePacket(TSPlayer player, out bool rejectPacket)
{ {
if (!Main.GameModeInfo.IsJourneyMode)
{
TShock.Log.ConsoleDebug(
"NetModuleHandler received attempt to unlock sacrifice while not in journey mode from",
player.Name
);
rejectPacket = true;
return;
}
if (UnknownField != 0) if (UnknownField != 0)
{ {
TShock.Log.ConsoleDebug( TShock.Log.ConsoleDebug(

View file

@ -1,4 +1,6 @@
using Terraria; using System;
using System.Collections.Generic;
using Terraria;
using static TShockAPI.GetDataHandlers; using static TShockAPI.GetDataHandlers;
namespace TShockAPI.Handlers.NetModules namespace TShockAPI.Handlers.NetModules
@ -8,6 +10,19 @@ namespace TShockAPI.Handlers.NetModules
/// </summary> /// </summary>
public class NetModulePacketHandler : IPacketHandler<LoadNetModuleEventArgs> public class NetModulePacketHandler : IPacketHandler<LoadNetModuleEventArgs>
{ {
/// <summary>
/// Maps net module types to handlers for the net module type. Add to or edit this dictionary to customise handling
/// </summary>
public static Dictionary<NetModulesTypes, Type> NetModulesToHandlersMap = new Dictionary<NetModulesTypes, Type>
{
{ NetModulesTypes.CreativePowers, typeof(CreativePowerHandler) },
{ NetModulesTypes.CreativeUnlocksPlayerReport, typeof(CreativeUnlocksHandler) },
{ NetModulesTypes.TeleportPylon, typeof(PylonHandler) },
{ NetModulesTypes.Liquid, typeof(LiquidHandler) },
{ NetModulesTypes.Bestiary, typeof(BestiaryHandler) },
{ NetModulesTypes.Ambience, typeof(AmbienceHandler) }
};
/// <summary> /// <summary>
/// Invoked when a load net module packet is received. This method picks a <see cref="INetModuleHandler"/> based on the /// Invoked when a load net module packet is received. This method picks a <see cref="INetModuleHandler"/> based on the
/// net module type being loaded, then forwards the data to the chosen handler to process /// net module type being loaded, then forwards the data to the chosen handler to process
@ -18,46 +33,11 @@ namespace TShockAPI.Handlers.NetModules
{ {
INetModuleHandler handler; INetModuleHandler handler;
switch (args.ModuleType) if (NetModulesToHandlersMap.ContainsKey(args.ModuleType))
{ {
case NetModulesTypes.CreativePowers: handler = (INetModuleHandler)Activator.CreateInstance(NetModulesToHandlersMap[args.ModuleType]);
{
handler = new CreativePowerHandler();
break;
} }
else
case NetModulesTypes.CreativeUnlocksPlayerReport:
{
if (!Main.GameModeInfo.IsJourneyMode)
{
TShock.Log.ConsoleDebug(
"NetModuleHandler received attempt to unlock sacrifice while not in journey mode from",
args.Player.Name
);
args.Handled = true;
return;
}
handler = new CreativeUnlocksHandler();
break;
}
case NetModulesTypes.TeleportPylon:
{
handler = new PylonHandler();
break;
}
case NetModulesTypes.Liquid:
{
handler = new LiquidHandler();
break;
}
case NetModulesTypes.Bestiary:
{
handler = new BestiaryHandler();
break;
}
default:
{ {
// As of 1.4.x.x, this is now used for more things: // As of 1.4.x.x, this is now used for more things:
// NetCreativePowersModule // NetCreativePowersModule
@ -73,7 +53,6 @@ namespace TShockAPI.Handlers.NetModules
args.Handled = false; args.Handled = false;
return; return;
} }
}
handler.Deserialize(args.Data); handler.Deserialize(args.Data);
handler.HandlePacket(args.Player, out bool rejectPacket); handler.HandlePacket(args.Player, out bool rejectPacket);

View file

@ -89,6 +89,7 @@
<Compile Include="DB\TileManager.cs" /> <Compile Include="DB\TileManager.cs" />
<Compile Include="Extensions\ExceptionExt.cs" /> <Compile Include="Extensions\ExceptionExt.cs" />
<Compile Include="Handlers\IPacketHandler.cs" /> <Compile Include="Handlers\IPacketHandler.cs" />
<Compile Include="Handlers\NetModules\AmbienceHandler.cs" />
<Compile Include="Handlers\NetModules\BestiaryHandler.cs" /> <Compile Include="Handlers\NetModules\BestiaryHandler.cs" />
<Compile Include="Handlers\NetModules\CreativePowerHandler.cs" /> <Compile Include="Handlers\NetModules\CreativePowerHandler.cs" />
<Compile Include="Handlers\NetModules\CreativeUnlocksHandler.cs" /> <Compile Include="Handlers\NetModules\CreativeUnlocksHandler.cs" />