mojira.dev
MC-217607

The resetting of the attack cooldown when switching items is based on the items' types as opposed to their attack speeds

Summary:

When switching from holding one item (e.g. an iron sword) to holding another item of the same type (e.g. another iron sword), the attack speed indicator does not get reset. This is presumably intended behaviour.

However, this behaviour does not take into account the edge case of switching between two items that are of the same type but have different attack speeds. For instance, if a player has a default iron sword as well as a custom iron sword with a slower attack speed, switching from the default sword to the custom sword will not reset the attack bar.

 

Steps to Reproduce:

1) Ensure that the attack indicator is enabled in the video settings.

2) Obtain a normal iron sword and place it in your hotbar.

3) Run this command to obtain an iron sword with a modified attack speed of -3.9:

give @s iron_sword[attribute_modifiers=[{id:"generic_attack_speed",type:"attack_speed",amount:-3.9,operation:"add_value",slot:"mainhand"}]] 1

4) Hit something using the default sword and wait for approximately five seconds.

5) Now, switch to holding the custom sword.

6) Divert your gaze to the attack indicator next to your crosshair or hotbar. You will see that the attack indicator will already be half full, despite having only just switched to the item you are holding.

 

The expected behaviour would be for the attack speed cooldown to be reset upon switching to an item with a different attack speed, regardless of whether or not the two items are of the same type.

Environment

Windows 10

Linked issues

Attachments

Comments 6

Confirmed in 1.20.3 pre 1

I would personally not want this fixed. Currently, you’re rewarded for using two items of the same type (such as a Sharpness diamond sword and a Smite diamond sword) by being able to freely switch between the two without triggering the attack cooldown. Carrying two of the same weapon is already niche and the attack speed behavior was one of its only redeeming qualities.

This behavior also allows for effective duel wielding (one weapon in the offhand and one in the mainhand, and freely switching between the two using F without reseting cooldown).

^ Which is used in netherite PvP (switching between a sword with Knockback 1 and one without seemlessly), and I wouldn’t want this removed.

I confirm this is a bug for sure. Because Mojang’s logic is bad in ItemStack.class. Switching items calls isSameItem(), which just refers to item’s name but ignore other components like enchantments.

ItemStackBug.png

In survival item can loss durability, so we can’t use isSameItemSameComponents(). Instead we should consider all the components expect damage. But Mojang doesn’t write that function.

Datapacks may make different components on same type items, like one item with unbreakable{}, weapon{} but the other not. This bug makes game can’t recognize them.

So my mod makes isSameItem() compare both item types and enchantments like this, and it can fix the bug in vanilla range.

ItemStackFix.png

I think this is an interesting issue. The game essentially never compares ItemStacks directly by reference and instead only compares them using their data. This allows players to switch slots freely while breaking blocks or charging the attack strength ticker, if those slots all contain the “same” item. I think it’s clear that switching between empty slots should not reset the current block breaking progress nor the attack strength, but I’m not sure if this should be intended behaviour if any of those slots are not empty. Intuitively all items within different slots of a players inventory are different items and it seems illogical that switching between two tools of the same type would allow you to keep breaking a block/charging an attack, whereas switching between two of the same tools but of different types doesn’t.

This introduces the question of what should happen when the player changes the item in it’s currently held slot in an inventory/container, or if the server overwrites it. Should enchanting an item (applied from server) reset the items block break progress/attack strength? Should the attack strength be reset if the player manually changes the item in it’s held slot to one of the same type? I believe in the first case the answer would most likely be no (durability changes resetting block break progress is annyoing), but I’m not so sure about the second one. I would say any client-initiated change of the currently held slot should cause a reset of those fields.

This would need a significant amount of changes, since simply comparing two ItemStacks do determine wheter attack strength/block break progress should be reset would not be sufficient anymore, as we also need to take the currently held slot into account.

Otherwise one could just make the attack strength ticker use isSameItemSameComponents() or possibly a new method that compares all data components except for durability (which is frequently changed during normal usage).

rorybd

(Unassigned)

Community Consensus

Platform

Normal

Items

command, item

1.16.5, 21w08b, 1.18.1, 1.19.2, 1.19.4, 1.20, 1.20.3 Pre-Release 1, 1.20.4, 1.21.1, 1.21.10

Retrieved