Slight HUD refactor, allow modules to depend on each other more easily
Some checks failed
/ Build BaseBand DSM & Broadway (push) Has been cancelled

This commit is contained in:
Daniella / Tove 2024-06-18 00:00:29 +02:00
parent b6b3ea61a1
commit 15852f0378
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
6 changed files with 139 additions and 60 deletions

View file

@ -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();
}
}
}

View file

@ -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<Class<? extends Feature>> 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<? extends Feature> 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<? extends Feature> 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();
}
}
}

View file

@ -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<EntityLivingBase> getAttackable() {
ArrayList<EntityLivingBase> 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

View file

@ -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,6 +248,103 @@ public class HUD extends Feature {
}
if(armor) {
renderArmor();
}
String initString = "§lBaseBand§r - \"" + BaseBand.buildString + "\" @ " + GitHash.GIT_HASH;
if(infoLoc == InfoLocation.TopLeft) {
initString += "\n" + infoString + "\n";
}
int maxWidth = TextSplitter.getStringWidth(initString);
Feature[] renderFeatures = Arrays.stream(Features.features).filter(m -> m.enabled && m.category != Category.COMMAND && m.getClass() != Client.class && m != this && m.displayOnHUD()).sorted(Comparator.comparingDouble(value -> -Minecraft.getMinecraft().fontRenderer.getStringWidth(includeStatus ? value.text : value.toString()))).toArray(Feature[]::new);
for (Feature f : renderFeatures) {
maxWidth = Math.max(mc.fontRenderer.getStringWidth(f.getHUDText()), maxWidth);
}
if(background) {
Gui.drawRect(2, 2, maxWidth + 4, renderFeatures.length * mc.fontRenderer.FONT_HEIGHT + TextSplitter.getStringHeight(initString) + 4, Pixels.mulTransparency(theme.getBackgroundColor(), 0.5f));
}
TextSplitter.drawString(initString, 3,3, theme.getGreenColor(), textShadow, false);
int y = 3 + TextSplitter.getStringHeight(initString);
for (int i = 0; i < renderFeatures.length; i++) {
Feature f = renderFeatures[i];
mc.fontRenderer.drawString(f.getHUDText(), 3, y, getColor(f, i), textShadow);
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;
int ySize = this.nVSize * 10;
int textOffset = (ySize / 2 - (mc.fontRenderer.FONT_HEIGHT / 2));
int x = 2;
if(nLocation == NotificationLocation.Center) {
x = sr.getScaledWidth() / 2 - (xSize / 2);
y = sr.getScaledHeight() / 4;
} else {
if(nLocation == NotificationLocation.Right) {
if(!(showInfo && infoLoc == InfoLocation.TopRight))
y = -5;
x = sr.getScaledWidth() - x - xSize;
}
y += 5;
y += nVSpace;
}
Notification[] notifs = HUD.notifs.toArray(new Notification[0]);
int i;
if(revNotif)
i = 0;
else
i = notifs.length - 1;
while (i >= 0 && i < notifs.length) {
Notification notif = notifs[i];
int localYSize = ySize;
String text = TextSplitter.breakText(notif.text, xSize - textOffset * 2);
localYSize += TextSplitter.getStringHeight(text) - mc.fontRenderer.FONT_HEIGHT;
int textboxYSize = localYSize;
localYSize = notif.opacity(localYSize, 1);
drawSizedBox(x, y, xSize, localYSize, 0x202040 + (notif.opacity(0x80, 1) << 24), isCenter == 1);
GlStateManager.enableBlend();
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);
@ -270,7 +373,7 @@ public class HUD extends Feature {
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));
mc.fontRenderer.drawStringWithShadow(dmg + "", x + 8 - (float) mc.fontRenderer.getStringWidth(String.valueOf(dmg)) / 2, y - 11, toHex((int) (red * 255), (int) (green * 255), 0));
}
@ -278,83 +381,6 @@ public class HUD extends Feature {
GlStateManager.disableLighting();
}
String initString = "§lBaseBand§r - \"" + BaseBand.buildString + "\" @ " + GitHash.GIT_HASH;
if(infoLoc == InfoLocation.TopLeft) {
initString += "\n" + infoString + "\n";
}
int maxWidth = TextSplitter.getStringWidth(initString);
Feature[] renderFeatures = Arrays.stream(Features.features).filter(m -> m.enabled && m.category != Category.COMMAND && m.getClass() != Client.class && m != this && m.displayOnHUD()).sorted(Comparator.comparingDouble(value -> -Minecraft.getMinecraft().fontRenderer.getStringWidth(includeStatus ? value.text : value.toString()))).toArray(Feature[]::new);
for (Feature f : renderFeatures) {
maxWidth = Math.max(mc.fontRenderer.getStringWidth(f.getHUDText()), maxWidth);
}
if(background) {
Gui.drawRect(2, 2, maxWidth + 4, renderFeatures.length * mc.fontRenderer.FONT_HEIGHT + TextSplitter.getStringHeight(initString) + 4, Pixels.mulTransparency(theme.getBackgroundColor(), 0.5f));
}
TextSplitter.drawString(initString, 3,3, theme.getGreenColor(), textShadow, false);
int y = 3 + TextSplitter.getStringHeight(initString);
for (int i = 0; i < renderFeatures.length; i++) {
Feature f = renderFeatures[i];
mc.fontRenderer.drawString(f.getHUDText(), 3, y, getColor(f, i), textShadow);
y = y + mc.fontRenderer.FONT_HEIGHT;
}
int dir = (nLocation == NotificationLocation.Center ? -1 : 1);
int isCenter = (nLocation == NotificationLocation.Center ? 1 : 0);
int xSize = this.nSize * 100;
int ySize = this.nVSize * 10;
int textOffset = (ySize / 2 - (mc.fontRenderer.FONT_HEIGHT / 2));
int x = 2;
if(nLocation == NotificationLocation.Center) {
x = sr.getScaledWidth() / 2 - (xSize / 2);
y = sr.getScaledHeight() / 4;
} else {
if(nLocation == NotificationLocation.Right) {
y = -5;
x = sr.getScaledWidth() - x - xSize;
}
y += 5;
y += nVSpace;
}
Notification[] notifs = HUD.notifs.toArray(new Notification[0]);
for (int i = notifs.length - 1; i >= 0; i--) {
Notification notif = notifs[i];
int localYSize = ySize;
String text = TextSplitter.breakText(notif.text, xSize - textOffset * 2);
localYSize += TextSplitter.getStringHeight(text) - mc.fontRenderer.FONT_HEIGHT;
int textboxYSize = localYSize;
localYSize = notif.opacity(localYSize, 1);
drawSizedBox(x, y, xSize, localYSize, 0x202040 + (notif.opacity(0x80, 1) << 24), isCenter == 1);
GlStateManager.enableBlend();
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;
}
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);
}
}
public static int toHex(final int r, final int g, final int b) {
return 0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF);
}

View file

@ -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<? extends Feature>[] value();
}

1
Website Submodule

@ -0,0 +1 @@
Subproject commit b3c3895b1349ba7bae90968970d8889195e8127e