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

133 lines
4.6 KiB
Java
Raw Normal View History

2018-12-22 14:57:19 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.blocks.tiles.TileEntityEndFlower;
import de.ellpeck.naturesaura.reg.ICreativeItem;
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.IModelProvider;
import de.ellpeck.naturesaura.reg.ModRegistry;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BushBlock;
2018-12-22 14:57:19 +01:00
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.SoundType;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.block.Blocks;
2018-12-22 14:57:19 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
2018-12-22 14:57:19 +01:00
import net.minecraftforge.fml.common.registry.GameRegistry;
import javax.annotation.Nullable;
2019-10-20 22:30:49 +02:00
public class BlockEndFlower extends BushBlock implements IModItem, ICreativeItem, IModelProvider, ITileEntityProvider {
2018-12-22 14:57:19 +01:00
public BlockEndFlower() {
this.setHardness(0.5F);
this.setSoundType(SoundType.PLANT);
MinecraftForge.EVENT_BUS.register(this);
ModRegistry.add(this);
}
@SubscribeEvent
public void onDraonTick(LivingUpdateEvent event) {
2019-10-20 22:30:49 +02:00
LivingEntity living = event.getEntityLiving();
if (living.world.isRemote || !(living instanceof EnderDragonEntity))
2018-12-22 14:57:19 +01:00
return;
2019-10-20 22:30:49 +02:00
EnderDragonEntity dragon = (EnderDragonEntity) living;
2018-12-22 14:57:19 +01:00
if (dragon.deathTicks < 150 || dragon.deathTicks % 10 != 0)
return;
for (int i = 0; i < 6; i++) {
int x = dragon.world.rand.nextInt(256) - 128;
int z = dragon.world.rand.nextInt(256) - 128;
BlockPos pos = new BlockPos(x, dragon.world.getHeight(x, z), z);
if (!dragon.world.isBlockLoaded(pos))
continue;
if (dragon.world.getBlockState(pos.down()).getBlock() != Blocks.END_STONE)
continue;
dragon.world.setBlockState(pos, this.getDefaultState());
}
}
@Override
2019-10-20 22:30:49 +02:00
protected boolean canSustainBush(BlockState state) {
2018-12-22 14:57:19 +01:00
return state.getBlock() == Blocks.END_STONE;
}
@Override
public boolean canPlaceBlockAt(World worldIn, BlockPos pos) {
return this.canSustainBush(worldIn.getBlockState(pos.down()));
}
@Override
2019-10-20 22:30:49 +02:00
public boolean canBlockStay(World worldIn, BlockPos pos, BlockState state) {
2018-12-22 14:57:19 +01:00
return this.canSustainBush(worldIn.getBlockState(pos.down()));
}
@Override
public String getBaseName() {
return "end_flower";
}
@Override
public void onPreInit(FMLPreInitializationEvent event) {
}
@Override
public void onInit(FMLInitializationEvent event) {
GameRegistry.registerTileEntity(TileEntityEndFlower.class, new ResourceLocation(NaturesAura.MOD_ID, "end_flower"));
}
@Override
public void onPostInit(FMLPostInitializationEvent event) {
}
@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileEntityEndFlower();
}
@Override
2019-10-20 22:30:49 +02:00
public void breakBlock(World worldIn, BlockPos pos, BlockState state) {
2018-12-22 14:57:19 +01:00
super.breakBlock(worldIn, pos, state);
worldIn.removeTileEntity(pos);
}
@Override
2019-10-20 22:30:49 +02:00
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, BlockState state, int fortune) {
2018-12-22 14:57:19 +01:00
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityEndFlower && ((TileEntityEndFlower) tile).isDrainMode)
return;
super.getDrops(drops, world, pos, state, fortune);
}
@Override
2019-10-20 22:30:49 +02:00
public boolean removedByPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest) {
2018-12-22 14:57:19 +01:00
return willHarvest || super.removedByPlayer(state, world, pos, player, false);
}
@Override
2019-10-20 22:30:49 +02:00
public void harvestBlock(World worldIn, PlayerEntity player, BlockPos pos, BlockState state, @Nullable TileEntity te, ItemStack stack) {
2018-12-22 14:57:19 +01:00
super.harvestBlock(worldIn, player, pos, state, te, stack);
worldIn.setBlockToAir(pos);
}
}