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

127 lines
4.8 KiB
Java

package com.thebrokenrail.sorcerycraft.entity;
import com.thebrokenrail.sorcerycraft.SorceryCraft;
import com.thebrokenrail.sorcerycraft.spell.registry.Spell;
import com.thebrokenrail.sorcerycraft.spell.registry.SpellRegistry;
import com.thebrokenrail.sorcerycraft.spell.util.SpellTag;
import com.thebrokenrail.sorcerycraft.spell.registry.Spells;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
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.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
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<SpellEntity> entityType, World world) {
super(entityType, world);
}
public SpellEntity(World world, LivingEntity owner) {
super(SorceryCraft.SPELL_ENTITY, owner, world);
}
private boolean firstTick = true;
public SpellEntity(World world, double x, double y, double z) {
super(SorceryCraft.SPELL_ENTITY, x, y, z, world);
}
private boolean didSpellSucceed(Map<Identifier, Integer> spells) {
return Math.random() > SorceryCraft.SPELL_FAILURE_CHANCE || spells.containsKey(Spells.STEADFAST_SPELL);
}
@Override
protected void onCollision(HitResult hitResult) {
Map<Identifier, Integer> spells = SpellTag.getSpells(getItem());
if (!spells.containsKey(Spells.INWARD_SPELL)) {
if (hitResult.getType() == HitResult.Type.BLOCK) {
remove();
} else if (hitResult.getType() == HitResult.Type.ENTITY) {
Entity entity = ((EntityHitResult) hitResult).getEntity();
boolean success = didSpellSucceed(spells);
for (Map.Entry<Identifier, Integer> entry : spells.entrySet()) {
Spell spell = SpellRegistry.getSpell(entry.getKey(), entry.getValue());
if (spell != null) {
if (success) {
spell.execute(entity, this, getOwner());
} else if (getOwner() != null) {
if (getOwner() instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) getOwner();
player.playSound(SoundEvents.ENCHANT_THORNS_HIT, SoundCategory.PLAYERS, 1.0f, 1.0f);
}
spell.execute(getOwner(), this, getOwner());
}
}
}
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 (firstTick) {
firstTick = false;
Map<Identifier, Integer> spells = SpellTag.getSpells(getItem());
if (spells.containsKey(Spells.INWARD_SPELL)) {
if (getOwner() != null) {
boolean success = didSpellSucceed(spells);
for (Map.Entry<Identifier, Integer> entry : spells.entrySet()) {
Spell spell = SpellRegistry.getSpell(entry.getKey(), entry.getValue());
if (spell != null) {
if (success) {
spell.execute(getOwner(), this, getOwner());
} else if (getOwner() instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) getOwner();
player.playSound(SoundEvents.ENCHANT_THORNS_HIT, SoundCategory.PLAYERS, 1.0f, 1.0f);
}
}
}
}
remove();
return;
}
}
if (!getEntityWorld().isClient()) {
getEntityWorld().sendEntityStatus(this, (byte) 0);
}
}
@Override
protected Item getDefaultItem() {
return SorceryCraft.SPELL_ITEM;
}
@Override
public Packet<?> createSpawnPacket() {
return new EntitySpawnS2CPacket(this);
}
}