This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
SorceryCraft/src/main/java/com/thebrokenrail/sorcerycraft/entity/SpellEntity.java

92 lines
3.0 KiB
Java

package com.thebrokenrail.sorcerycraft.entity;
import com.thebrokenrail.sorcerycraft.SorceryCraft;
import com.thebrokenrail.sorcerycraft.spell.Spell;
import com.thebrokenrail.sorcerycraft.spell.SpellRegistry;
import com.thebrokenrail.sorcerycraft.spell.SpellTag;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.*;
import net.minecraft.entity.thrown.ThrownItemEntity;
import net.minecraft.item.Item;
import net.minecraft.network.Packet;
import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;
import java.util.Map;
public class SpellEntity extends ThrownItemEntity {
public SpellEntity(EntityType<? extends SpellEntity> entityType, World world) {
super(entityType, world);
}
public SpellEntity(World world, LivingEntity owner) {
super(SorceryCraft.SPELL_ENTITY, owner, world);
}
public SpellEntity(World world, double x, double y, double z) {
super(SorceryCraft.SPELL_ENTITY, x, y, z, world);
}
@Override
protected void onCollision(HitResult hitResult) {
boolean remove = false;
if (hitResult.getType() == HitResult.Type.BLOCK) {
remove = true;
} else if (hitResult.getType() == HitResult.Type.ENTITY) {
Entity entity = ((EntityHitResult) hitResult).getEntity();
Map<String, Integer> spells = SpellTag.getSpells(getItem());
for (Map.Entry<String, Integer> entry : spells.entrySet()) {
Spell spell = SpellRegistry.getSpell(entry.getKey(), entry.getValue());
if (spell != null) {
if (Math.random() > 0.1 || spells.containsKey("steadfast_spell")) {
spell.execute(entity, this, getOwner());
} else if (getOwner() != null) {
getOwner().playSound(SoundEvents.ENCHANT_THORNS_HIT, 1.0f, 1.0f);
spell.execute(getOwner(), this, getOwner());
}
}
}
remove = true;
}
if (remove && !getEntityWorld().isClient()) {
remove();
}
}
@Override
@Environment(EnvType.CLIENT)
public void handleStatus(byte status) {
if (status == 0) {
for (int i = 0; i < 12; i++) {
getEntityWorld().addParticle(ParticleTypes.WITCH, getX(), getY(), getZ(), 0.0d, 0.0d, 0.0d);
}
}
}
@Override
public void tick() {
super.tick();
if (!getEntityWorld().isClient()) {
getEntityWorld().sendEntityStatus(this, (byte) 0);
}
}
@Override
protected Item getDefaultItem() {
return SorceryCraft.SPELL_ITEM;
}
@Override
public Packet<?> createSpawnPacket() {
return new EntitySpawnS2CPacket(this);
}
}