Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions TShockAPI/Bouncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Linq;
using Terraria.ID;
using TShockAPI.Net;
using Terraria;
using Microsoft.Xna.Framework;
using TShockAPI.Localization;
Expand All @@ -29,6 +28,7 @@ You should have received a copy of the GNU General Public License
using Terraria.Localization;
using TShockAPI.Models.PlayerUpdate;
using System.Threading.Tasks;
using OTAPI;
using Terraria.GameContent.Tile_Entities;

namespace TShockAPI
Expand Down Expand Up @@ -137,6 +137,7 @@ internal Bouncer()
GetDataHandlers.KillMe += OnKillMe;
GetDataHandlers.FishOutNPC += OnFishOutNPC;
GetDataHandlers.FoodPlatterTryPlacing += OnFoodPlatterTryPlacing;
OTAPI.Hooks.Chest.QuickStack += OnChestOnQuickStack;


// The following section is based off Player.PlaceThing_Tiles_PlaceIt and Player.PlaceThing_Tiles_PlaceIt_GetLegacyTileStyle.
Expand Down Expand Up @@ -2505,7 +2506,7 @@ internal void OnPlaceItemFrame(object sender, GetDataHandlers.PlaceItemFrameEven
Main.item[num].playerIndexTheItemIsReservedFor = args.Player.Index;
NetMessage.SendData((int)PacketTypes.ItemDrop, args.Player.Index, -1, NetworkText.Empty, num, 1f);
NetMessage.SendData((int)PacketTypes.ItemOwner, args.Player.Index, -1, NetworkText.Empty, num);

TShock.Log.ConsoleDebug(GetString("Bouncer / OnPlaceItemFrame rejected permissions from {0}", args.Player.Name));
NetMessage.SendData((int)PacketTypes.UpdateTileEntity, -1, -1, NetworkText.Empty, args.ItemFrame.ID, 0, 1);
args.Handled = true;
Expand Down Expand Up @@ -2871,6 +2872,44 @@ internal void OnFoodPlatterTryPlacing(object sender, GetDataHandlers.FoodPlatter
}
}

/// <summary>
/// Called when a player is trying to put an item into chest through Quick Stack.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
internal void OnChestOnQuickStack(object sender, OTAPI.Hooks.Chest.QuickStackEventArgs args)
{
var id = args.ChestIndex;
var plr = TShock.Players[args.PlayerId];

if (plr is not { Active: true })
{
args.Result = HookResult.Cancel;
return;
}

if (plr.IsBeingDisabled())
{
TShock.Log.ConsoleDebug(GetString("Bouncer / OnQuickStack rejected from disable from {0}", plr.Name));
args.Result = HookResult.Cancel;
return;
}

if (!plr.HasBuildPermission(Main.chest[id].x, Main.chest[id].y) && TShock.Config.Settings.RegionProtectChests)
{
TShock.Log.ConsoleDebug(GetString("Bouncer / OnQuickStack rejected from region protection? from {0}", plr.Name));
args.Result = HookResult.Cancel;
return;
}

if (!plr.IsInRange(Main.chest[id].x, Main.chest[id].y, 600/16))
{
TShock.Log.ConsoleDebug(GetString("Bouncer / OnQuickStack rejected from range check from {0}", plr.Name));
args.Result = HookResult.Cancel;
return;
}
}

internal void OnSecondUpdate()
{
Task.Run(() =>
Expand Down
37 changes: 37 additions & 0 deletions TShockAPI/GetDataHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public static void InitGetDataHandler()
{ PacketTypes.NumberOfAnglerQuestsCompleted, HandleNumberOfAnglerQuestsCompleted },
{ PacketTypes.PlaceObject, HandlePlaceObject },
{ PacketTypes.LoadNetModule, HandleLoadNetModule },
{ PacketTypes.ForceItemIntoNearestChest, HandleForceItemIntoNearestChest },
{ PacketTypes.PlaceTileEntity, HandlePlaceTileEntity },
{ PacketTypes.PlaceItemFrame, HandlePlaceItemFrame },
{ PacketTypes.UpdateItemDrop, HandleItemDrop },
Expand Down Expand Up @@ -1790,6 +1791,30 @@ private static bool OnPlaceObject(TSPlayer player, MemoryStream data, short x, s
return args.Handled;
}

/// <summary>For use in a ForceItemIntoNearestChest event.</summary>
public class ForceItemIntoNearestChestEventArgs : GetDataHandledEventArgs
{
/// <summary>The slot index of the item being attempted to put into a chest.</summary>
public short Slot { get; set; }

}

/// <summary>Fired when a ForceItemIntoNearestChest event occurs.</summary>
public static HandlerList<ForceItemIntoNearestChestEventArgs> ForceItemIntoNearestChest = new HandlerList<ForceItemIntoNearestChestEventArgs>();
private static bool OnForceItemIntoNearest(TSPlayer player, MemoryStream data, short slot)
{

var args = new ForceItemIntoNearestChestEventArgs
{
Player = player,
Data = data,
Slot = slot
};

ForceItemIntoNearestChest.Invoke(null, args);
return args.Handled;
}

/// <summary>For use in a PlaceTileEntity event.</summary>
public class PlaceTileEntityEventArgs : GetDataHandledEventArgs
{
Expand Down Expand Up @@ -4088,6 +4113,18 @@ private static bool HandleLoadNetModule(GetDataHandlerArgs args)
return false;
}

private static bool HandleForceItemIntoNearestChest(GetDataHandlerArgs args)
{
var slot = args.Data.ReadInt16();

if (OnForceItemIntoNearest(args.Player, args.Data, slot))
{
return true;
}

return false;
}

private static bool HandlePlaceTileEntity(GetDataHandlerArgs args)
{
var x = args.Data.ReadInt16();
Expand Down
Loading