Files
Mekanism-Community-Edition/common/mekanism/common/entity/EntityBalloon.java
T

385 lines
9.4 KiB
Java

package mekanism.common.entity;
import java.util.UUID;
import mekanism.api.Coord4D;
import mekanism.api.EnumColor;
import mekanism.api.Pos3D;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.EntityFX;
import net.minecraft.client.particle.EntityReddustFX;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
public class EntityBalloon extends Entity implements IEntityAdditionalSpawnData
{
public EnumColor color = EnumColor.DARK_BLUE;
public Coord4D latched;
public Entity latchedEntity;
/* server-only */
public boolean hasCachedEntity;
public UUID cachedEntityUUID;
public EntityBalloon(World world)
{
super(world);
ignoreFrustumCheck = true;
preventEntitySpawning = true;
setPosition(posX + 0.5F, posY + 3F, posZ + 0.5F);
yOffset = height / 2.0F;
setSize(0.25F, 0.25F);
motionY = 0.04;
dataWatcher.addObject(2, new Byte((byte)0)); /* Is latched */
dataWatcher.addObject(3, new Integer(0)); /* Latched X */
dataWatcher.addObject(4, new Integer(0)); /* Latched Y */
dataWatcher.addObject(5, new Integer(0)); /* Latched Z */
dataWatcher.addObject(6, new Integer(-1)); /* Latched entity ID */
}
public EntityBalloon(World world, double x, double y, double z, EnumColor c)
{
this(world);
setPosition(x + 0.5F, y + 3F, z + 0.5F);
prevPosX = posX;
prevPosY = posY;
prevPosZ = posZ;
color = c;
}
public EntityBalloon(Entity entity, EnumColor c)
{
this(entity.worldObj);
latchedEntity = entity;
setPosition(latchedEntity.posX, (latchedEntity.ySize/2), latchedEntity.posZ);
prevPosX = posX;
prevPosY = posY;
prevPosZ = posZ;
color = c;
dataWatcher.updateObject(2, new Byte((byte)2)); /* Is latched */
dataWatcher.updateObject(3, new Integer(0)); /* Latched X */
dataWatcher.updateObject(4, new Integer(0)); /* Latched Y */
dataWatcher.updateObject(5, new Integer(0)); /* Latched Z */
dataWatcher.updateObject(6, new Integer(entity.entityId)); /* Latched entity ID */
}
public EntityBalloon(World world, Coord4D obj, EnumColor c)
{
this(world);
latched = obj;
setPosition(latched.xCoord + 0.5F, latched.yCoord + 2.8F, latched.zCoord + 0.5F);
prevPosX = posX;
prevPosY = posY;
prevPosZ = posZ;
color = c;
dataWatcher.updateObject(2, new Byte((byte)1)); /* Is latched */
dataWatcher.updateObject(3, new Integer(latched != null ? latched.xCoord : 0)); /* Latched X */
dataWatcher.updateObject(4, new Integer(latched != null ? latched.yCoord : 0)); /* Latched Y */
dataWatcher.updateObject(5, new Integer(latched != null ? latched.zCoord : 0)); /* Latched Z */
dataWatcher.updateObject(6, new Integer(-1)); /* Latched entity ID */
}
@Override
public void onUpdate()
{
prevPosX = posX;
prevPosY = posY;
prevPosZ = posZ;
if(posY > 255)
{
pop();
return;
}
if(worldObj.isRemote)
{
if(dataWatcher.getWatchableObjectByte(2) == 1)
{
latched = new Coord4D(dataWatcher.getWatchableObjectInt(3), dataWatcher.getWatchableObjectInt(4), dataWatcher.getWatchableObjectInt(5), worldObj.provider.dimensionId);
}
else {
latched = null;
}
}
else {
if(hasCachedEntity)
{
findCachedEntity();
cachedEntityUUID = null;
hasCachedEntity = false;
}
if(ticksExisted == 1)
{
dataWatcher.updateObject(2, new Byte(latched != null ? (byte)1 : (latchedEntity != null ? (byte)2 : (byte)0))); /* Is latched */
dataWatcher.updateObject(3, new Integer(latched != null ? latched.xCoord : 0)); /* Latched X */
dataWatcher.updateObject(4, new Integer(latched != null ? latched.yCoord : 0)); /* Latched Y */
dataWatcher.updateObject(5, new Integer(latched != null ? latched.zCoord : 0)); /* Latched Z */
dataWatcher.updateObject(6, new Integer(latchedEntity != null ? latchedEntity.entityId : -1)); /* Latched entity ID */
}
}
if(!worldObj.isRemote)
{
if(latched != null && (latched.exists(worldObj) && latched.isAirBlock(worldObj)))
{
latched = null;
dataWatcher.updateObject(2, (byte)0); /* Is latched */
}
if(latchedEntity != null && (latchedEntity.isDead || !worldObj.loadedEntityList.contains(latchedEntity)))
{
latchedEntity = null;
dataWatcher.updateObject(2, (byte)0); /* Is latched */
}
}
if(!isLatched())
{
motionY = Math.min(motionY*1.02F, 0.2F);
moveEntity(motionX, motionY, motionZ);
motionX *= 0.98;
motionZ *= 0.98;
if(onGround)
{
motionX *= 0.7;
motionZ *= 0.7;
}
if(motionY == 0)
{
motionY = 0.04;
}
}
else if(latched != null)
{
motionX = 0;
motionY = 0;
motionZ = 0;
}
else if(latchedEntity != null)
{
posX = latchedEntity.posX;
posY = latchedEntity.posY + (latchedEntity.ySize/2);
posZ = latchedEntity.posZ;
}
}
private void findCachedEntity()
{
for(Object obj : worldObj.loadedEntityList)
{
if(obj instanceof Entity)
{
Entity entity = (Entity)obj;
if(entity.getUniqueID().equals(cachedEntityUUID))
{
latchedEntity = entity;
}
}
}
}
private void pop()
{
worldObj.playSoundAtEntity(this, "mekanism:etc.Pop", 1, 1);
if(worldObj.isRemote)
{
for(int i = 0; i < 10; i++)
{
try {
Pos3D pos = new Pos3D(posX + (rand.nextFloat()*.6 - 0.3), posY - 0.8 + (rand.nextFloat()*.6 - 0.3), posZ + (rand.nextFloat()*.6 - 0.3));
EntityFX fx = new EntityReddustFX(worldObj, pos.xPos, pos.yPos, pos.zPos, 1, 0, 0, 0);
fx.setRBGColorF(color.getColor(0), color.getColor(1), color.getColor(2));
Minecraft.getMinecraft().effectRenderer.addEffect(fx);
} catch(Exception e) {}
}
}
setDead();
}
@Override
public boolean canBePushed()
{
return latched == null;
}
@Override
public boolean canBeCollidedWith()
{
return !isDead;
}
@Override
protected boolean canTriggerWalking()
{
return false;
}
@Override
protected void entityInit() {}
@Override
protected void readEntityFromNBT(NBTTagCompound nbtTags)
{
color = EnumColor.values()[nbtTags.getInteger("color")];
if(nbtTags.hasKey("latched"))
{
latched = Coord4D.read(nbtTags.getCompoundTag("latched"));
}
if(nbtTags.hasKey("idMost"))
{
hasCachedEntity = true;
cachedEntityUUID = new UUID(nbtTags.getLong("idMost"), nbtTags.getLong("idLeast"));
}
}
@Override
protected void writeEntityToNBT(NBTTagCompound nbtTags)
{
nbtTags.setInteger("color", color.ordinal());
if(latched != null)
{
nbtTags.setCompoundTag("latched", latched.write(new NBTTagCompound()));
}
if(latchedEntity != null)
{
nbtTags.setLong("idMost", latchedEntity.getUniqueID().getMostSignificantBits());
nbtTags.setLong("idLeast", latchedEntity.getUniqueID().getLeastSignificantBits());
}
}
@Override
public boolean hitByEntity(Entity entity)
{
pop();
return true;
}
@Override
public void writeSpawnData(ByteArrayDataOutput data)
{
data.writeDouble(posX);
data.writeDouble(posY);
data.writeDouble(posZ);
data.writeInt(color.ordinal());
if(latched != null)
{
data.writeByte((byte)1);
data.writeInt(latched.xCoord);
data.writeInt(latched.yCoord);
data.writeInt(latched.zCoord);
data.writeInt(latched.dimensionId);
}
else if(latchedEntity != null)
{
data.writeByte((byte)2);
data.writeInt(latchedEntity.entityId);
}
else {
data.writeByte((byte)0);
}
}
@Override
public void readSpawnData(ByteArrayDataInput data)
{
setPosition(data.readDouble(), data.readDouble(), data.readDouble());
color = EnumColor.values()[data.readInt()];
byte type = data.readByte();
if(type == 1)
{
latched = Coord4D.read(data);
}
else if(type == 2)
{
latchedEntity = worldObj.getEntityByID(data.readInt());
}
else {
latched = null;
}
}
@Override
public void setDead()
{
super.setDead();
if(latchedEntity != null)
{
latchedEntity.isAirBorne = false;
}
}
@Override
public boolean isInRangeToRenderDist(double dist)
{
return dist <= 64;
}
@Override
public boolean isInRangeToRenderVec3D(Vec3 par1Vec3)
{
return true;
}
public boolean isLatched()
{
if(!worldObj.isRemote)
{
return latched != null || latchedEntity != null;
}
else {
return dataWatcher.getWatchableObjectByte(2) > 0;
}
}
public boolean isLatchedToEntity()
{
return dataWatcher.getWatchableObjectByte(2) == 2 && latchedEntity != null;
}
}