Skip to content

Commit c912260

Browse files
committed
refactor(OnChestOnQuickStack): use OTAPI.Hooks.Chest.QuickStack instead of GetDataHandlers.ForceItemIntoNearestChest
2 parents 0d627dd + fec1ead commit c912260

File tree

19 files changed

+29801
-28348
lines changed

19 files changed

+29801
-28348
lines changed

TShockAPI/GetDataHandlers.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,20 +2759,20 @@ private static bool HandleSpawn(GetDataHandlerArgs args)
27592759

27602760
if (OnPlayerSpawn(args.Player, args.Data, player, spawnX, spawnY, respawnTimer, numberOfDeathsPVE, numberOfDeathsPVP, context))
27612761
return true;
2762-
2762+
27632763
args.Player.Dead = respawnTimer > 0;
27642764

27652765
if (Main.ServerSideCharacter)
27662766
{
27672767
// As long as the player has not changed his spawnpoint since initial connection,
2768-
// we should not use the client's spawnpoint value. This is because the spawnpoint
2769-
// value is not saved on the client when SSC is enabled. Hence, we have to assert
2770-
// the server-saved spawnpoint value until we can detect that the player has changed
2768+
// we should not use the client's spawnpoint value. This is because the spawnpoint
2769+
// value is not saved on the client when SSC is enabled. Hence, we have to assert
2770+
// the server-saved spawnpoint value until we can detect that the player has changed
27712771
// his spawn. Once we detect the spawnpoint changed, the client's spawnpoint value
27722772
// becomes the correct one to use.
27732773
//
2774-
// Note that spawnpoint changes (right-clicking beds) are not broadcasted to the
2775-
// server. Hence, the only way to detect spawnpoint changes is from the
2774+
// Note that spawnpoint changes (right-clicking beds) are not broadcasted to the
2775+
// server. Hence, the only way to detect spawnpoint changes is from the
27762776
// PlayerSpawn packet.
27772777

27782778
// handle initial connection
@@ -2788,13 +2788,13 @@ private static bool HandleSpawn(GetDataHandlerArgs args)
27882788
args.Player.initialClientSpawnX = spawnX;
27892789
args.Player.initialClientSpawnY = spawnY;
27902790

2791-
// we first let the game handle completing the connection (state 3 => 10),
2792-
// then we will spawn the player at the saved spawnpoint in the next second,
2791+
// we first let the game handle completing the connection (state 3 => 10),
2792+
// then we will spawn the player at the saved spawnpoint in the next second,
27932793
// by reasserting the correct spawnpoint value
27942794
return false;
27952795
}
27962796

2797-
// once we detect the client has changed his spawnpoint in the current session,
2797+
// once we detect the client has changed his spawnpoint in the current session,
27982798
// the client spawnpoint value will be correct for the rest of the session
27992799
if (args.Player.spawnSynced || args.Player.initialClientSpawnX != spawnX || args.Player.initialClientSpawnY != spawnY)
28002800
{
@@ -2806,11 +2806,11 @@ private static bool HandleSpawn(GetDataHandlerArgs args)
28062806
// spawn the player before teleporting
28072807
NetMessage.SendData((int)PacketTypes.PlayerSpawn, -1, args.Player.Index, null, args.Player.Index, (int)PlayerSpawnContext.ReviveFromDeath);
28082808

2809-
// the player has not changed his spawnpoint yet, so we assert the server-saved spawnpoint
2809+
// the player has not changed his spawnpoint yet, so we assert the server-saved spawnpoint
28102810
// by teleporting the player instead of letting the game use the client's incorrect spawnpoint.
28112811
TShock.Log.ConsoleDebug(GetString("GetDataHandlers / HandleSpawn force ssc teleport for {0} at ({1},{2})", args.Player.Name, args.TPlayer.SpawnX, args.TPlayer.SpawnY));
28122812
args.Player.TeleportSpawnpoint();
2813-
2813+
28142814
args.TPlayer.respawnTimer = respawnTimer;
28152815
args.TPlayer.numberOfDeathsPVE = numberOfDeathsPVE;
28162816
args.TPlayer.numberOfDeathsPVP = numberOfDeathsPVP;
@@ -3906,7 +3906,7 @@ private static bool HandleTeleport(GetDataHandlerArgs args)
39063906
if (type == 0 && !args.Player.HasPermission(Permissions.rod))
39073907
{
39083908
TShock.Log.ConsoleDebug(GetString("GetDataHandlers / HandleTeleport rejected rod type {0} {1}", args.Player.Name, type));
3909-
args.Player.SendErrorMessage(GetString("You do not have permission to teleport using items.")); // Was going to write using RoD but Hook of Disonnance and Potion of Return both use the same teleport packet as RoD.
3909+
args.Player.SendErrorMessage(GetString("You do not have permission to teleport using items.")); // Was going to write using RoD but Hook of Disonnance and Potion of Return both use the same teleport packet as RoD.
39103910
args.Player.Teleport(args.TPlayer.position.X, args.TPlayer.position.Y); // Suggest renaming rod permission unless someone plans to add separate perms for the other 2 tp items.
39113911
return true;
39123912
}

TShockAPI/Utils.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,5 +1202,77 @@ internal void ComputeMaxStyles()
12021202
}
12031203
}
12041204
}
1205+
1206+
/// <summary>
1207+
/// Locates the nearest available chest suitable for quick-stacking items from the player's inventory.
1208+
/// </summary>
1209+
/// <param name="player">The player attempting to quick-stack items.</param>
1210+
/// <param name="slot">The inventory slot of the item being quick-stacked (currently unused in this method).</param>
1211+
/// <returns>
1212+
/// The index of the nearest available chest within range that isn't locked or in use, or -1 if no suitable chest is found.
1213+
/// </returns>
1214+
public static int LocateQuickStackChestIndex(TSPlayer player, short slot)
1215+
{
1216+
const float maxDistance = 600f;
1217+
Item item;
1218+
1219+
if (slot >= PlayerItemSlotID.Bank4_0 && slot < PlayerItemSlotID.Bank4_0 + NetItem.VoidSlots)
1220+
{
1221+
var index = slot - PlayerItemSlotID.Bank4_0;
1222+
item = player.TPlayer.bank4.item[index];
1223+
}
1224+
else
1225+
{
1226+
if (slot >= NetItem.InventorySlots - 1)
1227+
return -1;
1228+
item = player.TPlayer.inventory[slot];
1229+
}
1230+
1231+
for (var i = 0; i < Main.maxChests; i++)
1232+
{
1233+
var chest = Main.chest[i];
1234+
if (chest == null || Chest.IsPlayerInChest(i) || Chest.IsLocked(chest.x, chest.y))
1235+
continue;
1236+
1237+
var chestPos = new Vector2(chest.x * 16 + 16, chest.y * 16 + 16);
1238+
if ((chestPos - player.TPlayer.position).Length() >= maxDistance)
1239+
continue;
1240+
1241+
var canStack = false;
1242+
var hasSpace = false;
1243+
1244+
foreach (var chestItem in chest.item)
1245+
{
1246+
if (chestItem.IsAir)
1247+
{
1248+
hasSpace = true;
1249+
continue;
1250+
}
1251+
1252+
if (item.IsTheSameAs(chestItem))
1253+
{
1254+
canStack = true;
1255+
var space = chestItem.maxStack - chestItem.stack;
1256+
if (space > 0)
1257+
{
1258+
return i;
1259+
}
1260+
}
1261+
else
1262+
{
1263+
hasSpace = true;
1264+
}
1265+
}
1266+
1267+
if (canStack && hasSpace && item.stack > 0)
1268+
{
1269+
if (chest.item.Any(t => t.IsAir))
1270+
{
1271+
return i;
1272+
}
1273+
}
1274+
}
1275+
return -1;
1276+
}
12051277
}
12061278
}

0 commit comments

Comments
 (0)