Skip to content

Commit

Permalink
Add libhud support
Browse files Browse the repository at this point in the history
  • Loading branch information
MDLC01 committed Jul 30, 2023
1 parent f6d211d commit 1dfd14e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 38 deletions.
22 changes: 17 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@ plugins {
version = project.mod_version
group = project.maven_group

loom {
accessWidenerPath = file("src/main/resources/higher-chat.accesswidener")
}

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
exclusiveContent {
forRepository {
maven {
name "Modrinth"
url 'https://api.modrinth.com/maven'
}
}
filter {
includeGroup 'maven.modrinth'
}
}
}

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modImplementation 'maven.modrinth:libhud:1.0.0-beta4'
}

processResources {
Expand Down
68 changes: 42 additions & 26 deletions src/main/java/com/mdlc/higherchat/SharedStorage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mdlc.higherchat;

import dev.cheos.libhud.LibhudGui;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.ChatComponent;
import net.minecraft.client.gui.Gui;


/**
Expand All @@ -11,24 +13,11 @@ public final class SharedStorage {
private SharedStorage() {
}

/**
* Updated each frame to contain the height of the screen.
*/
private static int screenHeight;

/**
* Updated each frame to contain the chat width.
*/
private static int chatWidth;

/**
* Updated each frame to contain the chat height.
*/
private static int chatHeight;

/**
* Each frame, this variable is modified dynamically to contain the height of the highest bar that collides with the
* chat.
* <p>
* Note that this value is measured from the top of the screen.
*/
private static int maxBarHeight;

Expand All @@ -37,39 +26,66 @@ private SharedStorage() {
* <p>
* This function is called at the beginning of every frame.
*/
public static void resetData(ChatComponent chat, int screenHeight) {
SharedStorage.screenHeight = screenHeight;
chatWidth = chat.getWidth();
chatHeight = chat.getHeight();
maxBarHeight = screenHeight;
public static void resetData(Gui gui) {
maxBarHeight = gui.screenHeight;
}

/**
* Adapts the height of the chat knowing there is an icon at {@code (x, y)}.
*
* @param gui
* the current gui
* @param x
* the abscissa of the leftmost pixel of the icon
* @param y
* the ordinate of the high-most pixel of the icon
*/
public static void declareIconAt(int x, int y) {
if (x < chatWidth && y < maxBarHeight) {
public static void declareIconAt(Gui gui, int x, int y) {
if (x < gui.getChat().getWidth() && y < maxBarHeight) {
maxBarHeight = y;
}
}

/**
* Adapts the height of the chat knowing there is an icon at {@code (x, y)}.
*
* @param x
* the abscissa of the leftmost pixel of the icon
* @param y
* the ordinate of the high-most pixel of the icon
*/
public static void declareIconAt(int x, int y) {
declareIconAt(Minecraft.getInstance().gui, x, y);
}

/**
* Returns the position of the top of the bars that share a common abscissa with the chat.
* <p>
* The position is measured as an offset from the top of the screen.
*/
private static int getBarsTop(Gui gui) {
FabricLoader fabric = FabricLoader.getInstance();
if (fabric.isModLoaded("libhud") && gui instanceof LibhudGui libhudGui) {
resetData(gui);
declareIconAt(gui, libhudGui.screenWidth / 2 - 91, libhudGui.screenHeight - libhudGui.leftOffset + 10);
declareIconAt(gui, libhudGui.screenWidth / 2 + 10, libhudGui.screenHeight - libhudGui.rightOffset + 10);
}
return maxBarHeight;
}

/**
* Computes the optimal margin between the bottom of the screen and the chat.
*/
public static int getOptimalChatMargin() {
Gui gui = Minecraft.getInstance().gui;
// Leave space for the `chat.queue` message
boolean hasQueue = Minecraft.getInstance().getChatListener().queueSize() > 0;
int optimalBottomPos = maxBarHeight - (hasQueue ? 10 : 1);
if (optimalBottomPos < chatHeight) {
int optimalBottomPos = getBarsTop(gui) - (hasQueue ? 10 : 1);
if (optimalBottomPos < gui.getChat().getHeight()) {
// If we cannot fit the chat between the top of the screen and the bars,
// we move it back to its vanilla position.
return 0;
}
return screenHeight - optimalBottomPos;
return gui.screenHeight - optimalBottomPos;
}
}
8 changes: 1 addition & 7 deletions src/main/java/com/mdlc/higherchat/mixin/GuiMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import com.mdlc.higherchat.SharedStorage;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ChatComponent;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Final;
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.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -16,9 +13,6 @@

@Mixin(Gui.class)
public abstract class GuiMixin {
@Shadow @Final private ChatComponent chat;
@Shadow private int screenHeight;

protected GuiMixin() {
}

Expand All @@ -27,7 +21,7 @@ protected GuiMixin() {
*/
@Inject(method = "render", at = @At("HEAD"))
private void onRender(GuiGraphics graphics, float partialTick, CallbackInfo ci) {
SharedStorage.resetData(this.chat, this.screenHeight);
SharedStorage.resetData((Gui) (Object) this);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/mdlc/higherchat/mixin/HigherChatMixinPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mdlc.higherchat.mixin;

import net.fabricmc.loader.api.FabricLoader;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import java.util.List;
import java.util.Set;


public class HigherChatMixinPlugin implements IMixinConfigPlugin {
@Override
public void onLoad(String mixinPackage) {
}

@Override
public String getRefMapperConfig() {
return null;
}

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if ("com.mdlc.higherchat.mixin.GuiMixin".equals(mixinClassName)) {
return !FabricLoader.getInstance().isModLoaded("libhud");
}
return true;
}

@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
}

@Override
public List<String> getMixins() {
return null;
}

@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
}

@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
}
}
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"mixins": [
"higher-chat.mixins.json"
],
"accessWidener": "higher-chat.accesswidener",
"depends": {
"fabricloader": ">=0.14.21",
"minecraft": "1.20.1"
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/higher-chat.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessWidener v1 named

accessible field net/minecraft/client/gui/Gui screenWidth I
accessible field net/minecraft/client/gui/Gui screenHeight I
1 change: 1 addition & 0 deletions src/main/resources/higher-chat.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"minVersion": "0.8",
"package": "com.mdlc.higherchat.mixin",
"compatibilityLevel": "JAVA_17",
"plugin": "com.mdlc.higherchat.mixin.HigherChatMixinPlugin",
"mixins": [
],
"client": [
Expand Down

0 comments on commit 1dfd14e

Please sign in to comment.