2018-12-22 14:57:19 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityEndFlower;
|
2021-12-19 15:32:45 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.ITickableBlockEntity;
|
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.ModBlockEntities;
|
2020-01-29 00:40:28 +01:00
|
|
|
import de.ellpeck.naturesaura.data.BlockStateGenerator;
|
|
|
|
import de.ellpeck.naturesaura.data.ItemModelGenerator;
|
|
|
|
import de.ellpeck.naturesaura.reg.*;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.NonNullList;
|
|
|
|
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.BlockGetter;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.LevelReader;
|
2022-06-27 15:24:04 +02:00
|
|
|
import net.minecraft.world.level.block.*;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
2021-12-19 15:32:45 +01:00
|
|
|
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
2021-12-15 14:26:42 +01:00
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.levelgen.Heightmap;
|
|
|
|
import net.minecraft.world.level.material.FluidState;
|
|
|
|
import net.minecraft.world.level.material.Material;
|
|
|
|
import net.minecraft.world.level.storage.loot.LootContext;
|
|
|
|
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
|
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
2018-12-22 14:57:19 +01:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2022-08-01 16:14:37 +02:00
|
|
|
import net.minecraftforge.event.entity.living.LivingEvent;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
2018-12-22 14:57:19 +01:00
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
2020-01-24 17:05:41 +01:00
|
|
|
import java.util.List;
|
2018-12-22 14:57:19 +01:00
|
|
|
|
2022-08-01 16:14:37 +02:00
|
|
|
public class BlockEndFlower extends BushBlock implements IModItem, ICustomBlockState, ICustomItemModel, EntityBlock {
|
2018-12-22 14:57:19 +01:00
|
|
|
|
2022-06-27 15:24:04 +02:00
|
|
|
protected static final VoxelShape SHAPE = Block.box(5.0D, 0.0D, 5.0D, 11.0D, 10.0D, 11.0D);
|
2020-01-23 16:05:52 +01:00
|
|
|
|
2018-12-22 14:57:19 +01:00
|
|
|
public BlockEndFlower() {
|
2021-12-15 14:26:42 +01:00
|
|
|
super(Properties.of(Material.GRASS).noCollission().strength(0.5F).sound(SoundType.GRASS));
|
2018-12-22 14:57:19 +01:00
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
2022-06-27 15:24:04 +02:00
|
|
|
ModRegistry.ALL_ITEMS.add(this);
|
|
|
|
ModRegistry.ALL_ITEMS.add(new ModTileType<BlockEntityEndFlower>(BlockEntityEndFlower::new, this));
|
2018-12-22 14:57:19 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 16:05:52 +01:00
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public VoxelShape getShape(BlockState state, BlockGetter levelIn, BlockPos pos, CollisionContext context) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var vec3d = state.getOffset(levelIn, pos);
|
2022-06-27 15:24:04 +02:00
|
|
|
return BlockEndFlower.SHAPE.move(vec3d.x, vec3d.y, vec3d.z);
|
2020-01-23 16:05:52 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 14:57:19 +01:00
|
|
|
@SubscribeEvent
|
2022-08-01 16:14:37 +02:00
|
|
|
public void onDragonTick(LivingEvent.LivingTickEvent event) {
|
|
|
|
var living = event.getEntity();
|
2021-12-15 14:26:42 +01:00
|
|
|
if (living.level.isClientSide || !(living instanceof EnderDragon dragon))
|
2018-12-22 14:57:19 +01:00
|
|
|
return;
|
2022-02-11 20:59:31 +01:00
|
|
|
if (dragon.dragonDeathTime < 150 || dragon.dragonDeathTime % 10 != 0)
|
2018-12-22 14:57:19 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = 0; i < 6; i++) {
|
|
|
|
var x = dragon.level.random.nextInt(256) - 128;
|
|
|
|
var z = dragon.level.random.nextInt(256) - 128;
|
|
|
|
var pos = new BlockPos(x, dragon.level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z), z);
|
2021-12-15 14:26:42 +01:00
|
|
|
if (!dragon.level.isLoaded(pos))
|
2018-12-22 14:57:19 +01:00
|
|
|
continue;
|
2021-12-15 14:26:42 +01:00
|
|
|
if (dragon.level.getBlockState(pos.below()).getBlock() != Blocks.END_STONE)
|
2018-12-22 14:57:19 +01:00
|
|
|
continue;
|
2021-12-15 14:26:42 +01:00
|
|
|
dragon.level.setBlockAndUpdate(pos, this.defaultBlockState());
|
2018-12-22 14:57:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public boolean canSurvive(BlockState state, LevelReader levelIn, BlockPos pos) {
|
|
|
|
return levelIn.getBlockState(pos.below()).getBlock() == Blocks.END_STONE;
|
2018-12-22 14:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-23 16:05:52 +01:00
|
|
|
public String getBaseName() {
|
|
|
|
return "end_flower";
|
2018-12-22 14:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
|
|
|
|
return new BlockEntityEndFlower(pos, state);
|
2020-01-23 16:05:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 15:32:45 +01:00
|
|
|
@org.jetbrains.annotations.Nullable
|
|
|
|
@Override
|
|
|
|
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
|
|
|
|
return ITickableBlockEntity.createTickerHelper(type, ModBlockEntities.END_FLOWER);
|
|
|
|
}
|
|
|
|
|
2018-12-22 14:57:19 +01:00
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public boolean onDestroyedByPlayer(BlockState state, Level level, BlockPos pos, Player player, boolean willHarvest, FluidState fluid) {
|
|
|
|
return willHarvest || super.onDestroyedByPlayer(state, level, pos, player, willHarvest, fluid);
|
2018-12-22 14:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 14:26:42 +01:00
|
|
|
public void playerDestroy(Level levelIn, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity te, ItemStack stack) {
|
|
|
|
super.playerDestroy(levelIn, player, pos, state, te, stack);
|
|
|
|
levelIn.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
|
2018-12-22 14:57:19 +01:00
|
|
|
}
|
2020-01-24 17:05:41 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
|
2023-01-28 10:50:13 +01:00
|
|
|
var tile = builder.getOptionalParameter(LootContextParams.BLOCK_ENTITY);
|
|
|
|
if (tile instanceof BlockEntityEndFlower f && f.isDrainMode)
|
2020-01-24 17:05:41 +01:00
|
|
|
return NonNullList.create();
|
|
|
|
return super.getDrops(state, builder);
|
|
|
|
}
|
2020-01-29 00:40:28 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generateCustomBlockState(BlockStateGenerator generator) {
|
2022-08-01 16:14:37 +02:00
|
|
|
generator.simpleBlock(this, generator.models().cross(this.getBaseName(), generator.modLoc("block/" + this.getBaseName())).renderType("cutout"));
|
2020-01-29 00:40:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generateCustomItemModel(ItemModelGenerator generator) {
|
|
|
|
generator.withExistingParent(this.getBaseName(), "item/generated").texture("layer0", "block/" + this.getBaseName());
|
|
|
|
}
|
2018-12-22 14:57:19 +01:00
|
|
|
}
|