NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockAncientLeaves.java

171 lines
5.7 KiB
Java
Raw Normal View History

2018-10-13 23:46:30 +02:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.blocks.tiles.TileEntityAncientLeaves;
import de.ellpeck.naturesaura.reg.*;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.BlockPlanks;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class BlockAncientLeaves extends BlockLeaves implements
IModItem, IModelProvider, IColorProvidingBlock, IColorProvidingItem, ITileEntityProvider {
public BlockAncientLeaves() {
this.leavesFancy = true;
ModRegistry.addItemOrBlock(this);
}
@Override
public String getBaseName() {
return "ancient_leaves";
}
@Override
public boolean shouldAddCreative() {
return true;
}
@Override
public void onPreInit(FMLPreInitializationEvent event) {
}
@Override
public void onInit(FMLInitializationEvent event) {
GameRegistry.registerTileEntity(TileEntityAncientLeaves.class, new ResourceLocation(NaturesAura.MOD_ID, "ancient_leaves"));
}
@Override
public void onPostInit(FMLPostInitializationEvent event) {
}
@Override
public Map<ItemStack, ModelVariant> getModelLocations() {
return Collections.singletonMap(new ItemStack(this), new ModelVariant(new ResourceLocation(NaturesAura.MOD_ID, this.getBaseName()), "inventory"));
}
@Override
public List<ItemStack> onSheared(ItemStack item, IBlockAccess world, BlockPos pos, int fortune) {
return Collections.singletonList(new ItemStack(this, 1, 0));
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, CHECK_DECAY, DECAYABLE);
}
@Override
public IBlockState getStateFromMeta(int meta) {
boolean check = (meta & 1) != 0;
boolean decay = (meta & 2) != 0;
return this.getDefaultState().withProperty(CHECK_DECAY, check).withProperty(DECAYABLE, decay);
}
@Override
public int getMetaFromState(IBlockState state) {
boolean check = state.getValue(CHECK_DECAY);
boolean decay = state.getValue(DECAYABLE);
return (check ? 1 : 0) | (decay ? 1 : 0) << 1;
}
@Override
public void beginLeavesDecay(IBlockState state, World world, BlockPos pos) {
if (!state.getValue(CHECK_DECAY) && state.getValue(DECAYABLE)) {
world.setBlockState(pos, state.withProperty(CHECK_DECAY, true), 4);
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
super.breakBlock(worldIn, pos, state);
worldIn.removeTileEntity(pos);
}
@Override
public BlockPlanks.EnumType getWoodType(int meta) {
return null;
}
@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
2018-10-14 14:27:18 +02:00
return (meta & 2) != 0 ? new TileEntityAncientLeaves() : null;
2018-10-13 23:46:30 +02:00
}
@Override
@SideOnly(Side.CLIENT)
public IBlockColor getBlockColor() {
return (state, worldIn, pos, tintIndex) -> 0xE480D9;
}
@Override
@SideOnly(Side.CLIENT)
public IItemColor getItemColor() {
return (stack, tintIndex) -> 0xE480D9;
}
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
super.randomDisplayTick(stateIn, worldIn, pos, rand);
if (rand.nextFloat() >= 0.9F && !worldIn.getBlockState(pos.down()).isFullBlock()) {
2018-10-14 16:12:33 +02:00
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityAncientLeaves) {
if (((TileEntityAncientLeaves) tile).container().getStoredAura() > 0) {
NaturesAura.proxy.spawnMagicParticle(worldIn,
pos.getX() + rand.nextDouble(), pos.getY(), pos.getZ() + rand.nextDouble(),
0F, 0F, 0F, 0xc46df9,
rand.nextFloat() * 2F + 0.5F,
rand.nextInt(100) + 150,
rand.nextFloat() * 0.05F + 0.005F, true, true);
}
}
2018-10-13 23:46:30 +02:00
}
}
@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune) {
return Item.getItemFromBlock(ModBlocks.ANCIENT_SAPLING);
}
2018-10-14 16:12:33 +02:00
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
super.updateTick(worldIn, pos, state, rand);
if (!worldIn.isRemote) {
TileEntity tile = worldIn.getTileEntity(pos);
if (tile instanceof TileEntityAncientLeaves) {
if (((TileEntityAncientLeaves) tile).container().getStoredAura() <= 0) {
worldIn.setBlockState(pos, ModBlocks.DECAYED_LEAVES.getDefaultState());
}
}
}
}
2018-10-13 23:46:30 +02:00
}