Skip to content

Commit

Permalink
Hopefully make it harder to dupe via quick stack
Browse files Browse the repository at this point in the history
  • Loading branch information
sgkoishi committed Nov 11, 2024
1 parent debf829 commit 451424f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,16 @@ public record class MitigationSettings
/// </summary>
public Optional<bool> RecursiveTileBreak = Optional.Default(false, true);

/// <summary>
/// Force QuickStackChests to sync inventory incrementally.
/// <para>
/// 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).
/// </para>
/// </summary>
public Optional<bool> IncrementalChestStack = Optional.Default(true);

public enum DisabledDamageAction
{
AsIs,
Expand Down
26 changes: 26 additions & 0 deletions Core/Mitigations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Core/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions Core/WorldGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void Command_InspectTileFrame(CommandArgs args)

private readonly AsyncLocal<int> _frameCount = new AsyncLocal<int>();
private bool _worldgenHalting = false;
private readonly HashSet<ulong> _haltSource = new HashSet<ulong>();
private readonly HashSet<ulong> _haltSource = [];
private int _dumpCounter;

private void Detour_InspectTileFrame(Action<int, int, bool, bool> orig, int i, int j, bool resetFrame, bool noBreak)
Expand Down Expand Up @@ -139,12 +139,12 @@ private void Detour_InspectTileFrame(Action<int, int, bool, bool> 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<ulong> _pendingTileFrame = new List<ulong>();
private readonly List<ulong> _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);
Expand Down

0 comments on commit 451424f

Please sign in to comment.