refactor config + modules use @Module annotation now

This commit is contained in:
jane 2020-12-22 16:19:02 -05:00
parent 2600f92b2a
commit 7b41f24211
23 changed files with 220 additions and 92 deletions

View File

@ -3,41 +3,30 @@ package pm.j4.kerosene;
import java.util.Optional;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.client.options.KeyBinding;
import pm.j4.kerosene.modules.ExampleModule;
import pm.j4.kerosene.modules.bindings.BindingManager;
import pm.j4.kerosene.util.config.ConfigHolder;
import pm.j4.kerosene.util.config.ConfigManager;
import pm.j4.kerosene.util.config.Module;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase;
/**
* The type Kerosene mod.
*/
@Module(ExampleModule.class)
@Module(BindingManager.class)
public class KeroseneMod implements ModInitializer {
@Override
public void onInitialize() {
ConfigManager.initConfig("kerosene");
// always update mod data
Optional<ModContainer> modContainer = FabricLoader.getInstance().getModContainer("kerosene");
modContainer.ifPresent(container -> ModInfoProvider.modData = container.getMetadata());
ConfigManager.initConfig("kerosene", KeroseneMod.class);
Optional<ConfigHolder> conf = ConfigManager.getConfig("kerosene");
try {
ModInfoProvider.registerMod(ExampleModule.class);
ModInfoProvider.registerMod(BindingManager.class);
}
catch (Exception e) {
System.out.println(e);
}
//initialize any keybinds, data, etc.
ModInfoProvider.getRegisteredMods().forEach(ModuleBase::init);

View File

@ -32,13 +32,28 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
*/
protected final Text displayText;
private PModuleConfigPane parent;
/**
* The Parent.
*/
private final PModuleConfigPane parent;
private List<Element> elements = new ArrayList<>();
/**
* The Elements.
*/
private final List<Element> elements = new ArrayList<>();
private String trueValue;
private String falseValue;
/**
* The True value.
*/
private final String trueValue;
/**
* The False value.
*/
private final String falseValue;
/**
* The Selected.
*/
private Element selected;
/**
@ -46,6 +61,7 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
*
* @param option the option
* @param text the text
* @param parent the parent
*/
public PModuleConfigEntry(ConfigurationOption option, Text text, PModuleConfigPane parent) {
this.option = option;
@ -55,6 +71,15 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
this.falseValue = "No";
}
/**
* Instantiates a new P module config entry.
*
* @param option the option
* @param text the text
* @param parent the parent
* @param trueValue the true value
* @param falseValue the false value
*/
public PModuleConfigEntry(ConfigurationOption option, Text text, PModuleConfigPane parent, String trueValue, String falseValue) {
this.option = option;
this.displayText = text;
@ -147,9 +172,7 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
ButtonWidget bindButton = new ButtonWidget(x, y + (int)(fontHeight * 2.5),
entryWidth,
fontHeight * 2,
new LiteralText(option.getStringValue().toUpperCase()), (button) -> {
button.setMessage(new LiteralText("Press any key..."));
}) {
new LiteralText(option.getStringValue().toUpperCase()), (button) -> button.setMessage(new LiteralText("Press any key..."))) {
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (this.active && this.visible) {

View File

@ -22,6 +22,9 @@ public class PModuleConfigPane extends ElementListWidget<PModuleConfigEntry> {
*/
private POptionEntry lastSelected;
/**
* The Selected config entry.
*/
private PModuleConfigEntry selectedConfigEntry;
/**
@ -82,8 +85,6 @@ public class PModuleConfigPane extends ElementListWidget<PModuleConfigEntry> {
GlStateManager.DstFactor.ONE_MINUS_DST_ALPHA,
GlStateManager.SrcFactor.ZERO,
GlStateManager.DstFactor.ONE);
RenderSystem.disableAlphaTest();
RenderSystem.shadeModel(7425);
RenderSystem.disableTexture();
// darken config pane area
@ -111,8 +112,6 @@ public class PModuleConfigPane extends ElementListWidget<PModuleConfigEntry> {
this.renderList(matrices, rl, sc, mouseX, mouseY, delta);
RenderSystem.enableTexture();
RenderSystem.shadeModel(7424);
RenderSystem.enableAlphaTest();
RenderSystem.disableBlend();
}
}

View File

@ -19,6 +19,7 @@ import pm.j4.kerosene.util.module.ModuleBase;
/**
* The type P module configuration widget.
*/
@SuppressWarnings("deprecation")
public class PModuleConfigurationWidget extends AlwaysSelectedEntryListWidget<POptionEntry> {
/**
* The Parent.

View File

@ -14,6 +14,7 @@ import pm.j4.kerosene.util.module.ModuleBase;
/**
* The type P option entry.
*/
@SuppressWarnings("deprecation")
public class POptionEntry extends AlwaysSelectedEntryListWidget.Entry<POptionEntry> {
/**
@ -56,7 +57,7 @@ public class POptionEntry extends AlwaysSelectedEntryListWidget.Entry<POptionEnt
nameString = StringVisitable.concat(font.trimToWidth(nameString, maxNameWidth - font.getWidth(ellipse)), ellipse);
}
font.draw(matrices, Language.getInstance().reorder(nameString), x + 32 + 3, y + (entryHeight / 2), 0xFFFFFF);
font.draw(matrices, Language.getInstance().reorder(nameString), x + 32 + 3, y + ((float)entryHeight / 2), 0xFFFFFF);
}
@Override
@ -88,6 +89,7 @@ public class POptionEntry extends AlwaysSelectedEntryListWidget.Entry<POptionEnt
*
* @return the x offset
*/
@SuppressWarnings("SameReturnValue")
public int getXOffset() {
return 0;
}

View File

@ -14,8 +14,9 @@ import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.*;
import pm.j4.kerosene.KeroseneMod;
import net.minecraft.text.StringVisitable;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import pm.j4.kerosene.util.config.ConfigManager;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase;
@ -99,13 +100,11 @@ public class POptionsScreen extends Screen {
this);
this.configPane.setLeftPos(paneWidth);
this.children.add(this.configPane);
List<ModuleBase> configurableModules = new ArrayList<>();
configurableModules.addAll(ModInfoProvider.getRegisteredMods()
.stream().filter(ModuleBase::configurable)
.collect(Collectors.toList()));
List<ModuleBase> configurableModules = ModInfoProvider.getRegisteredMods()
.stream().filter(ModuleBase::configurable).collect(Collectors.toList());
configurableModules.forEach(module -> this.modules.addEntry(new POptionEntry(module, this.modules)));
this.addButton(new ButtonWidget(this.width / 2 - 75, this.height - 30, 150, 20, ScreenTexts.DONE, (buttonWidget) -> {
ConfigManager.saveAllModules();
ConfigManager.saveEverything();
assert this.client != null;
this.client.openScreen(this.previousScreen);
}));
@ -137,7 +136,7 @@ public class POptionsScreen extends Screen {
trimmedName = StringVisitable.concat(textRenderer.trimToWidth(name, maxNameWidth - textRenderer.getWidth(ellipsis)), ellipsis);
}
if (mouseX > x + offset && mouseY > paneY + 1 && mouseY < paneY + 1 + textRenderer.fontHeight && mouseX < x + offset + textRenderer.getWidth(trimmedName)) {
//TODO tooltop
//TODO tooltip
//selected.getModName()
}
textRenderer.draw(matrices, selected.getModName(), x + offset, paneY + 2 + lineSpacing, 0x808080);

View File

@ -5,7 +5,6 @@ import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.TranslatableText;
import org.lwjgl.glfw.GLFW;
import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.gui.PModuleConfigEntry;
import pm.j4.kerosene.gui.PModuleConfigPane;
import pm.j4.kerosene.util.config.ConfigHolder;
@ -109,9 +108,7 @@ public class BindingManager extends ModuleBase {
* @param b the b
*/
public static void removeBind(KeyBinding b) {
if(registeredBinds.contains(b)) {
registeredBinds.remove(b);
}
registeredBinds.remove(b);
}
/**

View File

@ -2,7 +2,6 @@ package pm.j4.kerosene.util.config;
import java.util.ArrayList;
import java.util.List;
import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase;

View File

@ -3,7 +3,6 @@ package pm.j4.kerosene.util.config;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase;

View File

@ -3,11 +3,15 @@ package pm.j4.kerosene.util.config;
import com.google.common.reflect.TypeToken;
import com.google.gson.*;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.*;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.modules.ExampleModule;
import pm.j4.kerosene.modules.bindings.BindingInfo;
import pm.j4.kerosene.modules.bindings.BindingManager;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.data.ModuleConfig;
import pm.j4.kerosene.util.data.OptionSerializiable;
@ -27,7 +31,7 @@ public class ConfigManager {
/**
* The constant config.
*/
private static Map<String, ConfigHolder> configs = new HashMap<>();
private static final Map<String, ConfigHolder> configs = new HashMap<>();
/**
* Prepare config file.
@ -37,18 +41,36 @@ public class ConfigManager {
* @return the file
*/
private static File prepareConfigFile(String path, String filename) {
if (path != "") {
if (!Objects.equals(path, "")) {
File directory = new File(FabricLoader.getInstance().getConfigDir().toString(), path);
if (!directory.exists()) directory.mkdir();
if (!directory.exists()) //noinspection ResultOfMethodCallIgnored
directory.mkdir();
}
return new File(FabricLoader.getInstance().getConfigDir().toString(), path + filename);
}
/**
* Init config.
*
* @param moduleName the module name
*/
public static void initConfig(String moduleName) {
public static void initConfig(String moduleName, Class parent) {
try {
Annotation moduleAnnotation = parent.getAnnotation(Modules.class);
assert moduleAnnotation != null;
Module[] modules = ((Modules)moduleAnnotation).value();
System.out.println(modules);
for (int i = 0; i < modules.length; i++) {
System.out.println(modules[i].value());
ModInfoProvider.registerMod(modules[i].value());
}
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("init " + moduleName);
if (configs.containsKey(moduleName)) {
System.out.println("contains " + configs.get(moduleName));
return;
}
@ -56,22 +78,32 @@ public class ConfigManager {
config.globalConfig = new DefaultConfig();
config.serverConfigs = new HashMap<>();
System.out.println("load cfg");
config = load(moduleName + "/", moduleName +".json", ConfigHolder.class);
initModules(moduleName);
initModules();
System.out.println("put cfg");
configs.put(moduleName, config);
}
/**
* Init modules.
*
*/
public static void initModules(String moduleName) {
public static void initModules() {
ModInfoProvider.getRegisteredMods().forEach(module -> {
ModuleConfig options = load(moduleName + "/modules/", module.getModuleName() + ".json", ModuleConfig.class);
if (options != null && options.options != null) {
options.options.forEach((key, option) -> {
if (module.hasOption(option.key)) {
module.setConfigOption(option.key, option.value);
}
});
System.out.println("module" + module.getModuleName() + " initialized " + module.initialized);
if (!module.initialized) {
ModuleConfig options = load(module.getParent() + "/modules/", module.getModuleName() + ".json", ModuleConfig.class);
System.out.println("initialize " + module.getModuleName());
if (options != null && options.options != null) {
options.options.forEach((key, option) -> {
if (module.hasOption(option.key)) {
System.out.println("setting " + option.key + " to " + option.value.getAsString());
module.setConfigOption(option.key, option.value);
}
});
}
module.initialized = true;
}
});
}
@ -147,7 +179,8 @@ public class ConfigManager {
/**
* Save module.
*
* @param b the b
* @param modName the mod name
* @param b the b
*/
public static void saveModule(String modName, ModuleBase b) {
ModuleConfig c = new ModuleConfig();
@ -160,13 +193,13 @@ public class ConfigManager {
*/
public static void saveAllModules() {
List<ModuleBase> mods = ModInfoProvider.getRegisteredMods();
mods.forEach((module) -> {
ConfigManager.saveModule(module.getParent(), module);
});
mods.forEach((module) -> ConfigManager.saveModule(module.getParent(), module));
}
/**
* Save global config.
*
* @param modName the mod name
*/
public static void saveGlobalConfig(String modName) {
if(configs.containsKey(modName)) {
@ -174,9 +207,17 @@ public class ConfigManager {
}
}
public static void saveEverything() {
configs.keySet().forEach(key -> {
saveGlobalConfig(key);
});
saveAllModules();
}
/**
* Gets config.
*
* @param modName the mod name
* @return the config
*/
public static Optional<ConfigHolder> getConfig(String modName) {
@ -190,6 +231,7 @@ public class ConfigManager {
/**
* The type Serialization helper.
*/
@SuppressWarnings("UnstableApiUsage")
class SerializationHelper {
/**

View File

@ -1,7 +1,5 @@
package pm.j4.kerosene.util.config;
import java.util.Collections;
/**
* The type Default config.
*/

View File

@ -5,7 +5,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.modules.bindings.BindingInfo;
import pm.j4.kerosene.modules.bindings.BindingManager;
import pm.j4.kerosene.util.data.ModInfoProvider;
@ -140,9 +139,7 @@ public class GlobalConfig extends Config {
public static Map<String, OptionSerializiable> serializeModuleConfiguration(ModuleBase module) {
Map<String, OptionSerializiable> opts = new HashMap<>();
Map<String, ConfigurationOption> configuration = module.getModuleConfiguration();
configuration.forEach((key, value) -> {
opts.put(key, new OptionSerializiable(key, value.toJson()));
});
configuration.forEach((key, value) -> opts.put(key, new OptionSerializiable(key, value.toJson())));
return opts;
}

View File

@ -0,0 +1,14 @@
package pm.j4.kerosene.util.config;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import pm.j4.kerosene.util.module.ModuleBase;
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Modules.class)
public @interface Module {
public Class<? extends ModuleBase> value();
}

View File

@ -0,0 +1,9 @@
package pm.j4.kerosene.util.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Modules {
Module[] value();
}

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.util.module.ModuleBase;
/**
@ -26,8 +25,7 @@ public class Category {
categoryMap.put(module.getCategory(), m);
} else {
List<ModuleBase> m = categoryMap.get(module.getCategory());
List<ModuleBase> nm = new ArrayList<>();
nm.addAll(m);
List<ModuleBase> nm = new ArrayList<>(m);
nm.add(module);
categoryMap.replace(module.getCategory(), nm);
}

View File

@ -3,18 +3,15 @@ package pm.j4.kerosene.util.data;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.server.integrated.IntegratedServer;
import pm.j4.kerosene.util.module.ModuleBase;
/**
* The type Mod info provider.
*/
//TODO refactor into multiple data holders
public class ModInfoProvider {
/**
* The Mod data.
*/
public static ModMetadata modData = null;
/**
* The constant client.
*/
@ -22,8 +19,15 @@ public class ModInfoProvider {
/**
* The constant activeMods.
*/
private static List<ModuleBase> registeredMods = new ArrayList<>();
private static final List<ModuleBase> registeredMods = new ArrayList<>();
/**
* Register mod.
*
* @param mod the mod
* @throws IllegalAccessException the illegal access exception
* @throws InstantiationException the instantiation exception
*/
public static void registerMod(Class<? extends ModuleBase> mod) throws IllegalAccessException, InstantiationException {
ModuleBase base = mod.newInstance();
if(!registeredMods.contains(base)) {

View File

@ -19,6 +19,7 @@ public abstract class ModuleBase {
* Instantiates a new Module base.
* Parameters should be constant across restarts.
*
* @param parent the parent
* @param name The name of the module
* @param category the category
* @param activatable Whether a module can be activated, or if it will remain in the state it was upon startup
@ -60,11 +61,23 @@ public abstract class ModuleBase {
config.ifPresent(configHolder -> configHolder.toggleModule(this.moduleName));
}
public boolean initialized;
/**
* The Parent.
*/
private final String parent;
/**
* Gets parent.
*
* @return the parent
*/
public String getParent() {
return this.parent;
}
/**
* The Module's name.
*/
@ -171,16 +184,19 @@ public abstract class ModuleBase {
*
* @param key the key
* @param value the value
* @return whether the operation was successful.
*/
public boolean setConfigOption(String key, JsonElement value) {
public void setConfigOption(String key, JsonElement value) {
if (moduleOptions.containsKey(key)) {
moduleOptions.get(key).fromJson(value);
return true;
}
return false;
}
/**
* Update config option.
*
* @param key the key
* @param option the option
*/
public void updateConfigOption(String key, ConfigurationOption option) {
System.out.println("update config option" + key + option.getStringValue());
System.out.println(moduleOptions.keySet());
@ -209,12 +225,15 @@ public abstract class ModuleBase {
return new ArrayList<>();
}
/**
* Convert default config map.
*
* @return the map
*/
private Map<String, ConfigurationOption> convertDefaultConfig() {
List<ConfigurationOption> options = this.getDefaultConfig();
Map<String, ConfigurationOption> mapped = new HashMap<>();
options.forEach((option) -> {
mapped.put(option.getConfigKey(), option);
});
options.forEach((option) -> mapped.put(option.getConfigKey(), option));
return mapped;
}
@ -234,6 +253,7 @@ public abstract class ModuleBase {
/**
* Gets config entries.
*
* @param sourcePane the source pane
* @return the config entries
*/
public List<PModuleConfigEntry> getConfigEntries(PModuleConfigPane sourcePane) {

View File

@ -16,16 +16,28 @@ public class BooleanOption extends ConfigurationOption {
/**
* Instantiates a new Configuration option.
*
* @param key
* @param key the key
* @param description the description
* @param parent the parent
*/
public BooleanOption(String key, String description, ModuleBase parent) {
super(key, description, parent);
}
/**
* Gets value.
*
* @return the value
*/
public boolean getValue() {
return value;
}
/**
* Sets value.
*
* @param value the value
*/
public void setValue(boolean value) { this.value = value; }
@Override

View File

@ -11,19 +11,28 @@ public abstract class ConfigurationOption {
* The Description.
*/
private final String description;
/**
* The Key.
*/
private final String key;
/**
* The Parent.
*/
private final ModuleBase parent;
/**
* Instantiates a new Configuration option.
*
* @param key the key
* @param description the description
* @param parent the parent
*/
public ConfigurationOption(String key, String description, ModuleBase parent) {
this.description = description;
this.key = key;
this.parent = parent;
}
/**
* Gets description.
*
@ -32,7 +41,19 @@ public abstract class ConfigurationOption {
public final String getDescription() {
return this.description;
}
/**
* Gets config key.
*
* @return the config key
*/
public final String getConfigKey() { return key; }
/**
* Gets parent.
*
* @return the parent
*/
public final ModuleBase getParent() { return parent; }
/**

View File

@ -16,9 +16,9 @@ public class IntegerOption extends ConfigurationOption {
/**
* Instantiates a new Configuration option.
*
* @param key
* @param key the key
* @param description the description
* @param parent
* @param parent the parent
*/
public IntegerOption(String key, String description, ModuleBase parent) {
super(key, description, parent);

View File

@ -24,14 +24,19 @@ public class KeybindOption extends ConfigurationOption {
/**
* Instantiates a new Configuration option.
*
* @param key
* @param key the key
* @param description the description
* @param parent
* @param parent the parent
*/
public KeybindOption(String key, String description, ModuleBase parent) {
super(key, description, parent);
}
/**
* Gets translation key.
*
* @return the translation key
*/
public String getTranslationKey() {
return value.getTranslationKey();
}

View File

@ -12,9 +12,9 @@ public class ListOption extends ConfigurationOption {
/**
* Instantiates a new Configuration option.
*
* @param key
* @param key the key
* @param description the description
* @param parent
* @param parent the parent
*/
public ListOption(String key, String description, ModuleBase parent) {
super(key, description, parent);

View File

@ -17,9 +17,9 @@ public class StringOption extends ConfigurationOption {
/**
* Instantiates a new Configuration option.
*
* @param key
* @param key the key
* @param description the description
* @param parent
* @param parent the parent
*/
public StringOption(String key, String description, ModuleBase parent) {
super(key, description, parent);