Skip to content

Commit

Permalink
Bugfixes and backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
r8420 committed May 26, 2024
1 parent bad1437 commit 1aa5496
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'fabric-loom' version '1.6-SNAPSHOT'
id 'maven-publish'
}

Expand Down
9 changes: 5 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ org.gradle.parallel=true
# check these on https://fabricmc.net/develop
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.6
loader_version=0.15.11

# Fabric API
fabric_version=0.97.0+1.20.4

# Mod Properties
mod_version=1.22.4.1-fabric
mod_version=1.22.4.2-fabric
maven_group=at.ridgo8.moreoverlays
archives_base_name=moreoverlays

# Dependencies
fabric_version=0.95.4+1.20.4
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

import at.ridgo8.moreoverlays.itemsearch.GuiHandler;
import at.ridgo8.moreoverlays.itemsearch.GuiUtils;
import net.fabricmc.loader.api.FabricLoader;


public final class ClientRegistrationHandler {

private static boolean enable_jei = false;

private ClientRegistrationHandler() {
// EMPTY
}

public static boolean isJeiInstalled() {
return enable_jei;
}

public static void setupClient() {
enable_jei = FabricLoader.getInstance().isModLoaded("jei");
KeyBindings.init();
GuiUtils.initUtil();
GuiHandler.init();
Expand Down
5 changes: 0 additions & 5 deletions src/client/java/at/ridgo8/moreoverlays/MoreOverlays.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,10 @@ public void onInitialize() {

ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
LightOverlayHandler.setEnabled(false);
if(GuiRenderer.INSTANCE.isEnabled()){
GuiRenderer.INSTANCE.toggleMode();
}
ChunkBoundsHandler.setMode(RenderMode.NONE);
});




Config.initialize();
ClientRegistrationHandler.setupClient();
}
Expand Down
15 changes: 13 additions & 2 deletions src/client/java/at/ridgo8/moreoverlays/itemsearch/GuiHandler.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package at.ridgo8.moreoverlays.itemsearch;

import at.ridgo8.moreoverlays.ClientRegistrationHandler;
import at.ridgo8.moreoverlays.chunkbounds.ChunkBoundsHandler;
import at.ridgo8.moreoverlays.chunkbounds.ChunkBoundsHandler.RenderMode;
import at.ridgo8.moreoverlays.lightoverlay.LightOverlayHandler;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.client.Minecraft;

public class GuiHandler {

public static void init() {
if (FabricLoader.getInstance().isModLoaded("jei")) {
if (ClientRegistrationHandler.isJeiInstalled()) {
registerEvents();
}
}
Expand Down Expand Up @@ -51,6 +55,13 @@ private static void registerEvents() {
if (Minecraft.getInstance().player == null) return;
GuiRenderer.INSTANCE.tick();
});

// World unload
ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
if(GuiRenderer.INSTANCE.isEnabled()){
GuiRenderer.INSTANCE.toggleMode();
}
});
}

@Deprecated
Expand Down
40 changes: 34 additions & 6 deletions src/client/java/at/ridgo8/moreoverlays/itemsearch/GuiUtils.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
package at.ridgo8.moreoverlays.itemsearch;

import at.ridgo8.moreoverlays.MoreOverlays;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;

import java.lang.reflect.Field;

import org.jetbrains.annotations.NotNull;

public class GuiUtils {

private static Field fieldLeft;
private static Field fieldTop;

public static void initUtil() {
try {
fieldLeft = AbstractContainerScreen.class.getDeclaredField("leftPos");

fieldLeft = findField(AbstractContainerScreen.class, "leftPos");
fieldLeft.setAccessible(true);

fieldTop = AbstractContainerScreen.class.getDeclaredField("topPos");
fieldTop = findField(AbstractContainerScreen.class, "topPos");
fieldTop.setAccessible(true);
} catch (NoSuchFieldException e) {
MoreOverlays.logger.error("Tried to load gui coordinate fields for reflection");
e.printStackTrace();
fieldTop = null;
fieldLeft = null;
try {
fieldLeft = findField(AbstractContainerScreen.class, "field_2776");
fieldLeft.setAccessible(true);

fieldTop = findField(AbstractContainerScreen.class, "field_2800");
fieldTop.setAccessible(true);
} catch (NoSuchFieldException f) {
MoreOverlays.logger.error("Failed to load gui coordinate fields for reflection");
f.printStackTrace();
fieldTop = null;
fieldLeft = null;
}
}
}

public static <T> Field findField(@NotNull final Class<? super T> clazz, @NotNull final String fieldName) throws NoSuchFieldException {
try {
MappingResolver resolver = FabricLoader.getInstance().getMappingResolver();
String currentNamespace = resolver.getCurrentRuntimeNamespace();
String className = clazz.getName().replace('.', '/');
String mappedFieldName = resolver.mapFieldName(currentNamespace, className, fieldName, null);

Field f = clazz.getDeclaredField(mappedFieldName);
f.setAccessible(true);
return f;
} catch (Exception e) {
throw new NoSuchFieldException("Failed to find field: " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.ridgo8.moreoverlays.mixin.client;

import at.ridgo8.moreoverlays.ClientRegistrationHandler;
import at.ridgo8.moreoverlays.itemsearch.GuiRenderer;
import at.ridgo8.moreoverlays.itemsearch.JeiModule;
import net.minecraft.client.gui.components.EditBox;
Expand All @@ -16,7 +17,7 @@ public abstract class MixinEditBox {
private void onClick(double d, double e, CallbackInfo cir) {
EditBox textField = (EditBox) (Object) this;

if(JeiModule.getJEITextField() != null && textField.getClass() == JeiModule.getJEITextField().getClass()){
if(ClientRegistrationHandler.isJeiInstalled() && JeiModule.getJEITextField() != null && textField.getClass() == JeiModule.getJEITextField().getClass()){
long now = System.currentTimeMillis();
if (now - firstClick < 1000) {
GuiRenderer.INSTANCE.toggleMode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package at.ridgo8.moreoverlays.mixin.client;


import java.lang.reflect.Field;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;


import at.ridgo8.moreoverlays.MoreOverlays;
import at.ridgo8.moreoverlays.chunkbounds.ChunkBoundsHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;



Expand All @@ -20,22 +23,43 @@ public class MixinOverlayRenderer {
private void onRender(GuiGraphics guiGraphics, float f, CallbackInfo ci) {
Minecraft mc = Minecraft.getInstance();
try {
if (mc.getDebugOverlay().showDebugScreen()) {
return;
}
// Checks if the debug screen is not shown
if (!mc.getDebugOverlay().showDebugScreen()) {
if (!ChunkBoundsHandler.regionInfo.isEmpty()) {
int y = 0;
for (String text : ChunkBoundsHandler.regionInfo) {
guiGraphics.drawString(mc.font, text, 10, y += 10, 0xFFFFFF);
}
}
} catch (NoSuchMethodError e) {
try{
Field renderDebugField = null;
// Use reflection to check if the renderDebug field exists in mc.options. Note: remove this for future versions
try{
renderDebugField = mc.options.getClass().getDeclaredField("field_1866");
renderDebugField.setAccessible(true);
} catch(Exception o){
renderDebugField = mc.options.getClass().getField("renderDebug");
}

boolean renderDebug = renderDebugField.getBoolean(mc.options);

if (renderDebug) {
return;
}

if (!ChunkBoundsHandler.regionInfo.isEmpty()) {
int y = 0;
for (String text : ChunkBoundsHandler.regionInfo) {
guiGraphics.drawString(mc.font, text, 10, y += 10, 0xFFFFFF);
}
}
} catch(Exception g){
// Ignore
}
} catch (NoSuchMethodError e) {
if (!ChunkBoundsHandler.regionInfo.isEmpty()) {
int y = 0;
for (String text : ChunkBoundsHandler.regionInfo) {
guiGraphics.drawString(mc.font, text, 10, y += 10, 0xFFFFFF);
}
}

}
}
}
2 changes: 1 addition & 1 deletion src/client/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": "moreoverlays",
"version": "${version}",
"name": "MoreOverlays",
"description": "This is an example description! Tell everyone what your mod is about!",
"description": "This mod adds some of the overlays back from NEI",
"authors": [
"feldim2425",
"RiDGo8 / R8420"
Expand Down

0 comments on commit 1aa5496

Please sign in to comment.