This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
RelicCraft/src/main/java/com/thebrokenrail/reliccraft/block/AbstractDragonEggHolderBloc...

126 lines
4.7 KiB
Java

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.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.screen.ScreenHandler;
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.lightLevel(state -> state.get(ACTIVE) ? 7 : 0));
setDefaultState(getStateManager().getDefaultState().with(ACTIVE, false));
}
@Override
public BlockEntity createBlockEntity(BlockView view) {
return new DragonEggHolderBlockEntity();
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity user, Hand hand, BlockHitResult hit) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof Inventory) {
Inventory inventory = (Inventory) blockEntity;
ItemStack stack = user.getStackInHand(hand);
if (inventory.isValid(0, stack) && inventory.getStack(0).isEmpty()) {
inventory.setStack(0, stack.split(1));
if (!world.isClient()) {
grantAdvancement(user);
}
return ActionResult.SUCCESS;
} else if (stack.isEmpty()) {
if (!inventory.getStack(0).isEmpty()) {
user.inventory.offerOrDrop(world, inventory.getStack(0));
inventory.removeStack(0);
return ActionResult.SUCCESS;
} else {
return ActionResult.PASS;
}
} else {
return ActionResult.PASS;
}
} else {
return ActionResult.FAIL;
}
}
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((double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, SoundEvents.BLOCK_PORTAL_AMBIENT, SoundCategory.BLOCKS, 0.5F, random.nextFloat() * 0.4F + 0.8F, false);
}
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 onStateReplaced(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.onStateReplaced(state, world, pos, newState, moved);
}
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(ACTIVE);
}
@Override
public boolean hasComparatorOutput(BlockState state) {
return true;
}
@Override
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos));
}
public abstract void tick(World world, BlockPos pos);
public abstract void grantAdvancement(PlayerEntity player);
}