Adds knockback attribute to players and updated knockback calculation to PlayerEntity#attack (#7806)

This commit is contained in:
Mike Garay authored and GitHub committed 2021-07-16 17:52:58 -07:00
1 parent e6c1814122
commit 2bd83b26aa
6 files changed
+89 -2

No files matched your search

@@ -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<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
static RegistryObject<Item> 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<Attribute, AttributeModifier> 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<Attribute, AttributeModifier> getDefaultAttributeModifiers(EquipmentSlotType equipmentSlotType) {
if(equipmentSlotType == EquipmentSlotType.MAINHAND){
if(this.defaultModifiers.isEmpty()){
Multimap<Attribute, AttributeModifier> 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);
}
}
}