| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import java.time.ZonedDateTime;
|
| 4 | import java.time.format.DateTimeFormatter;
|
| 5 | import java.util.Locale;
|
| 6 | import java.util.Objects;
|
| 7 | import net.minecraft.CrashReport;
|
| 8 | import net.minecraft.CrashReportCategory;
|
| 9 | import net.minecraft.ReportedException;
|
| 10 | import net.minecraft.commands.CommandSource;
|
| 11 | import net.minecraft.commands.CommandSourceStack;
|
| 12 | import net.minecraft.network.chat.CommonComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.ComponentSerialization;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.util.StringUtil;
|
| 17 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 18 | import net.minecraft.world.level.gamerules.GameRules;
|
| 19 | import net.minecraft.world.level.storage.ValueInput;
|
| 20 | import net.minecraft.world.level.storage.ValueOutput;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public abstract class BaseCommandBlock {
|
| 24 | private static final Component DEFAULT_NAME = Component.literal("@");
|
| 25 | private static final int NO_LAST_EXECUTION = -1;
|
| 26 | private long lastExecution = -1L;
|
| 27 | private boolean updateLastExecution = true;
|
| 28 | private int successCount;
|
| 29 | private boolean trackOutput = true;
|
| 30 | private @Nullable Component lastOutput;
|
| 31 | private String command = "";
|
| 32 | private @Nullable Component customName;
|
| 33 |
|
| 34 | public int getSuccessCount() {
|
| 35 | return this.successCount;
|
| 36 | }
|
| 37 |
|
| 38 | public void setSuccessCount(final int successCount) {
|
| 39 | this.successCount = successCount;
|
| 40 | }
|
| 41 |
|
| 42 | public Component getLastOutput() {
|
| 43 | return this.lastOutput == null ? CommonComponents.EMPTY : this.lastOutput;
|
| 44 | }
|
| 45 |
|
| 46 | public void save(final ValueOutput output) {
|
| 47 | output.putString("Command", this.command);
|
| 48 | output.putInt("SuccessCount", this.successCount);
|
| 49 | output.storeNullable("CustomName", ComponentSerialization.CODEC, this.customName);
|
| 50 | output.putBoolean("TrackOutput", this.trackOutput);
|
| 51 | if (this.trackOutput) {
|
| 52 | output.storeNullable("LastOutput", ComponentSerialization.CODEC, this.lastOutput);
|
| 53 | }
|
| 54 |
|
| 55 | output.putBoolean("UpdateLastExecution", this.updateLastExecution);
|
| 56 | if (this.updateLastExecution && this.lastExecution != -1L) {
|
| 57 | output.putLong("LastExecution", this.lastExecution);
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | public void load(final ValueInput input) {
|
| 62 | this.command = input.getStringOr("Command", "");
|
| 63 | this.successCount = input.getIntOr("SuccessCount", 0);
|
| 64 | this.setCustomName(BlockEntity.parseCustomNameSafe(input, "CustomName"));
|
| 65 | this.trackOutput = input.getBooleanOr("TrackOutput", true);
|
| 66 | if (this.trackOutput) {
|
| 67 | this.lastOutput = BlockEntity.parseCustomNameSafe(input, "LastOutput");
|
| 68 | } else {
|
| 69 | this.lastOutput = null;
|
| 70 | }
|
| 71 |
|
| 72 | this.updateLastExecution = input.getBooleanOr("UpdateLastExecution", true);
|
| 73 | if (this.updateLastExecution) {
|
| 74 | this.lastExecution = input.getLongOr("LastExecution", -1L);
|
| 75 | } else {
|
| 76 | this.lastExecution = -1L;
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public void setCommand(final String command) {
|
| 81 | this.command = command;
|
| 82 | this.successCount = 0;
|
| 83 | }
|
| 84 |
|
| 85 | public String getCommand() {
|
| 86 | return this.command;
|
| 87 | }
|
| 88 |
|
| 89 | public boolean performCommand(final ServerLevel level) {
|
| 90 | if (level.getGameTime() == this.lastExecution) {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | if ("Searge".equalsIgnoreCase(this.command)) {
|
| 95 | this.lastOutput = Component.literal("#itzlipofutzli");
|
| 96 | this.successCount = 1;
|
| 97 | return true;
|
| 98 | }
|
| 99 |
|
| 100 | this.successCount = 0;
|
| 101 | if (level.isCommandBlockEnabled() && !StringUtil.isNullOrEmpty(this.command)) {
|
| 102 | try {
|
| 103 | this.lastOutput = null;
|
| 104 |
|
| 105 | try (BaseCommandBlock.CloseableCommandBlockSource commandSource = this.createSource(level)) {
|
| 106 | CommandSource effectiveCommandSource = Objects.requireNonNullElse(commandSource, CommandSource.NULL);
|
| 107 | CommandSourceStack commandSourceStack = this.createCommandSourceStack(level, effectiveCommandSource).withCallback((success, result) -> {
|
| 108 | if (success) {
|
| 109 | this.successCount++;
|
| 110 | }
|
| 111 | });
|
| 112 | level.getServer().getCommands().performPrefixedCommand(commandSourceStack, this.command);
|
| 113 | }
|
| 114 | } catch (Throwable t) {
|
| 115 | CrashReport report = CrashReport.forThrowable(t, "Executing command block");
|
| 116 | CrashReportCategory category = report.addCategory("Command to be executed");
|
| 117 | category.setDetail("Command", this::getCommand);
|
| 118 | category.setDetail("Name", () -> this.getName().getString());
|
| 119 | throw new ReportedException(report);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | if (this.updateLastExecution) {
|
| 124 | this.lastExecution = level.getGameTime();
|
| 125 | } else {
|
| 126 | this.lastExecution = -1L;
|
| 127 | }
|
| 128 |
|
| 129 | return true;
|
| 130 | }
|
| 131 |
|
| 132 | private BaseCommandBlock.@Nullable CloseableCommandBlockSource createSource(final ServerLevel level) {
|
| 133 | return this.trackOutput ? new BaseCommandBlock.CloseableCommandBlockSource(level) : null;
|
| 134 | }
|
| 135 |
|
| 136 | public Component getName() {
|
| 137 | return this.customName != null ? this.customName : DEFAULT_NAME;
|
| 138 | }
|
| 139 |
|
| 140 | public @Nullable Component getCustomName() {
|
| 141 | return this.customName;
|
| 142 | }
|
| 143 |
|
| 144 | public void setCustomName(final @Nullable Component name) {
|
| 145 | this.customName = name;
|
| 146 | }
|
| 147 |
|
| 148 | public abstract void onUpdated(ServerLevel level);
|
| 149 |
|
| 150 | public void setLastOutput(final @Nullable Component lastOutput) {
|
| 151 | this.lastOutput = lastOutput;
|
| 152 | }
|
| 153 |
|
| 154 | public void setTrackOutput(final boolean trackOutput) {
|
| 155 | this.trackOutput = trackOutput;
|
| 156 | }
|
| 157 |
|
| 158 | public boolean isTrackOutput() {
|
| 159 | return this.trackOutput;
|
| 160 | }
|
| 161 |
|
| 162 | public abstract CommandSourceStack createCommandSourceStack(ServerLevel level, CommandSource source);
|
| 163 |
|
| 164 | public abstract boolean isValid();
|
| 165 |
|
| 166 | protected class CloseableCommandBlockSource implements CommandSource, AutoCloseable {
|
| 167 | private final ServerLevel level;
|
| 168 | private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ROOT);
|
| 169 | private boolean closed;
|
| 170 |
|
| 171 | protected CloseableCommandBlockSource(final ServerLevel level) {
|
| 172 | this.level = level;
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public boolean acceptsSuccess() {
|
| 177 | return !this.closed && this.level.getGameRules().get(GameRules.SEND_COMMAND_FEEDBACK);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public boolean acceptsFailure() {
|
| 182 | return !this.closed;
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public boolean shouldInformAdmins() {
|
| 187 | return !this.closed && this.level.getGameRules().get(GameRules.COMMAND_BLOCK_OUTPUT);
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public void sendSystemMessage(final Component message) {
|
| 192 | if (!this.closed) {
|
| 193 | BaseCommandBlock.this.lastOutput = Component.literal("[" + TIME_FORMAT.format(ZonedDateTime.now()) + "] ").append(message);
|
| 194 | BaseCommandBlock.this.onUpdated(this.level);
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public void close() throws Exception {
|
| 200 | this.closed = true;
|
| 201 | }
|
| 202 | }
|
| 203 | }
|
| 204 |
|