Add IPacketHandler

This commit is contained in:
Chris 2020-06-02 10:44:10 +09:30
parent b5c3b430fa
commit 5b72ff6c0a
4 changed files with 28 additions and 11 deletions

View file

@ -43,7 +43,7 @@ namespace TShockAPI
internal Bouncer() internal Bouncer()
{ {
STSHandler = new Handlers.SendTileSquareHandler(); STSHandler = new Handlers.SendTileSquareHandler();
GetDataHandlers.SendTileSquare += STSHandler.OnReceiveSendTileSquare; GetDataHandlers.SendTileSquare += STSHandler.OnReceive;
// Setup hooks // Setup hooks
GetDataHandlers.GetSection += OnGetSection; GetDataHandlers.GetSection += OnGetSection;

View file

@ -0,0 +1,16 @@
namespace TShockAPI.Handlers
{
/// <summary>
/// Describes a packet handler that receives a packet from a GetDataHandler
/// </summary>
/// <typeparam name="TEventArgs"></typeparam>
public interface IPacketHandler<TEventArgs> where TEventArgs : GetDataHandledEventArgs
{
/// <summary>
/// Invoked when the packet is received
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
void OnReceive(object sender, TEventArgs args);
}
}

View file

@ -14,12 +14,12 @@ namespace TShockAPI.Handlers
/// <summary> /// <summary>
/// Provides processors for handling Tile Square packets /// Provides processors for handling Tile Square packets
/// </summary> /// </summary>
public class SendTileSquareHandler public class SendTileSquareHandler : IPacketHandler<GetDataHandlers.SendTileSquareEventArgs>
{ {
/// <summary> /// <summary>
/// Maps grass-type blocks to flowers that can be grown on them with flower boots /// Maps grass-type blocks to flowers that can be grown on them with flower boots
/// </summary> /// </summary>
Dictionary<ushort, List<ushort>> _grassToPlantMap = new Dictionary<ushort, List<ushort>> public static Dictionary<ushort, List<ushort>> GrassToPlantMap = new Dictionary<ushort, List<ushort>>
{ {
{ TileID.Grass, new List<ushort> { TileID.Plants, TileID.Plants2 } }, { TileID.Grass, new List<ushort> { TileID.Plants, TileID.Plants2 } },
{ TileID.HallowedGrass, new List<ushort> { TileID.HallowedPlants, TileID.HallowedPlants2 } }, { TileID.HallowedGrass, new List<ushort> { TileID.HallowedPlants, TileID.HallowedPlants2 } },
@ -29,7 +29,7 @@ namespace TShockAPI.Handlers
/// <summary> /// <summary>
/// Item IDs that can spawn flowers while you walk /// Item IDs that can spawn flowers while you walk
/// </summary> /// </summary>
List<int> _flowerBootItems = new List<int> public static List<int> FlowerBootItems = new List<int>
{ {
ItemID.FlowerBoots, ItemID.FlowerBoots,
ItemID.FairyBoots ItemID.FairyBoots
@ -40,7 +40,7 @@ namespace TShockAPI.Handlers
/// Note: <see cref="Terraria.ID.TileEntityID"/> is empty at the time of writing, but entities are dynamically assigned their ID at initialize time /// Note: <see cref="Terraria.ID.TileEntityID"/> is empty at the time of writing, but entities are dynamically assigned their ID at initialize time
/// which is why we can use the _myEntityId field on each entity type /// which is why we can use the _myEntityId field on each entity type
/// </summary> /// </summary>
Dictionary<int, int> _tileEntityIdToTileIdMap = new Dictionary<int, int> public static Dictionary<int, int> TileEntityIdToTileIdMap = new Dictionary<int, int>
{ {
{ TileID.TargetDummy, TETrainingDummy._myEntityID }, { TileID.TargetDummy, TETrainingDummy._myEntityID },
{ TileID.ItemFrame, TEItemFrame._myEntityID }, { TileID.ItemFrame, TEItemFrame._myEntityID },
@ -57,7 +57,7 @@ namespace TShockAPI.Handlers
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="args"></param> /// <param name="args"></param>
public void OnReceiveSendTileSquare(object sender, GetDataHandlers.SendTileSquareEventArgs args) public void OnReceive(object sender, GetDataHandlers.SendTileSquareEventArgs args)
{ {
// By default, we'll handle everything // By default, we'll handle everything
args.Handled = true; args.Handled = true;
@ -196,9 +196,9 @@ namespace TShockAPI.Handlers
UpdateMultipleServerTileStates(realX, realY, width, height, newTiles); UpdateMultipleServerTileStates(realX, realY, width, height, newTiles);
// Tile entities have special placements that we should let the game deal with // Tile entities have special placements that we should let the game deal with
if (_tileEntityIdToTileIdMap.ContainsKey(tileType)) if (TileEntityIdToTileIdMap.ContainsKey(tileType))
{ {
TileEntity.PlaceEntityNet(realX, realY, _tileEntityIdToTileIdMap[tileType]); TileEntity.PlaceEntityNet(realX, realY, TileEntityIdToTileIdMap[tileType]);
} }
} }
@ -214,7 +214,7 @@ namespace TShockAPI.Handlers
{ {
// Some boots allow growing flowers on grass. This process sends a 1x1 tile square to grow the flowers // Some boots allow growing flowers on grass. This process sends a 1x1 tile square to grow the flowers
// The square size must be 1 and the player must have an accessory that allows growing flowers in order for this square to be valid // The square size must be 1 and the player must have an accessory that allows growing flowers in order for this square to be valid
if (squareSize == 1 && args.Player.Accessories.Any(a => a != null && _flowerBootItems.Contains(a.type))) if (squareSize == 1 && args.Player.Accessories.Any(a => a != null && FlowerBootItems.Contains(a.type)))
{ {
ProcessFlowerBoots(realX, realY, newTile, args); ProcessFlowerBoots(realX, realY, newTile, args);
return; return;
@ -254,7 +254,7 @@ namespace TShockAPI.Handlers
} }
ITile tile = Main.tile[realX, realY + 1]; ITile tile = Main.tile[realX, realY + 1];
if (!_grassToPlantMap.TryGetValue(tile.type, out List<ushort> plantTiles) && !plantTiles.Contains(newTile.Type)) if (!GrassToPlantMap.TryGetValue(tile.type, out List<ushort> plantTiles) && !plantTiles.Contains(newTile.Type))
{ {
// If the tile below the tile square isn't a valid plant tile (eg grass) then we don't update the server tile state // If the tile below the tile square isn't a valid plant tile (eg grass) then we don't update the server tile state
return; return;

View file

@ -88,6 +88,7 @@
<Compile Include="DB\ResearchDatastore.cs" /> <Compile Include="DB\ResearchDatastore.cs" />
<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\SendTileSquareHandler.cs" /> <Compile Include="Handlers\SendTileSquareHandler.cs" />
<Compile Include="Hooks\AccountHooks.cs" /> <Compile Include="Hooks\AccountHooks.cs" />
<Compile Include="Hooks\GeneralHooks.cs" /> <Compile Include="Hooks\GeneralHooks.cs" />
@ -212,7 +213,7 @@
</PropertyGroup> </PropertyGroup>
<ProjectExtensions> <ProjectExtensions>
<VisualStudio> <VisualStudio>
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" /> <UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
</VisualStudio> </VisualStudio>
</ProjectExtensions> </ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.