tcr/java/src/game/entity/projectile/EntityDie.java

165 lines
4.1 KiB
Java
Executable file

package game.entity.projectile;
import game.entity.DamageSource;
import game.entity.npc.EntityNPC;
import game.entity.types.EntityLiving;
import game.entity.types.EntityThrowable;
import game.entity.types.IObjectData;
import game.init.Items;
import game.init.SoundEvent;
import game.item.ItemDie;
import game.item.ItemStack;
import game.nbt.NBTTagCompound;
import game.world.HitPosition;
import game.world.World;
public class EntityDie extends EntityThrowable implements IObjectData
{
public int sides;
private boolean noPickup;
public EntityDie(World worldIn)
{
super(worldIn);
this.setSize(0.1f, 0.1f);
}
public EntityDie(World worldIn, EntityLiving throwerIn, int sides, boolean noPickup)
{
super(worldIn, throwerIn);
this.setSize(0.1f, 0.1f);
this.sides = sides;
this.noPickup = noPickup;
}
public EntityDie(World worldIn, double x, double y, double z, int sides)
{
super(worldIn, x, y, z);
this.setSize(0.1f, 0.1f);
this.sides = sides;
}
protected void onImpact(HitPosition pos)
{
if (!this.worldObj.client)
{
this.setValue(this.rand.roll(this.sides <= 0 ? 6 : this.sides));
this.motionX = this.motionY = this.motionZ = 0.0;
}
}
protected void entityInit()
{
super.entityInit();
this.dataWatcher.addObject(16, 0);
}
public void setValue(int value)
{
this.dataWatcher.updateObject(16, value);
}
public int getValue()
{
return this.dataWatcher.getWatchableObjectInt(16);
}
public void writeEntityToNBT(NBTTagCompound tagCompound)
{
super.writeEntityToNBT(tagCompound);
tagCompound.setInteger("Sides", this.sides);
tagCompound.setInteger("Value", this.getValue());
tagCompound.setBoolean("NoPickup", this.noPickup);
}
public void readEntityFromNBT(NBTTagCompound tagCompund)
{
super.readEntityFromNBT(tagCompund);
this.sides = tagCompund.getInteger("Sides");
this.setValue(tagCompund.getInteger("Value"));
this.noPickup = tagCompund.getBoolean("NoPickup");
}
public int getPacketData() {
return this.sides;
}
// public boolean hasSpawnVelocity() {
// return false;
// }
public void onUpdate()
{
if (this.getValue() != 0)
{
this.prevX = this.posX;
this.prevY = this.posY;
this.prevZ = this.posZ;
this.motionY -= 0.03999999910593033D;
this.moveEntity(this.motionX, this.motionY, this.motionZ);
this.motionX *= 0.9800000190734863D;
this.motionY *= 0.9800000190734863D;
this.motionZ *= 0.9800000190734863D;
}
else {
super.onUpdate();
}
}
protected float getVelocity()
{
return 0.3F;
}
public boolean canBeCollidedWith()
{
return this.getValue() != 0;
}
public int getStackMeta() {
int meta = 1;
for(int z = 0; z < ItemDie.DIE_SIDES.length; z++) {
if(this.sides == ItemDie.DIE_SIDES[z])
meta = z;
}
return meta;
}
public ItemStack getStack() {
return new ItemStack(Items.die, 1, this.getStackMeta());
}
public boolean interactFirst(EntityNPC player)
{
if(!this.worldObj.client)
{
if(this.noPickup)
this.setDead();
else if(player.inventory.addItemStackToInventory(this.getStack())) {
this.setDead();
player.worldObj.playSoundAtEntity(player, SoundEvent.POP, 0.2F);
player.inventoryContainer.detectAndSendChanges();
}
}
return true;
}
public boolean attackEntityFrom(DamageSource source, int amount)
{
if(this.isEntityInvulnerable(source))
return false;
if(!this.worldObj.client && !this.dead) {
if(!this.noPickup)
this.entityDropItem(this.getStack(), 0.0f);
this.setDead();
}
return true;
}
public String getDisplayName()
{
if(this.getValue() == 0)
return null;
return ItemDie.DIE_COLORS[this.getStackMeta()] + "" + this.getValue();
}
}