From 2bd83b26aa614f7ab7bba3df18dae0f801873f5d Mon Sep 17 00:00:00 2001 From: Mike Garay <61263845+Thelnfamous1@users.noreply.github.com> Date: Fri, 16 Jul 2021 19:52:58 -0500 Subject: [PATCH] Adds knockback attribute to players and updated knockback calculation to PlayerEntity#attack (#7806) --- .../entity/player/PlayerEntity.java.patch | 11 ++- .../player/PlayerAttackKnockbackTest.java | 67 ++++++++++++++++++ src/test/resources/META-INF/mods.toml | 4 +- .../lang/en_us.json | 3 + .../models/item/knockback_sword.json | 6 ++ .../textures/item/knockback_sword.png | Bin 0 -> 196 bytes 6 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/minecraftforge/debug/entity/player/PlayerAttackKnockbackTest.java create mode 100644 src/test/resources/assets/player_attack_knockback_test/lang/en_us.json create mode 100644 src/test/resources/assets/player_attack_knockback_test/models/item/knockback_sword.json create mode 100644 src/test/resources/assets/player_attack_knockback_test/textures/item/knockback_sword.png diff --git a/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch b/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch index 2667ad5310..754b453f9b 100644 --- a/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch +++ b/patches/minecraft/net/minecraft/entity/player/PlayerEntity.java.patch @@ -23,7 +23,7 @@ public static AttributeModifierMap.MutableAttribute func_234570_el_() { - return LivingEntity.func_233639_cI_().func_233815_a_(Attributes.field_233823_f_, 1.0D).func_233815_a_(Attributes.field_233821_d_, (double)0.1F).func_233814_a_(Attributes.field_233825_h_).func_233814_a_(Attributes.field_233828_k_); -+ return LivingEntity.func_233639_cI_().func_233815_a_(Attributes.field_233823_f_, 1.0D).func_233815_a_(Attributes.field_233821_d_, (double)0.1F).func_233814_a_(Attributes.field_233825_h_).func_233814_a_(Attributes.field_233828_k_).func_233814_a_(net.minecraftforge.common.ForgeMod.REACH_DISTANCE.get()); ++ return LivingEntity.func_233639_cI_().func_233815_a_(Attributes.field_233823_f_, 1.0D).func_233815_a_(Attributes.field_233821_d_, (double)0.1F).func_233814_a_(Attributes.field_233825_h_).func_233814_a_(Attributes.field_233828_k_).func_233814_a_(net.minecraftforge.common.ForgeMod.REACH_DISTANCE.get()).func_233814_a_(Attributes.field_233824_g_); } protected void func_70088_a() { @@ -198,6 +198,15 @@ if (p_71059_1_.func_70075_an()) { if (!p_71059_1_.func_85031_j(this)) { float f = (float)this.func_233637_b_(Attributes.field_233823_f_); +@@ -1028,7 +_,7 @@ + if (f > 0.0F || f1 > 0.0F) { + boolean flag = f2 > 0.9F; + boolean flag1 = false; +- int i = 0; ++ float i = (float)this.func_233637_b_(Attributes.field_233824_g_); // Forge: Initialize this value to the attack knockback attribute of the player, which is by default 0 + i = i + EnchantmentHelper.func_77501_a(this); + if (this.func_70051_ag() && flag) { + this.field_70170_p.func_184148_a((PlayerEntity)null, this.func_226277_ct_(), this.func_226278_cu_(), this.func_226281_cx_(), SoundEvents.field_187721_dT, this.func_184176_by(), 1.0F, 1.0F); @@ -1038,8 +_,10 @@ boolean flag2 = flag && this.field_70143_R > 0.0F && !this.field_70122_E && !this.func_70617_f_() && !this.func_70090_H() && !this.func_70644_a(Effects.field_76440_q) && !this.func_184218_aH() && p_71059_1_ instanceof LivingEntity; diff --git a/src/test/java/net/minecraftforge/debug/entity/player/PlayerAttackKnockbackTest.java b/src/test/java/net/minecraftforge/debug/entity/player/PlayerAttackKnockbackTest.java new file mode 100644 index 0000000000..6d19408ce9 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/entity/player/PlayerAttackKnockbackTest.java @@ -0,0 +1,67 @@ +package net.minecraftforge.debug.entity.player; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import net.minecraft.entity.ai.attributes.Attribute; +import net.minecraft.entity.ai.attributes.AttributeModifier; +import net.minecraft.entity.ai.attributes.Attributes; +import net.minecraft.inventory.EquipmentSlotType; +import net.minecraft.item.*; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.fml.RegistryObject; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; + +import java.util.UUID; + +/** + * Tests if the patch to PlayerEntity to make it utilize Attributes.ATTACK_KNOCKBACK works + * The Knockback Sword adds a modifier to the user's attack knockback + * The goal of this patch is to allow modders to more easily add equipment or potion effects that affect the attack knockback of players + * As well as allow command users to modify this attribute for a player + */ +@Mod(PlayerAttackKnockbackTest.MODID) +public class PlayerAttackKnockbackTest { + + static final String MODID = "player_attack_knockback_test"; + + static final float ATTACK_KNOCKBACK_VALUE = 2.0F; // Change this value for testing, but note that the attribute is capped at 5.0D + + static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); + + static RegistryObject KNOCKBACK_SWORD = ITEMS.register("knockback_sword", () -> + new KnockbackSwordItem(ItemTier.IRON, 3, -2.4F, ATTACK_KNOCKBACK_VALUE, (new Item.Properties()).tab(ItemGroup.TAB_COMBAT)) + ); + + public PlayerAttackKnockbackTest() + { + IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); + ITEMS.register(modEventBus); + } + + static class KnockbackSwordItem extends SwordItem{ + private final float attackKnockback; + private final Multimap defaultModifiers = ArrayListMultimap.create(); // initialize as empty + protected static final UUID BASE_ATTACK_KNOCKBACK_UUID = UUID.fromString("01efce91-ab3a-4163-b464-5c7bd1ae5496"); + + KnockbackSwordItem(IItemTier itemTier, int attackDamageIn, float attackSpeedIn, float attackKnockbackIn, Properties properties) { + super(itemTier, attackDamageIn, attackSpeedIn, properties); + this.attackKnockback = attackKnockbackIn; + } + + @Override + public Multimap getDefaultAttributeModifiers(EquipmentSlotType equipmentSlotType) { + if(equipmentSlotType == EquipmentSlotType.MAINHAND){ + if(this.defaultModifiers.isEmpty()){ + Multimap oldAttributeModifiers = super.getDefaultAttributeModifiers(equipmentSlotType); + this.defaultModifiers.putAll(oldAttributeModifiers); + this.defaultModifiers.put(Attributes.ATTACK_KNOCKBACK, new AttributeModifier(BASE_ATTACK_KNOCKBACK_UUID, "Weapon modifier", (double)this.attackKnockback, AttributeModifier.Operation.ADDITION)); + } + return this.defaultModifiers; + } + else return super.getDefaultAttributeModifiers(equipmentSlotType); + } + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index d90d9ce691..6af00dfb2a 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -125,4 +125,6 @@ license="LGPL v2.1" [[mods]] modId="text_linear_filtering_test" [[mods]] - modId="custom_tas_test" \ No newline at end of file + modId="player_attack_knockback_test" +[[mods]] + modId="custom_tas_test" diff --git a/src/test/resources/assets/player_attack_knockback_test/lang/en_us.json b/src/test/resources/assets/player_attack_knockback_test/lang/en_us.json new file mode 100644 index 0000000000..480daefff7 --- /dev/null +++ b/src/test/resources/assets/player_attack_knockback_test/lang/en_us.json @@ -0,0 +1,3 @@ +{ + "item.player_attack_knockback_test.knockback_sword": "Knockback Sword" +} \ No newline at end of file diff --git a/src/test/resources/assets/player_attack_knockback_test/models/item/knockback_sword.json b/src/test/resources/assets/player_attack_knockback_test/models/item/knockback_sword.json new file mode 100644 index 0000000000..148ef88325 --- /dev/null +++ b/src/test/resources/assets/player_attack_knockback_test/models/item/knockback_sword.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:item/handheld", + "textures": { + "layer0": "player_attack_knockback_test:item/knockback_sword" + } +} \ No newline at end of file diff --git a/src/test/resources/assets/player_attack_knockback_test/textures/item/knockback_sword.png b/src/test/resources/assets/player_attack_knockback_test/textures/item/knockback_sword.png new file mode 100644 index 0000000000000000000000000000000000000000..eec41ee90c47e00d6c5dd9038868bf94a82df2f3 GIT binary patch literal 196 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`-JULvAr_~T6BL+t?AYId6nPdciD<~85ry)@0?eyZ1Mx>ECx?kKbLh*2~7a?%1Uwo literal 0 HcmV?d00001