Skip to content

Commit

Permalink
Merge pull request #63 from TFSMads/enhancement/remove-old-widget-system
Browse files Browse the repository at this point in the history
Fjern gammelt gui widget system. Fordi addonet allerede bruger labymod 4 widget system.
  • Loading branch information
TFSMads authored Dec 16, 2024
2 parents 62a5ae5 + 101fc7b commit 04951e0
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 1,193 deletions.
3 changes: 0 additions & 3 deletions core/src/main/java/ml/volder/unikapi/loader/Laby4Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import ml.volder.unikapi.api.ApiReferenceStorageLaby4;
import ml.volder.unikapi.event.events.mainmenuopenevent.impl.Laby4MainMenuOpenEvent;
import ml.volder.unikapi.logger.Laby4Logger;
import ml.volder.unikapi.widgets.Laby4ModuleManager;
import ml.volder.unikapi.widgets.ModuleSystem;
import net.labymod.api.Laby;
import net.labymod.api.addon.LabyAddon;
import net.labymod.api.client.chat.command.Command;
Expand Down Expand Up @@ -34,7 +32,6 @@ protected void enable() {
UnikAPI.LOGGER = new Laby4Logger("UnikAPI");
UnikAPI.initAPI("labymod4", null, "*");
UnikAPI.registerReferenceStorage(ApiReferenceStorageLaby4.getInstance());
ModuleSystem.setModuleManager(new Laby4ModuleManager());
Loader.onEnable();
Laby.labyAPI().eventBus().registerListener(this);
Laby4MainMenuOpenEvent.checkMainMenu();
Expand Down
118 changes: 31 additions & 87 deletions core/src/main/java/ml/volder/unikapi/widgets/DefaultModuleManager.java
Original file line number Diff line number Diff line change
@@ -1,94 +1,38 @@
package ml.volder.unikapi.widgets;


import ml.volder.unikapi.UnikAPI;
import ml.volder.unikapi.api.player.PlayerAPI;
import ml.volder.unikapi.datasystem.Data;
import ml.volder.unikapi.datasystem.DataManager;
import ml.volder.unikapi.event.EventManager;
import ml.volder.unikapi.guisystem.elements.ControlElement;
import ml.volder.unikapi.types.Material;
import ml.volder.unikapi.widgets.editor.GuiEditor;
import ml.volder.unikapi.widgets.elements.ModuleCategoryElement;
import net.labymod.api.Laby;
import net.labymod.api.client.gui.hud.binding.category.HudWidgetCategory;
import net.labymod.api.client.gui.navigation.elements.ScreenNavigationElement;
import net.labymod.api.client.gui.screen.activity.types.TabbedActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class DefaultModuleManager implements ModuleManager{

public DefaultModuleManager() {
instance = this;
GuiModuleRenderer guiModuleRenderer = new GuiModuleRenderer(this, DataManager.getOrCreateDataManager(new File(UnikAPI.getCommonDataFolder(), "guiModule" + ".json")));
EventManager.registerEvents(guiModuleRenderer);
}

private List<GuiModule> guiModuleList = new ArrayList<>();
private List<ModuleCategoryElement> guiModuleCategories = new ArrayList<>();

public List<GuiModule> getGuiModuleList() {
return guiModuleList;
}

private static DefaultModuleManager instance;

public static GuiModule getModuleByKey(String key) {
for(GuiModule guiModule : instance.guiModuleList)
if(guiModule.getKey().equals(key))
return guiModule;
return null;
}

public List<ModuleCategoryElement> getGuiModuleCategories() {
return guiModuleCategories;
}

public void addModule(GuiModule module, ModuleCategoryElement category) {
module.setCategory(category);
guiModuleList.add(module);
}

public void addModule(GuiModule module) {
guiModuleList.add(module);
}

private final DataManager<Data> dataManager = DataManager.getOrCreateDataManager(new File(UnikAPI.getCommonDataFolder(), "guiModule" + ".json"));

@Override
public Object registerCategory(String displayName, Material icon, String description) {
ModuleCategoryElement category = new ModuleCategoryElement(displayName, new ControlElement.IconData(icon));
getGuiModuleCategories().add(category);
return category;
}

@Override
public void registerModule(String key, String defaultPrefix, boolean defaultIsEnabled, Object category, Material icon, Function<String, String> getDisplayValue) {


if(!(category instanceof ModuleCategoryElement))
return;
GuiModule module = new GuiModule(2,2, key, defaultPrefix, defaultIsEnabled, dataManager, (ModuleCategoryElement) category) {
@Override
public String getDisplayValue() {
return getDisplayValue.apply("");
}
};
ControlElement.IconData iconData = new ControlElement.IconData(icon);
iconData.setItemDamage(icon.getItemDamage(0));
module.setIconData(iconData);
getGuiModuleList().add(module);
}

@Override
public void openEditor() {
PlayerAPI.getAPI().openGuiScreen(new GuiEditor(this, PlayerAPI.getAPI().getCurrentScreen()));
}

public static DataManager<Data> getDataManager() {
if(instance != null && instance.dataManager != null)
return instance.dataManager;
return DataManager.getOrCreateDataManager(new File(UnikAPI.getCommonDataFolder(), "guiModule" + ".json"));
}

public class DefaultModuleManager implements ModuleManager {

int categoryId = 0;
@Override
public Object registerCategory(String title, Material material, String description) {
HudWidgetCategory category = new UnikHudWidgetCategory("unikapi-category-" + categoryId, title, description);
Laby.labyAPI().hudWidgetRegistry().categoryRegistry().register(category);
categoryId += 1;
return category;
}

@Override
public void registerModule(String key, String defaultPrefix, boolean defaultIsEnabled, Object category, Material icon, Function<String, String> getDisplayValue) {
if(!(category instanceof HudWidgetCategory))
return;
Laby.labyAPI().hudWidgetRegistry().register(new UnikHudWidget(key, (HudWidgetCategory) category, defaultPrefix, icon,getDisplayValue));
}

@Override
public void openEditor() {
ScreenNavigationElement navigationElement = Laby.references().navigationRegistry().getById("labymod") instanceof ScreenNavigationElement ? (ScreenNavigationElement) Laby.references().navigationRegistry().getById("labymod") : null;
if(navigationElement == null || navigationElement.getScreen() == null || !(navigationElement.getScreen() instanceof TabbedActivity))
return;
TabbedActivity tabbedActivity = (TabbedActivity) navigationElement.getScreen();
tabbedActivity.switchTab("widgets");
Laby.labyAPI().minecraft().minecraftWindow().displayScreen(tabbedActivity);
}
}
Loading

0 comments on commit 04951e0

Please sign in to comment.