2020-03-01 18:19:59 +00:00
|
|
|
package com.thebrokenrail.sorcerycraft.entity;
|
|
|
|
|
|
|
|
import com.thebrokenrail.sorcerycraft.SorceryCraft;
|
2020-03-06 23:35:29 +00:00
|
|
|
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;
|
2020-03-01 18:19:59 +00:00
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
2020-03-02 22:11:37 +00:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.EntityType;
|
|
|
|
import net.minecraft.entity.LivingEntity;
|
2020-03-04 01:22:40 +00:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2020-03-01 18:19:59 +00:00
|
|
|
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;
|
2020-03-04 01:22:40 +00:00
|
|
|
import net.minecraft.sound.SoundCategory;
|
2020-03-01 18:19:59 +00:00
|
|
|
import net.minecraft.sound.SoundEvents;
|
2020-03-03 23:08:22 +00:00
|
|
|
import net.minecraft.util.Identifier;
|
2020-03-01 18:19:59 +00:00
|
|
|
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 {
|
2020-03-03 02:43:25 +00:00
|
|
|
public SpellEntity(EntityType<SpellEntity> entityType, World world) {
|
2020-03-01 18:19:59 +00:00
|
|
|
super(entityType, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SpellEntity(World world, LivingEntity owner) {
|
|
|
|
super(SorceryCraft.SPELL_ENTITY, owner, world);
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:08:22 +00:00
|
|
|
private boolean firstTick = true;
|
2020-03-03 02:43:25 +00:00
|
|
|
|
2020-03-01 18:19:59 +00:00
|
|
|
public SpellEntity(World world, double x, double y, double z) {
|
|
|
|
super(SorceryCraft.SPELL_ENTITY, x, y, z, world);
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:08:22 +00:00
|
|
|
private boolean didSpellSucceed(Map<Identifier, Integer> spells) {
|
|
|
|
return Math.random() > SorceryCraft.SPELL_FAILURE_CHANCE || spells.containsKey(Spells.STEADFAST_SPELL);
|
2020-03-03 02:43:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 18:19:59 +00:00
|
|
|
@Override
|
|
|
|
protected void onCollision(HitResult hitResult) {
|
2020-03-03 23:08:22 +00:00
|
|
|
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();
|
2020-03-01 18:19:59 +00:00
|
|
|
|
2020-03-03 23:08:22 +00:00
|
|
|
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) {
|
2020-03-04 01:22:40 +00:00
|
|
|
if (getOwner() instanceof PlayerEntity) {
|
|
|
|
PlayerEntity player = (PlayerEntity) getOwner();
|
|
|
|
player.playSound(SoundEvents.ENCHANT_THORNS_HIT, SoundCategory.PLAYERS, 1.0f, 1.0f);
|
|
|
|
}
|
2020-03-03 23:08:22 +00:00
|
|
|
spell.execute(getOwner(), this, getOwner());
|
|
|
|
}
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:08:22 +00:00
|
|
|
remove();
|
|
|
|
}
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
2020-03-03 02:43:25 +00:00
|
|
|
if (firstTick) {
|
|
|
|
firstTick = false;
|
2020-03-03 23:08:22 +00:00
|
|
|
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());
|
2020-03-04 02:38:15 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-03-03 23:08:22 +00:00
|
|
|
}
|
2020-03-03 02:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 23:08:22 +00:00
|
|
|
remove();
|
2020-03-03 02:43:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 18:19:59 +00:00
|
|
|
if (!getEntityWorld().isClient()) {
|
|
|
|
getEntityWorld().sendEntityStatus(this, (byte) 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Item getDefaultItem() {
|
|
|
|
return SorceryCraft.SPELL_ITEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Packet<?> createSpawnPacket() {
|
|
|
|
return new EntitySpawnS2CPacket(this);
|
|
|
|
}
|
|
|
|
}
|