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.
This commit is contained in:
Patrikkk 2020-05-28 19:24:28 +02:00
parent 814801d894
commit 0fa8ae13d7
3 changed files with 111 additions and 3 deletions

View file

@ -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
}
}
/// <summary>
/// Called when a player is trying to place an item into a food plate.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
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(() =>

View file

@ -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
{
/// <summary>
/// The X tile position of the placement action.
/// </summary>
public short TileX { get; set; }
/// <summary>
/// The Y tile position of the placement action.
/// </summary>
public short TileY { get; set; }
/// <summary>
/// The Item ID that is being placed in the plate.
/// </summary>
public short ItemID { get; set; }
/// <summary>
/// The prefix of the item that is being placed in the plate.
/// </summary>
public byte Prefix { get; set; }
/// <summary>
/// The stack of the item that is being placed in the plate.
/// </summary>
public short Stack { get; set; }
}
/// <summary>
/// Called when a player is placing an item in a food plate.
/// </summary>
public static HandlerList<FoodPlatterTryPlacingEventArgs> FoodPlatterTryPlacing = new HandlerList<FoodPlatterTryPlacingEventArgs>();
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
{

View file

@ -31,7 +31,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>..\..\..\Debug\ServerPlugins\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@ -221,4 +221,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>