BloodMagic/src/main/java/WayofTime/bloodmagic/tile/TilePhantomBlock.java

79 lines
2.1 KiB
Java
Raw Normal View History

2015-12-27 19:38:12 -05:00
package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.api.Constants;
import lombok.NoArgsConstructor;
import net.minecraft.block.state.IBlockState;
2015-12-27 19:38:12 -05:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
2015-12-27 19:38:12 -05:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2015-12-27 19:38:12 -05:00
@NoArgsConstructor
public class TilePhantomBlock extends TileEntity implements ITickable
{
private int ticksRemaining = 10;
2015-12-27 19:38:12 -05:00
public TilePhantomBlock(int ticksRemaining)
{
this.ticksRemaining = ticksRemaining;
2015-12-27 19:38:12 -05:00
}
@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
2015-12-27 19:38:12 -05:00
super.readFromNBT(tagCompound);
this.ticksRemaining = tagCompound.getInteger(Constants.NBT.TICKS_REMAINING);
2015-12-27 19:38:12 -05:00
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound)
{
2015-12-27 19:38:12 -05:00
super.writeToNBT(tagCompound);
tagCompound.setInteger(Constants.NBT.TICKS_REMAINING, ticksRemaining);
return tagCompound;
2015-12-27 19:38:12 -05:00
}
@Override
public void update()
{
2015-12-27 19:38:12 -05:00
ticksRemaining--;
if (ticksRemaining <= 0)
{
worldObj.setBlockToAir(getPos());
worldObj.removeTileEntity(getPos());
2015-12-27 19:38:12 -05:00
}
}
@Override
public SPacketUpdateTileEntity getUpdatePacket()
{
NBTTagCompound nbt = new NBTTagCompound();
writeToNBT(nbt);
return new SPacketUpdateTileEntity(getPos(), -999, nbt);
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
super.onDataPacket(net, pkt);
readFromNBT(pkt.getNbtCompound());
}
2016-05-28 04:53:19 -07:00
@Override
public NBTTagCompound getUpdateTag()
{
return writeToNBT(new NBTTagCompound());
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState)
{
return oldState.getBlock() != newState.getBlock();
2015-12-27 19:38:12 -05:00
}
}