diff --git a/Core/Config.cs b/Core/Config.cs index ca9a050..ab1ad97 100644 --- a/Core/Config.cs +++ b/Core/Config.cs @@ -778,6 +778,16 @@ public record class MitigationSettings /// public Optional RecursiveTileBreak = Optional.Default(false, true); + /// + /// Force QuickStackChests to sync inventory incrementally. + /// + /// The new implementation will use MassWireOperationPay to consume items. + /// It will be harder to duplicate items using QuickStackChests, but only + /// the items from the inventory will be handled (void bag will be ignored). + /// + /// + public Optional IncrementalChestStack = Optional.Default(true); + public enum DisabledDamageAction { AsIs, diff --git a/Core/Mitigations.cs b/Core/Mitigations.cs index a4f0247..ef4597c 100644 --- a/Core/Mitigations.cs +++ b/Core/Mitigations.cs @@ -574,6 +574,32 @@ private void MMHook_Mitigation_WorldGenNextCount(On.Terraria.WorldGen.orig_nextC Terraria.WorldGen.numTileCount = Terraria.WorldGen.maxTileCount; } + private void MMHook_Chest_ServerPlaceItem(On.Terraria.Chest.orig_ServerPlaceItem orig, int plr, int slot) + { + var mitigation = this.config.Mitigation.Value; + if (mitigation.DisableAllMitigation || !mitigation.IncrementalChestStack) + { + orig(plr, slot); + return; + } + + if (slot >= Terraria.ID.PlayerItemSlotID.Inventory0) + { + return; + } + + var player = Terraria.Main.player[plr]; + var inv = player.inventory[slot]; + var (type, stack) = (inv.type, inv.stack); + var newItem = Terraria.Chest.PutItemInNearbyChest(inv, player.Center, plr); + var newStack = newItem.stack; + if (newStack < stack) + { + player.inventory[slot] = newItem; + Terraria.NetMessage.TrySendData((int) PacketTypes.MassWireOperationPay, plr, number: type, number2: stack - newStack, number3: plr); + } + } + private void GDHook_Mitigation_PlayerBuffUpdate(object? sender, TShockAPI.GetDataHandlers.PlayerBuffUpdateEventArgs args) { var currentPosition = args.Data.Position; diff --git a/Core/Plugin.cs b/Core/Plugin.cs index 08eef6b..8905b1f 100644 --- a/Core/Plugin.cs +++ b/Core/Plugin.cs @@ -322,6 +322,7 @@ public override void Initialize() On.Terraria.WorldGen.nextCount += this.MMHook_Mitigation_WorldGenNextCount; On.Terraria.WorldGen.KillTile += this.MMHook_WorldGen_KillTile; On.Terraria.WorldGen.TileFrame += this.MMHook_WorldGen_TileFrame; + On.Terraria.Chest.ServerPlaceItem += this.MMHook_Chest_ServerPlaceItem; OTAPI.Hooks.NetMessage.SendBytes += this.OTHook_Ghost_SendBytes; OTAPI.Hooks.NetMessage.SendBytes += this.OTHook_DebugPacket_SendBytes; OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Ping_GetData; @@ -367,6 +368,7 @@ protected override void Dispose(bool disposing) On.Terraria.WorldGen.nextCount -= this.MMHook_Mitigation_WorldGenNextCount; On.Terraria.WorldGen.KillTile -= this.MMHook_WorldGen_KillTile; On.Terraria.WorldGen.TileFrame -= this.MMHook_WorldGen_TileFrame; + On.Terraria.Chest.ServerPlaceItem -= this.MMHook_Chest_ServerPlaceItem; OTAPI.Hooks.NetMessage.SendBytes -= this.OTHook_Ghost_SendBytes; OTAPI.Hooks.NetMessage.SendBytes -= this.OTHook_DebugPacket_SendBytes; OTAPI.Hooks.MessageBuffer.GetData -= this.OTHook_Mitigation_GetData; diff --git a/Core/WorldGen.cs b/Core/WorldGen.cs index 4ecc907..32cf0bf 100644 --- a/Core/WorldGen.cs +++ b/Core/WorldGen.cs @@ -73,7 +73,7 @@ private void Command_InspectTileFrame(CommandArgs args) private readonly AsyncLocal _frameCount = new AsyncLocal(); private bool _worldgenHalting = false; - private readonly HashSet _haltSource = new HashSet(); + private readonly HashSet _haltSource = []; private int _dumpCounter; private void Detour_InspectTileFrame(Action orig, int i, int j, bool resetFrame, bool noBreak) @@ -139,12 +139,12 @@ private void Detour_InspectTileFrame(Action orig, int i, i } } - this._frameCount.Value += 1; + this._frameCount.Value++; orig(i, j, resetFrame, noBreak); - this._frameCount.Value -= 1; + this._frameCount.Value--; } - private readonly List _pendingTileFrame = new List(); + private readonly List _pendingTileFrame = []; private void MMHook_WorldGen_KillTile(On.Terraria.WorldGen.orig_KillTile orig, int i, int j, bool fail, bool effectOnly, bool noItem) { var pos = (((ulong) i) << 32) | ((uint) j);