Skip to content

Commit

Permalink
feat: better compat + mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxnii committed Mar 29, 2023
1 parent 146f23d commit 4ae9601
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 162 deletions.
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ minecraft.version("1.8.9")

repositories {
maven("https://jitpack.io")
maven("https://repo.spongepowered.org/maven/")
}

dependencies {
compileOnly("com.github.Weave-MC:Weave-Loader:0c09d7496f")
compileOnly("org.spongepowered:mixin:0.8.5")
compileOnly("com.github.Weave-MC:Weave-Loader:6a9e6a3245")
}

tasks.compileJava {
options.release.set(17)
}

tasks.jar {
manifest.attributes(
"Weave-Entry" to "wtf.zani.vanillamenu.VanillaMenu"
)
destinationDirectory.set(File("${System.getProperty("user.home")}/.lunarclient/mods"))
}
9 changes: 2 additions & 7 deletions src/main/java/wtf/zani/vanillamenu/VanillaMenu.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package wtf.zani.vanillamenu;

import club.maxstats.weave.loader.api.HookManager;
import club.maxstats.weave.loader.api.ModInitializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import wtf.zani.vanillamenu.hooks.MinecraftClientHook;

public class VanillaMenu implements ModInitializer {
public static final Logger logger = LogManager.getLogger();

@Override
public void preInit(@NotNull HookManager hookManager) {
logger.info("Adding Vanilla Menu's hook");

hookManager.register(new MinecraftClientHook());
public void init() {
logger.info("Initializing VanillaMenu");
}
}
105 changes: 0 additions & 105 deletions src/main/java/wtf/zani/vanillamenu/hooks/MinecraftClientHook.java

This file was deleted.

39 changes: 39 additions & 0 deletions src/main/java/wtf/zani/vanillamenu/hooks/VanillaMenuHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package wtf.zani.vanillamenu.hooks;

import club.maxstats.weave.loader.api.Hook;
import org.jetbrains.annotations.NotNull;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;

import java.util.Arrays;

@SuppressWarnings("unused")
public class VanillaMenuHook extends Hook {
public VanillaMenuHook() {
super("net/minecraft/client/Minecraft");
}

@Override
public void transform(@NotNull ClassNode classNode, @NotNull AssemblerConfig assemblerConfig) {
final MethodNode displayGuiScreen = classNode.methods
.stream()
.filter(methodNode -> methodNode.name.equals("displayGuiScreen"))
.findFirst()
.orElseThrow();
final InsnList filteredInstructions = new InsnList();

Arrays.stream(displayGuiScreen.instructions.toArray())
.filter(instruction -> {
if (instruction instanceof final MethodInsnNode methodCall) {
return !methodCall.name.endsWith("$impl$displayGuiScreen");
}

return true;
})
.forEach(filteredInstructions::add);

assemblerConfig.computeFrames();
}
}
32 changes: 32 additions & 0 deletions src/main/java/wtf/zani/vanillamenu/mixin/MinecraftMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package wtf.zani.vanillamenu.mixin;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.multiplayer.WorldClient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import wtf.zani.vanillamenu.VanillaMenu;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@Shadow
public WorldClient theWorld;

private boolean isFirst = true;

@ModifyVariable(at = @At(value = "HEAD"), method = "displayGuiScreen", argsOnly = true)
public GuiScreen modifyScreen(GuiScreen screen) {
if (this.theWorld == null && screen != null && !screen.getClass().getName().startsWith("net.minecraft") && isFirst) {
isFirst = false;

VanillaMenu.logger.info("Replaced Lunar's main menu!");

return new GuiMainMenu();
}

return screen;
}
}
46 changes: 0 additions & 46 deletions src/main/java/wtf/zani/vanillamenu/util/ClassUtil.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/resources/vanillamenu.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compatibilityLevel": "JAVA_8",
"package": "wtf.zani.vanillamenu.mixin",
"mixins": [
"MinecraftMixin"
]
}
11 changes: 11 additions & 0 deletions src/main/resources/weave.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"mixinConfigs": [
"vanillamenu.mixins.json"
],
"hooks": [
"wtf.zani.vanillamenu.hooks.VanillaMenuHook"
],
"entrypoints": [
"wtf.zani.vanillamenu.VanillaMenu"
]
}

0 comments on commit 4ae9601

Please sign in to comment.