NaturesAura/src/main/java/de/ellpeck/naturesaura/items/tools/ItemSword.java

71 lines
3.1 KiB
Java
Raw Normal View History

2018-10-19 18:32:20 +02:00
package de.ellpeck.naturesaura.items.tools;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.ItemModelGenerator;
2018-10-20 21:19:08 +02:00
import de.ellpeck.naturesaura.items.ModItems;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.reg.ICustomItemModel;
2018-10-19 18:32:20 +02:00
import de.ellpeck.naturesaura.reg.IModItem;
2020-01-22 01:32:26 +01:00
import de.ellpeck.naturesaura.reg.ModRegistry;
2023-02-28 14:30:19 +01:00
import net.minecraft.util.Mth;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
2023-02-28 14:30:19 +01:00
import net.minecraft.world.entity.Entity;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.entity.LivingEntity;
2023-02-28 14:30:19 +01:00
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Player;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.Tier;
2023-02-28 14:30:19 +01:00
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;
2018-10-19 18:32:20 +02:00
2020-01-29 00:40:28 +01:00
public class ItemSword extends SwordItem implements IModItem, ICustomItemModel {
2021-12-05 23:32:31 +01:00
2018-10-19 18:32:20 +02:00
private final String baseName;
2021-12-05 23:32:31 +01:00
public ItemSword(String baseName, Tier material, int damage, float speed) {
2023-07-08 12:32:27 +02:00
super(material, damage, speed, new Properties());
2018-10-19 18:32:20 +02:00
this.baseName = baseName;
2022-06-27 15:24:04 +02:00
ModRegistry.ALL_ITEMS.add(this);
2018-10-19 18:32:20 +02:00
}
@Override
public String getBaseName() {
return this.baseName;
}
@Override
2021-12-05 23:32:31 +01:00
public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) {
2020-05-13 15:52:46 +02:00
if (this == ModItems.INFUSED_IRON_SWORD) {
2021-12-05 23:32:31 +01:00
target.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 60, 2));
2020-05-13 15:52:46 +02:00
} else if (this == ModItems.SKY_SWORD) {
2021-12-05 23:32:31 +01:00
target.addEffect(new MobEffectInstance(MobEffects.LEVITATION, 60, 2));
2023-02-28 14:30:19 +01:00
} else if (this == ModItems.DEPTH_SWORD && attacker instanceof Player player) {
// this is just a modified copy of Player.attack's sweeping damage code
var damage = (float) player.getAttributeValue(Attributes.ATTACK_DAMAGE) * 0.75F;
2023-07-08 12:32:27 +02:00
for (var other : player.level().getEntitiesOfClass(LivingEntity.class, stack.getSweepHitBox(player, target))) {
if (other != player && other != target && !player.isAlliedTo(other) && (!(other instanceof ArmorStand stand) || !stand.isMarker()) && player.distanceToSqr(other) < Mth.square(player.getEntityReach())) {
2023-02-28 14:30:19 +01:00
other.knockback(0.4F, Mth.sin(player.getYRot() * (Mth.PI / 180)), -Mth.cos(player.getYRot() * (Mth.PI / 180)));
2023-07-08 12:32:27 +02:00
other.hurt(other.damageSources().playerAttack(player), damage);
2023-02-28 14:30:19 +01:00
}
}
// this just displays the particles
player.sweepAttack();
2020-05-13 15:52:46 +02:00
}
2021-12-05 23:32:31 +01:00
return super.hurtEnemy(stack, target, attacker);
}
2023-02-28 14:30:19 +01:00
@Override
public @NotNull AABB getSweepHitBox(@NotNull ItemStack stack, @NotNull Player player, @NotNull Entity target) {
if (this == ModItems.DEPTH_SWORD)
return target.getBoundingBox().inflate(2, 1, 2);
return super.getSweepHitBox(stack, player, target);
}
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomItemModel(ItemModelGenerator generator) {
generator.withExistingParent(this.getBaseName(), "item/handheld").texture("layer0", "item/" + this.getBaseName());
}
2018-10-19 18:32:20 +02:00
}