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

126 lines
5.3 KiB
Java
Raw Normal View History

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.*;
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.*;
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;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.material.FluidState;
2023-07-08 12:32:27 +02:00
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.living.LivingEvent;
import net.neoforged.bus.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
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() {
2023-07-08 12:32:27 +02:00
super(Properties.of().noCollission().strength(0.5F).sound(SoundType.GRASS));
2024-02-03 14:56:07 +01:00
NeoForge.EVENT_BUS.register(this);
2022-06-27 15:24:04 +02:00
ModRegistry.ALL_ITEMS.add(this);
2023-02-15 23:45:50 +01:00
ModRegistry.ALL_ITEMS.add(new ModTileType<>(BlockEntityEndFlower::new, this));
2018-12-22 14:57:19 +01:00
}
2020-01-23 16:05:52 +01:00
@Override
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
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
public void onDragonTick(LivingEvent.LivingTickEvent event) {
var living = event.getEntity();
2023-07-08 12:32:27 +02:00
if (living.level().isClientSide || !(living instanceof EnderDragon dragon))
2018-12-22 14:57:19 +01:00
return;
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++) {
2023-07-08 12:32:27 +02:00
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);
if (!dragon.level().isLoaded(pos))
2018-12-22 14:57:19 +01:00
continue;
2023-07-08 12:32:27 +02:00
if (dragon.level().getBlockState(pos.below()).getBlock() != Blocks.END_STONE)
2018-12-22 14:57:19 +01:00
continue;
2023-07-08 12:32:27 +02:00
dragon.level().setBlockAndUpdate(pos, this.defaultBlockState());
2018-12-22 14:57:19 +01:00
}
}
@Override
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
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
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
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
2023-07-08 12:32:27 +02:00
2020-01-24 17:05:41 +01:00
@Override
2023-02-15 23:45:50 +01:00
@SuppressWarnings("deprecation")
2023-07-08 12:32:27 +02:00
public List<ItemStack> getDrops(BlockState state, LootParams.Builder builder) {
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) {
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
}