Skip to content

Commit

Permalink
Make exception during command conversion non-fatal
Browse files Browse the repository at this point in the history
Instead of allowing chunks to fail to convert completely,
simply log the exception and the offending command. Command
conversion failure should never result in chunk data deletion,
as commands are not critical chunk data.

(cherry picked from commit 53e9d19)
  • Loading branch information
Spottedleaf authored and mworzala committed Jun 13, 2024
1 parent e47f8b4 commit 80e959b
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.logging.LogUtils;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.JsonOps;
import net.minecraft.SharedConstants;
Expand All @@ -24,6 +25,7 @@
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.TagParser;
import net.minecraft.util.GsonHelper;
import org.slf4j.Logger;
import java.util.Iterator;
import java.util.function.Supplier;

Expand All @@ -33,6 +35,8 @@ public final class V3818_Commands {

private static final boolean DISABLE_COMMAND_CONVERTER = Boolean.getBoolean("Paper.DisableCommandConverter");

private static final Logger LOGGER = LogUtils.getLogger();

public static String toCommandFormat(final CompoundTag components) {
final StringBuilder ret = new StringBuilder();
ret.append('[');
Expand Down Expand Up @@ -75,11 +79,15 @@ public static void walkComponent(final JsonElement primitive) {
final String cmdString = cmd.getAsString();

if ((actionString.equals("suggest_command") && cmdString.startsWith("/")) || actionString.equals("run_command")) {
final Object res = MCDataConverter.convert(
MCTypeRegistry.DATACONVERTER_CUSTOM_TYPE_COMMAND, cmdString, MCVersions.V1_20_4, SharedConstants.getCurrentVersion().getDataVersion().getVersion()
);
if (res instanceof String newCmd) {
clickEvent.addProperty("value", newCmd);
try {
final Object res = MCDataConverter.convert(
MCTypeRegistry.DATACONVERTER_CUSTOM_TYPE_COMMAND, cmdString, MCVersions.V1_20_4, SharedConstants.getCurrentVersion().getDataVersion().getVersion()
);
if (res instanceof String newCmd) {
clickEvent.addProperty("value", newCmd);
}
} catch (final Exception ex) {
LOGGER.error("Failed to convert command '" + cmdString + "'", ex);
}
}
}
Expand Down Expand Up @@ -172,6 +180,9 @@ private static String walkComponent(final String json) {
return GsonHelper.toStableString(element);
} catch (final JsonParseException ex) {
return json;
} catch (final Exception ex) {
LOGGER.error("Failed to convert text component '" + json + "'", ex);
return json;
}
}

Expand All @@ -192,7 +203,12 @@ public Object convert(final Object data, final long sourceVersion, final long to
}
// We use startsWith("/") because we aren't supporting WorldEdit style commands,
// and passing the context of whether the use supports leading slash would be high effort low return
return COMMAND_UPGRADER.get().upgradeCommandArguments(cmd, cmd.startsWith("/"));
try {
return COMMAND_UPGRADER.get().upgradeCommandArguments(cmd, cmd.startsWith("/"));
} catch (final Exception ex) {
LOGGER.error("Failed to convert command '" + cmd + "'", ex);
return null;
}
}
});

Expand Down

0 comments on commit 80e959b

Please sign in to comment.