From 15852f03785f923c327e66aaf539183b3029a597 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Tue, 18 Jun 2024 00:00:29 +0200 Subject: [PATCH] Slight HUD refactor, allow modules to depend on each other more easily --- .../client/event/StaticEventHandler.java | 5 +- .../com/baseband/client/feature/Feature.java | 36 ++++- .../feature/modules/combat/AutoKill.java | 13 +- .../client/feature/modules/render/HUD.java | 130 +++++++++++------- .../client/registry/annotation/Requires.java | 14 ++ Website | 1 + 6 files changed, 139 insertions(+), 60 deletions(-) create mode 100644 Client/src/main/java/de/com/baseband/client/registry/annotation/Requires.java create mode 160000 Website diff --git a/Client/src/main/java/de/com/baseband/client/event/StaticEventHandler.java b/Client/src/main/java/de/com/baseband/client/event/StaticEventHandler.java index d9d67e7..c1dd28c 100644 --- a/Client/src/main/java/de/com/baseband/client/event/StaticEventHandler.java +++ b/Client/src/main/java/de/com/baseband/client/event/StaticEventHandler.java @@ -81,10 +81,7 @@ public class StaticEventHandler { playerLastTick = mc.player; KeyManager.updateKeyBinds(); for(Feature feature : Features.features) { - feature.onEveryTick(); - if(feature.enabled) { - feature.onTick(); - } + feature.internalOnEveryTick(); } } } diff --git a/Client/src/main/java/de/com/baseband/client/feature/Feature.java b/Client/src/main/java/de/com/baseband/client/feature/Feature.java index 1255a40..c399afb 100644 --- a/Client/src/main/java/de/com/baseband/client/feature/Feature.java +++ b/Client/src/main/java/de/com/baseband/client/feature/Feature.java @@ -24,7 +24,7 @@ import java.awt.*; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.HashMap; +import java.util.*; /** * @author TudbuT @@ -37,6 +37,7 @@ public abstract class Feature extends ToggleButton implements SetCommand { public static final int M_ENABLED = MARKER_PREFIX + 1; protected de.com.baseband.client.Setup Setup; + protected Set> required = new HashSet<>(); protected Minecraft mc; public Category category; @@ -49,6 +50,9 @@ public abstract class Feature extends ToggleButton implements SetCommand { public Feature() { super("Uninit", null, "Enabled"); this.setLambda(this::updateEnabled); + Requires requiredAnnotation = getClass().getDeclaredAnnotation(Requires.class); + if(requiredAnnotation != null) + required.addAll(Arrays.asList(requiredAnnotation.value())); } // feature methods @@ -268,6 +272,9 @@ public abstract class Feature extends ToggleButton implements SetCommand { public void setEnabledSilent(boolean enable) { if(enabled == enable) return; + if(!enabled && !tryEnable()) { + return; + } enabled = enable; updateEnabled(); } @@ -279,11 +286,24 @@ public abstract class Feature extends ToggleButton implements SetCommand { @KeyBound("Toggle") public void toggle() { + if(!enabled && !tryEnable()) { + return; + } enabled = !enabled; handle.poll("Enabled"); updateEnabled(); } + protected boolean tryEnable() { + for (Class feature : required) { + if(!Features.isFeatureEnabled(feature)) { + BaseBand.notify("§c" + this + " cannot work without " + feature + "."); + return false; + } + } + return true; + } + public boolean setWithString(String setting, String value) { if(setting.equals("Enabled") && !value.equalsIgnoreCase(String.valueOf(enabled))) { toggle(); @@ -293,6 +313,11 @@ public abstract class Feature extends ToggleButton implements SetCommand { } public boolean canExist() { + for (Class feature : required) { + if(!Features.isFeaturePresent(feature)) { + return false; + } + } return true; } @@ -314,4 +339,13 @@ public abstract class Feature extends ToggleButton implements SetCommand { return text + (meta != null ? ChatFormatting.GRAY + " [" + meta + "]" : ""); } } + + public void internalOnEveryTick() { + if(enabled && !tryEnable()) + toggle(); + onEveryTick(); + if(enabled) { + onTick(); + } + } } diff --git a/Client/src/main/java/de/com/baseband/client/feature/modules/combat/AutoKill.java b/Client/src/main/java/de/com/baseband/client/feature/modules/combat/AutoKill.java index f801c49..2d16284 100644 --- a/Client/src/main/java/de/com/baseband/client/feature/modules/combat/AutoKill.java +++ b/Client/src/main/java/de/com/baseband/client/feature/modules/combat/AutoKill.java @@ -8,6 +8,7 @@ import de.com.baseband.client.registry.PlayerDB; import de.com.baseband.client.registry.annotation.Config; import de.com.baseband.client.registry.annotation.Gate; import de.com.baseband.client.registry.annotation.Range; +import de.com.baseband.client.registry.annotation.Requires; import de.com.baseband.client.util.adapt.Marker; import de.tudbut.parsing.TCN; import de.tudbut.tools.Lock; @@ -62,6 +63,12 @@ public class AutoKill extends Feature { @Config("PacketHit") public boolean packetHit = false; + @Override + public void onDisable() { + Features.ifFeatureEnabled(AutoHit.class, Feature::toggle); + Features.ifFeatureEnabled(AutoCrystal.class, Feature::toggle); + } + ArrayList getAttackable() { ArrayList allowed = new ArrayList<>(); for (Entity entity : mc.world.loadedEntityList) { @@ -182,6 +189,7 @@ public class AutoKill extends Feature { } @Combat + @Requires(AutoKill.class) public class AutoHit extends Feature { @Config("Speed (ms)") @@ -193,8 +201,6 @@ public class AutoKill extends Feature { @Override public void onTick() { - if(!AutoKill.this.enabled) - return; if(notIngame()) return; if(threaded) { @@ -213,7 +219,7 @@ public class AutoKill extends Feature { } private void runThread() { - while (threaded) { + while (threaded && enabled) { if(notIngame()) break; @@ -246,6 +252,7 @@ public class AutoKill extends Feature { } @Combat + @Requires(AutoKill.class) public class AutoCrystal extends Feature { @Override diff --git a/Client/src/main/java/de/com/baseband/client/feature/modules/render/HUD.java b/Client/src/main/java/de/com/baseband/client/feature/modules/render/HUD.java index 6d118f7..61a747a 100644 --- a/Client/src/main/java/de/com/baseband/client/feature/modules/render/HUD.java +++ b/Client/src/main/java/de/com/baseband/client/feature/modules/render/HUD.java @@ -170,6 +170,12 @@ public class HUD extends Feature { @Description("Where to display notifications. Left and Right display the most recent at the top, Center displays the most recent at the bottom.") public NotificationLocation nLocation = NotificationLocation.Left; + // TODO: finish implementation + @Marker(Integer.MIN_VALUE) + @Config("Notifications like chat") + @Description("Reverses the order in which notifications are shown and shows a fixed number of notifications, in case of Left and Right location.") + public boolean revNotif = false; + @Config("Notification size X") @Description("How wide a notification should be. The width in pixels is this multiplied by 100.") @Range("2..6") @@ -242,40 +248,7 @@ public class HUD extends Feature { } if(armor) { - GlStateManager.enableTexture2D(); - - ScaledResolution resolution = new ScaledResolution(mc); - int i = resolution.getScaledWidth() / 2; - int iteration = 0; - int y = resolution.getScaledHeight() - 55 - (mc.player.isInWater() ? 10 : 0); - for (ItemStack is : mc.player.inventory.armorInventory) { - iteration++; - if (is.isEmpty()) continue; - int x = i - 90 + (9 - iteration) * 20 + 2; - GlStateManager.enableDepth(); - - itemRender.zLevel = 200F; - itemRender.renderItemAndEffectIntoGUI(is, x, y); - itemRender.renderItemOverlayIntoGUI(mc.fontRenderer, is, x, y, ""); - itemRender.zLevel = 0F; - - GlStateManager.enableTexture2D(); - GlStateManager.disableLighting(); - GlStateManager.disableDepth(); - - String s = is.getCount() > 1 ? is.getCount() + "" : ""; - mc.fontRenderer.drawStringWithShadow(s, x + 19 - 2 - mc.fontRenderer.getStringWidth(s), y + 9, 0xffffff); - - - float green = ((float) is.getMaxDamage() - (float) is.getItemDamage()) / (float) is.getMaxDamage(); - float red = 1 - green; - int dmg = 100 - (int) (red * 100); - mc.fontRenderer.drawStringWithShadow(dmg + "", x + 8 - mc.fontRenderer.getStringWidth(dmg + "") / 2, y - 11, toHex((int) (red * 255), (int) (green * 255), 0)); - - } - - GlStateManager.enableDepth(); - GlStateManager.disableLighting(); + renderArmor(); } String initString = "§lBaseBand§r - \"" + BaseBand.buildString + "\" @ " + GitHash.GIT_HASH; @@ -301,6 +274,32 @@ public class HUD extends Feature { y = y + mc.fontRenderer.FONT_HEIGHT; } + if(infoLoc != InfoLocation.TopLeft) { + int x; + y = 3; + switch (infoLoc) { + case BottomRight: + y = sr.getScaledHeight() - 3 - TextSplitter.getStringHeight(infoString); + case TopRight: + x = sr.getScaledWidth() - 3 - TextSplitter.getStringWidth(infoString); + break; + default: + throw new IllegalStateException(); + } + + if(background) { + drawSizedBox(x - 1, y - 1, TextSplitter.getStringWidth(infoString) + 2, TextSplitter.getStringHeight(infoString) + 2, Pixels.mulTransparency(theme.getBackgroundColor(), 0.5f), false); + } + + TextSplitter.drawString(infoString, x, y, theme.getGreenColor(), textShadow, textMatchOrientation); + y += TextSplitter.getStringHeight(infoString); + } + + renderNotifications(sr, y); + } + + // TODO: rewrite asap + private void renderNotifications(ScaledResolution sr, int y) { int dir = (nLocation == NotificationLocation.Center ? -1 : 1); int isCenter = (nLocation == NotificationLocation.Center ? 1 : 0); int xSize = this.nSize * 100; @@ -312,7 +311,8 @@ public class HUD extends Feature { y = sr.getScaledHeight() / 4; } else { if(nLocation == NotificationLocation.Right) { - y = -5; + if(!(showInfo && infoLoc == InfoLocation.TopRight)) + y = -5; x = sr.getScaledWidth() - x - xSize; } y += 5; @@ -320,7 +320,12 @@ public class HUD extends Feature { } Notification[] notifs = HUD.notifs.toArray(new Notification[0]); - for (int i = notifs.length - 1; i >= 0; i--) { + int i; + if(revNotif) + i = 0; + else + i = notifs.length - 1; + while (i >= 0 && i < notifs.length) { Notification notif = notifs[i]; int localYSize = ySize; @@ -335,24 +340,45 @@ public class HUD extends Feature { TextSplitter.drawString(text, x + textOffset, y + textOffset - isCenter * textboxYSize, 0xffffff + (Math.max(8, notif.opacity(0xff, 2)) << 24), notif.opacity(2) == 1.0 && textShadow, false); GlStateManager.disableBlend(); y += (localYSize + nVSpace) * dir; + i += revNotif ? 1 : -1; + } + } + + private void renderArmor() { + GlStateManager.enableTexture2D(); + + ScaledResolution resolution = new ScaledResolution(mc); + int i = resolution.getScaledWidth() / 2; + int iteration = 0; + int y = resolution.getScaledHeight() - 55 - (mc.player.isInWater() ? 10 : 0); + for (ItemStack is : mc.player.inventory.armorInventory) { + iteration++; + if (is.isEmpty()) continue; + int x = i - 90 + (9 - iteration) * 20 + 2; + GlStateManager.enableDepth(); + + itemRender.zLevel = 200F; + itemRender.renderItemAndEffectIntoGUI(is, x, y); + itemRender.renderItemOverlayIntoGUI(mc.fontRenderer, is, x, y, ""); + itemRender.zLevel = 0F; + + GlStateManager.enableTexture2D(); + GlStateManager.disableLighting(); + GlStateManager.disableDepth(); + + String s = is.getCount() > 1 ? is.getCount() + "" : ""; + mc.fontRenderer.drawStringWithShadow(s, x + 19 - 2 - mc.fontRenderer.getStringWidth(s), y + 9, 0xffffff); + + + float green = ((float) is.getMaxDamage() - (float) is.getItemDamage()) / (float) is.getMaxDamage(); + float red = 1 - green; + int dmg = 100 - (int) (red * 100); + mc.fontRenderer.drawStringWithShadow(dmg + "", x + 8 - (float) mc.fontRenderer.getStringWidth(String.valueOf(dmg)) / 2, y - 11, toHex((int) (red * 255), (int) (green * 255), 0)); + } - if(infoLoc != InfoLocation.TopLeft) { - y = 3; - switch (infoLoc) { - case BottomRight: - y = sr.getScaledHeight() - 3 - TextSplitter.getStringHeight(infoString); - case TopRight: - x = sr.getScaledWidth() - 3 - TextSplitter.getStringWidth(infoString); - break; - } - - if(background) { - drawSizedBox(x - 1, y - 1, TextSplitter.getStringWidth(infoString) + 2, TextSplitter.getStringHeight(infoString) + 2, Pixels.mulTransparency(theme.getBackgroundColor(), 0.5f), false); - } - - TextSplitter.drawString(infoString, x, y, theme.getGreenColor(), textShadow, textMatchOrientation); - } + GlStateManager.enableDepth(); + GlStateManager.disableLighting(); } public static int toHex(final int r, final int g, final int b) { diff --git a/Client/src/main/java/de/com/baseband/client/registry/annotation/Requires.java b/Client/src/main/java/de/com/baseband/client/registry/annotation/Requires.java new file mode 100644 index 0000000..ef0bcb0 --- /dev/null +++ b/Client/src/main/java/de/com/baseband/client/registry/annotation/Requires.java @@ -0,0 +1,14 @@ +package de.com.baseband.client.registry.annotation; + +import de.com.baseband.client.feature.Feature; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Requires { + Class[] value(); +} diff --git a/Website b/Website new file mode 160000 index 0000000..b3c3895 --- /dev/null +++ b/Website @@ -0,0 +1 @@ +Subproject commit b3c3895b1349ba7bae90968970d8889195e8127e