diff --git a/src/main/java/org/saturnclient/modules/HudMod.java b/src/main/java/org/saturnclient/modules/HudMod.java index 881e02d..fa5b280 100644 --- a/src/main/java/org/saturnclient/modules/HudMod.java +++ b/src/main/java/org/saturnclient/modules/HudMod.java @@ -1,11 +1,11 @@ package org.saturnclient.modules; -import net.minecraft.client.gui.DrawContext; +import org.saturnclient.ui2.RenderScope; public interface HudMod { public ModDimensions getDimensions(); - public void renderDummy(DrawContext context); + public void renderDummy(RenderScope scope); - public void render(DrawContext context); + public void renderHud(RenderScope scope); } diff --git a/src/main/java/org/saturnclient/modules/ModDimensions.java b/src/main/java/org/saturnclient/modules/ModDimensions.java index deafd46..013f6d2 100644 --- a/src/main/java/org/saturnclient/modules/ModDimensions.java +++ b/src/main/java/org/saturnclient/modules/ModDimensions.java @@ -1,64 +1,49 @@ package org.saturnclient.modules; -import org.saturnclient.saturnclient.config.ConfigManager; +import java.util.Map; + +import org.saturnclient.saturnclient.config.NamedProperty; import org.saturnclient.saturnclient.config.Property; public class ModDimensions { public Property x = new Property<>(0); public Property y = new Property<>(0); public Property scale = new Property<>(1.0f); + public Property bgColor = new Property<>(0xFF000000, Property.PropertyType.HEX); + public Property fgColor = new Property<>(0xFFffffff, Property.PropertyType.HEX); + public Property radius = new Property<>(0); public int width = 0; public int height = 0; - private final ConfigManager dimensionsConfig; - - public ModDimensions(ConfigManager configManager) { - dimensionsConfig = new ConfigManager(configManager, "Dimensions"); - dimensionsConfig.property("x", this.x); - dimensionsConfig.property("y", this.y); - dimensionsConfig.property("scale", this.scale); + public NamedProperty>> prop() { + return new Property>>(Map.of( + "X", x, + "Y", y, + "Scale", scale, + "Background Color", bgColor, + "Foreground Color", fgColor, + "Corner Radius", radius + )).named("Hud Position"); } - public ModDimensions(ConfigManager configManager, int x, int y) { - dimensionsConfig = new ConfigManager(configManager, "Dimensions"); - this.x.value = x; - this.y.value = y; - dimensionsConfig.property("x", this.x); - dimensionsConfig.property("y", this.y); - dimensionsConfig.property("scale", this.scale); + public ModDimensions(int width, int height) { + this.width = width; + this.height = height; } - public ModDimensions(ConfigManager configManager, int x, int y, int width, int height) { - dimensionsConfig = new ConfigManager(configManager, "Dimensions"); + public ModDimensions(int x, int y, int width, int height) { this.x.value = x; this.y.value = y; this.width = width; this.height = height; - dimensionsConfig.property("x", this.x); - dimensionsConfig.property("y", this.y); - dimensionsConfig.property("scale", this.scale); } - public ModDimensions(ConfigManager configManager, int x, int y, float scale) { - dimensionsConfig = new ConfigManager(configManager, "Dimensions"); - this.x.value = x; - this.y.value = y; - this.scale.value = scale; - dimensionsConfig.property("x", this.x); - dimensionsConfig.property("y", this.y); - dimensionsConfig.property("scale", this.scale); - } - - public ModDimensions(ConfigManager configManager, int x, int y, int width, int height, float scale) { - dimensionsConfig = new ConfigManager(configManager, "Dimensions"); + public ModDimensions(int x, int y, int width, int height, float scale) { this.x.value = x; this.y.value = y; this.scale.value = scale; this.width = width; this.height = height; - dimensionsConfig.property("x", this.x); - dimensionsConfig.property("y", this.y); - dimensionsConfig.property("scale", this.scale); } } diff --git a/src/main/java/org/saturnclient/modules/ModManager.java b/src/main/java/org/saturnclient/modules/ModManager.java index 2eba97f..12f5b46 100644 --- a/src/main/java/org/saturnclient/modules/ModManager.java +++ b/src/main/java/org/saturnclient/modules/ModManager.java @@ -3,7 +3,6 @@ package org.saturnclient.modules; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.util.math.MatrixStack; import org.saturnclient.modules.mods.*; import org.saturnclient.saturnclient.SaturnClient; @@ -15,6 +14,7 @@ public class ModManager { public static Module[] MODS = { new Crosshair(), new AutoSprint(), + new ArmorDisplay() }; public static void init() { @@ -38,16 +38,17 @@ public class ModManager { if (m instanceof HudMod && m.isEnabled()) { ModDimensions dim = ((HudMod) m).getDimensions(); - MatrixStack matrices = context.getMatrices(); - matrices.push(); + renderScope.matrices.push(); - matrices.translate(dim.x.value, (float) dim.y.value, 0); + renderScope.matrices.translate(dim.x.value, (float) dim.y.value, 0); - matrices.scale(dim.scale.value, dim.scale.value, 1.0f); + renderScope.matrices.scale(dim.scale.value, dim.scale.value, 1.0f); - ((HudMod) m).render(context); + renderScope.drawRoundedRectangle(0, 0, dim.width, dim.height, dim.radius.value, dim.bgColor.value); - matrices.pop(); + ((HudMod) m).renderHud(renderScope); + + renderScope.matrices.pop(); } else { m.render(renderScope); } diff --git a/src/main/java/org/saturnclient/modules/mods/ArmorDisplay.java b/src/main/java/org/saturnclient/modules/mods/ArmorDisplay.java new file mode 100644 index 0000000..fe9806e --- /dev/null +++ b/src/main/java/org/saturnclient/modules/mods/ArmorDisplay.java @@ -0,0 +1,71 @@ +package org.saturnclient.modules.mods; + +import org.saturnclient.modules.HudMod; +import org.saturnclient.modules.ModDimensions; +import org.saturnclient.modules.Module; +import org.saturnclient.modules.ModuleDetails; +import org.saturnclient.saturnclient.SaturnClient; +import org.saturnclient.saturnclient.config.Property; +import org.saturnclient.ui2.RenderScope; + +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; + +public class ArmorDisplay extends Module implements HudMod { + private static Property enabled = new Property<>(true); + private static ModDimensions dimensions = new ModDimensions(40, 60); + + public ArmorDisplay() { + super(new ModuleDetails("Armor Display", "armor").description("Displays armor durability") + .version("0.1.0") + .tags("utility"), + enabled.named("Enabled"), + dimensions.prop()); + } + + public void renderArmor(RenderScope scope, ItemStack helmet, ItemStack chestplate, ItemStack leggings, ItemStack boots) { + scope.drawItem(helmet, 0, 0); + scope.drawItem(chestplate, 0, 15); + scope.drawItem(leggings, 0, 30); + scope.drawItem(boots, 0, 45); + + renderHealth(scope, 0, helmet.getMaxDamage(), helmet.getDamage()); + renderHealth(scope, 1, chestplate.getMaxDamage(), chestplate.getDamage()); + renderHealth(scope, 2, leggings.getMaxDamage(), leggings.getDamage()); + renderHealth(scope, 3, boots.getMaxDamage(), boots.getDamage()); + } + + public void renderHealth(RenderScope scope, int i, int max, int current) { + scope.drawText(0.5f, String.valueOf(((double) current / max) * 100) + "%", 17, (15 * i) + 3, false, dimensions.fgColor.value); + } + + @Override + public void renderHud(RenderScope scope) { + renderArmor(scope, + SaturnClient.client.player.getEquippedStack(EquipmentSlot.HEAD), + SaturnClient.client.player.getEquippedStack(EquipmentSlot.CHEST), + SaturnClient.client.player.getEquippedStack(EquipmentSlot.LEGS), + SaturnClient.client.player.getEquippedStack(EquipmentSlot.FEET)); + } + + @Override + public void renderDummy(RenderScope scope) { + renderArmor(scope, new ItemStack(Items.DIAMOND_HELMET), new ItemStack(Items.DIAMOND_CHESTPLATE), new ItemStack(Items.DIAMOND_LEGGINGS), new ItemStack(Items.DIAMOND_BOOTS)); + } + + @Override + public ModDimensions getDimensions() { + return dimensions; + } + + @Override + public boolean isEnabled() { + return enabled.value; + } + + @Override + public void setEnabled(boolean e) { + enabled.value = e; + } +} diff --git a/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java b/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java index 3bcf10c..5affa2d 100644 --- a/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java +++ b/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java @@ -40,6 +40,12 @@ public class ConfigManager { System.out.println("Created sub-namespace: " + namespace); } + public void sub(String namespace) { + Property>> namespaceProperty = new Property<>(currentMap, + Property.PropertyType.NAMESPACE); + property(namespace, namespaceProperty); + } + // Generic method to store any type of property public Property property(String name, Property value) { System.out.println("Adding property: " + name); diff --git a/src/main/java/org/saturnclient/saturnclient/menus/HudEditor.java b/src/main/java/org/saturnclient/saturnclient/menus/HudEditor.java index 2d9b92a..0973d16 100644 --- a/src/main/java/org/saturnclient/saturnclient/menus/HudEditor.java +++ b/src/main/java/org/saturnclient/saturnclient/menus/HudEditor.java @@ -10,6 +10,8 @@ import org.saturnclient.modules.ModDimensions; import org.saturnclient.modules.ModManager; import org.saturnclient.modules.Module; import org.saturnclient.saturnclient.config.ConfigManager; +import org.saturnclient.saturnclient.mixin.DrawContextAccessor; +import org.saturnclient.ui2.RenderScope; import org.saturnclient.ui2.SaturnScreen; import java.util.ArrayList; @@ -93,6 +95,8 @@ public class HudEditor extends Screen { SaturnScreen.ROTATING_PANORAMA_RENDERER.render(context, this.width, this.height, 1.0F, delta); } + RenderScope scope = new RenderScope(context.getMatrices(), ((DrawContextAccessor) context).getVertexConsumers()); + for (HudMod mod : hudMods) { ModDimensions dim = mod.getDimensions(); MatrixStack matrices = context.getMatrices(); @@ -102,7 +106,7 @@ public class HudEditor extends Screen { matrices.scale(dim.scale.value, dim.scale.value, 1.0f); // Render the mod - mod.renderDummy(context); + mod.renderDummy(scope); matrices.pop(); diff --git a/src/main/java/org/saturnclient/ui2/RenderScope.java b/src/main/java/org/saturnclient/ui2/RenderScope.java index 53285c7..c1793c8 100644 --- a/src/main/java/org/saturnclient/ui2/RenderScope.java +++ b/src/main/java/org/saturnclient/ui2/RenderScope.java @@ -15,13 +15,23 @@ import org.saturnclient.saturnclient.SaturnClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.font.TextRenderer.TextLayerType; import net.minecraft.client.gui.ScreenRect; +import net.minecraft.client.render.DiffuseLighting; +import net.minecraft.client.render.OverlayTexture; import net.minecraft.client.render.RenderLayer; import net.minecraft.client.render.VertexConsumer; import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.render.item.ItemRenderState; import net.minecraft.client.util.Window; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ModelTransformationMode; import net.minecraft.util.Identifier; +import net.minecraft.util.crash.CrashException; +import net.minecraft.util.crash.CrashReport; +import net.minecraft.util.crash.CrashReportSection; import net.minecraft.util.math.RotationAxis; +import net.minecraft.world.World; public class RenderScope { public MatrixStack matrices; @@ -29,11 +39,13 @@ public class RenderScope { private Function renderLayers; private ScissorStack scissorStack = new ScissorStack(); private int opacity = 255 << 24; + private final ItemRenderState itemRenderState; public RenderScope(MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers) { this.matrices = matrices; this.vertexConsumers = vertexConsumers; this.scissorStack = new ScissorStack(); + this.itemRenderState = new ItemRenderState(); } public void setOpacity(float alpha) { @@ -80,11 +92,11 @@ public class RenderScope { vertexConsumer.vertex(matrix4f, (float) x2, (float) y1, 0).color(color); } - public void drawText(String text, int x, int y, boolean bold, int color) { + public void drawText(String text, int x, int y, boolean bold, int color) { drawText(1.0f, text, x, y, bold, color); - } + } - public void drawText(float scale, String text, int x, int y, boolean bold, int color) { + public void drawText(float scale, String text, int x, int y, boolean bold, int color) { int i = 0; for (String line : text.split("\n")) { color = getColor(color); @@ -276,4 +288,70 @@ public class RenderScope { return this.stack.isEmpty() ? true : this.stack.peek().contains(p_329411_, p_333404_); } } + + public void drawItem(ItemStack item, int x, int y) { + this.drawItem(SaturnClient.client.player, SaturnClient.client.world, item, x, y, 0); + } + + public void drawItem(ItemStack stack, int x, int y, int seed) { + this.drawItem(SaturnClient.client.player, SaturnClient.client.world, stack, x, y, seed); + } + + public void drawItem(ItemStack stack, int x, int y, int seed, int z) { + this.drawItem(SaturnClient.client.player, SaturnClient.client.world, stack, x, y, seed, z); + } + + public void drawItemWithoutEntity(ItemStack stack, int x, int y) { + this.drawItemWithoutEntity(stack, x, y, 0); + } + + public void drawItemWithoutEntity(ItemStack stack, int x, int y, int seed) { + this.drawItem((LivingEntity)null, SaturnClient.client.world, stack, x, y, seed); + } + + public void drawItem(LivingEntity entity, ItemStack stack, int x, int y, int seed) { + this.drawItem(entity, entity.getWorld(), stack, x, y, seed); + } + + private void drawItem(@Nullable LivingEntity entity, @Nullable World world, ItemStack stack, int x, int y, int seed) { + this.drawItem(entity, world, stack, x, y, seed, 0); + } + + private void drawItem(@Nullable LivingEntity entity, @Nullable World world, ItemStack stack, int x, int y, int seed, int z) { + if (!stack.isEmpty()) { + SaturnClient.client.getItemModelManager().update(this.itemRenderState, stack, ModelTransformationMode.GUI, false, world, entity, seed); + this.matrices.push(); + this.matrices.translate((float)(x + 8), (float)(y + 8), (float)(150 + (this.itemRenderState.hasDepth() ? z : 0))); + + try { + this.matrices.scale(16.0F, -16.0F, 16.0F); + boolean bl = !this.itemRenderState.isSideLit(); + if (bl) { + this.draw(); + DiffuseLighting.disableGuiDepthLighting(); + } + + this.itemRenderState.render(this.matrices, this.vertexConsumers, 15728880, OverlayTexture.DEFAULT_UV); + this.draw(); + if (bl) { + DiffuseLighting.enableGuiDepthLighting(); + } + } catch (Throwable var11) { + CrashReport crashReport = CrashReport.create(var11, "Rendering item"); + CrashReportSection crashReportSection = crashReport.addElement("Item being rendered"); + crashReportSection.add("Item Type", () -> { + return String.valueOf(stack.getItem()); + }); + crashReportSection.add("Item Components", () -> { + return String.valueOf(stack.getComponents()); + }); + crashReportSection.add("Item Foil", () -> { + return String.valueOf(stack.hasGlint()); + }); + throw new CrashException(crashReport); + } + + this.matrices.pop(); + } + } }