Need a shield API #4284
Unanswered
rikka0w0
asked this question in
Mod Dev Support
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
There was FabricShieldLib, but it was not updated for 1.21.2 and above. I think it is time to have this available in Fabric API, as this is a commonly used function.
The vanilla code uses
xxx instanceof ShieldItem
anditemStack.isOf(Items.SHIELD)
to check if an item should be considered as a shield. If someone wants to implement his/her own shield, he/she would need to mixin these two points with @ModifyConstant and @reDIrect. Without doing this, the modded shield won't be able to block damage. These two mixins can only be applied once and will prevent others from adding more shields. One possible walkaround is to use the chainable @ModifyExpression from MixinExtra, like this:https://github.com/rikka0w0/Fabric-Shield-Lib/blob/de00cea01f8a4aef2dede903ac988ed147d6a54f/src/main/java/com/github/crimsondawn45/fabricshieldlib/mixin/LivingEntityMixin.java#L83
https://github.com/rikka0w0/Fabric-Shield-Lib/blob/de00cea01f8a4aef2dede903ac988ed147d6a54f/src/main/java/com/github/crimsondawn45/fabricshieldlib/mixin/PlayerEntityMixin.java#L54
However, we cannot guarantee everyone follows the same standard. Moreover, the injection point may change as the Minecraft codebase evolves.
Hence, we need to make the shield API available in Fabric API. It does two things:
In addition, modders will have a standard way of checking whether an item should be considered a shield.
In the implementation of the shield API, we can treat everything in tag
ConventionalItemTags.SHIELD_TOOLS
(Or another new tag, namelyfabric:shield
) as shield:Then, we use MixinExtra's @ModifyExpression to change the behavior of
xxx instanceof ShieldItem
anditemStack.isOf(Items.SHIELD)
.Modders can simply include their modded shields in
ConventionalItemTags.SHIELD_TOOLS
with data generators, and the modded shields will be able to block damage.Sometimes, a modder may want to set a different cooling downtime for the modded shield. To achieve this, the shield API can introduce a new DataComponentType
COOLDOWN_TICKS
, a non-negative integer. If a shield item has this DataComponent, the cooldown tick will be the value set in the DataComponent. Otherwise, use vanilla cooldown ticks (100).I have an implementation here:
https://github.com/rikka0w0/Fabric-Shield-Lib/tree/experimental
If everyone is happy, I can start to work on a PR to bring this feature to Fabric API.
Beta Was this translation helpful? Give feedback.
All reactions