WIP: hammers

This commit is contained in:
Aleksey 2020-10-03 09:45:16 +03:00
parent a0aa8c929e
commit 0b36a7f07b
4 changed files with 49 additions and 68 deletions

View file

@ -0,0 +1,47 @@
package ru.betterend.mixin.common;
import java.util.Collection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.item.Item;
@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin {
private Entity lastAttacker;
@Inject(method = "damage", at = @At("HEAD"))
public void damage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
this.lastAttacker = source.getAttacker();
}
@ModifyArg(method = "damage", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;takeKnockback(FDD)V"))
private float increaseKnockback(float value, double x, double z) {
if (lastAttacker != null && lastAttacker instanceof LivingEntity) {
LivingEntity attacker = (LivingEntity) lastAttacker;
value += this.getKnockback(attacker.getMainHandStack().getItem());
}
return value;
}
private double getKnockback(Item tool) {
if (tool == null) return 0.0D;
Collection<EntityAttributeModifier> modifiers = tool.getAttributeModifiers(EquipmentSlot.MAINHAND)
.get(EntityAttributes.GENERIC_ATTACK_KNOCKBACK);
if (modifiers.size() > 0) {
return modifiers.iterator().next().getValue();
}
return 0.0D;
}
}

View file

@ -1,66 +0,0 @@
package ru.betterend.mixin.common;
import java.util.Collection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
@Mixin(PlayerEntity.class)
public abstract class PlayerEntityMixin extends LivingEntity {
protected PlayerEntityMixin(EntityType<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}
@ModifyArg(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;takeKnockback(FDD)V", ordinal = 0))
private float increaseKnockback(float value, double x, double z) {
PlayerEntity player = (PlayerEntity) (Object) this;
value += this.getKnockback(player.getMainHandStack().getItem());
return value;
}
@Inject(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;onAttacking(Lnet/minecraft/entity/Entity;)V", shift = Shift.BEFORE))
public void attack(Entity target, CallbackInfo cinfo) {
if (target instanceof LivingEntity) {
PlayerEntity player = (PlayerEntity) (Object) this;
int baseKnockback = EnchantmentHelper.getKnockback(player);
if (isSprinting() && getAttackCooldownProgress(0.5F) > 0.9F) {
baseKnockback++;
}
if (baseKnockback == 0) {
Item tool = player.getMainHandStack().getItem();
LivingEntity livingTarget = (LivingEntity) target;
livingTarget.takeKnockback((float) this.getKnockback(tool), MathHelper.sin(yaw * 0.017453292F), -MathHelper.cos(yaw * 0.017453292F));
}
}
}
@Shadow
abstract float getAttackCooldownProgress(float base);
private double getKnockback(Item tool) {
if (tool == null) return 0.0D;
Collection<EntityAttributeModifier> modifiers = tool.getAttributeModifiers(EquipmentSlot.MAINHAND).get(EntityAttributes.GENERIC_ATTACK_KNOCKBACK);
if (modifiers.size() > 0) {
return modifiers.iterator().next().getValue();
}
return 0.0D;
}
}

View file

@ -56,7 +56,7 @@ public class ItemRegistry {
public static ToolItem AETERNIUM_PICKAXE = registerTool("aeternium_pickaxe", new EndPickaxe(EndToolMaterial.AETERNIUM, 1, -2.8F, new Item.Settings().group(CreativeTab.END_TAB)));
public static ToolItem AETERNIUM_AXE = registerTool("aeternium_axe", new EndAxe(EndToolMaterial.AETERNIUM, 5.0F, -3.0F, new Item.Settings().group(CreativeTab.END_TAB)));
public static ToolItem AETERNIUM_HOE = registerTool("aeternium_hoe", new EndHoe(EndToolMaterial.AETERNIUM, -3, 0.0F, new Item.Settings().group(CreativeTab.END_TAB)));
public static ToolItem IRON_HAMMER = registerTool("iron_hammer", new EndHammer(ToolMaterials.IRON, 4.0F, -3.0F, 0.2D, new Item.Settings().group(CreativeTab.END_TAB)));
public static ToolItem IRON_HAMMER = registerTool("iron_hammer", new EndHammer(ToolMaterials.IRON, 5.0F, -3.2F, 0.2D, new Item.Settings().group(CreativeTab.END_TAB)));
protected static Item registerItem(String name, Item item) {
if (item != Items.AIR) {

View file

@ -10,7 +10,7 @@
"TagGroupLoaderMixin",
"CraftingScreenHandlerMixin",
"GenerationSettingsMixin",
"PlayerEntityMixin",
"LivingEntityMixin",
"BiomeMixin",
"TagAccessor"
],