Merge branch 'general-devel' into addtileentityinteraction

This commit is contained in:
Patrikkk 2020-06-25 13:41:14 +02:00
commit d4e4692d8b
5 changed files with 138 additions and 3 deletions

View file

@ -39,6 +39,7 @@ namespace TShockAPI
internal Handlers.SendTileSquareHandler STSHandler { get; set; }
internal Handlers.NetModules.NetModulePacketHandler NetModuleHandler { get; set; }
internal Handlers.EmojiHandler EmojiHandler { get; set; }
internal Handlers.DisplayDollItemSyncHandler DisplayDollItemSyncHandler { get; set; }
internal Handlers.RequestTileEntityInteractionHandler RequestTileEntityInteractionHandler { get; set; }
internal Handlers.LandGolfBallInCupHandler LandGolfBallInCupHandler { get; set; }
internal Handlers.SyncTilePickingHandler SyncTilePickingHandler { get; set; }
@ -56,6 +57,9 @@ namespace TShockAPI
EmojiHandler = new Handlers.EmojiHandler();
GetDataHandlers.Emoji += EmojiHandler.OnReceive;
DisplayDollItemSyncHandler = new Handlers.DisplayDollItemSyncHandler();
GetDataHandlers.DisplayDollItemSync += DisplayDollItemSyncHandler.OnReceive;
RequestTileEntityInteractionHandler = new Handlers.RequestTileEntityInteractionHandler();
GetDataHandlers.RequestTileEntityInteraction += RequestTileEntityInteractionHandler.OnReceive;

View file

@ -152,6 +152,7 @@ namespace TShockAPI
{ PacketTypes.PlayerHurtV2, HandlePlayerDamageV2 },
{ PacketTypes.PlayerDeathV2, HandlePlayerKillMeV2 },
{ PacketTypes.Emoji, HandleEmoji },
{ PacketTypes.TileEntityDisplayDollItemSync, HandleTileEntityDisplayDollItemSync },
{ PacketTypes.RequestTileEntityInteraction, HandleRequestTileEntityInteraction },
{ PacketTypes.SyncTilePicking, HandleSyncTilePicking },
{ PacketTypes.SyncRevengeMarker, HandleSyncRevengeMarker },
@ -1904,6 +1905,64 @@ namespace TShockAPI
return args.Handled;
}
/// For use in a TileEntityDisplayDollItemSync event.
/// </summary>
public class DisplayDollItemSyncEventArgs : GetDataHandledEventArgs
{
/// <summary>
/// The player index in the packet who modifies the DisplayDoll item slot.
/// </summary>
public byte PlayerIndex { get; set; }
/// <summary>
/// The ID of the TileEntity that is being modified.
/// </summary>
public int TileEntityID { get; set; }
/// <summary>
/// The TEDisplayDoll object that is being modified.
/// </summary>
public TEDisplayDoll DisplayDollEntity { get; set; }
/// <summary>
/// The slot of the DisplayDoll that is being modified.
/// </summary>
public int Slot { get; set; }
/// <summary>
/// Wether or not the slot that is being modified is a Dye slot.
/// </summary>
public bool IsDye { get; set; }
/// <summary>
/// The current item that is present in the slot before the modification.
/// </summary>
public Item OldItem { get; set; }
/// <summary>
/// The item that is about to replace the OldItem in the slot that is being modified.
/// </summary>
public Item NewItem { get; set; }
}
/// <summary>
/// Called when a player modifies a DisplayDoll (Mannequin) item slot.
/// </summary>
public static HandlerList<DisplayDollItemSyncEventArgs> DisplayDollItemSync = new HandlerList<DisplayDollItemSyncEventArgs>();
private static bool OnDisplayDollItemSync(TSPlayer player, MemoryStream data, byte playerIndex, int tileEntityID, TEDisplayDoll displayDollEntity, int slot, bool isDye, Item oldItem, Item newItem)
{
if (DisplayDollItemSync == null)
return false;
var args = new DisplayDollItemSyncEventArgs
{
Player = player,
Data = data,
PlayerIndex = playerIndex,
TileEntityID = tileEntityID,
DisplayDollEntity = displayDollEntity,
Slot = slot,
IsDye = isDye,
OldItem = oldItem,
NewItem = newItem
};
DisplayDollItemSync.Invoke(null, args);
return args.Handled;
}
/// For use in an OnRequestTileEntityInteraction event.
/// </summary>
public class RequestTileEntityInteractionEventArgs : GetDataHandledEventArgs
@ -3736,6 +3795,48 @@ namespace TShockAPI
return false;
}
private static bool HandleTileEntityDisplayDollItemSync(GetDataHandlerArgs args)
{
byte playerIndex = args.Data.ReadInt8();
int tileEntityID = args.Data.ReadInt32();
int slot = args.Data.ReadByte();
bool isDye = false;
if (slot >= 8)
{
isDye = true;
slot -= 8;
}
Item newItem = new Item();
Item oldItem = new Item();
if (!TileEntity.ByID.TryGetValue(tileEntityID, out TileEntity tileEntity))
return false;
TEDisplayDoll displayDoll = tileEntity as TEDisplayDoll;
if (displayDoll != null)
{
oldItem = displayDoll._items[slot];
if (isDye)
oldItem = displayDoll._dyes[slot];
ushort itemType = args.Data.ReadUInt16();
ushort stack = args.Data.ReadUInt16();
int prefix = args.Data.ReadByte();
if (oldItem.type == 0 && newItem.type == 0)
return false;
newItem.SetDefaults(itemType);
newItem.stack = stack;
newItem.Prefix(prefix);
if (OnDisplayDollItemSync(args.Player, args.Data, playerIndex, tileEntityID, displayDoll, slot, isDye, oldItem, newItem))
return true;
}
return false;
}
private static bool HandleRequestTileEntityInteraction(GetDataHandlerArgs args)
{
int tileEntityID = args.Data.ReadInt32();

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static TShockAPI.GetDataHandlers;
namespace TShockAPI.Handlers
{
/// <summary>
/// Handles the TileEntityDisplayDollItemSync packets and checks for permissions.
/// </summary>
public class DisplayDollItemSyncHandler : IPacketHandler<DisplayDollItemSyncEventArgs>
{
public void OnReceive(object sender, DisplayDollItemSyncEventArgs args)
{
/// If the player has no building permissions means that they couldn't even see the content of the doll in the first place.
/// Thus, they would not be able to modify its content. This means that a hacker attempted to send this packet directly, or through raw bytes to tamper with the DisplayDoll. This is why I do not bother with making sure the player gets their item back.
if (!args.Player.HasBuildPermission(args.DisplayDollEntity.Position.X, args.DisplayDollEntity.Position.Y, false))
{
args.Player.SendErrorMessage("You do not have permission to modify a Mannequin in a protected area!");
args.Handled = true;
return;
}
}
}
}

View file

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