Skip to content

Commit

Permalink
Merge pull request #24 from Dev0Louis/dev
Browse files Browse the repository at this point in the history
Very, very sane code to make this mod work from 1.19.4 up to 1.20.4.
  • Loading branch information
Dev0Louis authored Dec 14, 2023
2 parents a322ad7 + 46407b8 commit fb18934
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.1
loader_version=0.15.1
yarn_mappings=1.20.4+build.3
loader_version=0.15.2
# Mod Properties
mod_version=4.0.1
mod_version=4.0.2
maven_group=dev.louis
archives_base_name=Nebula
# Dependencies
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/dev/louis/nebula/NebulaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class NebulaClient implements ClientModInitializer {
public static final Consumer<PacketByteBuf> retainer = getConsumer("retain", () -> PacketByteBuf::retain);
public static final Consumer<PacketByteBuf> releaser = getConsumer("release", () -> PacketByteBuf::release);

@Override
public void onInitializeClient() {
registerPacketReceivers();
Expand All @@ -23,14 +30,32 @@ private void registerPacketReceivers() {
}

public static void runWithBuf(MinecraftClient client, PacketByteBuf buf, Runnable runnable) {
buf.retain();
retainer.accept(buf);
client.executeSync(() -> {
runnable.run();
buf.release();
releaser.accept(buf);
});
}

private void registerReceiver(Identifier id, ClientPlayNetworking.PlayChannelHandler playChannelHandler) {
ClientPlayNetworking.registerGlobalReceiver(id, playChannelHandler);
}

private static Consumer<PacketByteBuf> getConsumer(String methodName, Supplier<Consumer<PacketByteBuf>> defaultConsumer) {
try {
//Needed to throw NoSuchMethodException if the method doesn't exist.
PacketByteBuf.class.getMethod(methodName);
return buf -> invoke(buf, methodName);
} catch (NoSuchMethodException e) {
return defaultConsumer.get();
}
}

private static void invoke(PacketByteBuf buf, String methodName) {
try {
PacketByteBuf.class.getMethod(methodName).invoke(buf);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"depends": {
"fabricloader": ">=0.15.0",
"fabric": "*",
"minecraft": ">=1.20.2"
"minecraft": ">=1.19.4"
},
"custom": {
"loom:injected_interfaces": {
Expand Down

0 comments on commit fb18934

Please sign in to comment.