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

121 lines
5.1 KiB
Java

package com.thebrokenrail.sorcerycraft.entity;
import com.thebrokenrail.sorcerycraft.SorceryCraft;
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
import com.thebrokenrail.sorcerycraft.spell.api.registry.SpellRegistry;
import com.thebrokenrail.sorcerycraft.spell.util.SpellHelper;
import com.thebrokenrail.sorcerycraft.spell.api.registry.Spells;
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.projectile.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.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.world.World;
import java.util.List;
import java.util.Map;
import java.util.Objects;
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);
}
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.getConfig().failureChance || spells.containsKey(Spells.STEADFAST_SPELL);
}
@Override
protected void onCollision(HitResult hitResult) {
super.onCollision(hitResult);
if (!getEntityWorld().isClient()) {
Map<Identifier, Integer> spells = SpellHelper.getSpells(getItem());
if (!spells.containsKey(Spells.INWARD_SPELL)) {
boolean success = didSpellSucceed(spells);
for (Map.Entry<Identifier, Integer> entry : spells.entrySet()) {
Spell spell = SpellRegistry.getSpell(entry);
if (spell != null) {
if (success) {
if (hitResult.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockHitResult = (BlockHitResult) hitResult;
spell.execute(getEntityWorld(), this, getOwner(), blockHitResult);
} else if (hitResult.getType() == HitResult.Type.ENTITY) {
Entity entity = ((EntityHitResult) hitResult).getEntity();
spell.execute(world, this, getOwner(), entity);
}
} 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(world, this, getOwner(), getOwner());
}
}
}
remove();
}
}
}
@Override
public void tick() {
super.tick();
if (!getEntityWorld().isClient()) {
Map<Identifier, Integer> spells = SpellHelper.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);
if (spell != null) {
if (success) {
spell.execute(world, this, getOwner(), getOwner());
} else if (getOwner() instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) getOwner();
player.playSound(SoundEvents.ENCHANT_THORNS_HIT, SoundCategory.PLAYERS, 1.0f, 1.0f);
}
}
}
}
remove();
return;
}
List<ServerPlayerEntity> viewers = Objects.requireNonNull(getServer()).getPlayerManager().getPlayerList();
for (ServerPlayerEntity viewer : viewers) {
((ServerWorld) getEntityWorld()).spawnParticles(viewer, ParticleTypes.WITCH, true, getX(), getY(), getZ(), 8, 0.1d, 0.1d, 0.1d, 0d);
}
}
}
@Override
protected Item getDefaultItem() {
return SorceryCraft.SPELL_ITEM;
}
@Override
public Packet<?> createSpawnPacket() {
return new EntitySpawnS2CPacket(this);
}
}