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

97 lines
3.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 WayofTime.bloodmagic.registry.ModBlocks;
import WayofTime.bloodmagic.tile.base.TileTicking;
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-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;
2015-12-27 19:38:12 -05:00
public class TileSpectralBlock extends TileTicking
{
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 deserialize(NBTTagCompound tagCompound)
{
2015-12-27 19:38:12 -05:00
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 serialize(NBTTagCompound tagCompound)
{
2015-12-27 19:38:12 -05:00
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 void onUpdate()
{
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);
2016-09-10 16:13:20 -07:00
world.setBlockState(blockPos, ModBlocks.SPECTRAL_BLOCK.getDefaultState());
2015-12-27 19:38:12 -05:00
TileSpectralBlock tile = (TileSpectralBlock) world.getTileEntity(blockPos);
tile.setContainedBlockInfo(cachedState);
tile.setDuration(duration);
}
}