2015-12-27 19:38:12 -05:00
|
|
|
package WayofTime.bloodmagic.tile;
|
|
|
|
|
2016-03-17 13:00:44 -07:00
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
|
|
|
import WayofTime.bloodmagic.registry.ModBlocks;
|
2016-09-07 17:46:06 -07:00
|
|
|
import WayofTime.bloodmagic.tile.base.TileTicking;
|
2016-03-17 13:00:44 -07:00
|
|
|
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;
|
2016-03-17 13:00:44 -07:00
|
|
|
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
|
|
|
|
2016-09-07 17:46:06 -07:00
|
|
|
public class TileSpectralBlock extends TileTicking
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2015-12-27 19:38:12 -05:00
|
|
|
private int ticksRemaining;
|
|
|
|
private String containedBlockName;
|
|
|
|
private int containedBlockMeta;
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public TileSpectralBlock()
|
|
|
|
{
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-07 17:46:06 -07:00
|
|
|
public void deserialize(NBTTagCompound tagCompound)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
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
|
2016-09-07 17:46:06 -07:00
|
|
|
public NBTTagCompound serialize(NBTTagCompound tagCompound)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
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);
|
2016-05-19 17:43:33 -07:00
|
|
|
return tagCompound;
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
|
2016-05-28 04:53:19 -07:00
|
|
|
@Override
|
2016-09-07 17:46:06 -07:00
|
|
|
public void onUpdate()
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2016-06-17 10:00:33 -04:00
|
|
|
if (worldObj.isRemote)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-27 19:38:12 -05:00
|
|
|
ticksRemaining--;
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (ticksRemaining <= 0)
|
|
|
|
{
|
2015-12-27 19:38:12 -05:00
|
|
|
returnContainedBlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
private void setDuration(int duration)
|
|
|
|
{
|
2015-12-27 19:38:12 -05:00
|
|
|
ticksRemaining = duration;
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public void resetDuration(int reset)
|
|
|
|
{
|
|
|
|
if (ticksRemaining < reset)
|
|
|
|
ticksRemaining = reset;
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -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)))
|
2016-03-18 13:05:57 -07:00
|
|
|
getWorld().notifyBlockUpdate(getPos(), getWorld().getBlockState(getPos()), getWorld().getBlockState(getPos()), 3);
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -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);
|
|
|
|
}
|
|
|
|
}
|