2015-12-27 19:38:12 -05:00
|
|
|
package WayofTime.bloodmagic.tile;
|
|
|
|
|
2018-02-15 18:49:01 -08:00
|
|
|
import WayofTime.bloodmagic.util.Constants;
|
2017-08-15 18:14:28 -07:00
|
|
|
import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
|
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
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class TileSpectralBlock extends TileTicking {
|
2015-12-27 19:38:12 -05:00
|
|
|
private int ticksRemaining;
|
|
|
|
private String containedBlockName;
|
|
|
|
private int containedBlockMeta;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public TileSpectralBlock() {
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
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
|
2017-08-15 21:30:48 -07:00
|
|
|
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);
|
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
|
2017-08-15 21:30:48 -07:00
|
|
|
public void onUpdate() {
|
|
|
|
if (getWorld().isRemote) {
|
2016-06-17 10:00:33 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-27 19:38:12 -05:00
|
|
|
ticksRemaining--;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (ticksRemaining <= 0) {
|
2015-12-27 19:38:12 -05:00
|
|
|
returnContainedBlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07: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);
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
private void setDuration(int duration) {
|
2015-12-27 19:38:12 -05:00
|
|
|
ticksRemaining = duration;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public void resetDuration(int reset) {
|
2015-12-30 15:34:40 -05:00
|
|
|
if (ticksRemaining < reset)
|
|
|
|
ticksRemaining = reset;
|
2015-12-27 19:38:12 -05:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07: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
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
if (block != null && getWorld().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
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public static void createSpectralBlock(World world, BlockPos blockPos, int duration) {
|
2015-12-30 15:34:40 -05:00
|
|
|
if (world.isAirBlock(blockPos))
|
|
|
|
return;
|
2015-12-27 19:38:12 -05:00
|
|
|
IBlockState cachedState = world.getBlockState(blockPos);
|
2017-08-14 20:53:42 -07:00
|
|
|
world.setBlockState(blockPos, RegistrarBloodMagicBlocks.SPECTRAL.getDefaultState());
|
2015-12-27 19:38:12 -05:00
|
|
|
TileSpectralBlock tile = (TileSpectralBlock) world.getTileEntity(blockPos);
|
|
|
|
tile.setContainedBlockInfo(cachedState);
|
|
|
|
tile.setDuration(duration);
|
|
|
|
}
|
|
|
|
}
|