| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.server.level.ServerPlayer;
|
| 9 | import net.minecraft.sounds.SoundEvent;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.EquipmentSlotGroup;
|
| 14 | import net.minecraft.world.entity.LivingEntity;
|
| 15 | import net.minecraft.world.entity.TamableAnimal;
|
| 16 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 17 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 18 | import net.minecraft.world.entity.decoration.ArmorStand;
|
| 19 | import net.minecraft.world.entity.player.Player;
|
| 20 | import net.minecraft.world.item.component.ItemAttributeModifiers;
|
| 21 | import net.minecraft.world.item.component.Tool;
|
| 22 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.phys.Vec3;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class MaceItem extends Item {
|
| 28 | private static final int DEFAULT_ATTACK_DAMAGE = 5;
|
| 29 | private static final float DEFAULT_ATTACK_SPEED = -3.4F;
|
| 30 | public static final float SMASH_ATTACK_FALL_THRESHOLD = 1.5F;
|
| 31 | private static final float SMASH_ATTACK_HEAVY_THRESHOLD = 5.0F;
|
| 32 | public static final float SMASH_ATTACK_KNOCKBACK_RADIUS = 3.5F;
|
| 33 | private static final float SMASH_ATTACK_KNOCKBACK_POWER = 0.7F;
|
| 34 |
|
| 35 | public MaceItem(final Item.Properties properties) {
|
| 36 | super(properties);
|
| 37 | }
|
| 38 |
|
| 39 | public static ItemAttributeModifiers createAttributes() {
|
| 40 | return ItemAttributeModifiers.builder()
|
| 41 | .add(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_ID, 5.0, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND)
|
| 42 | .add(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_ID, -3.4F, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND)
|
| 43 | .build();
|
| 44 | }
|
| 45 |
|
| 46 | public static Tool createToolProperties() {
|
| 47 | return new Tool(List.of(), 1.0F, 2, false);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void hurtEnemy(final ItemStack itemStack, final LivingEntity mob, final LivingEntity attacker) {
|
| 52 | if (canSmashAttack(attacker)) {
|
| 53 | ServerLevel level = (ServerLevel)attacker.level();
|
| 54 | attacker.setDeltaMovement(attacker.getDeltaMovement().with(Direction.Axis.Y, 0.01F));
|
| 55 | attacker.setIgnoreFallDamageFromCurrentImpulse(true, this.calculateImpactPosition(attacker));
|
| 56 | if (attacker instanceof ServerPlayer player) {
|
| 57 | player.connection.send(new ClientboundSetEntityMotionPacket(player));
|
| 58 | }
|
| 59 |
|
| 60 | if (mob.onGround()) {
|
| 61 | if (attacker instanceof ServerPlayer player) {
|
| 62 | player.setSpawnExtraParticlesOnFall(true);
|
| 63 | }
|
| 64 |
|
| 65 | SoundEvent sound = attacker.fallDistance > 5.0 ? SoundEvents.MACE_SMASH_GROUND_HEAVY : SoundEvents.MACE_SMASH_GROUND;
|
| 66 | level.playSound(null, attacker.getX(), attacker.getY(), attacker.getZ(), sound, attacker.getSoundSource(), 1.0F, 1.0F);
|
| 67 | } else {
|
| 68 | level.playSound(null, attacker.getX(), attacker.getY(), attacker.getZ(), SoundEvents.MACE_SMASH_AIR, attacker.getSoundSource(), 1.0F, 1.0F);
|
| 69 | }
|
| 70 |
|
| 71 | knockback(level, attacker, mob);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private Vec3 calculateImpactPosition(final LivingEntity attacker) {
|
| 76 | return attacker.isIgnoringFallDamageFromCurrentImpulse() && attacker.currentImpulseImpactPos.y <= attacker.position().y
|
| 77 | ? attacker.currentImpulseImpactPos
|
| 78 | : attacker.position();
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void postHurtEnemy(final ItemStack itemStack, final LivingEntity mob, final LivingEntity attacker) {
|
| 83 | if (canSmashAttack(attacker)) {
|
| 84 | attacker.resetFallDistance();
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public float getAttackDamageBonus(final Entity victim, final float ignoredDamage, final DamageSource damageSource) {
|
| 90 | if (damageSource.getDirectEntity() instanceof LivingEntity attacker) {
|
| 91 | if (!canSmashAttack(attacker)) {
|
| 92 | return 0.0F;
|
| 93 | }
|
| 94 |
|
| 95 | double fallHeightThreshold1 = 3.0;
|
| 96 | double fallHeightThreshold2 = 8.0;
|
| 97 | double fallDistance = attacker.fallDistance;
|
| 98 | double damage;
|
| 99 | if (fallDistance <= 3.0) {
|
| 100 | damage = 4.0 * fallDistance;
|
| 101 | } else if (fallDistance <= 8.0) {
|
| 102 | damage = 12.0 + 2.0 * (fallDistance - 3.0);
|
| 103 | } else {
|
| 104 | damage = 22.0 + fallDistance - 8.0;
|
| 105 | }
|
| 106 |
|
| 107 | return attacker.level() instanceof ServerLevel level
|
| 108 | ? (float)(damage + EnchantmentHelper.modifyFallBasedDamage(level, attacker.getWeaponItem(), victim, damageSource, 0.0F) * fallDistance)
|
| 109 | : (float)damage;
|
| 110 | } else {
|
| 111 | return 0.0F;
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | private static void knockback(final Level level, final Entity attacker, final Entity entity) {
|
| 116 | level.levelEvent(2013, entity.getOnPos(), 750);
|
| 117 | level.getEntitiesOfClass(LivingEntity.class, entity.getBoundingBox().inflate(3.5), knockbackPredicate(attacker, entity)).forEach(nearby -> {
|
| 118 | Vec3 direction = nearby.position().subtract(entity.position());
|
| 119 | double knockbackPower = getKnockbackPower(attacker, nearby, direction);
|
| 120 | Vec3 knockbackVector = direction.normalize().scale(knockbackPower);
|
| 121 | if (knockbackPower > 0.0) {
|
| 122 | nearby.push(knockbackVector.x, 0.7F, knockbackVector.z);
|
| 123 | if (nearby instanceof ServerPlayer otherPlayer) {
|
| 124 | otherPlayer.connection.send(new ClientboundSetEntityMotionPacket(otherPlayer));
|
| 125 | }
|
| 126 | }
|
| 127 | });
|
| 128 | }
|
| 129 |
|
| 130 | private static Predicate<LivingEntity> knockbackPredicate(final Entity attacker, final Entity entity) {
|
| 131 | return nearby -> {
|
| 132 | boolean notSpectator = !nearby.isSpectator();
|
| 133 | boolean notPlayer = nearby != attacker && nearby != entity;
|
| 134 | boolean notAlliedToPlayer = !attacker.isAlliedTo(nearby);
|
| 135 | boolean notTamedByPlayer = !(
|
| 136 | nearby instanceof TamableAnimal animal && entity instanceof LivingEntity livingAttacker && animal.isTame() && animal.isOwnedBy(livingAttacker)
|
| 137 | );
|
| 138 | boolean notArmorStand = !(nearby instanceof ArmorStand armorStand && armorStand.isMarker());
|
| 139 | boolean withinRange = entity.distanceToSqr(nearby) <= Math.pow(3.5, 2.0);
|
| 140 | boolean notFlyingInCreative = !(nearby instanceof Player player && player.isCreative() && player.getAbilities().flying);
|
| 141 | return notSpectator && notPlayer && notAlliedToPlayer && notTamedByPlayer && notArmorStand && withinRange && notFlyingInCreative;
|
| 142 | };
|
| 143 | }
|
| 144 |
|
| 145 | private static double getKnockbackPower(final Entity attacker, final LivingEntity nearby, final Vec3 direction) {
|
| 146 | return (3.5 - direction.length()) * 0.7F * (attacker.fallDistance > 5.0 ? 2 : 1) * (1.0 - nearby.getAttributeValue(Attributes.KNOCKBACK_RESISTANCE));
|
| 147 | }
|
| 148 |
|
| 149 | public static boolean canSmashAttack(final LivingEntity attacker) {
|
| 150 | return attacker.fallDistance > 1.5 && !attacker.isFallFlying();
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public @Nullable DamageSource getItemDamageSource(final LivingEntity attacker) {
|
| 155 | return canSmashAttack(attacker) ? attacker.damageSources().mace(attacker) : super.getItemDamageSource(attacker);
|
| 156 | }
|
| 157 | }
|
| 158 |
|