Fixed spellbooks despawning

This commit is contained in:
Sollace 2019-02-10 22:37:01 +02:00
parent 2a6497914b
commit a687b0b8d5

View file

@ -30,6 +30,7 @@ public class EntitySpellbook extends EntityLiving implements IMagicals {
public EntitySpellbook(World worldIn) { public EntitySpellbook(World worldIn) {
super(worldIn); super(worldIn);
setSize(0.6f, 0.6f); setSize(0.6f, 0.6f);
enablePersistence();
} }
@Override @Override
@ -77,15 +78,21 @@ public class EntitySpellbook extends EntityLiving implements IMagicals {
this.isJumping = open && isInWater(); this.isJumping = open && isInWater();
super.onUpdate(); super.onUpdate();
if (open && world.isRemote) { if (open && world.isRemote) {
for (int offX = -2; offX <= 1; ++offX) { for (int offX = -2; offX <= 1; ++offX) {
for (int offZ = -2; offZ <= 1; ++offZ) { for (int offZ = -2; offZ <= 1; ++offZ) {
if (offX > -1 && offX < 1 && offZ == -1) offZ = 1; if (offX > -1 && offX < 1 && offZ == -1) {
offZ = 1;
}
if (rand.nextInt(320) == 0) { if (rand.nextInt(320) == 0) {
for (int offY = 0; offY <= 1; ++offY) { for (int offY = 0; offY <= 1; ++offY) {
float vX = (float)offX/2 + rand.nextFloat(); world.spawnParticle(EnumParticleTypes.ENCHANTMENT_TABLE,
float vY = (float)offY/2 - rand.nextFloat() + 0.5f; posX, posY, posZ,
float vZ = (float)offZ/2 + rand.nextFloat(); offX/2F + rand.nextFloat(),
world.spawnParticle(EnumParticleTypes.ENCHANTMENT_TABLE, posX, posY, posZ, vX, vY, vZ, new int[0]); offY/2F - rand.nextFloat() + 0.5f,
offZ/2F + rand.nextFloat()
);
} }
} }
} }
@ -94,13 +101,18 @@ public class EntitySpellbook extends EntityLiving implements IMagicals {
if (world.rand.nextInt(30) == 0) { if (world.rand.nextInt(30) == 0) {
float celest = world.getCelestialAngle(1) * 4; float celest = world.getCelestialAngle(1) * 4;
boolean isDay = celest > 3 || celest < 1; boolean isDay = celest > 3 || celest < 1;
Boolean userState = getUserSetState(); Boolean userState = getUserSetState();
boolean canToggle = (isDay != open) && (userState == null || userState == isDay); boolean canToggle = (isDay != open) && (userState == null || userState == isDay);
if (canToggle) { if (canToggle) {
setUserSetState(null); setUserSetState(null);
setIsOpen(isDay); setIsOpen(isDay);
} }
if (userState != null && (isDay == open) && (userState == open)) { if (userState != null && (isDay == open) && (userState == open)) {
setUserSetState(null); setUserSetState(null);
} }
@ -111,8 +123,11 @@ public class EntitySpellbook extends EntityLiving implements IMagicals {
public boolean attackEntityFrom(DamageSource source, float amount) { public boolean attackEntityFrom(DamageSource source, float amount) {
if (!world.isRemote) { if (!world.isRemote) {
setDead(); setDead();
SoundType sound = SoundType.WOOD; SoundType sound = SoundType.WOOD;
world.playSound(posX, posY, posZ, sound.getBreakSound(), SoundCategory.BLOCKS, sound.getVolume(), sound.getPitch(), true); world.playSound(posX, posY, posZ, sound.getBreakSound(), SoundCategory.BLOCKS, sound.getVolume(), sound.getPitch(), true);
if (world.getGameRules().getBoolean("doTileDrops")) { if (world.getGameRules().getBoolean("doTileDrops")) {
entityDropItem(new ItemStack(UItems.spellbook), 0); entityDropItem(new ItemStack(UItems.spellbook), 0);
} }
@ -146,19 +161,18 @@ public class EntitySpellbook extends EntityLiving implements IMagicals {
@Override @Override
public void readEntityFromNBT(NBTTagCompound compound) { public void readEntityFromNBT(NBTTagCompound compound) {
super.readEntityFromNBT(compound); super.readEntityFromNBT(compound);
setIsOpen(compound.getBoolean("open")); setIsOpen(compound.getBoolean("open"));
if (compound.hasKey("force_open")) { setUserSetState(compound.hasKey("force_open") ? compound.getBoolean("force_open") : null);
setUserSetState(compound.getBoolean("force_open"));
} else {
setUserSetState(null);
}
} }
@Override @Override
public void writeEntityToNBT(NBTTagCompound compound) { public void writeEntityToNBT(NBTTagCompound compound) {
super.writeEntityToNBT(compound); super.writeEntityToNBT(compound);
compound.setBoolean("open", getIsOpen()); compound.setBoolean("open", getIsOpen());
Boolean state = getUserSetState(); Boolean state = getUserSetState();
if (state != null) { if (state != null) {
compound.setBoolean("force_open", state); compound.setBoolean("force_open", state);
} }