2020-04-04 02:32:34 +00:00
|
|
|
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
|
2020-04-15 22:19:40 +00:00
|
|
|
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity user, Hand hand, BlockHitResult hit) {
|
2020-04-09 15:34:33 +00:00
|
|
|
BlockEntity blockEntity = world.getBlockEntity(pos);
|
2020-04-04 02:32:34 +00:00
|
|
|
|
2020-04-09 15:34:33 +00:00
|
|
|
if (blockEntity instanceof Inventory) {
|
|
|
|
Inventory inventory = (Inventory) blockEntity;
|
2020-04-15 22:19:40 +00:00
|
|
|
ItemStack stack = user.getStackInHand(hand);
|
2020-04-04 02:32:34 +00:00
|
|
|
|
2020-04-15 22:19:40 +00:00
|
|
|
if (inventory.isValidInvStack(0, stack) && inventory.getInvStack(0).isEmpty()) {
|
2020-04-16 16:17:12 +00:00
|
|
|
inventory.setInvStack(0, stack.split(1));
|
2020-04-15 22:19:40 +00:00
|
|
|
|
|
|
|
if (!world.isClient()) {
|
|
|
|
grantAdvancement(user);
|
|
|
|
}
|
|
|
|
return ActionResult.SUCCESS;
|
2020-04-15 22:32:03 +00:00
|
|
|
} else if (stack.isEmpty()) {
|
2020-04-09 15:34:33 +00:00
|
|
|
if (!inventory.getInvStack(0).isEmpty()) {
|
2020-04-15 22:32:03 +00:00
|
|
|
user.inventory.offerOrDrop(world, inventory.getInvStack(0));
|
2020-04-15 22:19:40 +00:00
|
|
|
|
2020-04-09 15:34:33 +00:00
|
|
|
inventory.removeInvStack(0);
|
2020-04-04 02:32:34 +00:00
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
} else {
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
2020-04-15 22:32:03 +00:00
|
|
|
} else {
|
|
|
|
return ActionResult.PASS;
|
2020-04-04 02:32:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-09 15:34:33 +00:00
|
|
|
return ActionResult.FAIL;
|
2020-04-04 02:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-04-06 17:20:15 +00:00
|
|
|
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);
|
2020-04-04 02:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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<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 Container.calculateComparatorOutput(world.getBlockEntity(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void tick(World world, BlockPos pos, Inventory inventory);
|
|
|
|
|
|
|
|
public abstract void grantAdvancement(PlayerEntity player);
|
|
|
|
}
|