Skip to content

Commit d20247e

Browse files
Fixed soo many bugs. Polished but now its a bit bloated.
1 parent 263cf57 commit d20247e

24 files changed

+231
-152
lines changed

src/main/java/com/tanishisherewith/dynamichud/IntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public DynamicHudConfigurator configure(DynamicHudConfigurator configurator) {
6666
.addWidget(HelloWidget)
6767
.addWidget(DynamicHUDWidget)
6868
.configureRenderer(renderer -> {
69-
renderer.shouldRenderInGameHud(true);
69+
//Already true by default
70+
//renderer.shouldRenderInGameHud(true);
7071
renderer.addScreen(TitleScreen.class);
7172
})
7273
.withMoveableScreen(config -> new AbstractMoveableScreen(Text.literal("Editor Screen"), config.getRenderer()) {});

src/main/java/com/tanishisherewith/dynamichud/integration/DynamicHudConfigurator.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,31 @@ public DynamicHudConfigurator addWidget(Widget widget){
3232
return this;
3333
}
3434

35+
/**
36+
* Configure the existing renderer object with this method
37+
*/
3538
public DynamicHudConfigurator configureRenderer(Consumer<WidgetRenderer> wrConsumer) {
36-
return configureRenderer(wrConsumer, widgets);
39+
if(renderer == null){
40+
this.renderer = new WidgetRenderer(widgets);
41+
}
42+
wrConsumer.accept(renderer);
43+
return this;
3744
}
3845

3946
public DynamicHudConfigurator configureRenderer(Consumer<WidgetRenderer> wrConsumer, List<Widget> widgets) {
4047
this.renderer = new WidgetRenderer(widgets);
4148
wrConsumer.accept(renderer);
4249
return this;
4350
}
51+
52+
/**
53+
* Override the present widget renderer with your own instance
54+
*/
55+
public DynamicHudConfigurator overrideRenderer(WidgetRenderer renderer) {
56+
this.renderer = renderer;
57+
return this;
58+
}
59+
4460
/**
4561
* Called before saving these widgets
4662
*/

src/main/java/com/tanishisherewith/dynamichud/integration/DynamicHudIntegration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.tanishisherewith.dynamichud.screens.AbstractMoveableScreen;
44
import com.tanishisherewith.dynamichud.widget.WidgetData;
55
import com.tanishisherewith.dynamichud.widget.WidgetManager;
6+
import com.tanishisherewith.dynamichud.widget.WidgetRenderer;
67
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
78
import net.fabricmc.loader.api.FabricLoader;
89
import net.minecraft.client.option.KeyBinding;
@@ -55,6 +56,11 @@ public interface DynamicHudIntegration {
5556
*/
5657
void init();
5758

59+
/**
60+
* This method is called after widgets from the widget file have been loaded successfully and added to the renderer.
61+
*/
62+
default void postWidgetLoading(WidgetRenderer renderer) {}
63+
5864
/**
5965
* To register custom widgets. This method can be overridden by implementations.
6066
* <p>

src/main/java/com/tanishisherewith/dynamichud/integration/IntegrationManager.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,22 @@ public static void integrate(){
110110

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

113-
114113
//Gets the widget file to save and load the widgets from
115114
widgetsFile = DHIntegration.getWidgetsFile();
116115

117-
// Get the instance of AbstractMoveableScreen
118-
screen = Objects.requireNonNull(configurator.getMovableScreen(), "AbstractMovableScreen instance should not be null!");
119-
120116
// Adds / loads widgets from file
121117
if (WidgetManager.doesWidgetFileExist(widgetsFile)) {
122-
WidgetManager.loadWidgets(widgetsFile);
118+
List<Widget> widgets = WidgetManager.loadWidgets(widgetsFile);
119+
configurator.configureRenderer(renderer -> renderer.clearAndAdd(widgets));
120+
DHIntegration.postWidgetLoading(configurator.getRenderer());
123121
} else {
124122
configurator.registerWidgets();
125123
}
126124

125+
126+
// Get the instance of AbstractMoveableScreen
127+
screen = Objects.requireNonNull(configurator.getMovableScreen(), "AbstractMovableScreen instance should not be null!");
128+
127129
// Get the keybind to open the screen instance
128130
binding = DHIntegration.getKeyBind();
129131

src/main/java/com/tanishisherewith/dynamichud/internal/System.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22

33
import com.tanishisherewith.dynamichud.utils.DynamicValueRegistry;
44

5-
import java.util.ArrayList;
6-
import java.util.List;
7-
import java.util.Map;
5+
import java.util.*;
86
import java.util.concurrent.ConcurrentHashMap;
97

108
public abstract class System {
119
// A map to store all instances of DynamicValueRegistry by modId
12-
protected static final Map<String, List<DynamicValueRegistry>> instances = new ConcurrentHashMap<>();
13-
protected final String modId;
10+
private static final Map<Class<?>, Map<String, Set<Object>>> instanceRegistry = new HashMap<>();
1411

15-
public System(String modId) {
16-
this.modId = modId;
17-
instances.computeIfAbsent(modId, k -> new ArrayList<>()).add((DynamicValueRegistry) this);
12+
public static void registerInstance(Object instance, String modId) {
13+
Class<?> cls = instance.getClass();
14+
Map<String, Set<Object>> modMap = instanceRegistry.computeIfAbsent(cls, k -> new HashMap<>());
15+
Set<Object> list = modMap.computeIfAbsent(modId, k -> new HashSet<>());
16+
list.add(instance);
1817
}
1918

20-
public static List<DynamicValueRegistry> getInstances(String modId) {
21-
return instances.get(modId);
22-
}
23-
24-
public String getModId() {
25-
return modId;
19+
public static <T> List<T> getInstances(Class<T> cls, String modId) {
20+
Map<String, Set<Object>> modMap = instanceRegistry.get(cls);
21+
if (modMap == null) return Collections.emptyList();
22+
Set<Object> list = modMap.get(modId);
23+
if (list == null) return Collections.emptyList();
24+
List<T> typedList = new ArrayList<>();
25+
for (Object obj : list) {
26+
if (cls.isInstance(obj)) {
27+
typedList.add(cls.cast(obj));
28+
}
29+
}
30+
return typedList;
2631
}
2732
}

src/main/java/com/tanishisherewith/dynamichud/screens/AbstractMoveableScreen.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
import net.minecraft.client.gui.DrawContext;
88
import net.minecraft.client.gui.screen.Screen;
99
import net.minecraft.text.Text;
10+
import org.lwjgl.glfw.GLFW;
1011

1112
public abstract class AbstractMoveableScreen extends Screen {
1213
public final WidgetRenderer widgetRenderer;
1314

14-
//TrayWidget trayWidget;
1515
/**
1616
* Constructs a AbstractMoveableScreen object.
1717
*/
1818
public AbstractMoveableScreen(Text title, WidgetRenderer renderer) {
1919
super(title);
2020
this.widgetRenderer = renderer;
21-
// this.trayWidget = new TrayWidget(width - 210,height - 10);
2221
}
2322

2423
@Override
@@ -29,8 +28,6 @@ protected void init() {
2928
@Override
3029
public void onDisplayed() {
3130
super.onDisplayed();
32-
// this.trayWidget.updatePosition(width, height);
33-
// trayWidget.setFocused(true);
3431
widgetRenderer.isInEditor = true;
3532
}
3633

@@ -68,12 +65,10 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) {
6865
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
6966
widgetRenderer.keyPressed(keyCode, scanCode, modifiers);
7067
ContextMenuManager.getInstance().keyPressed(keyCode, scanCode, modifiers);
71-
/*
7268
if(widgetRenderer.selectedWidget != null && (keyCode == GLFW.GLFW_KEY_DELETE || keyCode == GLFW.GLFW_KEY_BACKSPACE)){
73-
trayWidget.minimizeWidget(widgetRenderer.selectedWidget);
69+
// trayWidget.minimizeWidget(widgetRenderer.selectedWidget);
7470
}
7571

76-
*/
7772
return super.keyPressed(keyCode, scanCode, modifiers);
7873
}
7974

@@ -121,7 +116,6 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
121116
}
122117
}
123118
}
124-
// trayWidget.render(drawContext,mouseX,mouseY,delta);
125119
}
126120

127121
public void handleClickOnWidget(Widget widget, double mouseX, double mouseY, int button) {}

src/main/java/com/tanishisherewith/dynamichud/utils/DynamicValueRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* </pre>
2020
* </p>
2121
*/
22-
public class DynamicValueRegistry extends System {
22+
public class DynamicValueRegistry {
2323
/**
2424
* A map that holds the global registry of suppliers.
2525
*
@@ -40,7 +40,7 @@ public class DynamicValueRegistry extends System {
4040
* @param modId The ID of the mod for which this registry is being created. Doesn't need to be modId, it can simply be used as a standard unique identifier string.
4141
*/
4242
public DynamicValueRegistry(String modId) {
43-
super(modId);
43+
System.registerInstance(this,modId);
4444
}
4545

4646
/**
@@ -99,7 +99,7 @@ public void setLocalRegistry(Map<String, Supplier<?>> map) {
9999
* @return A list of DynamicValueRegistry instances, or an empty list if none exist.
100100
*/
101101
public static List<DynamicValueRegistry> getInstances(String modId) {
102-
return instances.getOrDefault(modId, Collections.emptyList());
102+
return System.getInstances(DynamicValueRegistry.class, modId);
103103
}
104104

105105
/**

src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/ContextMenu.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import com.tanishisherewith.dynamichud.DynamicHUD;
44
import com.tanishisherewith.dynamichud.helpers.DrawHelper;
5-
import com.tanishisherewith.dynamichud.helpers.animationhelper.animations.MathAnimations;
5+
import com.tanishisherewith.dynamichud.internal.System;
66
import com.tanishisherewith.dynamichud.utils.Input;
77
import com.tanishisherewith.dynamichud.utils.contextmenu.contextmenuscreen.ContextMenuScreenFactory;
8+
import com.tanishisherewith.dynamichud.utils.contextmenu.contextmenuscreen.ContextMenuScreenRegistry;
89
import com.tanishisherewith.dynamichud.utils.contextmenu.contextmenuscreen.DefaultContextMenuScreenFactory;
910
import com.tanishisherewith.dynamichud.utils.contextmenu.options.Option;
1011
import net.minecraft.client.gui.DrawContext;
@@ -19,6 +20,7 @@
1920
import java.util.List;
2021
import java.util.Objects;
2122

23+
@SuppressWarnings({"rawtypes", "unchecked"})
2224
public class ContextMenu<T extends ContextMenuProperties> implements Input {
2325
public final Color darkerBackgroundColor;
2426
//The properties of a context menu
@@ -57,6 +59,9 @@ public ContextMenu(int x, int y, @NotNull T properties, ContextMenuScreenFactory
5759
this.darkerBackgroundColor = properties.getBackgroundColor().darker().darker().darker().darker().darker().darker();
5860
this.parentMenu = parentMenu;
5961
this.properties.getSkin().setContextMenu(this);
62+
63+
Screen dummy = screenFactory.create(this, properties);
64+
System.registerInstance(new ContextMenuScreenRegistry(dummy.getClass()), DynamicHUD.MOD_ID);
6065
}
6166

6267
public void addOption(Option<?> option) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.tanishisherewith.dynamichud.utils.contextmenu.contextmenuscreen;
2+
3+
import net.minecraft.client.gui.screen.Screen;
4+
5+
public class ContextMenuScreenRegistry{
6+
public Class<? extends Screen> screenKlass;
7+
8+
public ContextMenuScreenRegistry(Class<? extends Screen> screenKlass){
9+
this.screenKlass = screenKlass;
10+
}
11+
}

src/main/java/com/tanishisherewith/dynamichud/utils/contextmenu/options/BooleanOption.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212
public class BooleanOption extends Option<Boolean> {
1313
private final BooleanType booleanType;
1414

15-
public BooleanOption(String name, Supplier<Boolean> getter, Consumer<Boolean> setter, BooleanType booleanType) {
15+
public BooleanOption(Text name, Supplier<Boolean> getter, Consumer<Boolean> setter, BooleanType booleanType) {
1616
super(name,getter, setter);
1717
this.booleanType = booleanType;
1818
this.renderer.init(this);
1919
}
2020

21-
public BooleanOption(String name, Supplier<Boolean> getter, Consumer<Boolean> setter) {
21+
public BooleanOption(Text name, Supplier<Boolean> getter, Consumer<Boolean> setter) {
2222
this(name, getter, setter, BooleanType.TRUE_FALSE);
2323
}
2424

25-
public BooleanOption(String name, boolean defaultValue) {
25+
public BooleanOption(Text name, boolean defaultValue) {
2626
this(name, defaultValue, BooleanType.TRUE_FALSE);
2727
}
2828

29-
public BooleanOption(String name, boolean defaultValue, BooleanType type) {
30-
this(name, () -> BooleanPool.get(name), value -> BooleanPool.put(name, value), type);
31-
BooleanPool.put(name, defaultValue);
29+
public BooleanOption(Text name, boolean defaultValue, BooleanType type) {
30+
this(name, () -> BooleanPool.get(name.getString()), value -> BooleanPool.put(name.getString(), value), type);
31+
BooleanPool.put(name.getString(), defaultValue);
3232
}
3333

3434
@Override

0 commit comments

Comments
 (0)