Skip to content

Commit

Permalink
Add the clisten command (#598)
Browse files Browse the repository at this point in the history
* Add the clisten command

* Move classes to features package

* Implement most requested changes

* Make calls to MappingsHelper blocking

* Gracefully handle empty results

* Wrap mappings trees in CompletableFuture

* Optional -> @nullable + gracefully handle errors

* Fail CompletableFuture on exception

* Extract exception handling to function

* Make sure mappings dir is created first

* Do not nest flatMap calls

* Minor rewrite

* Fix bug

* Use Util#toMap

* Implement requested changes
  • Loading branch information
xpple authored Mar 7, 2024
1 parent 4f5e59b commit 9c69919
Show file tree
Hide file tree
Showing 13 changed files with 1,534 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ dependencies {
modRuntimeOnly('me.djtheredstoner:DevAuth-fabric:1.1.0') {
exclude group: 'net.fabricmc', module: 'fabric-loader'
}

include api('net.fabricmc:mapping-io:0.5.1')
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.xpple.betterconfig.api.ModConfigBuilder;
import io.netty.buffer.Unpooled;
import net.earthcomputer.clientcommands.command.*;
import net.earthcomputer.clientcommands.features.MappingsHelper;
import net.earthcomputer.clientcommands.render.RenderQueue;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
Expand Down Expand Up @@ -85,6 +86,8 @@ public void onInitializeClient() {
new ModConfigBuilder("clientcommands", Configs.class).build();

ItemGroupCommand.registerItemGroups();

MappingsHelper.load();
}

private static Set<String> getCommands(CommandDispatcher<?> dispatcher) {
Expand Down Expand Up @@ -161,6 +164,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
WeatherCommand.register(dispatcher);
PluginsCommand.register(dispatcher);
CGameModeCommand.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
11 changes: 11 additions & 0 deletions src/main/java/net/earthcomputer/clientcommands/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,15 @@ public static void setMaxChorusItemThrows(int maxChorusItemThrows) {
public static boolean conditionLessThan1_20() {
return MultiVersionCompat.INSTANCE.getProtocolVersion() < MultiVersionCompat.V1_20;
}

@Config
public static PacketDumpMethod packetDumpMethod = PacketDumpMethod.REFLECTION;

public enum PacketDumpMethod {
REFLECTION,
BYTE_BUF,
}

@Config
public static int maximumPacketFieldDepth = 10;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.earthcomputer.clientcommands;

import java.lang.reflect.Field;
import java.util.stream.Stream;

public final class ReflectionUtils {

private ReflectionUtils() {
}

public static Stream<Field> getAllFields(Class<?> clazz) {
Stream.Builder<Field> builder = Stream.builder();
Class<?> targetClass = clazz;
while (targetClass.getSuperclass() != null) {
Field[] fields = targetClass.getDeclaredFields();
for (Field field : fields) {
builder.add(field);
}
targetClass = targetClass.getSuperclass();
}
return builder.build();
}
}
54 changes: 54 additions & 0 deletions src/main/java/net/earthcomputer/clientcommands/UnsafeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.earthcomputer.clientcommands;

import com.mojang.logging.LogUtils;
import net.minecraft.Util;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import sun.misc.Unsafe;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;

/**
* @author Gaming32
*/
public final class UnsafeUtils {

private UnsafeUtils() {
}

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

private static final @Nullable Unsafe UNSAFE = Util.make(() -> {
try {
final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
return (Unsafe) unsafeField.get(null);
} catch (Exception e) {
LOGGER.error("Could not access theUnsafe", e);
return null;
}
});

private static final @Nullable MethodHandles.Lookup IMPL_LOOKUP = Util.make(() -> {
try {
//noinspection ConstantValue
if (UNSAFE == null) {
return null;
}
final Field implLookupField = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
return (MethodHandles.Lookup) UNSAFE.getObject(UNSAFE.staticFieldBase(implLookupField), UNSAFE.staticFieldOffset(implLookupField));
} catch (Exception e) {
LOGGER.error("Could not access IMPL_LOOKUP", e);
return null;
}
});

public static @Nullable Unsafe getUnsafe() {
return UNSAFE;
}

public static @Nullable MethodHandles.Lookup getImplLookup() {
return IMPL_LOOKUP;
}
}
Loading

0 comments on commit 9c69919

Please sign in to comment.