Skip to content

Commit

Permalink
Fixed a few bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Apr 13, 2024
1 parent 9ac1100 commit 18665bb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.tanishisherewith.dynamichud.newTrial.widget.WidgetRenderer;
import com.tanishisherewith.dynamichud.newTrial.widgets.TextWidget;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand All @@ -30,7 +31,7 @@ public class DynamicHUD implements ClientModInitializer {
* <p>
* Allows saving widgets across different mods with same save file name.
*/
public static final HashMap<String, List<Widget>> fileMap = new HashMap<>();
public static final HashMap<String, List<Widget>> FILE_MAP = new HashMap<>();
private static final Logger logger = LoggerFactory.getLogger("DynamicHud");
private static final List<WidgetRenderer> widgetRenderers = new ArrayList<>();
public static MinecraftClient MC = MinecraftClient.getInstance();
Expand Down Expand Up @@ -89,46 +90,63 @@ public void onInitializeClient() {
File widgetsFile;
try {
DynamicHudIntegration DHIntegration = entrypoint.getEntrypoint();

//Calls the init method
DHIntegration.init();

//Gets the widget file to save and load the widgets from
widgetsFile = DHIntegration.getWidgetsFile();

// Adds / loads widgets from file
if (widgetsFile.exists()) {
WidgetManager.loadWidgets(widgetsFile);
} else {
DHIntegration.addWidgets();
}

//Calls the second init method
DHIntegration.initAfter();

// Get the instance of AbstractMoveableScreen
screen = DHIntegration.getMovableScreen();

// Get the keybind to open the screen instance
binding = DHIntegration.getKeyBind();

//Register custom widget datas by WidgetManager.registerCustomWidgets();
DHIntegration.registerCustomWidgets();

//WidgetRenderer with widgets instance
widgetRenderer = DHIntegration.getWidgetRenderer();
addWidgetRenderer(widgetRenderer);

List<Widget> widgets = fileMap.get(widgetsFile.getName());
List<Widget> widgets = FILE_MAP.get(widgetsFile.getName());

if (widgets == null) {
fileMap.put(widgetsFile.getName(), widgetRenderer.getWidgets());
if (widgets == null || widgets.isEmpty()) {
FILE_MAP.put(widgetsFile.getName(), widgetRenderer.getWidgets());
} else {
widgetRenderer.getWidgets().addAll(widgets);
fileMap.put(widgetsFile.getName(), widgetRenderer.getWidgets());
widgets.addAll(widgetRenderer.getWidgets());
FILE_MAP.put(widgetsFile.getName(), widgets);
}

//Register events for rendering, saving, loading, and opening the hudEditor
ClientTickEvents.START_CLIENT_TICK.register((client) -> {
openDynamicScreen(binding, screen);
});

// Save during exiting a world, server or Minecraft itself
// Also saved when a resource pack is reloaded.
ServerLifecycleEvents.SERVER_STOPPING.register(server -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName())));
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName())));
ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName())));
Runtime.getRuntime().addShutdownHook(new Thread(() -> saveWidgetsSafely(widgetsFile, fileMap.get(widgetsFile.getName()))));
/* === Saving === */

//When a player exits a world (SinglePlayer worlds) or a server stops
ServerLifecycleEvents.SERVER_STOPPING.register(server -> saveWidgetsSafely(widgetsFile, FILE_MAP.get(widgetsFile.getName())));

// When a resource pack is reloaded.
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> saveWidgetsSafely(widgetsFile, FILE_MAP.get(widgetsFile.getName())));

//When player disconnects
ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> saveWidgetsSafely(widgetsFile, FILE_MAP.get(widgetsFile.getName())));

//When minecraft closes
ClientLifecycleEvents.CLIENT_STOPPING.register((minecraftClient)->saveWidgetsSafely(widgetsFile,FILE_MAP.get(widgetsFile.getName())));

printInfo(String.format("Integration of mod %s was successful", modId));
} catch (Throwable e) {
Expand All @@ -143,7 +161,7 @@ public void onInitializeClient() {
ServerLifecycleEvents.SERVER_STOPPING.register(server -> GlobalConfig.HANDLER.save());
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, s) -> GlobalConfig.HANDLER.save());
ServerPlayConnectionEvents.DISCONNECT.register((handler, packetSender) -> GlobalConfig.HANDLER.save());
Runtime.getRuntime().addShutdownHook(new Thread(() -> GlobalConfig.HANDLER.save()));
ClientLifecycleEvents.CLIENT_STOPPING.register((minecraftClient)->GlobalConfig.HANDLER.save());

HudRenderCallback.EVENT.register(new HudRender());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tanishisherewith.dynamichud.newTrial;

import com.tanishisherewith.dynamichud.newTrial.screens.AbstractMoveableScreen;
import com.tanishisherewith.dynamichud.newTrial.widget.WidgetData;
import com.tanishisherewith.dynamichud.newTrial.widget.WidgetManager;
import com.tanishisherewith.dynamichud.newTrial.widget.WidgetRenderer;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
Expand Down Expand Up @@ -75,6 +76,14 @@ public interface DynamicHudIntegration {

/**
* To register custom widgets. This method can be overridden by implementations.
* <p>
* Use {@link WidgetManager#registerCustomWidget(WidgetData)} to register custom widgets.
* <pre>
* Example:
* {@code
* WidgetManager.registerCustomWidget(TextWidget.DATA);
* }
* </pre>
*/
default void registerCustomWidgets() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public AbstractMoveableScreen(Text title, WidgetRenderer renderer) {
this.widgetRenderer = renderer;
}

@Override
public void onDisplayed() {
super.onDisplayed();
widgetRenderer.isInEditor = true;
}

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
widgetRenderer.mouseDragged(mouseX, mouseY, button, snapSize);
Expand Down Expand Up @@ -68,7 +74,6 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
this.renderBackgroundTexture(drawContext);
}
// Draw each widget
widgetRenderer.isInEditor = true;
widgetRenderer.renderWidgets(drawContext, mouseX, mouseY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ContextMenu {
public int x, y;
public int width = 0;
public int height = 0;
public int backgroundColor = new Color(107, 112, 126, 124).getRGB();// Semi-transparent light grey color
public int backgroundColor = new Color(107, 112, 126, 124).getRGB();// Semi-transparent light greyish - blue color
public int padding = 5; // The amount of padding around the rectangle
public int heightOffset = 4; // Height offset from the widget
public boolean shouldDisplay = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private void renderWidgetInEditor(DrawContext context, int mouseX, int mouseY) {

renderWidget(context, mouseX, mouseY);
}
/* Input related methods. Override with super call to add your own input-based code like contextMenu */

public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (widgetBox.isMouseOver(mouseX, mouseY) && button == GLFW.GLFW_MOUSE_BUTTON_LEFT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public void writeToTag(NbtCompound tag) {
tag.putBoolean("Rainbow", rainbow);
tag.putInt("TextColor", textColor.getRGB());
tag.putInt("RainbowSpeed", rainbowSpeed);

// If true then it means that we should use local registry and if false (i.e. null) then use global registry
tag.putBoolean("DynamicValueRegistry", dynamicValueRegistry != null);
}
Expand All @@ -150,17 +149,16 @@ public void readFromTag(NbtCompound tag) {

// If true then it means that we should use local registry and if false (i.e. null) then use global registry
boolean dvrObj = tag.getBoolean("DynamicValueRegistry");
System.out.println(dvrObj);
if (!dvrObj) {
this.textSupplier = (Supplier<String>) DynamicValueRegistry.getGlobal(dynamicRegistryKey);
System.out.println(textSupplier);
return;
}

for (DynamicValueRegistry dvr : DynamicValueRegistry.getInstances(modId)) {
//Unfortunately, this method takes the value from the first local registry with the key.
//It returns to prevent overriding with other registries
this.textSupplier = (Supplier<String>) dvr.get(dynamicRegistryKey);
dynamicValueRegistry = dvr;
return;
}
createMenu();
Expand Down

0 comments on commit 18665bb

Please sign in to comment.