Skip to content

Commit

Permalink
Implement requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xpple committed Mar 3, 2024
1 parent d9e9984 commit 856c51a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,9 +164,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
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;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/earthcomputer/clientcommands/UnsafeUtils.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand All @@ -76,6 +81,7 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
}

private static int add(FabricClientCommandSource source, Class<? extends Packet<?>> packetClass) throws CommandSyntaxException {
checkEnabled();
if (!packets.add(packetClass)) {
throw ALREADY_LISTENING_EXCEPTION.create();
}
Expand Down Expand Up @@ -126,6 +132,7 @@ private static int add(FabricClientCommandSource source, Class<? extends Packet<
}

private static int remove(FabricClientCommandSource source, Class<? extends Packet<?>> packetClass) throws CommandSyntaxException {
checkEnabled();
if (!packets.remove(packetClass)) {
throw NOT_LISTENING_EXCEPTION.create();
}
Expand All @@ -134,7 +141,8 @@ private static int remove(FabricClientCommandSource source, Class<? extends Pack
return Command.SINGLE_SUCCESS;
}

private static int list(FabricClientCommandSource source) {
private static int list(FabricClientCommandSource source) throws CommandSyntaxException {
checkEnabled();
int amount = packets.size();
if (amount == 0) {
source.sendFeedback(Component.translatable("commands.clisten.list.none"));
Expand All @@ -151,13 +159,20 @@ private static int list(FabricClientCommandSource source) {
return amount;
}

private static int clear(FabricClientCommandSource source) {
private static int clear(FabricClientCommandSource source) throws CommandSyntaxException {
checkEnabled();
int amount = packets.size();
packets.clear();
source.sendFeedback(Component.translatable("commands.clisten.clear"));
return amount;
}

private static void checkEnabled() throws CommandSyntaxException {
if (!isEnabled) {
throw COMMAND_DISABLED_EXCEPTION.create();
}
}

private static Component serialize(Object object, Set<Object> seen, int depth) {
try {
if (depth <= Configs.maximumPacketFieldDepth && seen.add(object)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static void load() {
})
.whenComplete((result, exception) -> {
if (exception != null) {
ListenCommand.isEnabled = false;
ListenCommand.disable();
}
})
.thenApply(HttpResponse::body)
Expand All @@ -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)) {
Expand All @@ -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;
}
});
Expand Down Expand Up @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/clientcommands/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 856c51a

Please sign in to comment.