122 lines
4.2 KiB
Java
122 lines
4.2 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.core.particles.DustParticleOptions;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.util.valueproviders.UniformInt;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.ItemInteractionResult;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.BlockItem;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
|
|
public class RedStoneOreBlock extends Block {
|
|
public static final MapCodec<RedStoneOreBlock> CODEC = simpleCodec(RedStoneOreBlock::new);
|
|
public static final BooleanProperty LIT = RedstoneTorchBlock.LIT;
|
|
|
|
@Override
|
|
public MapCodec<RedStoneOreBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
public RedStoneOreBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState(this.defaultBlockState().setValue(LIT, false));
|
|
}
|
|
|
|
@Override
|
|
protected void attack(BlockState state, Level level, BlockPos pos, Player player) {
|
|
interact(state, level, pos);
|
|
super.attack(state, level, pos, player);
|
|
}
|
|
|
|
@Override
|
|
public void stepOn(Level level, BlockPos pos, BlockState state, Entity entity) {
|
|
if (!entity.isSteppingCarefully()) {
|
|
interact(state, level, pos);
|
|
}
|
|
|
|
super.stepOn(level, pos, state, entity);
|
|
}
|
|
|
|
@Override
|
|
protected ItemInteractionResult useItemOn(
|
|
ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult
|
|
) {
|
|
if (level.isClientSide) {
|
|
spawnParticles(level, pos);
|
|
} else {
|
|
interact(state, level, pos);
|
|
}
|
|
|
|
return stack.getItem() instanceof BlockItem && new BlockPlaceContext(player, hand, stack, hitResult).canPlace()
|
|
? ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
|
|
: ItemInteractionResult.SUCCESS;
|
|
}
|
|
|
|
private static void interact(BlockState state, Level level, BlockPos pos) {
|
|
spawnParticles(level, pos);
|
|
if (!(Boolean)state.getValue(LIT)) {
|
|
level.setBlock(pos, state.setValue(LIT, true), 3);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected boolean isRandomlyTicking(BlockState state) {
|
|
return (Boolean)state.getValue(LIT);
|
|
}
|
|
|
|
@Override
|
|
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
if ((Boolean)state.getValue(LIT)) {
|
|
level.setBlock(pos, state.setValue(LIT, false), 3);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void spawnAfterBreak(BlockState state, ServerLevel level, BlockPos pos, ItemStack stack, boolean dropExperience) {
|
|
super.spawnAfterBreak(state, level, pos, stack, dropExperience);
|
|
if (dropExperience) {
|
|
this.tryDropExperience(level, pos, stack, UniformInt.of(1, 5));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void animateTick(BlockState state, Level level, BlockPos pos, RandomSource random) {
|
|
if ((Boolean)state.getValue(LIT)) {
|
|
spawnParticles(level, pos);
|
|
}
|
|
}
|
|
|
|
private static void spawnParticles(Level level, BlockPos pos) {
|
|
double d = 0.5625;
|
|
RandomSource randomSource = level.random;
|
|
|
|
for (Direction direction : Direction.values()) {
|
|
BlockPos blockPos = pos.relative(direction);
|
|
if (!level.getBlockState(blockPos).isSolidRender(level, blockPos)) {
|
|
Direction.Axis axis = direction.getAxis();
|
|
double e = axis == Direction.Axis.X ? 0.5 + 0.5625 * direction.getStepX() : randomSource.nextFloat();
|
|
double f = axis == Direction.Axis.Y ? 0.5 + 0.5625 * direction.getStepY() : randomSource.nextFloat();
|
|
double g = axis == Direction.Axis.Z ? 0.5 + 0.5625 * direction.getStepZ() : randomSource.nextFloat();
|
|
level.addParticle(DustParticleOptions.REDSTONE, pos.getX() + e, pos.getY() + f, pos.getZ() + g, 0.0, 0.0, 0.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(LIT);
|
|
}
|
|
}
|