| 1 | package net.minecraft.world.entity.vehicle.minecart;
|
| 2 |
|
| 3 | import net.minecraft.commands.CommandSource;
|
| 4 | import net.minecraft.commands.CommandSourceStack;
|
| 5 | import net.minecraft.network.chat.CommonComponents;
|
| 6 | import net.minecraft.network.chat.Component;
|
| 7 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 8 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 9 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 12 | import net.minecraft.world.InteractionHand;
|
| 13 | import net.minecraft.world.InteractionResult;
|
| 14 | import net.minecraft.world.entity.EntityType;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.Item;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.item.Items;
|
| 19 | import net.minecraft.world.level.BaseCommandBlock;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.storage.ValueInput;
|
| 24 | import net.minecraft.world.level.storage.ValueOutput;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 |
|
| 27 | public class MinecartCommandBlock extends AbstractMinecart {
|
| 28 | private static final EntityDataAccessor<String> DATA_ID_COMMAND_NAME = SynchedEntityData.defineId(MinecartCommandBlock.class, EntityDataSerializers.STRING);
|
| 29 | private static final EntityDataAccessor<Component> DATA_ID_LAST_OUTPUT = SynchedEntityData.defineId(
|
| 30 | MinecartCommandBlock.class, EntityDataSerializers.COMPONENT
|
| 31 | );
|
| 32 | private final BaseCommandBlock commandBlock = new MinecartCommandBlock.MinecartCommandBase();
|
| 33 | private static final int ACTIVATION_DELAY = 4;
|
| 34 | private int lastActivated;
|
| 35 |
|
| 36 | public MinecartCommandBlock(final EntityType<? extends MinecartCommandBlock> type, final Level level) {
|
| 37 | super(type, level);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected Item getDropItem() {
|
| 42 | return Items.MINECART;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public ItemStack getPickResult() {
|
| 47 | return new ItemStack(Items.COMMAND_BLOCK_MINECART);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 52 | super.defineSynchedData(entityData);
|
| 53 | entityData.define(DATA_ID_COMMAND_NAME, "");
|
| 54 | entityData.define(DATA_ID_LAST_OUTPUT, CommonComponents.EMPTY);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 59 | super.readAdditionalSaveData(input);
|
| 60 | this.commandBlock.load(input);
|
| 61 | this.getEntityData().set(DATA_ID_COMMAND_NAME, this.getCommandBlock().getCommand());
|
| 62 | this.getEntityData().set(DATA_ID_LAST_OUTPUT, this.getCommandBlock().getLastOutput());
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 67 | super.addAdditionalSaveData(output);
|
| 68 | this.commandBlock.save(output);
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public BlockState getDefaultDisplayBlockState() {
|
| 73 | return Blocks.COMMAND_BLOCK.defaultBlockState();
|
| 74 | }
|
| 75 |
|
| 76 | public BaseCommandBlock getCommandBlock() {
|
| 77 | return this.commandBlock;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void activateMinecart(final ServerLevel level, final int xt, final int yt, final int zt, final boolean state) {
|
| 82 | if (state && this.tickCount - this.lastActivated >= 4) {
|
| 83 | this.getCommandBlock().performCommand(level);
|
| 84 | this.lastActivated = this.tickCount;
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 90 | if (!player.canUseGameMasterBlocks()) {
|
| 91 | return InteractionResult.PASS;
|
| 92 | }
|
| 93 |
|
| 94 | if (player.level().isClientSide()) {
|
| 95 | player.openMinecartCommandBlock(this);
|
| 96 | }
|
| 97 |
|
| 98 | return InteractionResult.SUCCESS;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 103 | super.onSyncedDataUpdated(accessor);
|
| 104 | if (DATA_ID_LAST_OUTPUT.equals(accessor)) {
|
| 105 | try {
|
| 106 | this.commandBlock.setLastOutput(this.getEntityData().get(DATA_ID_LAST_OUTPUT));
|
| 107 | } catch (Throwable var3) {
|
| 108 | }
|
| 109 | } else if (DATA_ID_COMMAND_NAME.equals(accessor)) {
|
| 110 | this.commandBlock.setCommand(this.getEntityData().get(DATA_ID_COMMAND_NAME));
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private class MinecartCommandBase extends BaseCommandBlock {
|
| 115 | @Override
|
| 116 | public void onUpdated(final ServerLevel level) {
|
| 117 | MinecartCommandBlock.this.getEntityData().set(MinecartCommandBlock.DATA_ID_COMMAND_NAME, this.getCommand());
|
| 118 | MinecartCommandBlock.this.getEntityData().set(MinecartCommandBlock.DATA_ID_LAST_OUTPUT, this.getLastOutput());
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public CommandSourceStack createCommandSourceStack(final ServerLevel level, final CommandSource source) {
|
| 123 | return new CommandSourceStack(
|
| 124 | source,
|
| 125 | MinecartCommandBlock.this.position(),
|
| 126 | MinecartCommandBlock.this.getRotationVector(),
|
| 127 | level,
|
| 128 | LevelBasedPermissionSet.GAMEMASTER,
|
| 129 | level.getServer(),
|
| 130 | MinecartCommandBlock.this
|
| 131 | );
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public boolean isValid() {
|
| 136 | return !MinecartCommandBlock.this.isRemoved();
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 |
|