From 0fa8ae13d79ec4acdce9d3943ebb3d02573dea4b Mon Sep 17 00:00:00 2001 From: Patrikkk Date: Thu, 28 May 2020 19:24:28 +0200 Subject: [PATCH] Implement FoodPlatter placing event. This is called when a player is placing a fruit (item) in a plate. Adding checks to see if they have permission to place or replace a fruit in the item. Checks if they are within range. And a check to see if they are legitimately placing the item from their hand, and not by sending a raw packet. --- TShockAPI/Bouncer.cs | 49 +++++++++++++++++++++++++++++ TShockAPI/GetDataHandlers.cs | 61 +++++++++++++++++++++++++++++++++++- TShockAPI/TShockAPI.csproj | 4 +-- 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index ad6e516a..abd5fe05 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -67,6 +67,7 @@ namespace TShockAPI GetDataHandlers.MassWireOperation += OnMassWireOperation; GetDataHandlers.PlayerDamage += OnPlayerDamage; GetDataHandlers.KillMe += OnKillMe; + GetDataHandlers.FoodPlatterTryPlacing += OnFoodPlatterTryPlacing; } internal void OnGetSection(object sender, GetDataHandlers.GetSectionEventArgs args) @@ -2062,6 +2063,54 @@ namespace TShockAPI } } + /// + /// Called when a player is trying to place an item into a food plate. + /// + /// + /// + internal void OnFoodPlatterTryPlacing(object sender, GetDataHandlers.FoodPlatterTryPlacingEventArgs args) + { + if (args.Player.ItemInHand.type != args.ItemID) + { + TShock.Log.ConsoleDebug("Bouncer / OnFoodPlatterTryPlacing rejected item not placed by hand from {0}", args.Player.Name); + args.Player.SendTileSquare(args.TileX, args.TileY, 1); + args.Handled = true; + return; + } + if (args.Player.IsBeingDisabled()) + { + TShock.Log.ConsoleDebug("Bouncer / OnFoodPlatterTryPlacing rejected disabled from {0}", args.Player.Name); + Item item = new Item(); + item.netDefaults(args.ItemID); + args.Player.GiveItemCheck(args.ItemID, item.Name, args.Stack, args.Prefix); + args.Player.SendTileSquare(args.TileX, args.TileY, 1); + args.Handled = true; + return; + } + + if (!args.Player.HasBuildPermission(args.TileX, args.TileY)) + { + TShock.Log.ConsoleDebug("Bouncer / OnFoodPlatterTryPlacing rejected permissions from {0}", args.Player.Name); + Item item = new Item(); + item.netDefaults(args.ItemID); + args.Player.GiveItemCheck(args.ItemID, item.Name, args.Stack, args.Prefix); + args.Player.SendTileSquare(args.TileX, args.TileY, 1); + args.Handled = true; + return; + } + + if (!args.Player.IsInRange(args.TileX, args.TileY)) + { + TShock.Log.ConsoleDebug("Bouncer / OnFoodPlatterTryPlacing rejected range checks from {0}", args.Player.Name); + Item item = new Item(); + item.netDefaults(args.ItemID); + args.Player.GiveItemCheck(args.ItemID, item.Name, args.Stack, args.Prefix); + args.Player.SendTileSquare(args.TileX, args.TileY, 1); + args.Handled = true; + return; + } + } + internal void OnSecondUpdate() { Task.Run(() => diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 1f90f29e..74dbff85 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -150,7 +150,8 @@ namespace TShockAPI { PacketTypes.ToggleParty, HandleToggleParty }, { PacketTypes.CrystalInvasionStart, HandleOldOnesArmy }, { PacketTypes.PlayerHurtV2, HandlePlayerDamageV2 }, - { PacketTypes.PlayerDeathV2, HandlePlayerKillMeV2 } + { PacketTypes.PlayerDeathV2, HandlePlayerKillMeV2 }, + { PacketTypes.FoodPlatterTryPlacing, HandleFoodPlatterTryPlacing } }; } @@ -1863,6 +1864,52 @@ namespace TShockAPI return args.Handled; } + public class FoodPlatterTryPlacingEventArgs : GetDataHandledEventArgs + { + /// + /// The X tile position of the placement action. + /// + public short TileX { get; set; } + /// + /// The Y tile position of the placement action. + /// + public short TileY { get; set; } + /// + /// The Item ID that is being placed in the plate. + /// + public short ItemID { get; set; } + /// + /// The prefix of the item that is being placed in the plate. + /// + public byte Prefix { get; set; } + /// + /// The stack of the item that is being placed in the plate. + /// + public short Stack { get; set; } + } + /// + /// Called when a player is placing an item in a food plate. + /// + public static HandlerList FoodPlatterTryPlacing = new HandlerList(); + private static bool OnFoodPlatterTryPlacing(TSPlayer player, MemoryStream data, short tileX, short tileY, short itemID, byte prefix, short stack) + { + if (FoodPlatterTryPlacing == null) + return false; + + var args = new FoodPlatterTryPlacingEventArgs + { + Player = player, + Data = data, + TileX = tileX, + TileY = tileY, + ItemID = itemID, + Prefix = prefix, + Stack = stack, + }; + FoodPlatterTryPlacing.Invoke(null, args); + return args.Handled; + } + #endregion private static bool HandlePlayerInfo(GetDataHandlerArgs args) @@ -3559,7 +3606,19 @@ namespace TShockAPI return false; } + private static bool HandleFoodPlatterTryPlacing(GetDataHandlerArgs args) + { + short tileX = args.Data.ReadInt16(); + short tileY = args.Data.ReadInt16(); + short itemID = args.Data.ReadInt16(); + byte prefix = args.Data.ReadInt8(); + short stack = args.Data.ReadInt16(); + if (OnFoodPlatterTryPlacing(args.Player, args.Data, tileX, tileY, itemID, prefix, stack)) + return true; + + return false; + } public enum EditAction { diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index c941ea0a..ab982025 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -31,7 +31,7 @@ true full false - bin\Debug\ + ..\..\..\Debug\ServerPlugins\ DEBUG;TRACE prompt 4 @@ -221,4 +221,4 @@ --> - + \ No newline at end of file