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

132 lines
4 KiB
Java
Raw Normal View History

2015-12-27 19:38:12 -05:00
package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.registry.ModBlocks;
import com.google.common.base.Strings;
2015-12-27 19:38:12 -05:00
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
2016-05-28 04:53:19 -07:00
import net.minecraft.network.NetworkManager;
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;
2016-04-24 10:06:28 -07:00
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
2015-12-27 19:38:12 -05:00
import net.minecraft.world.World;
2016-04-24 10:06:28 -07:00
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-12-27 19:38:12 -05:00
public class TileSpectralBlock extends TileEntity implements ITickable
{
2015-12-27 19:38:12 -05:00
private int ticksRemaining;
private String containedBlockName;
private int containedBlockMeta;
public TileSpectralBlock()
{
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);
ticksRemaining = tagCompound.getInteger(Constants.NBT.TICKS_REMAINING);
containedBlockName = tagCompound.getString(Constants.NBT.CONTAINED_BLOCK_NAME);
containedBlockMeta = tagCompound.getInteger(Constants.NBT.CONTAINED_BLOCK_META);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound)
{
2015-12-27 19:38:12 -05:00
super.writeToNBT(tagCompound);
tagCompound.setInteger(Constants.NBT.TICKS_REMAINING, ticksRemaining);
2016-01-11 13:03:22 -08:00
tagCompound.setString(Constants.NBT.CONTAINED_BLOCK_NAME, Strings.isNullOrEmpty(containedBlockName) ? "" : containedBlockName);
2015-12-27 19:38:12 -05:00
tagCompound.setInteger(Constants.NBT.CONTAINED_BLOCK_META, containedBlockMeta);
return tagCompound;
2015-12-27 19:38:12 -05:00
}
2016-05-28 04:53:19 -07:00
@Override
public SPacketUpdateTileEntity getUpdatePacket()
{
NBTTagCompound nbt = new NBTTagCompound();
writeToNBT(nbt);
return new SPacketUpdateTileEntity(getPos(), -999, nbt);
}
@Override
@SideOnly(Side.CLIENT)
2016-05-28 04:53:19 -07:00
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
super.onDataPacket(net, pkt);
readFromNBT(pkt.getNbtCompound());
}
@Override
public NBTTagCompound getUpdateTag()
{
return writeToNBT(new NBTTagCompound());
}
@Override
public void handleUpdateTag(NBTTagCompound tag)
{
readFromNBT(tag);
}
2015-12-27 19:38:12 -05:00
@Override
public void update()
{
if (worldObj.isRemote)
{
return;
}
2015-12-27 19:38:12 -05:00
ticksRemaining--;
if (ticksRemaining <= 0)
{
2015-12-27 19:38:12 -05:00
returnContainedBlock();
}
}
private void setContainedBlockInfo(IBlockState blockState)
{
2016-04-24 10:06:28 -07:00
containedBlockName = blockState.getBlock().getRegistryName().toString();
2015-12-27 19:38:12 -05:00
containedBlockMeta = blockState.getBlock().getMetaFromState(blockState);
}
private void setDuration(int duration)
{
2015-12-27 19:38:12 -05:00
ticksRemaining = duration;
}
public void resetDuration(int reset)
{
if (ticksRemaining < reset)
ticksRemaining = reset;
2015-12-27 19:38:12 -05:00
}
public void returnContainedBlock()
{
2015-12-27 19:38:12 -05:00
Block block = null;
if (!Strings.isNullOrEmpty(containedBlockName))
2016-04-24 10:06:28 -07:00
block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(containedBlockName));
2015-12-27 19:38:12 -05:00
if (block != null && worldObj.setBlockState(pos, block.getStateFromMeta(containedBlockMeta)))
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(getPos()), getWorld().getBlockState(getPos()), 3);
2015-12-27 19:38:12 -05:00
}
public static void createSpectralBlock(World world, BlockPos blockPos, int duration)
{
if (world.isAirBlock(blockPos))
return;
2015-12-27 19:38:12 -05:00
IBlockState cachedState = world.getBlockState(blockPos);
world.setBlockState(blockPos, ModBlocks.spectralBlock.getDefaultState());
TileSpectralBlock tile = (TileSpectralBlock) world.getTileEntity(blockPos);
tile.setContainedBlockInfo(cachedState);
tile.setDuration(duration);
}
}