| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.commands.CommandSource;
|
| 4 | import net.minecraft.commands.CommandSourceStack;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.component.DataComponentGetter;
|
| 8 | import net.minecraft.core.component.DataComponentMap;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 12 | import net.minecraft.world.level.BaseCommandBlock;
|
| 13 | import net.minecraft.world.level.block.Block;
|
| 14 | import net.minecraft.world.level.block.Blocks;
|
| 15 | import net.minecraft.world.level.block.CommandBlock;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.storage.ValueInput;
|
| 18 | import net.minecraft.world.level.storage.ValueOutput;
|
| 19 | import net.minecraft.world.phys.Vec2;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 |
|
| 22 | public class CommandBlockEntity extends BlockEntity {
|
| 23 | private static final boolean DEFAULT_POWERED = false;
|
| 24 | private static final boolean DEFAULT_CONDITION_MET = false;
|
| 25 | private static final boolean DEFAULT_AUTOMATIC = false;
|
| 26 | private boolean powered = false;
|
| 27 | private boolean auto = false;
|
| 28 | private boolean conditionMet = false;
|
| 29 | private final BaseCommandBlock commandBlock = new BaseCommandBlock() {
|
| 30 | @Override
|
| 31 | public void setCommand(final String command) {
|
| 32 | super.setCommand(command);
|
| 33 | CommandBlockEntity.this.setChanged();
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void onUpdated(final ServerLevel level) {
|
| 38 | BlockState state = level.getBlockState(CommandBlockEntity.this.worldPosition);
|
| 39 | level.sendBlockUpdated(CommandBlockEntity.this.worldPosition, state, state, 3);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public CommandSourceStack createCommandSourceStack(final ServerLevel level, final CommandSource source) {
|
| 44 | Direction facing = CommandBlockEntity.this.getBlockState().getValue(CommandBlock.FACING);
|
| 45 | return new CommandSourceStack(
|
| 46 | source,
|
| 47 | Vec3.atCenterOf(CommandBlockEntity.this.worldPosition),
|
| 48 | new Vec2(0.0F, facing.toYRot()),
|
| 49 | level,
|
| 50 | LevelBasedPermissionSet.GAMEMASTER,
|
| 51 | this.getName(),
|
| 52 | level.getServer()
|
| 53 | );
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public boolean isValid() {
|
| 58 | return !CommandBlockEntity.this.isRemoved();
|
| 59 | }
|
| 60 | };
|
| 61 |
|
| 62 | public CommandBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 63 | super(BlockEntityTypes.COMMAND_BLOCK, worldPosition, blockState);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void saveAdditional(final ValueOutput output) {
|
| 68 | super.saveAdditional(output);
|
| 69 | this.commandBlock.save(output);
|
| 70 | output.putBoolean("powered", this.isPowered());
|
| 71 | output.putBoolean("conditionMet", this.wasConditionMet());
|
| 72 | output.putBoolean("auto", this.isAutomatic());
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void loadAdditional(final ValueInput input) {
|
| 77 | super.loadAdditional(input);
|
| 78 | this.commandBlock.load(input);
|
| 79 | this.powered = input.getBooleanOr("powered", false);
|
| 80 | this.conditionMet = input.getBooleanOr("conditionMet", false);
|
| 81 | this.setAutomatic(input.getBooleanOr("auto", false));
|
| 82 | }
|
| 83 |
|
| 84 | public BaseCommandBlock getCommandBlock() {
|
| 85 | return this.commandBlock;
|
| 86 | }
|
| 87 |
|
| 88 | public void setPowered(final boolean powered) {
|
| 89 | this.powered = powered;
|
| 90 | }
|
| 91 |
|
| 92 | public boolean isPowered() {
|
| 93 | return this.powered;
|
| 94 | }
|
| 95 |
|
| 96 | public boolean isAutomatic() {
|
| 97 | return this.auto;
|
| 98 | }
|
| 99 |
|
| 100 | public void setAutomatic(final boolean auto) {
|
| 101 | boolean previousAuto = this.auto;
|
| 102 | this.auto = auto;
|
| 103 | if (!previousAuto && auto && !this.powered && this.level != null && this.getMode() != CommandBlockEntity.Mode.SEQUENCE) {
|
| 104 | this.scheduleTick();
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | public void onModeSwitch() {
|
| 109 | CommandBlockEntity.Mode newMode = this.getMode();
|
| 110 | if (newMode == CommandBlockEntity.Mode.AUTO && (this.powered || this.auto) && this.level != null) {
|
| 111 | this.scheduleTick();
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | private void scheduleTick() {
|
| 116 | Block commandBlock = this.getBlockState().getBlock();
|
| 117 | if (commandBlock instanceof CommandBlock) {
|
| 118 | this.markConditionMet();
|
| 119 | this.level.scheduleTick(this.worldPosition, commandBlock, 1);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public boolean wasConditionMet() {
|
| 124 | return this.conditionMet;
|
| 125 | }
|
| 126 |
|
| 127 | public boolean markConditionMet() {
|
| 128 | this.conditionMet = true;
|
| 129 | if (this.isConditional()) {
|
| 130 | BlockPos relative = this.worldPosition.relative(this.level.getBlockState(this.worldPosition).getValue(CommandBlock.FACING).getOpposite());
|
| 131 | if (this.level.getBlockState(relative).getBlock() instanceof CommandBlock) {
|
| 132 | this.conditionMet = this.level.getBlockEntity(relative) instanceof CommandBlockEntity commandBlockEntity
|
| 133 | && commandBlockEntity.getCommandBlock().getSuccessCount() > 0;
|
| 134 | } else {
|
| 135 | this.conditionMet = false;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | return this.conditionMet;
|
| 140 | }
|
| 141 |
|
| 142 | public CommandBlockEntity.Mode getMode() {
|
| 143 | BlockState state = this.getBlockState();
|
| 144 | if (state.is(Blocks.COMMAND_BLOCK)) {
|
| 145 | return CommandBlockEntity.Mode.REDSTONE;
|
| 146 | } else if (state.is(Blocks.REPEATING_COMMAND_BLOCK)) {
|
| 147 | return CommandBlockEntity.Mode.AUTO;
|
| 148 | } else {
|
| 149 | return state.is(Blocks.CHAIN_COMMAND_BLOCK) ? CommandBlockEntity.Mode.SEQUENCE : CommandBlockEntity.Mode.REDSTONE;
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | public boolean isConditional() {
|
| 154 | BlockState blockState = this.level.getBlockState(this.getBlockPos());
|
| 155 | return blockState.getBlock() instanceof CommandBlock ? blockState.getValue(CommandBlock.CONDITIONAL) : false;
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 160 | super.applyImplicitComponents(components);
|
| 161 | this.commandBlock.setCustomName(components.get(DataComponents.CUSTOM_NAME));
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 166 | super.collectImplicitComponents(components);
|
| 167 | components.set(DataComponents.CUSTOM_NAME, this.commandBlock.getCustomName());
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 172 | super.removeComponentsFromTag(output);
|
| 173 | output.discard("CustomName");
|
| 174 | output.discard("conditionMet");
|
| 175 | output.discard("powered");
|
| 176 | }
|
| 177 |
|
| 178 | public enum Mode {
|
| 179 | SEQUENCE,
|
| 180 | AUTO,
|
| 181 | REDSTONE;
|
| 182 | }
|
| 183 | }
|
| 184 |
|