Unicopia/src/main/java/com/minelittlepony/unicopia/magic/ITossedEffect.java
2020-01-17 14:27:26 +01:00

68 lines
2.2 KiB
Java

package com.minelittlepony.unicopia.magic;
import javax.annotation.Nullable;
import com.minelittlepony.unicopia.UItems;
import com.minelittlepony.unicopia.entity.AdvancedProjectileEntity;
import com.minelittlepony.unicopia.magic.spells.SpellRegistry;
import com.minelittlepony.unicopia.projectile.IAdvancedProjectile;
import com.minelittlepony.unicopia.projectile.ITossable;
import net.minecraft.entity.Entity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.world.World;
/**
* Magic effects that can be thrown.
*/
public interface ITossedEffect extends IMagicEffect, ITossable<ICaster<?>> {
@Override
default SoundEvent getThrowSound(ICaster<?> caster) {
return SoundEvents.ITEM_CHORUS_FRUIT_TELEPORT;
}
/**
* Gets the appearance to be used when projecting this spell.
*/
default ItemStack getCastAppearance(ICaster<?> caster) {
Item item = getAffinity() == Affinity.BAD ? UItems.curse : UItems.spell;
return SpellRegistry.instance().enchantStack(new ItemStack(item), getName());
}
/**
* Projects this spell.
*
* Returns the resulting projectile entity for customization (or null if on the client).
*/
@Nullable
default IAdvancedProjectile toss(ICaster<?> caster) {
World world = caster.getWorld();
Entity entity = caster.getOwner();
world.playSound(null, entity.x, entity.y, entity.z, getThrowSound(caster), SoundCategory.NEUTRAL, 0.7F, 0.4F / (world.random.nextFloat() * 0.4F + 0.8F));
if (caster.isLocal()) {
IAdvancedProjectile projectile = new AdvancedProjectileEntity(null, world, caster.getOwner());
projectile.setItem(getCastAppearance(caster));
projectile.setThrowDamage(getThrowDamage(caster));
projectile.setOwner(caster.getOwner());
projectile.setEffect(this);
projectile.setHydrophobic();
projectile.launch(entity, entity.pitch, entity.yaw, 0, 1.5F, 1);
projectile.spawn(world);
return projectile;
}
return null;
}
}