2022-09-18 21:39:42 +02:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
|
|
|
import com.minelittlepony.unicopia.entity.UEntityAttributes;
|
|
|
|
|
2022-09-23 15:38:48 +02:00
|
|
|
import net.minecraft.block.*;
|
2022-09-18 21:39:42 +02:00
|
|
|
import net.minecraft.entity.EquipmentSlot;
|
2022-09-23 15:38:48 +02:00
|
|
|
import net.minecraft.entity.LivingEntity;
|
2022-09-18 21:39:42 +02:00
|
|
|
import net.minecraft.entity.attribute.*;
|
|
|
|
import net.minecraft.item.*;
|
2022-09-23 15:38:48 +02:00
|
|
|
import net.minecraft.network.packet.s2c.play.EntityVelocityUpdateS2CPacket;
|
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
2022-09-18 21:39:42 +02:00
|
|
|
|
|
|
|
public class PolearmItem extends SwordItem {
|
|
|
|
protected static final UUID ATTACK_RANGE_MODIFIER_ID = UUID.fromString("A7B3659C-AA74-469C-963A-09A391DCAA0F");
|
|
|
|
|
|
|
|
private final Multimap<EntityAttribute, EntityAttributeModifier> attributeModifiers;
|
|
|
|
|
|
|
|
private final int attackRange;
|
|
|
|
|
|
|
|
public PolearmItem(ToolMaterial material, int damage, float speed, int range, Settings settings) {
|
|
|
|
super(material, damage, speed, settings);
|
|
|
|
this.attackRange = range;
|
|
|
|
ImmutableMultimap.Builder<EntityAttribute, EntityAttributeModifier> builder = ImmutableMultimap.builder();
|
|
|
|
builder.putAll(super.getAttributeModifiers(EquipmentSlot.MAINHAND));
|
|
|
|
builder.put(UEntityAttributes.EXTENDED_REACH_DISTANCE, new EntityAttributeModifier(ATTACK_RANGE_MODIFIER_ID, "Weapon modifier", attackRange, EntityAttributeModifier.Operation.ADDITION));
|
|
|
|
attributeModifiers = builder.build();
|
|
|
|
}
|
|
|
|
|
2022-09-23 15:38:48 +02:00
|
|
|
@Override
|
|
|
|
public float getMiningSpeedMultiplier(ItemStack stack, BlockState state) {
|
|
|
|
if (state.isOf(Blocks.COBWEB)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return super.getMiningSpeedMultiplier(stack, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isSuitableFor(BlockState state) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:39:42 +02:00
|
|
|
@Override
|
|
|
|
public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(EquipmentSlot slot) {
|
|
|
|
if (slot == EquipmentSlot.MAINHAND) {
|
|
|
|
return attributeModifiers;
|
|
|
|
}
|
|
|
|
return super.getAttributeModifiers(slot);
|
|
|
|
}
|
2022-09-23 15:38:48 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
|
|
|
|
boolean tooNear = target.distanceTo(attacker) <= 2;
|
|
|
|
stack.damage(tooNear ? 4 : 1, attacker, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
|
|
|
|
target.takeKnockback(0.15, attacker.getX() - target.getX(), attacker.getZ() - target.getZ());
|
|
|
|
if (tooNear) {
|
|
|
|
attacker.takeKnockback(attacker.getRandom().nextTriangular(0.4, 0.2), target.getX() - attacker.getX(), target.getZ() - attacker.getZ());
|
|
|
|
if (attacker instanceof ServerPlayerEntity ply) {
|
|
|
|
ply.networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(ply));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-18 21:39:42 +02:00
|
|
|
}
|