diff --git a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch index 2fbf5e56f4..d2d45bcb8f 100644 --- a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch @@ -579,3 +579,12 @@ Item item = p_147234_.m_41720_(); if (!p_147234_.m_150930_(Items.f_42047_) && (!(item instanceof BlockItem) || !(((BlockItem)item).m_40614_() instanceof AbstractSkullBlock))) { if (item instanceof ArmorItem) { +@@ -3217,7 +_,7 @@ + } else if (p_147234_.m_150930_(Items.f_42741_)) { + return EquipmentSlot.CHEST; + } else { +- return p_147234_.m_150930_(Items.f_42740_) ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND; ++ return p_147234_.canPerformAction(net.minecraftforge.common.ToolActions.SHIELD_BLOCK) ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND; + } + } else { + return EquipmentSlot.HEAD; diff --git a/patches/minecraft/net/minecraft/world/entity/monster/piglin/StopHoldingItemIfNoLongerAdmiring.java.patch b/patches/minecraft/net/minecraft/world/entity/monster/piglin/StopHoldingItemIfNoLongerAdmiring.java.patch index 6d1ae564c7..f2ee3e920c 100644 --- a/patches/minecraft/net/minecraft/world/entity/monster/piglin/StopHoldingItemIfNoLongerAdmiring.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/monster/piglin/StopHoldingItemIfNoLongerAdmiring.java.patch @@ -5,7 +5,7 @@ protected boolean m_6114_(ServerLevel p_35255_, E p_35256_) { - return !p_35256_.m_21206_().m_41619_() && !p_35256_.m_21206_().m_150930_(Items.f_42740_); -+ return !p_35256_.m_21206_().m_41619_() && !p_35256_.m_21206_().isShield(p_35256_); ++ return !p_35256_.m_21206_().m_41619_() && !p_35256_.m_21206_().canPerformAction(net.minecraftforge.common.ToolActions.SHIELD_BLOCK); } protected void m_6735_(ServerLevel p_35258_, E p_35259_, long p_35260_) { diff --git a/patches/minecraft/net/minecraft/world/entity/player/Player.java.patch b/patches/minecraft/net/minecraft/world/entity/player/Player.java.patch index b28007b034..4136246203 100644 --- a/patches/minecraft/net/minecraft/world/entity/player/Player.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/player/Player.java.patch @@ -129,7 +129,7 @@ protected void m_7909_(float p_36383_) { - if (this.f_20935_.m_150930_(Items.f_42740_)) { -+ if (this.f_20935_.isShield(this)) { ++ if (this.f_20935_.canPerformAction(net.minecraftforge.common.ToolActions.SHIELD_BLOCK)) { if (!this.f_19853_.f_46443_) { this.m_36246_(Stats.f_12982_.m_12902_(this.f_20935_.m_41720_())); } diff --git a/patches/minecraft/net/minecraft/world/item/ShieldItem.java.patch b/patches/minecraft/net/minecraft/world/item/ShieldItem.java.patch new file mode 100644 index 0000000000..69944c432d --- /dev/null +++ b/patches/minecraft/net/minecraft/world/item/ShieldItem.java.patch @@ -0,0 +1,14 @@ +--- a/net/minecraft/world/item/ShieldItem.java ++++ b/net/minecraft/world/item/ShieldItem.java +@@ -49,4 +_,11 @@ + public static DyeColor m_43102_(ItemStack p_43103_) { + return DyeColor.m_41053_(p_43103_.m_41698_("BlockEntityTag").m_128451_("Base")); + } ++ ++ /* ******************** FORGE START ******************** */ ++ ++ @Override ++ public boolean canPerformAction(ItemStack stack, net.minecraftforge.common.ToolAction toolAction) { ++ return net.minecraftforge.common.ToolActions.DEFAULT_SHIELD_ACTIONS.contains(toolAction); ++ } + } diff --git a/src/main/java/net/minecraftforge/common/ToolActions.java b/src/main/java/net/minecraftforge/common/ToolActions.java index 31d58fafd4..f3128291ac 100644 --- a/src/main/java/net/minecraftforge/common/ToolActions.java +++ b/src/main/java/net/minecraftforge/common/ToolActions.java @@ -109,6 +109,10 @@ public class ToolActions // */ // TODO: public static final ToolAction HOE_TILL = ToolAction.get("till"); + /** + * A tool action corresponding to the 'block' action of shields. + */ + public static final ToolAction SHIELD_BLOCK = ToolAction.get("shield_block"); // Default actions supported by each tool type public static final Set DEFAULT_AXE_ACTIONS = of(AXE_DIG, AXE_STRIP, AXE_SCRAPE, AXE_WAX_OFF); @@ -117,6 +121,7 @@ public class ToolActions public static final Set DEFAULT_PICKAXE_ACTIONS = of(PICKAXE_DIG); public static final Set DEFAULT_SWORD_ACTIONS = of(SWORD_DIG, SWORD_SWEEP); public static final Set DEFAULT_SHEARS_ACTIONS = of(SHEARS_DIG, SHEARS_HARVEST, SHEARS_CARVE, SHEARS_DISARM); + public static final Set DEFAULT_SHIELD_ACTIONS = of(SHIELD_BLOCK); private static Set of(ToolAction... actions) { return Stream.of(actions).collect(Collectors.toCollection(Sets::newIdentityHashSet)); diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java b/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java index 2f622346f0..d627fdb478 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java @@ -51,6 +51,7 @@ import net.minecraft.util.Mth; import net.minecraft.network.chat.Component; import net.minecraft.world.level.Level; import net.minecraftforge.common.ToolAction; +import net.minecraftforge.common.ToolActions; // TODO review most of the methods in this "patch" public interface IForgeItem @@ -524,7 +525,8 @@ public interface IForgeItem */ default boolean canPerformAction(ItemStack stack, ToolAction toolAction) { - return false; + // Temporary patch to keep #isShield working until its removed; change to `return false` once removed + return isShield(stack, null) && ToolActions.DEFAULT_SHIELD_ACTIONS.contains(toolAction); } /** @@ -688,12 +690,15 @@ public interface IForgeItem } /** - * Is this Item a shield + * {@return {@code true} if this item is considered a shield} * - * @param stack The ItemStack - * @param entity The Entity holding the ItemStack - * @return True if the ItemStack is considered a shield + * @param stack the item stack + * @param entity the entity holding the item stack + * @deprecated To be removed in 1.18. Override {@link #canPerformAction(ItemStack, ToolAction)} and return + * {@code true} if the passed in tool action is contained in {@link ToolActions#DEFAULT_SHIELD_ACTIONS} or is + * equals to {@link ToolActions#SHIELD_BLOCK}. */ + @Deprecated(since = "1.17.1", forRemoval = true) default boolean isShield(ItemStack stack, @Nullable LivingEntity entity) { return stack.getItem() == Items.SHIELD; diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java b/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java index 2a36e4967a..67d00b3aed 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java @@ -43,6 +43,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.world.level.Level; import net.minecraftforge.common.ToolAction; +import net.minecraftforge.common.ToolActions; import net.minecraftforge.common.capabilities.ICapabilitySerializable; /* @@ -207,11 +208,14 @@ public interface IForgeItemStack extends ICapabilitySerializable } /** - * Is this Item a shield + * {@return {@code true} if this item is considered a shield} * - * @param entity The Entity holding the ItemStack - * @return True if the ItemStack is considered a shield + * @param entity the entity holding the item stack + * @deprecated To be removed in 1.18. Call {@link #canPerformAction(ToolAction)} with + * {@link ToolActions#SHIELD_BLOCK} instead. */ + @SuppressWarnings("removal") + @Deprecated(since = "1.17.1", forRemoval = true) default boolean isShield(@Nullable LivingEntity entity) { return self().getItem().isShield(self(), entity); diff --git a/src/test/java/net/minecraftforge/debug/item/CustomShieldTest.java b/src/test/java/net/minecraftforge/debug/item/CustomShieldTest.java new file mode 100644 index 0000000000..bcfc9a8f39 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/item/CustomShieldTest.java @@ -0,0 +1,83 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2021. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.debug.item; + +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResultHolder; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.UseAnim; +import net.minecraft.world.level.Level; +import net.minecraftforge.common.ToolAction; +import net.minecraftforge.common.ToolActions; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.fmllegacy.RegistryObject; +import net.minecraftforge.registries.DeferredRegister; + +@Mod("custom_shield_test") +public class CustomShieldTest +{ + private static final DeferredRegister ITEMS = DeferredRegister.create(Item.class, "custom_shield_test"); + + private static final RegistryObject CUSTOM_SHIELD_ITEM = ITEMS.register("custom_shield", + () -> new CustomShieldItem((new Item.Properties()).durability(336).tab(CreativeModeTab.TAB_COMBAT))); + + public CustomShieldTest() + { + ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus()); + } + + private static class CustomShieldItem extends Item + { + public CustomShieldItem(Properties properties) + { + super(properties); + } + + @Override + public UseAnim getUseAnimation(ItemStack stack) + { + return UseAnim.BLOCK; + } + + @Override + public int getUseDuration(ItemStack stack) + { + return 72000; + } + + @Override + public InteractionResultHolder use(Level world, Player player, InteractionHand hand) + { + ItemStack itemstack = player.getItemInHand(hand); + player.startUsingItem(hand); + return InteractionResultHolder.consume(itemstack); + } + + @Override + public boolean canPerformAction(ItemStack stack, ToolAction toolAction) + { + return toolAction.equals(ToolActions.SHIELD_BLOCK); + } + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 1d72229c78..42fb1b7a87 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -150,5 +150,7 @@ license="LGPL v2.1" modId="check_spawn_event_test" [[mods]] modId="permissions_changed_event_test" +[[mods]] + modId="custom_shield_test" # ADD ABOVE THIS LINE