Unicopia/src/main/java/com/minelittlepony/unicopia/entity/RainbowEntity.java

188 lines
5.2 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
2019-02-04 14:25:12 +01:00
import com.minelittlepony.unicopia.Race;
2020-01-16 12:35:46 +01:00
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.packet.EntitySpawnGlobalS2CPacket;
import net.minecraft.entity.Entity;
2020-01-17 14:27:26 +01:00
import net.minecraft.entity.EntityType;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.LightningEntity;
2020-01-17 14:27:26 +01:00
import net.minecraft.entity.SpawnType;
2020-01-16 12:35:46 +01:00
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.NetworkThreadUtils;
import net.minecraft.network.Packet;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.Box;
2019-02-04 14:25:12 +01:00
import net.minecraft.util.math.MathHelper;
2020-01-17 14:27:26 +01:00
import net.minecraft.world.IWorld;
2019-02-04 14:25:12 +01:00
import net.minecraft.world.World;
2020-01-17 14:27:26 +01:00
import net.minecraft.world.biome.Biome.SpawnEntry;
2019-02-04 14:25:12 +01:00
public class RainbowEntity extends Entity implements InAnimate {
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
public static final SpawnEntry SPAWN_ENTRY = new SpawnEntry(UEntities.RAINBOW_SPAWNER, 1, 1, 1);
2019-02-04 14:25:12 +01:00
private int ticksAlive;
private double radius;
public static final int RAINBOW_MAX_SIZE = 180;
public static final int RAINBOW_MIN_SIZE = 50;
2020-01-16 12:35:46 +01:00
public static final Box SPAWN_COLLISSION_RADIUS = new Box(
-RAINBOW_MAX_SIZE, -RAINBOW_MAX_SIZE, -RAINBOW_MAX_SIZE,
RAINBOW_MAX_SIZE, RAINBOW_MAX_SIZE, RAINBOW_MAX_SIZE
2020-01-17 14:27:26 +01:00
).expand(RAINBOW_MAX_SIZE);
2020-01-16 12:35:46 +01:00
2020-01-17 14:27:26 +01:00
public RainbowEntity(EntityType<RainbowEntity> type, World world) {
super(type, world);
2020-01-16 12:35:46 +01:00
2020-01-17 14:27:26 +01:00
float yaw = (int)MathHelper.nextDouble(random, 0, 360);
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
setPositionAndAngles(0, 0, 0, yaw, 0);
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
radius = MathHelper.nextDouble(random, RAINBOW_MIN_SIZE, RAINBOW_MAX_SIZE);
2019-02-04 14:25:12 +01:00
ticksAlive = 10000;
2020-01-17 14:27:26 +01:00
ignoreCameraFrustum = true;
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
//width = (float)radius;
//height = width;
}
@Override
public boolean canInteract(Race race) {
return false;
}
2019-02-04 14:25:12 +01:00
@Override
public void setPosition(double x, double y, double z) {
2020-01-17 14:27:26 +01:00
this.x = x;
this.y = y;
this.z = z;
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
float width = getDimensions(getPose()).width;
2020-01-16 12:35:46 +01:00
setBoundingBox(new Box(
2019-02-04 14:25:12 +01:00
x - width, y - radius/2, z,
x + width, y + radius/2, z
));
}
@Override
public SoundCategory getSoundCategory() {
return SoundCategory.WEATHER;
}
@Override
2020-01-17 14:27:26 +01:00
public boolean shouldRenderAtDistance(double distance) {
2019-02-04 14:25:12 +01:00
return true;
}
public double getRadius() {
return radius;
}
@Override
2020-01-16 12:35:46 +01:00
public void tick() {
super.tick();
2019-02-04 14:25:12 +01:00
if (ticksAlive-- <= 0) {
2020-01-16 12:35:46 +01:00
remove();
2019-02-04 14:25:12 +01:00
}
2020-01-16 12:35:46 +01:00
if (!removed) {
2020-01-17 14:27:26 +01:00
Box bounds = SPAWN_COLLISSION_RADIUS.offset(getPos());
2020-01-17 14:27:26 +01:00
world.getEntities(RainbowEntity.class, bounds).forEach(this::attackCompetitor);
world.getEntities(RainbowEntity.Spawner.class, bounds).forEach(this::attackCompetitor);
}
}
private void attackCompetitor(Entity other) {
if (other != this) {
2020-01-16 12:35:46 +01:00
other.remove();
}
2019-02-04 14:25:12 +01:00
}
@Override
2020-01-16 12:35:46 +01:00
protected void initDataTracker() {
}
@Override
protected void readCustomDataFromTag(CompoundTag var1) {
2019-02-04 14:25:12 +01:00
}
@Override
2020-01-16 12:35:46 +01:00
protected void writeCustomDataToTag(CompoundTag var1) {
2019-02-04 14:25:12 +01:00
}
@Override
2020-01-16 12:35:46 +01:00
public Packet<?> createSpawnPacket() {
return new SpawnPacket(this);
2019-02-04 14:25:12 +01:00
}
2020-01-16 12:35:46 +01:00
public static class Spawner extends MobEntity {
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
public Spawner(EntityType<Spawner> type, World world) {
super(type, world);
2019-02-04 14:25:12 +01:00
this.setInvisible(true);
}
@Override
2020-01-17 14:27:26 +01:00
public boolean canSpawn(IWorld world, SpawnType type) {
2020-01-16 12:35:46 +01:00
Box bounds = SPAWN_COLLISSION_RADIUS.offset(getPos());
2019-02-04 14:25:12 +01:00
2020-01-17 14:27:26 +01:00
return super.canSpawn(world, type)
&& world.getEntities(RainbowEntity.class, bounds).isEmpty()
&& world.getEntities(RainbowEntity.Spawner.class, bounds).isEmpty();
2019-02-04 14:25:12 +01:00
}
@Override
2020-01-17 14:27:26 +01:00
public int getLimitPerChunk() {
2019-02-04 14:25:12 +01:00
return 1;
}
@Override
2020-01-16 12:35:46 +01:00
public void tick() {
super.tick();
if (!this.dead) {
2020-01-16 12:35:46 +01:00
remove();
trySpawnRainbow();
}
}
2019-02-04 14:25:12 +01:00
public void trySpawnRainbow() {
2020-01-17 14:27:26 +01:00
RainbowEntity rainbow = UEntities.RAINBOW.create(world);
2020-01-16 12:35:46 +01:00
rainbow.setPosition(x, y, z);
2019-02-04 14:25:12 +01:00
world.spawnEntity(rainbow);
}
}
2020-01-16 12:35:46 +01:00
static class SpawnPacket extends EntitySpawnGlobalS2CPacket {
2020-01-17 14:27:26 +01:00
public SpawnPacket(RainbowEntity entity) {
2020-01-16 12:35:46 +01:00
super(entity);
}
@Override
public void method_11188(ClientPlayPacketListener listener) {
// TODO: Packet needs to be registered, and handling separated
MinecraftClient client = MinecraftClient.getInstance();
NetworkThreadUtils.forceMainThread(this, listener, client);
double x = getX();
double y = getY();
double z = getZ();
LightningEntity entity = new LightningEntity(client.world, x, y, z, false);
entity.updateTrackedPosition(x, y, z);
entity.yaw = 0;
entity.pitch = 0;
entity.setEntityId(getId());
client.world.addLightning(entity);
}
}
2019-02-04 14:25:12 +01:00
}