Unicopia/src/main/java/com/minelittlepony/unicopia/util/MagicalDamageSource.java

91 lines
3 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.util;
2018-09-12 01:29:49 +02:00
2020-05-04 00:40:43 +02:00
import java.util.ArrayList;
import java.util.List;
2019-04-09 13:45:36 +02:00
import javax.annotation.Nullable;
2018-09-12 01:29:49 +02:00
import net.minecraft.entity.Entity;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.EntityDamageSource;
import net.minecraft.entity.damage.ProjectileDamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ArrowEntity;
2018-09-12 01:29:49 +02:00
import net.minecraft.item.ItemStack;
2020-01-16 12:35:46 +01:00
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
2018-09-12 01:29:49 +02:00
public class MagicalDamageSource extends EntityDamageSource {
2018-09-19 09:09:43 +02:00
2020-05-04 00:40:43 +02:00
public static final DamageSource FOOD_POISONING = mundane("food_poisoning");
public static final DamageSource ACID = mundane("acid");
public static final DamageSource ALICORN_AMULET = new MagicalDamageSource("alicorn_amulet", true, true);
public static final DamageSource DARKNESS = create("darkness");
public static final DamageSource ZAP_APPLE = create("zap");
public static DamageSource mundane(String type) {
return new DamageSource(type) {};
}
2020-01-16 12:35:46 +01:00
public static DamageSource create(String type) {
2020-05-04 00:40:43 +02:00
return new MagicalDamageSource(type, false, false);
}
2018-09-19 09:09:43 +02:00
2020-01-16 12:35:46 +01:00
public static DamageSource causePlayerDamage(String type, PlayerEntity player) {
2019-04-09 13:45:36 +02:00
return causeMobDamage(type, player);
}
2018-09-19 09:09:43 +02:00
2020-01-16 12:35:46 +01:00
public static DamageSource causeMobDamage(String type, LivingEntity source) {
2020-05-04 00:40:43 +02:00
return new MagicalDamageSource(type, source, false, false);
}
2018-09-19 09:09:43 +02:00
2020-01-16 12:35:46 +01:00
public static DamageSource causeIndirect(String type, ArrowEntity source, @Nullable Entity instigator) {
return new ProjectileDamageSource(type, source, instigator).setProjectile();
2019-04-09 13:45:36 +02:00
}
2020-05-04 00:40:43 +02:00
protected MagicalDamageSource(String type, boolean direct, boolean unblockable) {
this(type, null, direct, unblockable);
}
2018-09-19 09:09:43 +02:00
2020-05-04 00:40:43 +02:00
protected MagicalDamageSource(String type, Entity source, boolean direct, boolean unblockable) {
super(type, source);
2020-01-16 12:35:46 +01:00
setUsesMagic();
2020-05-04 00:40:43 +02:00
if (direct) {
setBypassesArmor();
}
if (unblockable) {
setUnblockable();
}
2018-09-12 01:29:49 +02:00
}
2018-09-19 09:09:43 +02:00
2020-01-16 12:35:46 +01:00
@Override
public Text getDeathMessage(LivingEntity target) {
2020-05-04 00:40:43 +02:00
2020-01-16 12:35:46 +01:00
String basic = "death.attack." + name;
2018-09-19 09:09:43 +02:00
2020-05-04 00:40:43 +02:00
List<Text> params = new ArrayList<>();
params.add(target.getDisplayName());
2018-09-19 09:09:43 +02:00
2020-05-04 00:40:43 +02:00
@Nullable
Entity attacker = source instanceof LivingEntity ? source : target.getPrimeAdversary();
2018-09-19 09:09:43 +02:00
2020-05-04 00:40:43 +02:00
if (attacker instanceof LivingEntity) {
if (attacker == target) {
basic += ".self";
} else {
basic += ".attacker";
params.add(((LivingEntity)attacker).getDisplayName());
}
2020-01-17 14:27:26 +01:00
2020-05-04 00:40:43 +02:00
ItemStack item = ((LivingEntity)attacker).getMainHandStack();
if (!item.isEmpty() && item.hasCustomName()) {
basic += ".item";
params.add(item.toHoverableText());
}
2020-01-17 14:27:26 +01:00
}
2020-05-04 00:40:43 +02:00
return new TranslatableText(basic, params.toArray());
2020-01-17 14:27:26 +01:00
}
2018-09-12 01:29:49 +02:00
}