package com.thebrokenrail.reliccraft.block; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.container.Container; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.particle.ParticleTypes; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.ItemScatterer; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockView; import net.minecraft.world.World; import java.util.Random; @SuppressWarnings("deprecation") public abstract class AbstractDragonEggHolderBlock extends Block implements BlockEntityProvider { public static final BooleanProperty ACTIVE = BooleanProperty.of("active"); public AbstractDragonEggHolderBlock(Settings settings) { super(settings); setDefaultState(getStateManager().getDefaultState().with(ACTIVE, false)); } @Override public int getLuminance(BlockState state) { return state.get(ACTIVE) ? 7 : 0; } @Override public BlockEntity createBlockEntity(BlockView view) { return new DragonEggHolderBlockEntity(); } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { Inventory blockEntity = (Inventory) world.getBlockEntity(pos); if (blockEntity != null) { ItemStack stack = player.getStackInHand(hand); if (!stack.isEmpty()) { if (blockEntity.isValidInvStack(0, stack) && blockEntity.getInvStack(0).isEmpty()) { blockEntity.setInvStack(0, stack.split(1)); if (!world.isClient()) { grantAdvancement(player); } return ActionResult.SUCCESS; } else { return ActionResult.PASS; } } else { if (!blockEntity.getInvStack(0).isEmpty()) { player.inventory.offerOrDrop(world, blockEntity.getInvStack(0)); blockEntity.removeInvStack(0); return ActionResult.SUCCESS; } else { return ActionResult.PASS; } } } else { return ActionResult.PASS; } } private boolean isActive(World world, BlockPos pos) { return world.getBlockState(pos).get(ACTIVE); } @Override @Environment(EnvType.CLIENT) public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) { if (isActive(world, pos)) { if (random.nextInt(100) == 0) { world.playSound(null, pos, SoundEvents.BLOCK_PORTAL_AMBIENT, SoundCategory.BLOCKS, 0.5F, random.nextFloat() * 0.4F + 0.8F); } for (int i = 0; i < 24; ++i) { double x = (double) pos.getX() + 0.5D + (double) (0.5F - random.nextFloat()); double y = (double) pos.getY() + 0.5D + (double) (0.5F - random.nextFloat()); double z = (double) pos.getZ() + 0.5D + (double) (0.5F - random.nextFloat()); world.addParticle(ParticleTypes.PORTAL, x, y, z, (random.nextDouble() - 0.5D) * 2.0D, -random.nextDouble(), (random.nextDouble() - 0.5D) * 2.0D); } } } @Override public void onBlockRemoved(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { if (state.getBlock() != newState.getBlock()) { BlockEntity blockEntity = world.getBlockEntity(pos); if (blockEntity instanceof Inventory) { ItemScatterer.spawn(world, pos, (Inventory) blockEntity); } super.onBlockRemoved(state, world, pos, newState, moved); } } @Override protected void appendProperties(StateManager.Builder builder) { builder.add(ACTIVE); } @Override public boolean hasComparatorOutput(BlockState state) { return true; } @Override public int getComparatorOutput(BlockState state, World world, BlockPos pos) { return Container.calculateComparatorOutput(world.getBlockEntity(pos)); } public abstract void tick(World world, BlockPos pos, Inventory inventory); public abstract void grantAdvancement(PlayerEntity player); }