2021-02-18 21:30:43 +01:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
|
|
|
|
2022-01-04 23:43:07 +01:00
|
|
|
import com.minelittlepony.unicopia.USounds;
|
2023-06-02 21:20:30 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.damage.UDamageSources;
|
2021-02-18 21:30:43 +01:00
|
|
|
import com.minelittlepony.unicopia.item.UItems;
|
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.EntityType;
|
|
|
|
import net.minecraft.entity.damage.DamageSource;
|
|
|
|
import net.minecraft.entity.data.DataTracker;
|
|
|
|
import net.minecraft.entity.data.TrackedData;
|
|
|
|
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2021-08-04 15:38:03 +02:00
|
|
|
import net.minecraft.nbt.NbtCompound;
|
2021-02-18 21:30:43 +01:00
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.math.MathHelper;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2023-06-02 21:20:30 +02:00
|
|
|
public class FloatingArtefactEntity extends Entity implements UDamageSources {
|
2021-02-18 21:30:43 +01:00
|
|
|
|
|
|
|
private static final TrackedData<ItemStack> ITEM = DataTracker.registerData(FloatingArtefactEntity.class, TrackedDataHandlerRegistry.ITEM_STACK);
|
|
|
|
private static final TrackedData<Byte> STATE = DataTracker.registerData(FloatingArtefactEntity.class, TrackedDataHandlerRegistry.BYTE);
|
2021-02-21 21:17:37 +01:00
|
|
|
private static final TrackedData<Float> SPIN = DataTracker.registerData(FloatingArtefactEntity.class, TrackedDataHandlerRegistry.FLOAT);
|
|
|
|
|
|
|
|
private float bobAmount;
|
|
|
|
private float spinAmount;
|
2021-02-18 21:30:43 +01:00
|
|
|
|
|
|
|
private float health = 1;
|
|
|
|
public final float positionSeed;
|
|
|
|
|
2021-02-21 21:17:37 +01:00
|
|
|
private int spinupDuration;
|
|
|
|
|
|
|
|
private float sourceSpin = 1;
|
|
|
|
private float targetSpin = 1;
|
|
|
|
private float spinChange;
|
|
|
|
private float spinChangeProgress;
|
|
|
|
|
2021-02-18 21:30:43 +01:00
|
|
|
public FloatingArtefactEntity(EntityType<?> entityType, World world) {
|
|
|
|
super(entityType, world);
|
|
|
|
|
|
|
|
positionSeed = (float)(Math.random() * Math.PI * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initDataTracker() {
|
|
|
|
dataTracker.startTracking(ITEM, ItemStack.EMPTY);
|
|
|
|
dataTracker.startTracking(STATE, (byte)0);
|
2021-02-21 21:17:37 +01:00
|
|
|
dataTracker.startTracking(SPIN, 1F);
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getStack() {
|
|
|
|
return dataTracker.get(ITEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setStack(ItemStack stack) {
|
|
|
|
dataTracker.set(ITEM, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
public State getState() {
|
|
|
|
return State.valueOf(dataTracker.get(STATE));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setState(State state) {
|
|
|
|
dataTracker.set(STATE, (byte)state.ordinal());
|
|
|
|
}
|
|
|
|
|
2021-02-21 21:17:37 +01:00
|
|
|
public void addSpin(float spin, int duration) {
|
|
|
|
if (spin >= getSpin()) {
|
|
|
|
setSpin(spin);
|
|
|
|
spinupDuration = duration;
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 21:17:37 +01:00
|
|
|
public void setSpin(float spin) {
|
|
|
|
dataTracker.set(SPIN, spin);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getSpin() {
|
|
|
|
return dataTracker.get(SPIN);
|
|
|
|
}
|
|
|
|
|
2021-02-18 21:30:43 +01:00
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
Vec3d pos = Vec3d.ofBottomCenter(getBlockPos());
|
|
|
|
|
|
|
|
setPos(pos.x, pos.y, pos.z);
|
|
|
|
|
|
|
|
super.tick();
|
|
|
|
|
|
|
|
ItemStack stack = getStack();
|
|
|
|
|
|
|
|
if (stack.isEmpty()) {
|
|
|
|
setStack(UItems.EMPTY_JAR.getDefaultStack());
|
|
|
|
}
|
|
|
|
|
2023-06-03 13:40:54 +02:00
|
|
|
if (getWorld().isClient) {
|
2021-02-21 21:17:37 +01:00
|
|
|
float spin = getSpin();
|
|
|
|
if (Math.abs(spin - targetSpin) > 1.0E-5F) {
|
|
|
|
spinChange = spin - targetSpin;
|
|
|
|
targetSpin = spin;
|
|
|
|
spinChangeProgress = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spinChange != 0) {
|
|
|
|
if (spinChangeProgress < 1) {
|
|
|
|
spinChangeProgress += 0.05F;
|
|
|
|
} else {
|
|
|
|
sourceSpin = targetSpin;
|
|
|
|
spinChange = 0;
|
|
|
|
spinChangeProgress = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spinAmount += sourceSpin + (spinChange * spinChangeProgress);
|
|
|
|
bobAmount++;
|
|
|
|
} else {
|
|
|
|
spinupDuration = Math.max(0, spinupDuration - 1);
|
|
|
|
if (spinupDuration <= 0) {
|
|
|
|
setSpin(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 21:30:43 +01:00
|
|
|
if (stack.getItem() instanceof Artifact) {
|
|
|
|
((Artifact)stack.getItem()).onArtifactTick(this);
|
|
|
|
}
|
|
|
|
|
2023-06-03 13:40:54 +02:00
|
|
|
if (getWorld().getTime() % 80 == 0) {
|
2021-02-18 21:30:43 +01:00
|
|
|
State state = getState();
|
2022-01-04 23:43:07 +01:00
|
|
|
playSound(USounds.ENTITY_ARTEFACT_AMBIENT, state.getVolume(), state.getPitch());
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getVerticalOffset(float tickDelta) {
|
2021-02-21 21:17:37 +01:00
|
|
|
return MathHelper.sin((bobAmount + tickDelta) / 10F + positionSeed) * 0.025F + 0.05F;
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public float getRotation(float tickDelta) {
|
2021-02-21 21:17:37 +01:00
|
|
|
return (spinAmount + tickDelta) / 20 + positionSeed;
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
protected void readCustomDataFromNbt(NbtCompound compound) {
|
2023-08-05 16:53:01 +02:00
|
|
|
setStack(ItemStack.fromNbt(compound.getCompound("Item")));
|
2021-02-18 21:30:43 +01:00
|
|
|
setState(State.valueOf(compound.getInt("State")));
|
2023-08-05 16:53:01 +02:00
|
|
|
setSpin(compound.getFloat("spin"));
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
protected void writeCustomDataToNbt(NbtCompound compound) {
|
2021-02-18 21:30:43 +01:00
|
|
|
ItemStack stack = getStack();
|
|
|
|
if (!stack.isEmpty()) {
|
2021-08-04 15:38:03 +02:00
|
|
|
compound.put("Item", stack.writeNbt(new NbtCompound()));
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
compound.putInt("State", getState().ordinal());
|
2023-08-05 16:53:01 +02:00
|
|
|
compound.putFloat("spin", getSpin());
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean damage(DamageSource damageSource, float amount) {
|
|
|
|
if (isInvulnerableTo(damageSource) || !getStack().getItem().damage(damageSource)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduleVelocityUpdate();
|
|
|
|
|
|
|
|
health -= amount;
|
|
|
|
if (health <= 0) {
|
2021-08-04 15:38:03 +02:00
|
|
|
remove(RemovalReason.KILLED);
|
2021-02-18 21:30:43 +01:00
|
|
|
|
|
|
|
ItemStack stack = getStack();
|
|
|
|
|
|
|
|
if (!(stack.getItem() instanceof Artifact) || ((Artifact)stack.getItem()).onArtifactDestroyed(this) != ActionResult.SUCCESS) {
|
|
|
|
dropStack(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-08-31 23:56:43 +02:00
|
|
|
public boolean canHit() {
|
2022-09-01 20:03:54 +02:00
|
|
|
return true;
|
2021-02-18 21:30:43 +01:00
|
|
|
}
|
|
|
|
|
2023-06-02 21:20:30 +02:00
|
|
|
@Override
|
|
|
|
public World asWorld() {
|
2023-06-03 13:40:54 +02:00
|
|
|
return getWorld();
|
2023-06-02 21:20:30 +02:00
|
|
|
}
|
|
|
|
|
2021-02-18 21:30:43 +01:00
|
|
|
public enum State {
|
|
|
|
INITIALISING,
|
|
|
|
RUNNING,
|
|
|
|
SHUTTING_DOWN;
|
|
|
|
|
|
|
|
static final State[] VALUES = values();
|
|
|
|
|
|
|
|
public float getVolume() {
|
|
|
|
return this == SHUTTING_DOWN ? 1 : 0.2F;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getPitch() {
|
|
|
|
return this == INITIALISING ? 1 : this == RUNNING ? 2 : 0.5F;
|
|
|
|
}
|
|
|
|
|
|
|
|
static State valueOf(int state) {
|
|
|
|
return state <= 0 || state >= VALUES.length ? INITIALISING : VALUES[state];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface Artifact {
|
|
|
|
void onArtifactTick(FloatingArtefactEntity entity);
|
|
|
|
|
|
|
|
ActionResult onArtifactDestroyed(FloatingArtefactEntity entity);
|
|
|
|
}
|
|
|
|
}
|