Skip to content

Commit

Permalink
Error fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Oct 9, 2024
1 parent 8ace820 commit 25af2e0
Show file tree
Hide file tree
Showing 32 changed files with 644 additions and 580 deletions.
148 changes: 75 additions & 73 deletions src/main/java/com/tanishisherewith/dynamichud/DynamicHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.tanishisherewith.dynamichud.config.GlobalConfig;
import com.tanishisherewith.dynamichud.screens.AbstractMoveableScreen;
import com.tanishisherewith.dynamichud.utils.BooleanPool;
import com.tanishisherewith.dynamichud.widget.Widget;
import com.tanishisherewith.dynamichud.widget.WidgetManager;
import com.tanishisherewith.dynamichud.widget.WidgetRenderer;
Expand All @@ -19,21 +18,18 @@
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.fabricmc.loader.api.metadata.ModOrigin;
import net.fabricmc.loader.language.JavaLanguageAdapter;
import net.fabricmc.loader.language.LanguageAdapter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.main.Main;
import net.minecraft.client.option.KeyBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Environment(EnvType.CLIENT)
public class DynamicHUD implements ClientModInitializer {
Expand All @@ -43,6 +39,7 @@ public class DynamicHUD implements ClientModInitializer {
* Allows saving widgets across different mods with same save file name.
*/
public static final HashMap<String, List<Widget>> FILE_MAP = new HashMap<>();

public 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 @@ -77,6 +74,16 @@ public static void openDynamicScreen(KeyBinding key, AbstractMoveableScreen scre
}
}

public void checkToEnableTestIntegration(){
String[] args = FabricLoader.getInstance().getLaunchArguments(true);
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--dynamicHudTest") && i + 1 < args.length) {
enableTestIntegration = Boolean.parseBoolean(args[i + 1]);
break;
}
}
}

@Override
public void onInitializeClient() {
printInfo("Initialising DynamicHud");
Expand All @@ -90,13 +97,7 @@ public void onInitializeClient() {
//YACL load
GlobalConfig.HANDLER.load();

String[] args = FabricLoader.getInstance().getLaunchArguments(true);
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--dynamicHudTest") && i + 1 < args.length) {
enableTestIntegration = Boolean.parseBoolean(args[i + 1]);
break;
}
}
checkToEnableTestIntegration();

printInfo("Integrating mods...");
List<EntrypointContainer<DynamicHudIntegration>> integrations = new ArrayList<>(getRegisteredIntegrations());
Expand All @@ -110,81 +111,82 @@ public void onInitializeClient() {
}

for (EntrypointContainer<DynamicHudIntegration> entrypoint : integrations) {
ModMetadata metadata = entrypoint.getProvider().getMetadata();
String modId = metadata.getId();
ModMetadata metadata = entrypoint.getProvider().getMetadata();
String modId = metadata.getId();

printInfo(String.format("Supported mod with id %s was found!", modId));
printInfo(String.format("Supported mod with id %s was found!", modId));

AbstractMoveableScreen screen;
KeyBinding binding;
WidgetRenderer widgetRenderer;
File widgetsFile;
try {
DynamicHudIntegration DHIntegration = entrypoint.getEntrypoint();
AbstractMoveableScreen screen;
KeyBinding binding;
WidgetRenderer widgetRenderer;
File widgetsFile;
try {
DynamicHudIntegration DHIntegration = entrypoint.getEntrypoint();

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

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

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

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

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

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

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

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

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

if (widgets == null || widgets.isEmpty()) {
FILE_MAP.put(widgetsFile.getName(), widgetRenderer.getWidgets());
} else {
widgets.addAll(widgetRenderer.getWidgets());
FILE_MAP.put(widgetsFile.getName(), widgets);
}
if (widgets == null || widgets.isEmpty()) {
FILE_MAP.put(widgetsFile.getName(), widgetRenderer.getWidgets());
} else {
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));
//Register events for rendering, saving, loading, and opening the hudEditor
ClientTickEvents.START_CLIENT_TICK.register((client) -> openDynamicScreen(binding, screen));

/* === Saving === */
/* === Saving === */
// Each mod is hooked to the fabric's event system to save its widget.

//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 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 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 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())));
//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) {
if (e instanceof IOException) {
logger.warn("An error has occurred while loading widgets of mod {}", modId, e);
} else {
logger.error("Mod {} has improper implementation of DynamicHUD", modId, e);
}
}
printInfo(String.format("Integration of mod %s was successful", modId));
} catch (Throwable e) {
if (e instanceof IOException) {
logger.warn("An error has occurred while loading widgets of mod {}", modId, e);
} else {
logger.error("Mod {} has improper implementation of DynamicHUD", modId, e);
}
}
}
printInfo("(DynamicHUD) Integration of supported mods was successful");

Expand All @@ -209,7 +211,7 @@ private List<EntrypointContainer<DynamicHudIntegration>> getRegisteredIntegratio
/**
* This makes it so that if minecraft is launched with the program arguments
* <p>
* {@code --dynamicHudTest true}
* {@code --dynamicHudTest true}
* </p>
* then it will
* load the {@link DynamicHudTest} class as an entrypoint, eliminating any errors due to human incapacity of
Expand All @@ -236,7 +238,7 @@ public DynamicHudIntegration getEntrypoint() {

@Override
public ModContainer getProvider() {
return FabricLoader.getInstance().getModContainer("dynamichud").orElseThrow();
return FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.NonnullDefault;

import java.io.File;

Expand Down Expand Up @@ -85,7 +84,7 @@ public interface DynamicHudIntegration {
* WidgetManager.registerCustomWidget(TextWidget.DATA);
* }
* </pre>
*
* <p>
* Custom widgets can be registered in any method in the interface
* but to avoid any errors and mishaps it is recommended you add them here
*/
Expand Down Expand Up @@ -125,11 +124,11 @@ default KeyBinding getKeyBind() {
* <h3>
* !! Should never be null !!
* </h3>
*</p>
* </p>
*
* @return The movable screen.
*/
AbstractMoveableScreen getMovableScreen();
AbstractMoveableScreen getMovableScreen();

/**
* To return a {@link WidgetRenderer} object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Using the fabric event {@link HudRenderCallback} to render widgets in the game HUD.
* Mouse positions are passed in the negatives even though theoretically it's in the centre of the screen.
*/
public class HudRender implements HudRenderCallback{
public class HudRender implements HudRenderCallback {

@Override
public void onHudRender(DrawContext drawContext, RenderTickCounter tickCounter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.tanishisherewith.dynamichud.config.GlobalConfig;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import net.minecraft.client.gui.screen.Screen;


public class ModMenuIntegration implements ModMenuApi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static GlobalConfig get() {
return INSTANCE;
}

public final Screen createYACLGUI() {
public Screen createYACLGUI() {
return YetAnotherConfigLib.createBuilder()
.title(Text.literal("DynamicHUD config screen."))
.category(ConfigCategory.createBuilder()
Expand Down Expand Up @@ -71,16 +71,17 @@ public final Screen createYACLGUI() {
.option(Option.<Integer>createBuilder()
.name(Text.literal("Snap Size"))
.description(OptionDescription.of(Text.literal("Grid size for snapping widgets")))
.binding( 100, () -> this.snapSize, newVal -> this.snapSize = newVal)
.controller(integerOption -> IntegerFieldControllerBuilder.create(integerOption).range(10,500))
.binding(100, () -> this.snapSize, newVal -> this.snapSize = newVal)
.controller(integerOption -> IntegerFieldControllerBuilder.create(integerOption).range(10, 500))
.build())
.build())
.build())
.save(HANDLER::save)
.build()
.generateScreen(null);
}
public float getScale(){

public float getScale() {
return scale;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void drawGradient(Matrix4f matrix4f, float x, float y, float width
RenderSystem.defaultBlendFunc();
RenderSystem.blendFunc(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA);
RenderSystem.setShader(GameRenderer::getPositionColorProgram);

switch (direction) {
case LEFT_RIGHT:
bufferBuilder.vertex(matrix4f, x, y + height, 0.0F).color(startRed, startGreen, startBlue, startAlpha);
Expand Down Expand Up @@ -508,7 +508,7 @@ public static void drawFilledQuadrant(Matrix4f matrix4f, float xCenter, float yC
float alpha = (float) (color >> 24 & 255) / 255.0F;

Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferBuilder = tessellator.begin(VertexFormat.DrawMode.TRIANGLE_FAN, VertexFormats.POSITION_COLOR);
BufferBuilder bufferBuilder = tessellator.begin(VertexFormat.DrawMode.TRIANGLE_FAN, VertexFormats.POSITION_COLOR);

RenderSystem.setShader(GameRenderer::getPositionColorProgram);
RenderSystem.enableBlend();
Expand Down
Loading

0 comments on commit 25af2e0

Please sign in to comment.