minecraft-source/src/net/minecraft/world/level/block/CommandBlock.java

236 lines
9.8 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import org.apache.logging.log4j.LogManager;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.BaseCommandBlock;
import net.minecraft.util.StringUtil;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.CommandBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import org.apache.logging.log4j.Logger;
public class CommandBlock extends BaseEntityBlock {
private static final Logger LOGGER;
public static final DirectionProperty FACING;
public static final BooleanProperty CONDITIONAL;
public CommandBlock(final Properties c) {
super(c);
this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)CommandBlock.FACING, Direction.NORTH)).<Comparable, Boolean>setValue((Property<Comparable>)CommandBlock.CONDITIONAL, false));
}
@Override
public BlockEntity newBlockEntity(final BlockGetter bjd) {
final CommandBlockEntity bwn3 = new CommandBlockEntity();
bwn3.setAutomatic(this == Blocks.CHAIN_COMMAND_BLOCK);
return bwn3;
}
@Override
public void neighborChanged(final BlockState byg, final Level bjt, final BlockPos fk3, final Block bpe, final BlockPos fk5, final boolean boolean6) {
if (bjt.isClientSide) {
return;
}
final BlockEntity bwi8 = bjt.getBlockEntity(fk3);
if (!(bwi8 instanceof CommandBlockEntity)) {
return;
}
final CommandBlockEntity bwn9 = (CommandBlockEntity)bwi8;
final boolean boolean7 = bjt.hasNeighborSignal(fk3);
final boolean boolean8 = bwn9.isPowered();
bwn9.setPowered(boolean7);
if (boolean8 || bwn9.isAutomatic() || bwn9.getMode() == CommandBlockEntity.Mode.SEQUENCE) {
return;
}
if (boolean7) {
bwn9.markConditionMet();
bjt.getBlockTicks().scheduleTick(fk3, this, this.getTickDelay(bjt));
}
}
@Override
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
final BlockEntity bwi6 = xd.getBlockEntity(fk);
if (bwi6 instanceof CommandBlockEntity) {
final CommandBlockEntity bwn7 = (CommandBlockEntity)bwi6;
final BaseCommandBlock biz8 = bwn7.getCommandBlock();
final boolean boolean9 = !StringUtil.isNullOrEmpty(biz8.getCommand());
final CommandBlockEntity.Mode a10 = bwn7.getMode();
final boolean boolean10 = bwn7.wasConditionMet();
if (a10 == CommandBlockEntity.Mode.AUTO) {
bwn7.markConditionMet();
if (boolean10) {
this.execute(byg, xd, fk, biz8, boolean9);
}
else if (bwn7.isConditional()) {
biz8.setSuccessCount(0);
}
if (bwn7.isPowered() || bwn7.isAutomatic()) {
xd.getBlockTicks().scheduleTick(fk, this, this.getTickDelay(xd));
}
}
else if (a10 == CommandBlockEntity.Mode.REDSTONE) {
if (boolean10) {
this.execute(byg, xd, fk, biz8, boolean9);
}
else if (bwn7.isConditional()) {
biz8.setSuccessCount(0);
}
}
xd.updateNeighbourForOutputSignal(fk, this);
}
}
private void execute(final BlockState byg, final Level bjt, final BlockPos fk, final BaseCommandBlock biz, final boolean boolean5) {
if (boolean5) {
biz.performCommand(bjt);
}
else {
biz.setSuccessCount(0);
}
executeChain(bjt, fk, byg.<Direction>getValue((Property<Direction>)CommandBlock.FACING));
}
@Override
public int getTickDelay(final LevelReader bjw) {
return 1;
}
@Override
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
final BlockEntity bwi8 = bjt.getBlockEntity(fk);
if (bwi8 instanceof CommandBlockEntity && ayg.canUseGameMasterBlocks()) {
ayg.openCommandBlock((CommandBlockEntity)bwi8);
return InteractionResult.SUCCESS;
}
return InteractionResult.PASS;
}
@Override
public boolean hasAnalogOutputSignal(final BlockState byg) {
return true;
}
@Override
public int getAnalogOutputSignal(final BlockState byg, final Level bjt, final BlockPos fk) {
final BlockEntity bwi5 = bjt.getBlockEntity(fk);
if (bwi5 instanceof CommandBlockEntity) {
return ((CommandBlockEntity)bwi5).getCommandBlock().getSuccessCount();
}
return 0;
}
@Override
public void setPlacedBy(final Level bjt, final BlockPos fk, final BlockState byg, final LivingEntity akw, final ItemStack bek) {
final BlockEntity bwi7 = bjt.getBlockEntity(fk);
if (!(bwi7 instanceof CommandBlockEntity)) {
return;
}
final CommandBlockEntity bwn8 = (CommandBlockEntity)bwi7;
final BaseCommandBlock biz9 = bwn8.getCommandBlock();
if (bek.hasCustomHoverName()) {
biz9.setName(bek.getHoverName());
}
if (!bjt.isClientSide) {
if (bek.getTagElement("BlockEntityTag") == null) {
biz9.setTrackOutput(bjt.getGameRules().getBoolean(GameRules.RULE_SENDCOMMANDFEEDBACK));
bwn8.setAutomatic(this == Blocks.CHAIN_COMMAND_BLOCK);
}
if (bwn8.getMode() == CommandBlockEntity.Mode.SEQUENCE) {
final boolean boolean10 = bjt.hasNeighborSignal(fk);
bwn8.setPowered(boolean10);
}
}
}
@Override
public RenderShape getRenderShape(final BlockState byg) {
return RenderShape.MODEL;
}
@Override
public BlockState rotate(final BlockState byg, final Rotation btr) {
return ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Direction>setValue((Property<Comparable>)CommandBlock.FACING, btr.rotate(byg.<Direction>getValue((Property<Direction>)CommandBlock.FACING)));
}
@Override
public BlockState mirror(final BlockState byg, final Mirror bsr) {
return byg.rotate(bsr.getRotation(byg.<Direction>getValue((Property<Direction>)CommandBlock.FACING)));
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(CommandBlock.FACING, CommandBlock.CONDITIONAL);
}
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)CommandBlock.FACING, bcn.getNearestLookingDirection().getOpposite());
}
private static void executeChain(final Level bjt, final BlockPos fk, Direction fp) {
final BlockPos.MutableBlockPos a4 = new BlockPos.MutableBlockPos(fk);
final GameRules bjp5 = bjt.getGameRules();
int integer6 = bjp5.getInt(GameRules.RULE_MAX_COMMAND_CHAIN_LENGTH);
while (integer6-- > 0) {
a4.move(fp);
final BlockState byg7 = bjt.getBlockState(a4);
final Block bpe8 = byg7.getBlock();
if (bpe8 != Blocks.CHAIN_COMMAND_BLOCK) {
break;
}
final BlockEntity bwi9 = bjt.getBlockEntity(a4);
if (!(bwi9 instanceof CommandBlockEntity)) {
break;
}
final CommandBlockEntity bwn10 = (CommandBlockEntity)bwi9;
if (bwn10.getMode() != CommandBlockEntity.Mode.SEQUENCE) {
break;
}
if (bwn10.isPowered() || bwn10.isAutomatic()) {
final BaseCommandBlock biz11 = bwn10.getCommandBlock();
if (bwn10.markConditionMet()) {
if (!biz11.performCommand(bjt)) {
break;
}
bjt.updateNeighbourForOutputSignal(a4, bpe8);
}
else if (bwn10.isConditional()) {
biz11.setSuccessCount(0);
}
}
fp = byg7.<Direction>getValue((Property<Direction>)CommandBlock.FACING);
}
if (integer6 <= 0) {
final int integer7 = Math.max(bjp5.getInt(GameRules.RULE_MAX_COMMAND_CHAIN_LENGTH), 0);
CommandBlock.LOGGER.warn("Command Block chain tried to execute more than {} steps!", integer7);
}
}
static {
LOGGER = LogManager.getLogger();
FACING = DirectionalBlock.FACING;
CONDITIONAL = BlockStateProperties.CONDITIONAL;
}
}