diff --git a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java index c552051ce..a4d947b63 100644 --- a/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java +++ b/src/main/java/net/earthcomputer/clientcommands/ClientCommands.java @@ -15,7 +15,6 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.DetectedVersion; import net.minecraft.Util; import net.minecraft.client.Minecraft; import net.minecraft.commands.CommandBuildContext; @@ -165,9 +164,7 @@ public static void registerCommands(CommandDispatcher WeatherCommand.register(dispatcher); PluginsCommand.register(dispatcher); CGameModeCommand.register(dispatcher); - if (ListenCommand.isEnabled) { - ListenCommand.register(dispatcher); - } + ListenCommand.register(dispatcher); Calendar calendar = Calendar.getInstance(); boolean registerChatCommand = calendar.get(Calendar.MONTH) == Calendar.APRIL && calendar.get(Calendar.DAY_OF_MONTH) == 1; diff --git a/src/main/java/net/earthcomputer/clientcommands/UnsafeUtils.java b/src/main/java/net/earthcomputer/clientcommands/UnsafeUtils.java index 3090adb7b..863252886 100644 --- a/src/main/java/net/earthcomputer/clientcommands/UnsafeUtils.java +++ b/src/main/java/net/earthcomputer/clientcommands/UnsafeUtils.java @@ -1,8 +1,8 @@ package net.earthcomputer.clientcommands; import com.mojang.logging.LogUtils; -import net.earthcomputer.clientcommands.command.ListenCommand; import net.minecraft.Util; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import sun.misc.Unsafe; @@ -19,7 +19,7 @@ private UnsafeUtils() { private static final Logger LOGGER = LogUtils.getLogger(); - private static final Unsafe UNSAFE = Util.make(() -> { + private static final @Nullable Unsafe UNSAFE = Util.make(() -> { try { final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe"); unsafeField.setAccessible(true); @@ -30,7 +30,7 @@ private UnsafeUtils() { } }); - private static final MethodHandles.Lookup IMPL_LOOKUP = Util.make(() -> { + private static final @Nullable MethodHandles.Lookup IMPL_LOOKUP = Util.make(() -> { try { //noinspection ConstantValue if (UNSAFE == null) { @@ -44,11 +44,11 @@ private UnsafeUtils() { } }); - public static Unsafe getUnsafe() { + public static @Nullable Unsafe getUnsafe() { return UNSAFE; } - public static MethodHandles.Lookup getImplLookup() { + public static @Nullable MethodHandles.Lookup getImplLookup() { return IMPL_LOOKUP; } } diff --git a/src/main/java/net/earthcomputer/clientcommands/command/ListenCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/ListenCommand.java index bb1a77e69..a08c6a3f5 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/ListenCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/ListenCommand.java @@ -50,8 +50,13 @@ public class ListenCommand { - public static boolean isEnabled = true; + private static volatile boolean isEnabled = true; + public static void disable() { + isEnabled = false; + } + + private static final SimpleCommandExceptionType COMMAND_DISABLED_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.clisten.commandDisabled")); private static final SimpleCommandExceptionType ALREADY_LISTENING_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.clisten.add.failed")); private static final SimpleCommandExceptionType NOT_LISTENING_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.clisten.remove.failed")); @@ -76,6 +81,7 @@ public static void register(CommandDispatcher dispatc } private static int add(FabricClientCommandSource source, Class> packetClass) throws CommandSyntaxException { + checkEnabled(); if (!packets.add(packetClass)) { throw ALREADY_LISTENING_EXCEPTION.create(); } @@ -126,6 +132,7 @@ private static int add(FabricClientCommandSource source, Class> packetClass) throws CommandSyntaxException { + checkEnabled(); if (!packets.remove(packetClass)) { throw NOT_LISTENING_EXCEPTION.create(); } @@ -134,7 +141,8 @@ private static int remove(FabricClientCommandSource source, Class seen, int depth) { try { if (depth <= Configs.maximumPacketFieldDepth && seen.add(object)) { diff --git a/src/main/java/net/earthcomputer/clientcommands/features/MappingsHelper.java b/src/main/java/net/earthcomputer/clientcommands/features/MappingsHelper.java index e60a24739..0319a6978 100644 --- a/src/main/java/net/earthcomputer/clientcommands/features/MappingsHelper.java +++ b/src/main/java/net/earthcomputer/clientcommands/features/MappingsHelper.java @@ -90,7 +90,7 @@ public static void load() { }) .whenComplete((result, exception) -> { if (exception != null) { - ListenCommand.isEnabled = false; + ListenCommand.disable(); } }) .thenApply(HttpResponse::body) @@ -116,7 +116,7 @@ public static void load() { return tree; } catch (IOException ex) { LOGGER.error("Could not read ProGuard mappings file", ex); - ListenCommand.isEnabled = false; + ListenCommand.disable(); throw new UncheckedIOException(ex); } finally { try (BufferedWriter writer = Files.newBufferedWriter(MAPPINGS_DIR.resolve(version + ".txt"), StandardOpenOption.CREATE)) { @@ -141,7 +141,7 @@ public static void load() { return tree; } catch (IOException e) { LOGGER.error("Could not read mappings.tiny", e); - ListenCommand.isEnabled = false; + ListenCommand.disable(); return null; } }); @@ -284,7 +284,7 @@ private static MemoryMappingTree getMojmapOfficial() { return mojmapOfficial.get(); } catch (ExecutionException | InterruptedException e) { LOGGER.error("mojmap mappings were not available", e); - ListenCommand.isEnabled = false; + ListenCommand.disable(); return null; } } diff --git a/src/main/resources/assets/clientcommands/lang/en_us.json b/src/main/resources/assets/clientcommands/lang/en_us.json index 6dcef4a0a..322be7ce1 100644 --- a/src/main/resources/assets/clientcommands/lang/en_us.json +++ b/src/main/resources/assets/clientcommands/lang/en_us.json @@ -142,6 +142,7 @@ "commands.ckit.list": "Available kits: %s", "commands.ckit.list.empty": "No available kits", + "commands.clisten.commandDisabled": "The command was disabled, check your logs", "commands.clisten.unknownPacket": "Unknown packet %s", "commands.clisten.receivedPacket": "Received the following packet: %s", "commands.clisten.sentPacket": "Sent the following packet: %s",