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

85 lines
3 KiB
Java
Raw Normal View History

2015-12-27 19:38:12 -05:00
package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.util.Constants;
import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
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
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);
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) {
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) {
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)))
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) {
if (world.isAirBlock(blockPos))
return;
2015-12-27 19:38:12 -05:00
IBlockState cachedState = world.getBlockState(blockPos);
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);
}
}