Skip to content

Commit

Permalink
fix not calling forge events after cancel, fix compat with old skyhan…
Browse files Browse the repository at this point in the history
…ni, add skyhanni custom scoreboard support
  • Loading branch information
Wyvest committed Jun 25, 2024
1 parent c73f97b commit 53fc72c
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/dummy/java/at/hannibal2/skyhanni/config/Features.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package at.hannibal2.skyhanni.config;

import at.hannibal2.skyhanni.config.features.gui.GUIConfig;
import at.hannibal2.skyhanni.config.features.misc.MiscConfig;

public class Features {
public GUIConfig gui = new GUIConfig();
public MiscConfig misc = new MiscConfig();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package at.hannibal2.skyhanni.config.features.gui;

import at.hannibal2.skyhanni.config.features.gui.customscoreboard.CustomScoreboardConfig;
import at.hannibal2.skyhanni.config.features.misc.compacttablist.CompactTabListConfig;

public class GUIConfig {
public CompactTabListConfig compactTabList = new CompactTabListConfig();
public CustomScoreboardConfig customScoreboard = new CustomScoreboardConfig();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package at.hannibal2.skyhanni.config.features.gui.customscoreboard;

import at.hannibal2.skyhanni.deps.moulconfig.observer.Property;

public class CustomScoreboardConfig {
public Property<Boolean> enabled = Property.of(false); // todo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package at.hannibal2.skyhanni.config.features.misc;

import at.hannibal2.skyhanni.config.features.misc.compacttablist.CompactTabListConfig;

public class MiscConfig {
public CompactTabListConfig compactTabList = new CompactTabListConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import at.hannibal2.skyhanni.deps.moulconfig.observer.Property;

public class CompactTabListConfig {
public Property<Boolean> enabled = Property.of(false);
public Property<Boolean> enabled = Property.of(false); // todo
}
121 changes: 112 additions & 9 deletions src/main/java/org/polyfrost/vanillahud/VanillaHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import org.polyfrost.vanillahud.config.ModConfig;
import org.polyfrost.vanillahud.hud.*;
import org.polyfrost.vanillahud.utils.TabListManager;
import org.polyfrost.vanillahud.utils.Utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

@net.minecraftforge.fml.common.Mod(modid = VanillaHUD.MODID, name = VanillaHUD.NAME, version = VanillaHUD.VERSION)
public class VanillaHUD {
Expand All @@ -28,13 +33,15 @@ public class VanillaHUD {
public static boolean isHytils = false;
private static boolean isSBA = false;
private static boolean isSkyHanni = false;
private static boolean forceDisableCompactTab = false;
private static boolean skyHanniField = false;

@net.minecraftforge.fml.common.Mod.EventHandler
public void onFMLInitialization(net.minecraftforge.fml.common.event.FMLInitializationEvent event) {
modConfig = new ModConfig();
TabListManager.asyncUpdateList();
EventManager.INSTANCE.register(this);
EventManager.INSTANCE.register(new Utils());
}

@net.minecraftforge.fml.common.Mod.EventHandler
Expand All @@ -51,29 +58,117 @@ public void onPostInit(net.minecraftforge.fml.common.event.FMLPostInitialization
isHytils = Loader.isModLoaded("hytils-reborn");
isSBA = Loader.isModLoaded("skyblockaddons") || Loader.isModLoaded("sbaunofficial");
isSkyHanni = Loader.isModLoaded("skyhanni");

checkForSkyHanni();
doDebugMigration();
updateHeight();
doPatcherMigration();
}

private void checkForSkyHanni() {
if (isSkyHanni) {
try {
// make sure the classes are loaded
Class.forName("at.hannibal2.skyhanni.config.features.gui.GUIConfig", false, getClass().getClassLoader());
Class.forName("at.hannibal2.skyhanni.config.features.misc.compacttablist.CompactTabListConfig", false, getClass().getClassLoader());
Class.forName("at.hannibal2.skyhanni.config.Features", false, getClass().getClassLoader());
Class.forName("at.hannibal2.skyhanni.deps.moulconfig.observer.Property", false, getClass().getClassLoader());
Class<?> guiConfig = Class.forName("at.hannibal2.skyhanni.config.features.gui.GUIConfig", false, getClass().getClassLoader());
try {
guiConfig.getDeclaredField("compactTabList");
guiConfig.getDeclaredField("customScoreboard");
} catch (NoSuchFieldException e) {
forceDisableCompactTab = true;
System.out.println("SkyHanni: compactTabList not found");
return;
}
Class<?> property = Class.forName("at.hannibal2.skyhanni.deps.moulconfig.observer.Property", false, getClass().getClassLoader());
Class.forName("at.hannibal2.skyhanni.deps.moulconfig.observer.GetSetter", false, getClass().getClassLoader());
Class.forName("at.hannibal2.skyhanni.features.misc.compacttablist.RenderColumn", false, getClass().getClassLoader());
Class.forName("at.hannibal2.skyhanni.features.misc.compacttablist.TabListReader", false, getClass().getClassLoader());
Class.forName("at.hannibal2.skyhanni.utils.LorenzUtils", false, getClass().getClassLoader());
Class<?> compactTabListConfig = Class.forName("at.hannibal2.skyhanni.config.features.misc.compacttablist.CompactTabListConfig", false, getClass().getClassLoader());
try {
Field enabled = compactTabListConfig.getDeclaredField("enabled");
if (enabled.getType() != property) {
forceDisableCompactTab = true;
System.out.println("SkyHanni: enabled not found");
return;
}
} catch (NoSuchFieldException e) {
forceDisableCompactTab = true;
System.out.println("SkyHanni: enabled not found");
return;
}
Class<?> features = Class.forName("at.hannibal2.skyhanni.config.Features", false, getClass().getClassLoader());
try {
features.getDeclaredField("gui");
} catch (NoSuchFieldException e) {
//skyHanniMisc = true;
forceDisableCompactTab = true;
System.out.println("SkyHanni: gui not found");
return;
//try {
// features.getDeclaredField("misc");
//} catch (NoSuchFieldException e1) {
// isSkyHanni = false;
// return;
//}
}
Class<?> renderColumn = Class.forName("at.hannibal2.skyhanni.features.misc.compacttablist.RenderColumn", false, getClass().getClassLoader());
Class<?> tabListReader = Class.forName("at.hannibal2.skyhanni.features.misc.compacttablist.TabListReader", false, getClass().getClassLoader());
try {
Method method = tabListReader.getDeclaredMethod("getRenderColumns"); // this is a list
// get the type parameter of the list
java.lang.reflect.Type returnType = method.getGenericReturnType();
if (!(returnType instanceof java.lang.reflect.ParameterizedType)) {
System.out.println("SkyHanni: !(returnType instanceof java.lang.reflect.ParameterizedType)");
forceDisableCompactTab = true;
return;
}
java.lang.reflect.ParameterizedType parameterizedType = (java.lang.reflect.ParameterizedType) returnType;
java.lang.reflect.Type[] typeParameters = parameterizedType.getActualTypeArguments();
if (typeParameters.length != 1) {
System.out.println("SkyHanni: typeParameters.length != 1");
forceDisableCompactTab = true;
return;
} else {
Type renderColumnParameter = typeParameters[0];
if (renderColumnParameter == null || !renderColumn.getName().equals(renderColumnParameter.getTypeName())) {
System.out.println("SkyHanni: renderColumnParameter == null || !renderColumn.equals(renderColumnParameter.getGenericDeclaration())");
forceDisableCompactTab = true;
return;
}
}
} catch (NoSuchMethodException e) {
forceDisableCompactTab = true;
System.out.println("SkyHanni: getRenderColumns not found");
return;
}
Class<?> lorenzUtils = Class.forName("at.hannibal2.skyhanni.utils.LorenzUtils", false, getClass().getClassLoader());
try {
lorenzUtils.getDeclaredMethod("getInSkyBlock");
} catch (NoSuchMethodException e) {
forceDisableCompactTab = true;
System.out.println("SkyHanni: getInSkyBlock not found");
return;
}
Class<?> clazz = Class.forName("at.hannibal2.skyhanni.SkyHanniMod", true, getClass().getClassLoader());
try {
clazz.getDeclaredField("feature");
skyHanniField = true;
} catch (NoSuchFieldException e) {
skyHanniField = false;
try {
clazz.getDeclaredMethod("getFeature");
} catch (NoSuchMethodException e1) {
forceDisableCompactTab = true;
System.out.println("SkyHanni: getFeature not found");
return;
}
}
} catch (ClassNotFoundException e) {
isSkyHanni = false;
System.out.println("SkyHanni: class not found");
e.printStackTrace();
forceDisableCompactTab = true;
}
}
}

private void doDebugMigration() {
if (!ModConfig.doneDebugMigration) {
if (!ModConfig.actionBar.hud.showInDebug) {
ModConfig.actionBar.hud.showInDebug = true;
Expand Down Expand Up @@ -138,15 +233,19 @@ public void onPostInit(net.minecraftforge.fml.common.event.FMLPostInitialization
ModConfig.doneDebugMigration = true;
modConfig.save();
}
}

private void updateHeight() {
if (!TabList.TabHud.updatedHeight) {
TabList.TabHud.updatedHeight = true;
if (TabList.hud.position.getY() == 10) {
TabList.hud.position.setY(TabList.hud.position.getY() + 10);
ModConfig.tab.save();
}
}
}

private void doPatcherMigration() {
if (isPatcher) {
try {
if (ModConfig.hasMigratedPatcher) return;
Expand Down Expand Up @@ -226,6 +325,10 @@ public static boolean isApec() {

public static boolean isCompactTab() {
return (isSBA && SkyblockAddons.getInstance().getUtils().isOnSkyblock() && SkyblockAddons.getInstance().getConfigValues().isEnabled(Feature.COMPACT_TAB_LIST) && TabListParser.getRenderColumns() != null)
|| (isSkyHanni && LorenzUtils.INSTANCE.getInSkyBlock() && (skyHanniField ? SkyHanniMod.feature.gui.compactTabList.enabled.get() : SkyHanniMod.getFeature().gui.compactTabList.enabled.get()) && TabListReader.INSTANCE.getRenderColumns() != null);
|| (isSkyHanni && ((forceDisableCompactTab && Utils.inSkyblock) || (LorenzUtils.INSTANCE.getInSkyBlock() && (skyHanniField ? SkyHanniMod.feature.gui.compactTabList.enabled.get() : SkyHanniMod.getFeature().gui.compactTabList.enabled.get()) && TabListReader.INSTANCE.getRenderColumns() != null)));
}

public static boolean isSkyHanniScoreboard() {
return (isSkyHanni && ((forceDisableCompactTab && Utils.inSkyblock) || (LorenzUtils.INSTANCE.getInSkyBlock() && (skyHanniField ? SkyHanniMod.feature.gui.customScoreboard.enabled.get() : SkyHanniMod.getFeature().gui.customScoreboard.enabled.get()))));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/polyfrost/vanillahud/hud/Scoreboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void draw(UMatrixStack matrices, float x, float y, float scale, boolean e

@Override
protected boolean shouldShow() {
if (VanillaHUD.isApec() || !ScoreboardHook.canDraw) { // I love Apec Mod Minecraft
if (VanillaHUD.isApec() || !ScoreboardHook.canDraw || VanillaHUD.isSkyHanniScoreboard()) { // I love Apec Mod Minecraft
return false;
}
ScoreObjective objective = mc.theWorld.getScoreboard().getObjectiveInDisplaySlot(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,71 @@
package org.polyfrost.vanillahud.mixin;

import net.minecraftforge.client.GuiIngameForge;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import org.polyfrost.vanillahud.hud.*;
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.callback.CallbackInfo;

@Mixin(value = GuiIngameForge.class, priority = 999)
public class GuiIngameForgeMixin_CancelLow {
public abstract class GuiIngameForgeMixin_CancelLow {
@Shadow protected abstract void post(RenderGameOverlayEvent.ElementType type);

@Inject(method = "renderJumpBar", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glColor4f(FFFF)V", ordinal = 0), cancellable = true, remap = false)
private void cancelHorsePower(CallbackInfo ci) {
if (!Experience.hud.shouldRender()) ci.cancel();
if (!Experience.hud.shouldRender()) {
post(RenderGameOverlayEvent.ElementType.JUMPBAR);
ci.cancel();
}
}

@Inject(method = "renderHealthMount", at = @At(value = "INVOKE", target = "Lnet/minecraft/profiler/Profiler;endStartSection(Ljava/lang/String;)V"), cancellable = true)
private void cancelHorseHealth(CallbackInfo ci) {
if (!Hunger.getMountHud().shouldRender()) ci.cancel();
if (!Hunger.getMountHud().shouldRender()) {
post(RenderGameOverlayEvent.ElementType.HEALTHMOUNT);
ci.cancel();
}
}

@Inject(method = "renderExperience", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GlStateManager;color(FFFF)V", ordinal = 0), cancellable = true)
private void cancelExpBar(CallbackInfo ci) {
if (!Experience.hud.shouldRender()) ci.cancel();
if (!Experience.hud.shouldRender()) {
post(RenderGameOverlayEvent.ElementType.EXPERIENCE);
ci.cancel();
}
}

@Inject(method = "renderHealth", at = @At(value = "INVOKE", target = "Lnet/minecraft/profiler/Profiler;startSection(Ljava/lang/String;)V"), cancellable = true)
private void cancelHealth(CallbackInfo ci) {
if (!Health.hud.shouldRender()) ci.cancel();
if (!Health.hud.shouldRender()) {
post(RenderGameOverlayEvent.ElementType.HEALTH);
ci.cancel();
}
}

@Inject(method = "renderFood", at = @At(value = "HEAD"), cancellable = true, remap = false)
private void cancelHunger(CallbackInfo ci) {
if (!Hunger.hud.shouldRender()) ci.cancel();
if (!Hunger.hud.shouldRender()) {
post(RenderGameOverlayEvent.ElementType.FOOD);
ci.cancel();
}
}

@Inject(method = "renderArmor", at = @At(value = "INVOKE", target = "Lnet/minecraft/profiler/Profiler;startSection(Ljava/lang/String;)V"), cancellable = true)
private void cancelArmor(CallbackInfo ci) {
if (!Armor.hud.shouldRender()) ci.cancel();
if (!Armor.hud.shouldRender()) {
post(RenderGameOverlayEvent.ElementType.ARMOR);
ci.cancel();
}
}

@Inject(method = "renderAir", at = @At(value = "INVOKE", target = "Lnet/minecraft/profiler/Profiler;startSection(Ljava/lang/String;)V"), cancellable = true)
private void cancelAir(CallbackInfo ci) {
if (!Air.hud.shouldRender()) ci.cancel();
if (!Air.hud.shouldRender()) {
post(RenderGameOverlayEvent.ElementType.AIR);
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ private void cancelBossBar(CallbackInfo ci) {

@Inject(method = "renderScoreboard", at = @At(value = "INVOKE", target = "Lnet/minecraft/scoreboard/ScoreObjective;getScoreboard()Lnet/minecraft/scoreboard/Scoreboard;", shift = At.Shift.AFTER), cancellable = true)
private void cancelScoreboard(ScoreObjective s, ScaledResolution sr, CallbackInfo ci) {
if (VanillaHUD.isSkyHanniScoreboard()) {
return;
}
ScoreboardHook.canDraw = true;
ci.cancel();
}
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/polyfrost/vanillahud/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.polyfrost.vanillahud.utils;

import cc.polyfrost.oneconfig.events.event.Stage;
import cc.polyfrost.oneconfig.events.event.TickEvent;
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import net.minecraft.client.Minecraft;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.util.StringUtils;

public class Utils {

public static boolean inSkyblock = false;
private int tickAmount = 0;

@Subscribe
private void onTick(TickEvent event) {
if (event.stage == Stage.START) {
tickAmount++;
if (tickAmount % 20 == 0) {
if (UMinecraft.getPlayer() != null) {
Minecraft mc = UMinecraft.getMinecraft();
if (mc.theWorld != null && !mc.isSingleplayer()) {
ScoreObjective scoreboardObj = mc.theWorld.getScoreboard().getObjectiveInDisplaySlot(1);
if (scoreboardObj != null) {
String scObjName = cleanSB(scoreboardObj.getDisplayName());
if (scObjName.contains("SKYBLOCK")) {
inSkyblock = true;
return;
}
}
}
inSkyblock = false;
}

tickAmount = 0;
}
}
}

private static String cleanSB(String scoreboard) {
char[] nvString = StringUtils.stripControlCodes(scoreboard).toCharArray();
StringBuilder cleaned = new StringBuilder();

for (char c : nvString) {
if ((int) c > 20 && (int) c < 127) {
cleaned.append(c);
}
}

return cleaned.toString();
}
}
4 changes: 1 addition & 3 deletions src/main/resources/mixins.vanillahud.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
"GuiSpectatorAccessor",
"GuiSpectatorMixin",
"GuiUtilsMixin",
"HUDUtilsMixin",
"HUDOverlayHandlerMixin",
"MinecraftAccessor"
],
"mixins": [
"HUDUtilsMixin"
]
}

1 comment on commit 53fc72c

@j10a1n15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ty for doing this <3

Please sign in to comment.