2020-05-20 14:55:59 +02:00
|
|
|
package de.ellpeck.naturesaura.entities;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2021-12-05 23:32:31 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.particles.ParticleTypes;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-05 23:32:31 +01:00
|
|
|
import net.minecraft.network.syncher.EntityDataAccessor;
|
|
|
|
import net.minecraft.network.syncher.EntityDataSerializers;
|
|
|
|
import net.minecraft.network.syncher.SynchedEntityData;
|
|
|
|
import net.minecraft.sounds.SoundEvents;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.entity.EntityType;
|
|
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
|
|
|
import net.minecraft.world.entity.projectile.EyeOfEnder;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
|
|
|
|
public class EntityStructureFinder extends EyeOfEnder {
|
|
|
|
|
|
|
|
public static final EntityDataAccessor<Integer> COLOR = SynchedEntityData.defineId(EntityStructureFinder.class, EntityDataSerializers.INT);
|
2020-05-20 14:55:59 +02:00
|
|
|
|
|
|
|
private double targetX;
|
|
|
|
private double targetY;
|
|
|
|
private double targetZ;
|
|
|
|
private int despawnTimer;
|
|
|
|
private boolean shatterOrDrop;
|
|
|
|
|
2021-12-05 23:32:31 +01:00
|
|
|
public EntityStructureFinder(EntityType<? extends EyeOfEnder> type, Level level) {
|
2021-12-04 15:40:09 +01:00
|
|
|
super(type, level);
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-05 23:32:31 +01:00
|
|
|
protected void defineSynchedData() {
|
|
|
|
super.defineSynchedData();
|
2022-06-27 15:24:04 +02:00
|
|
|
this.entityData.define(EntityStructureFinder.COLOR, 0);
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-05 23:32:31 +01:00
|
|
|
public void addAdditionalSaveData(CompoundTag compound) {
|
|
|
|
super.addAdditionalSaveData(compound);
|
2022-06-27 15:24:04 +02:00
|
|
|
compound.putInt("color", this.entityData.get(EntityStructureFinder.COLOR));
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-05 23:32:31 +01:00
|
|
|
public void readAdditionalSaveData(CompoundTag compound) {
|
|
|
|
super.readAdditionalSaveData(compound);
|
2022-06-27 15:24:04 +02:00
|
|
|
this.entityData.set(EntityStructureFinder.COLOR, compound.getInt("color"));
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-05 23:32:31 +01:00
|
|
|
public void signalTo(BlockPos pos) {
|
2020-05-20 14:55:59 +02:00
|
|
|
double d0 = pos.getX();
|
2021-12-15 16:30:22 +01:00
|
|
|
var i = pos.getY();
|
2020-05-20 14:55:59 +02:00
|
|
|
double d1 = pos.getZ();
|
2021-12-15 16:30:22 +01:00
|
|
|
var d2 = d0 - this.getX();
|
|
|
|
var d3 = d1 - this.getZ();
|
2021-12-05 23:32:31 +01:00
|
|
|
var f = Math.sqrt(d2 * d2 + d3 * d3);
|
2020-05-20 14:55:59 +02:00
|
|
|
if (f > 12.0F) {
|
2022-06-27 15:24:04 +02:00
|
|
|
this.targetX = this.getX() + d2 / f * 12.0D;
|
|
|
|
this.targetZ = this.getZ() + d3 / f * 12.0D;
|
2021-12-05 23:32:31 +01:00
|
|
|
this.targetY = this.getY() + 8.0D;
|
2020-05-20 14:55:59 +02:00
|
|
|
} else {
|
|
|
|
this.targetX = d0;
|
|
|
|
this.targetY = i;
|
|
|
|
this.targetZ = d1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.despawnTimer = 0;
|
2021-12-05 23:32:31 +01:00
|
|
|
this.shatterOrDrop = this.random.nextInt(4) > 0;
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
this.baseTick();
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var vec3d = this.getDeltaMovement();
|
|
|
|
var d0 = this.getX() + vec3d.x;
|
|
|
|
var d1 = this.getY() + vec3d.y;
|
|
|
|
var d2 = this.getZ() + vec3d.z;
|
2021-12-05 23:32:31 +01:00
|
|
|
var f = Math.sqrt(vec3d.horizontalDistance());
|
|
|
|
this.setYRot((float) (Mth.atan2(vec3d.x, vec3d.z) * (double) (180F / (float) Math.PI)));
|
|
|
|
this.setXRot((float) (Mth.atan2(vec3d.y, f) * (double) (180F / (float) Math.PI)));
|
|
|
|
while (this.getXRot() - this.xRotO < -180.0F)
|
|
|
|
this.xRotO -= 360.0F;
|
|
|
|
while (this.getXRot() - this.xRotO >= 180.0F)
|
|
|
|
this.xRotO += 360.0F;
|
|
|
|
while (this.getYRot() - this.yRotO < -180.0F)
|
|
|
|
this.yRotO -= 360.0F;
|
|
|
|
while (this.getYRot() - this.yRotO >= 180.0F)
|
|
|
|
this.yRotO += 360.0F;
|
|
|
|
this.setXRot(Mth.lerp(0.2F, this.xRotO, this.getXRot()));
|
|
|
|
this.setYRot(Mth.lerp(0.2F, this.yRotO, this.getYRot()));
|
2023-07-08 12:32:27 +02:00
|
|
|
if (!this.level().isClientSide) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var d3 = this.targetX - d0;
|
|
|
|
var d4 = this.targetZ - d2;
|
|
|
|
var f1 = (float) Math.sqrt(d3 * d3 + d4 * d4);
|
|
|
|
var f2 = (float) Mth.atan2(d4, d3);
|
|
|
|
var d5 = Mth.lerp(0.0025D, f, f1);
|
|
|
|
var d6 = vec3d.y;
|
2020-05-20 14:55:59 +02:00
|
|
|
if (f1 < 1.0F) {
|
|
|
|
d5 *= 0.8D;
|
|
|
|
d6 *= 0.8D;
|
|
|
|
}
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var j = this.getY() < this.targetY ? 1 : -1;
|
2021-12-05 23:32:31 +01:00
|
|
|
vec3d = new Vec3(Math.cos(f2) * d5, d6 + ((double) j - d6) * (double) 0.015F, Math.sin(f2) * d5);
|
|
|
|
this.setDeltaMovement(vec3d);
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isInWater()) {
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = 0; i < 4; ++i)
|
2023-07-08 12:32:27 +02:00
|
|
|
this.level().addParticle(ParticleTypes.BUBBLE, d0 - vec3d.x * 0.25D, d1 - vec3d.y * 0.25D, d2 - vec3d.z * 0.25D, vec3d.x, vec3d.y, vec3d.z);
|
|
|
|
} else if (this.level().isClientSide) {
|
2022-06-27 15:24:04 +02:00
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(d0 - vec3d.x * 0.25D + this.random.nextDouble() * 0.6D - 0.3D, d1 - vec3d.y * 0.25D - 0.5D, d2 - vec3d.z * 0.25D + this.random.nextDouble() * 0.6D - 0.3D, vec3d.x * 0.25F, vec3d.y * 0.25F, vec3d.z * 0.25F, this.entityData.get(EntityStructureFinder.COLOR), 1, 50, 0, false, true);
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 12:32:27 +02:00
|
|
|
if (!this.level().isClientSide) {
|
2021-12-05 23:32:31 +01:00
|
|
|
this.setPos(d0, d1, d2);
|
2020-05-20 14:55:59 +02:00
|
|
|
++this.despawnTimer;
|
2023-07-08 12:32:27 +02:00
|
|
|
if (this.despawnTimer > 80 && !this.level().isClientSide) {
|
2021-12-05 23:32:31 +01:00
|
|
|
this.playSound(SoundEvents.ENDER_EYE_DEATH, 1.0F, 1.0F);
|
|
|
|
this.remove(RemovalReason.DISCARDED);
|
2020-05-20 14:55:59 +02:00
|
|
|
if (this.shatterOrDrop) {
|
2023-07-08 12:32:27 +02:00
|
|
|
this.level().addFreshEntity(new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), this.getItem()));
|
2020-05-20 14:55:59 +02:00
|
|
|
} else {
|
2023-07-08 12:32:27 +02:00
|
|
|
PacketHandler.sendToAllAround(this.level(), this.blockPosition(), 32, new PacketParticles((float) this.getX(), (float) this.getY(), (float) this.getZ(), PacketParticles.Type.STRUCTURE_FINDER, this.getId()));
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-05 23:32:31 +01:00
|
|
|
this.setPosRaw(d0, d1, d2);
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|