Skip to content

Commit

Permalink
Supports Scaling. Fixed duplicated save entries and also fixed saving…
Browse files Browse the repository at this point in the history
… for servers
  • Loading branch information
tanishisherewithhh committed Jul 26, 2023
1 parent 85375e8 commit 2b87249
Show file tree
Hide file tree
Showing 31 changed files with 531 additions and 366 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ org.gradle.jvmargs=-Xmx3G
loader_version=0.14.21

# Mod Properties
mod_version = 1.1.4
mod_version = 1.2.0
maven_group = com.tanishisherewith
archives_base_name = dynamichud

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/tanishisherewith/dynamichud/DynamicHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.resource.LifecycledResourceManager;
import net.minecraft.server.MinecraftServer;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -101,11 +104,9 @@ public void onInitializeClient() {
if (iWigdets != null) {
if (!WIDGETS_FILE.exists()) {
if (!dynamicutil.WidgetAdded) {
printInfo("Widgets added");
iWigdets.addWigdets(dynamicutil);
}
if (!dynamicutil.MainMenuWidgetAdded) {
printInfo("MainMenu Widgets added");
iWigdets.addMainMenuWigdets(dynamicutil);
}
}
Expand All @@ -124,7 +125,17 @@ public void onInitializeClient() {
HudRenderCallback.EVENT.register((drawContext, tickDelta) -> dynamicutil.render(drawContext, tickDelta));

// Save during exiting a world, server or Minecraft itself
ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStopping);
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register(this::onEndDataPackReload);
ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE));
Runtime.getRuntime().addShutdownHook(new Thread(() -> dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE)));
}

private void onEndDataPackReload(MinecraftServer server, LifecycledResourceManager lifecycledResourceManager, boolean b) {
dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE);
}

private void onServerStopping(MinecraftServer server) {
dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE);
}
}
203 changes: 13 additions & 190 deletions src/main/java/com/tanishisherewith/dynamichud/DynamicHUDmod.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;

import static com.tanishisherewith.dynamichud.DynamicHUD.WIDGETS_FILE;
import static com.tanishisherewith.dynamichud.DynamicHUD.printInfo;

public class DynamicHUDmod implements ClientModInitializer, IWigdets, WidgetLoading {
protected Set<Widget> widgets = new HashSet<>();
Expand All @@ -44,7 +45,7 @@ public void onInitializeClient() {
@Override
public void addWigdets(DynamicUtil dynamicUtil) {
if (mc.player != null) {
System.out.println("Added widgets");
printInfo("Widgets added");
addTextWidgets(dynamicUtil);
addArmorWidgets(dynamicUtil);
addItemWidgets(dynamicUtil);
Expand All @@ -53,11 +54,11 @@ public void addWigdets(DynamicUtil dynamicUtil) {
}

private void addTextWidgets(DynamicUtil dynamicUtil) {
widgets.add(new TextWidget(mc, "FPS: ", () -> mc.fpsDebugString.split(" ")[0], 0.5f, 0.5f, true, true, false, -1, -1, true));
widgets.add(new TextWidget(mc, "Dynamic", () -> "HUD", 0.7f, 0.3f, false, false, false, -1, -1, true));
widgets.add(new TextWidget(mc, "Ping: ", () -> "", 0.08f, 0.5f, false, false, false, -1, -1, true));
widgets.add(new TextWidget(mc, "Position: ", () -> "", 0.4f, 0.8f, false, false, false, -1, -1, true));
widgets.add(new TextWidget(mc, "Day/Night: ", () -> "", 0.83f, 0.8f, false, false, false, -1, -1, true));
widgets.add(new TextWidget(mc, "FPS: ", () -> mc.fpsDebugString.split(" ")[0], 0.5f, 0.5f, true, true, -1, -1, true));
widgets.add(new TextWidget(mc, "Dynamic", () -> "HUD", 0.7f, 0.3f, false, false, -1, -1, true));
widgets.add(new TextWidget(mc, "Ping: ", () -> "", 0.08f, 0.5f, false, false, -1, -1, true));
widgets.add(new TextWidget(mc, "Position: ", () -> "", 0.4f, 0.8f, false, false, -1, -1, true));
widgets.add(new TextWidget(mc, "Day/Night: ", () -> "", 0.83f, 0.8f, false, false, -1, -1, true));

for (Widget widget : widgets) {
if (widget instanceof TextWidget textWidget) {
Expand Down Expand Up @@ -117,9 +118,11 @@ private void addItemWidgets(DynamicUtil dynamicUtil) {

@Override
public void addMainMenuWigdets(DynamicUtil dynamicUtil) {
MainMenuwidgets.add(new TextWidget(mc, "Day/Night: ", () -> "", 0.83f, 0.8f, false, false, false, -1, -1, true));
MainMenuwidgets.add(new TextWidget(mc, "Fps: ", () -> "", 0.85f, 0.3f, false, false, false, -1, -1, true));
MainMenuwidgets.add(new TextWidget(mc, "Test: ", () -> "", 0.87f, 0.5f, false, false, false, -1, -1, true));
printInfo("MainMenu Widgets added");

MainMenuwidgets.add(new TextWidget(mc, "Day/NightS: ", () -> "", 0.83f, 0.8f, false, false, -1, -1, true));
MainMenuwidgets.add(new TextWidget(mc, "FpsE: ", () -> "", 0.85f, 0.3f, false, false, -1, -1, true));
MainMenuwidgets.add(new TextWidget(mc, "Test: ", () -> "", 0.87f, 0.5f, false, false, -1, -1, true));
for (Widget mmwigdet : MainMenuwidgets) {
if (mmwigdet instanceof TextWidget textWidget) {
if (textWidget.getText().equalsIgnoreCase("fps: ")) {
Expand All @@ -143,9 +146,9 @@ public void loadWigdets(DynamicUtil dynamicUtil) {
Widget.addTextGenerator("Day/Night: ", () -> "");
Widget.addTextGenerator("Text", () -> "Text");
Widget.addTextGenerator("FPS", () -> String.valueOf(mc.getCurrentFps()));
Widget.addTextGenerator("FpsE:", () -> String.valueOf(mc.getCurrentFps()));
Widget.addTextGenerator("Label", () -> "Label");


for (Widget widget : widgets) {
dynamicUtil.getWidgetManager().addWidget(widget);
}
Expand All @@ -168,186 +171,6 @@ public Widget loadWidgetsFromTag(String className, NbtCompound widgetTag) {
}*/
return WidgetLoading.super.loadWidgetsFromTag(className, widgetTag);
}

/**
* The following code in comments (Line 100-272) code is from SMP-HACK client (not present in the latest version)
* This code shows how one can implement the library using just one class (or more, if you plan to add more widgets which I have done in that mod)
* >>
* I will add Documentation to understand this code better as it looks awful as a comment
* This is basically using advanced dynamic ItemWidgets and ArmorWidgets
* The code rn is kind of complicated for first time using this library (Don't worry, it's going to improve), But this is better than writing 10 different classes from scratch. (Not an excuss for being a bad coder)
* As an excuse, I will write a perfect documentation explaining how to use even the smallest feature. (I will also set up a template with each of the widgets with dynamic texts and colors)
*/
private void NoDocumentationDangling() {
}
/*protected List<Widget> widgets = new ArrayList<>();
protected boolean WidgetAdded = false;
protected boolean WidgetLoaded = false;
MinecraftClient mc = MinecraftClient.getInstance();
net.minecraft.item.ItemStack[] ItemStack = new ItemStack[3];
private DynamicUtil dynamicutil;
private String ArmorDurabilityDisplay;
private Color color;
private void InventoryChange() {
// Update the values of the ItemStack objects in the array
ItemStack[0] = mc.player.getInventory().get;
ItemStack[1] = mc.player.getInventory().getStack(1);
ItemStack[2] = mc.player.getInventory().getStack(3);
}
//To change the stack if the player changes his inventory
private void changeStack(WidgetManager widgetManager)
{
int stackIndex=0;
for (Widget widget: widgetManager.getWidgets())
{
if (widget instanceof ItemWidget itemWidget)
{
ItemStack itemStack = ItemStack[stackIndex++];
itemWidget.setItemStack(()->itemStack);
}
}
}
@Override
public void onInitializeClient() {
dynamicutil = new DynamicUtil(mc);
widgets.clear();
// Add default widgets if this is the first run
ClientTickEvents.START_CLIENT_TICK.register(server -> {
if (!WIDGETS_FILE.exists() && mc.player != null && !WidgetAdded) {
addWigdets(dynamicutil);
}
if (mc.player != null) InventoryChange();
if (WIDGETS_FILE.exists() && mc.player != null && !WidgetLoaded) {
loadWigdets(dynamicutil);
WidgetLoaded = true;
}
changeStack(dynamicutil.getWidgetManager());
});
DynamicHUD.setAbstractScreen(new MoveableScreenExtension(Text.of("Editor Screen"), dynamicutil));
ServerLifecycleEvents.SERVER_STOPPED.register(client -> {
dynamicutil.getWidgetManager().saveWidgets(WIDGETS_FILE);
});
HudRenderCallback.EVENT.register((matrices, tickDelta) -> {
dynamicutil.render(matrices, tickDelta);
});
}
public void addWigdets(DynamicUtil dynamicUtil) {
assert mc.player != null;
widgets.add(new ArmorWidget(mc, EquipmentSlot.CHEST, 0.01f, 0.01f, true, TextureHelper.Position.ABOVE, () -> getDurabilityForStack(mc.player.getEquippedStack(EquipmentSlot.CHEST)), () -> color));
widgets.add(new ArmorWidget(mc, EquipmentSlot.HEAD, 0.03f, 0.01f, true, TextureHelper.Position.BELOW, () -> getDurabilityForStack(mc.player.getEquippedStack(EquipmentSlot.HEAD)), () -> color));
widgets.add(new ArmorWidget(mc, EquipmentSlot.LEGS, 0.05f, 0.01f, true, TextureHelper.Position.LEFT, () -> getDurabilityForStack(mc.player.getEquippedStack(EquipmentSlot.LEGS)), () -> color));
widgets.add(new ItemWidget(mc, () -> mc.player.getInventory().getStack(4), 0.05f, 0.05f, true, TextureHelper.Position.ABOVE, () -> String.valueOf(getItemCount(4)), () -> Color.YELLOW));
widgets.add(new ItemWidget(mc, () -> mc.player.getInventory().getStack(1), 0.1f, 0.1f, true, TextureHelper.Position.ABOVE, () -> String.valueOf(getItemCount(1)), () -> Color.YELLOW));
widgets.add(new ItemWidget(mc, () -> mc.player.getInventory().getStack(3), 0.15f, 0.15f, true, TextureHelper.Position.ABOVE, () -> String.valueOf(getItemCount(3)), () -> Color.YELLOW));
widgets.add(new Muppet(mc, 0.9f, 0.8f, true));
for (Widget wigdet : widgets) {
dynamicutil.getWidgetManager().addWidget(wigdet);
}
WidgetAdded = true;
}
private int getItemCount(int slot) {
return mc.player.getInventory().count(mc.player.getInventory().getStack(slot).getItem());
}
public void setColor(Color color) {
this.color = color;
}
private String getDurabilityForStack(ItemStack stack) {
String durabilityText = null;
PlayerEntity player = MinecraftClient.getInstance().player;
if (player != null) {
ArmorDurabilityDisplay = String.valueOf(ConfigUtil.config.ArmorDurability);
double durabilityPercentage = ((double) stack.getMaxDamage() - (double) stack.getDamage()) / (double) stack.getMaxDamage();
durabilityText = (int) (durabilityPercentage * 100.0D) + "%";
if (durabilityPercentage > 0.65D) setColor(Color.GREEN); // green
else if (durabilityPercentage > 0.25D) setColor(Color.YELLOW); // yellow
else setColor(Color.RED);
switch (ArmorDurabilityDisplay) {
case "Percent" -> {
//Percent Display
return (stack.getDamage() > 0) ? durabilityText : null;
}
case "Bar" -> {
// Bar display
int barWidth = (int) (durabilityPercentage * 16);
return (stack.getDamage() > 0) ? String.valueOf(barWidth) : null;
}
// DrawHelper.fill(matrices,Wodge,mc.textRenderer.fontHeight*0.5f,);
// DrawableHelper.drawBorder(matrices, x + i * 20 -1 , (y-30)*2 , x + i * 20 + barWidth , (y-26)*2 , color);
case "Number" -> {
//Number display
return (stack.getDamage() > 0) ? stack.getMaxDamage() - stack.getDamage() + "/" + stack.getMaxDamage() : null;
}
}
}
return durabilityText;
}
public void loadWigdets(DynamicUtil dynamicUtil) {
dynamicUtil.getWidgetManager().setWidgetLoading(new DynamicHUDLoading());
List<Widget> widgets = dynamicUtil.getWidgetManager().loadWigdets(WIDGETS_FILE);
int armorIndex = 0;
int itemTextIndex = 0;
int itemStackIndex = 0;
assert mc.player != null;
TextGenerator[] ArmorWidgetText = new TextGenerator[]{
() -> getDurabilityForStack(mc.player.getEquippedStack(EquipmentSlot.CHEST)),
() -> getDurabilityForStack(mc.player.getEquippedStack(EquipmentSlot.HEAD)),
() -> getDurabilityForStack(mc.player.getEquippedStack(EquipmentSlot.LEGS))
};
TextGenerator[] ItemWidgetText = new TextGenerator[]{
() -> String.valueOf(getItemCount(4)),
() -> String.valueOf(getItemCount(1)),
() -> String.valueOf(getItemCount(3))
};
for (Widget widget : widgets) {
if (widget instanceof ArmorWidget armorWidget) {
if (armorIndex<3) {
TextGenerator textGenerator = ArmorWidgetText[armorIndex++];
// TextGenerator textGenerator = ()-> String.valueOf(mc.player.getInventory().count(Items.TOTEM_OF_UNDYING));
armorWidget.setTextGenerator(textGenerator);
armorWidget.setColor(() -> color);
}
dynamicutil.getWidgetManager().addWidget(armorWidget);
}
if (widget instanceof ItemWidget itemWidget) {
TextGenerator textGenerator = ItemWidgetText[itemTextIndex++];
itemWidget.setTextGenerator(textGenerator);
ItemStack itemStack = ItemStack[itemStackIndex++];
itemWidget.setItemStack(() -> itemStack);
dynamicutil.getWidgetManager().addWidget(itemWidget);
}
if (widget instanceof Muppet muppet) {
dynamicutil.getWidgetManager().addWidget(muppet);
}
}
}
@Override
public Widget loadWidgetsFromTag(String className, NbtCompound widgetTag) {
if (className.equals(Muppet.class.getName())) {
Muppet widget = new Muppet(MinecraftClient.getInstance(), 0, 0, true);
widget.readFromTag(widgetTag);
return widget;
}
return WidgetLoading.super.loadWidgetsFromTag(className, widgetTag);
}
*/
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DefaultDragHandler implements DragHandler {

@Override
public boolean startDragging(Widget widget, double mouseX, double mouseY) {
if (widget.getWidgetBox().contains(widget, mouseX, mouseY)) {
if (widget.getWidgetBox().contains(widget, mouseX, mouseY, Widget.getScale())) {
dragStartX = (int) (mouseX - widget.getX());
dragStartY = (int) (mouseY - widget.getY());
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public boolean mouseDragged(double mouseX, double mouseY, int button, double del
if (sliderWidget != null) {
for (SliderWidget sliderWidget : sliderWidget) {
if (sliderWidget != null && sliderWidget.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) {
TextWidget.setRainbowSpeed(sliderWidget.getValue());
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package com.tanishisherewith.dynamichud.helpers;

import net.minecraft.network.packet.Packet;
import net.minecraft.util.math.MathHelper;

import java.awt.*;

/**
* This class provides helper methods for working with colors.
*/
public class ColorHelper {
public static int r, g, b, a;
public ColorHelper(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
this.a = 255;
validate();
}
public ColorHelper() {
}
public void validate() {
if (r < 0) r = 0;
else if (r > 255) r = 255;

if (g < 0) g = 0;
else if (g > 255) g = 255;

if (b < 0) b = 0;
else if (b > 255) b = 255;

if (a < 0) a = 0;
else if (a > 255) a = 255;
}
/**
* Returns a color as an integer value given its red, green and blue components.
*
Expand Down Expand Up @@ -57,5 +83,38 @@ public static int getColorFromHue(float hue) {
public static int ColorToInt(Color color) {
return color.getRGB();
}
public static float[] getRainbowColor()
{
float x = System.currentTimeMillis() % 2000 / 1000F;
float pi = (float)Math.PI;

float[] rainbow = new float[3];
rainbow[0] = 0.5F + 0.5F * MathHelper.sin(x * pi);
rainbow[1] = 0.5F + 0.5F * MathHelper.sin((x + 4F / 3F) * pi);
rainbow[2] = 0.5F + 0.5F * MathHelper.sin((x + 8F / 3F) * pi);
return rainbow;
}
public static int fromRGBA(int r, int g, int b, int a) {
return (r << 16) + (g << 8) + (b) + (a << 24);
}

public static int toRGBAR(int color) {
return (color >> 16) & 0x000000FF;
}

public static int toRGBAG(int color) {
return (color >> 8) & 0x000000FF;
}

public static int toRGBAB(int color) {
return (color) & 0x000000FF;
}

public static int toRGBAA(int color) {
return (color >> 24) & 0x000000FF;
}
public int toInt()
{
return new Color(r,b,g,a).getRGB();
}
}
Loading

0 comments on commit 2b87249

Please sign in to comment.