Add new tool action for shield blocking, replacing IForgeItem#isShield (#8055)

This commit is contained in:
sciwhiz12 authored and GitHub committed 2021-09-08 13:14:47 -07:00
1 parent 9d7f5dd32a
commit b7379953d5
9 files changed
+132 -10

No files matched your search

@@ -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;
@@ -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_) {
@@ -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_()));
}
@@ -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);
+ }
}
@@ -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<ToolAction> DEFAULT_AXE_ACTIONS = of(AXE_DIG, AXE_STRIP, AXE_SCRAPE, AXE_WAX_OFF);
@@ -117,6 +121,7 @@ public class ToolActions
public static final Set<ToolAction> DEFAULT_PICKAXE_ACTIONS = of(PICKAXE_DIG);
public static final Set<ToolAction> DEFAULT_SWORD_ACTIONS = of(SWORD_DIG, SWORD_SWEEP);
public static final Set<ToolAction> DEFAULT_SHEARS_ACTIONS = of(SHEARS_DIG, SHEARS_HARVEST, SHEARS_CARVE, SHEARS_DISARM);
public static final Set<ToolAction> DEFAULT_SHIELD_ACTIONS = of(SHIELD_BLOCK);
private static Set<ToolAction> of(ToolAction... actions) {
return Stream.of(actions).collect(Collectors.toCollection(Sets::newIdentityHashSet));
@@ -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;
@@ -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<CompoundTag>
}
/**
* 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);
@@ -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<Item> ITEMS = DeferredRegister.create(Item.class, "custom_shield_test");
private static final RegistryObject<CustomShieldItem> 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<ItemStack> 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);
}
}
}
+2
View File
@@ -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