0.1.6!!!
This commit is contained in:
parent
7b41f24211
commit
2ea888ecaa
16 changed files with 222 additions and 112 deletions
|
@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
|
||||||
loader_version=0.10.8
|
loader_version=0.10.8
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 0.1.6-SNAPSHOT
|
mod_version = 0.1.6
|
||||||
maven_group = pm.j4
|
maven_group = pm.j4
|
||||||
archives_base_name = kerosene
|
archives_base_name = kerosene
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package pm.j4.kerosene;
|
package pm.j4.kerosene;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||||
|
@ -11,6 +12,7 @@ import pm.j4.kerosene.util.config.ConfigManager;
|
||||||
import pm.j4.kerosene.util.config.Module;
|
import pm.j4.kerosene.util.config.Module;
|
||||||
import pm.j4.kerosene.util.data.ModInfoProvider;
|
import pm.j4.kerosene.util.data.ModInfoProvider;
|
||||||
import pm.j4.kerosene.util.module.ModuleBase;
|
import pm.j4.kerosene.util.module.ModuleBase;
|
||||||
|
import pm.j4.kerosene.util.module.ModuleFunction;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,15 +34,29 @@ public class KeroseneMod implements ModInitializer {
|
||||||
ModInfoProvider.getRegisteredMods().forEach(ModuleBase::init);
|
ModInfoProvider.getRegisteredMods().forEach(ModuleBase::init);
|
||||||
|
|
||||||
//initialize keybind handler
|
//initialize keybind handler
|
||||||
conf.ifPresent(configHolder -> ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||||
if (ModInfoProvider.client != client) {
|
if (ModInfoProvider.client != client) {
|
||||||
ModInfoProvider.client = client;
|
ModInfoProvider.client = client;
|
||||||
}
|
}
|
||||||
for (KeyBinding b : BindingManager.getActiveKeybinds()) {
|
Map<KeyBinding, ModuleFunction> binds = BindingManager.getActiveKeybinds();
|
||||||
while (b.wasPressed()) {
|
binds.forEach((bind, func) -> {
|
||||||
configHolder.globalConfig.bindings.get(b).activate(client);
|
while (bind.wasPressed()) {
|
||||||
|
Optional<ConfigHolder> config = ConfigManager.getConfig(func.getParent().getParent());
|
||||||
|
config.ifPresent(configHolder -> {
|
||||||
|
Map<KeyBinding, ModuleFunction> bindings = configHolder.globalConfig.bindings;
|
||||||
|
if (bindings.containsKey(bind)) {
|
||||||
|
bindings.get(bind).activate(client);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("binding function no longer present");
|
||||||
|
BindingManager.removeBind(bind);
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//save any defaults that have been set
|
||||||
|
ConfigManager.saveEverything();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import net.minecraft.text.LiteralText;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import pm.j4.kerosene.util.config.ConfigHolder;
|
import pm.j4.kerosene.util.config.ConfigHolder;
|
||||||
import pm.j4.kerosene.util.config.ConfigManager;
|
import pm.j4.kerosene.util.config.ConfigManager;
|
||||||
|
import pm.j4.kerosene.util.module.ModuleFunction;
|
||||||
import pm.j4.kerosene.util.module.option.BooleanOption;
|
import pm.j4.kerosene.util.module.option.BooleanOption;
|
||||||
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
||||||
import pm.j4.kerosene.util.module.option.KeybindOption;
|
import pm.j4.kerosene.util.module.option.KeybindOption;
|
||||||
|
@ -136,7 +137,6 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
|
||||||
if (this.displayText != null) {
|
if (this.displayText != null) {
|
||||||
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, displayText, x, y, 0xAAAAAA);
|
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, displayText, x, y, 0xAAAAAA);
|
||||||
}
|
}
|
||||||
System.out.println(option);
|
|
||||||
if (this.option != null) {
|
if (this.option != null) {
|
||||||
//TODO option text box (?)
|
//TODO option text box (?)
|
||||||
// option should be centered or otherwise offset
|
// option should be centered or otherwise offset
|
||||||
|
@ -145,12 +145,9 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
|
||||||
//TODO use TranslatableText instead of LiteralText
|
//TODO use TranslatableText instead of LiteralText
|
||||||
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, new LiteralText(option.getDescription() + " " + option.getStringValue()), x, y + fontHeight + 4, 0xFFFFFF);
|
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, new LiteralText(option.getDescription() + " " + option.getStringValue()), x, y + fontHeight + 4, 0xFFFFFF);
|
||||||
|
|
||||||
System.out.println(elements.size());
|
|
||||||
if(elements.size() == 0) {
|
if(elements.size() == 0) {
|
||||||
String className = option.getClass().toString();
|
String className = option.getClass().toString();
|
||||||
System.out.println(className);
|
|
||||||
if (className.equals(BooleanOption.class.toString())) {
|
if (className.equals(BooleanOption.class.toString())) {
|
||||||
System.out.println("boolean");
|
|
||||||
elements.add(new ButtonWidget(x, y + (int)(fontHeight * 2.5),
|
elements.add(new ButtonWidget(x, y + (int)(fontHeight * 2.5),
|
||||||
entryWidth,
|
entryWidth,
|
||||||
fontHeight * 2,
|
fontHeight * 2,
|
||||||
|
@ -165,10 +162,9 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
|
||||||
else if (className.equals(ListOption.class.toString())) {
|
else if (className.equals(ListOption.class.toString())) {
|
||||||
// TODO: determine whether list options are viable,
|
// TODO: determine whether list options are viable,
|
||||||
// considering that it would be easier to split lists into multiple PModuleConfigEntries
|
// considering that it would be easier to split lists into multiple PModuleConfigEntries
|
||||||
System.out.println("list");
|
System.out.println("NYI list");
|
||||||
}
|
}
|
||||||
else if (className.equals(KeybindOption.class.toString())) {
|
else if (className.equals(KeybindOption.class.toString())) {
|
||||||
System.out.println("keybind");
|
|
||||||
ButtonWidget bindButton = new ButtonWidget(x, y + (int)(fontHeight * 2.5),
|
ButtonWidget bindButton = new ButtonWidget(x, y + (int)(fontHeight * 2.5),
|
||||||
entryWidth,
|
entryWidth,
|
||||||
fontHeight * 2,
|
fontHeight * 2,
|
||||||
|
@ -180,14 +176,27 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
|
||||||
if (keyCode != 257 && keyCode != 32 && keyCode != 335) {
|
if (keyCode != 257 && keyCode != 32 && keyCode != 335) {
|
||||||
KeybindOption newValue = new KeybindOption(option.getConfigKey(), option.getDescription(), option.getParent());
|
KeybindOption newValue = new KeybindOption(option.getConfigKey(), option.getDescription(), option.getParent());
|
||||||
KeyBinding bind = new KeyBinding(((KeybindOption)option).getTranslationKey(), keyCode, "category." + option.getParent().getParent());
|
KeyBinding bind = new KeyBinding(((KeybindOption)option).getTranslationKey(), keyCode, "category." + option.getParent().getParent());
|
||||||
newValue.fromKeybind(bind, option.getParent());
|
System.out.println("looking for binds in "
|
||||||
|
+ option.getParent().getModuleName()
|
||||||
|
+ " that match the name "
|
||||||
|
+ ((KeybindOption)option).getBindingInfo().attachedFunctionName);
|
||||||
|
List<ModuleFunction> foundBinds = option.getParent().matchBinding(((KeybindOption)option).getBindingInfo().attachedFunctionName);
|
||||||
|
if (foundBinds.size() > 0) {
|
||||||
|
newValue.fromKeybind(bind, foundBinds.get(0));
|
||||||
Optional<ConfigHolder> config = ConfigManager.getConfig(option.getParent().getParent());
|
Optional<ConfigHolder> config = ConfigManager.getConfig(option.getParent().getParent());
|
||||||
assert config.isPresent();
|
if (config.isPresent()) {
|
||||||
config.get().globalConfig.setBinding(bind, option.getParent());
|
config.get().globalConfig.setBinding(bind, foundBinds.get(0));
|
||||||
option = newValue;
|
option = newValue;
|
||||||
this.setMessage(new LiteralText(option.getStringValue().toUpperCase()));
|
this.setMessage(new LiteralText(option.getStringValue().toUpperCase()));
|
||||||
|
}
|
||||||
selected = null;
|
selected = null;
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("no binds found to set!");
|
||||||
|
selected = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
|
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
|
||||||
this.onPress();
|
this.onPress();
|
||||||
|
@ -201,7 +210,7 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
|
||||||
elements.add(bindButton);
|
elements.add(bindButton);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.println("other/string");
|
System.out.println("NYI other/string");
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,9 +73,10 @@ public class POptionsScreen extends Screen {
|
||||||
@Override
|
@Override
|
||||||
public void onClose() {
|
public void onClose() {
|
||||||
super.onClose();
|
super.onClose();
|
||||||
assert this.client != null;
|
if (this.client != null) {
|
||||||
this.client.openScreen(previousScreen);
|
this.client.openScreen(previousScreen);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void init() {
|
protected void init() {
|
||||||
paneY = 48;
|
paneY = 48;
|
||||||
|
@ -105,8 +106,9 @@ public class POptionsScreen extends Screen {
|
||||||
configurableModules.forEach(module -> this.modules.addEntry(new POptionEntry(module, this.modules)));
|
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) -> {
|
this.addButton(new ButtonWidget(this.width / 2 - 75, this.height - 30, 150, 20, ScreenTexts.DONE, (buttonWidget) -> {
|
||||||
ConfigManager.saveEverything();
|
ConfigManager.saveEverything();
|
||||||
assert this.client != null;
|
if (this.client != null) {
|
||||||
this.client.openScreen(this.previousScreen);
|
this.client.openScreen(this.previousScreen);
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,8 +35,9 @@ public class OptionsMenuMixin extends Screen {
|
||||||
method = "init()V")
|
method = "init()V")
|
||||||
protected void init(CallbackInfo ci) {
|
protected void init(CallbackInfo ci) {
|
||||||
this.addButton(new ButtonWidget(this.width / 2 - 75, this.height / 6 + 140, 150, 20, new TranslatableText("kerosene.options"), (buttonWidget) -> {
|
this.addButton(new ButtonWidget(this.width / 2 - 75, this.height / 6 + 140, 150, 20, new TranslatableText("kerosene.options"), (buttonWidget) -> {
|
||||||
assert this.client != null;
|
if (this.client != null) {
|
||||||
this.client.openScreen(new POptionsScreen(this));
|
this.client.openScreen(new POptionsScreen(this));
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
package pm.j4.kerosene.modules;
|
package pm.j4.kerosene.modules;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import net.minecraft.client.options.KeyBinding;
|
||||||
|
import net.minecraft.client.util.InputUtil;
|
||||||
|
import org.lwjgl.glfw.GLFW;
|
||||||
import pm.j4.kerosene.util.module.ModuleBase;
|
import pm.j4.kerosene.util.module.ModuleBase;
|
||||||
|
import pm.j4.kerosene.util.module.ModuleFunction;
|
||||||
import pm.j4.kerosene.util.module.option.BooleanOption;
|
import pm.j4.kerosene.util.module.option.BooleanOption;
|
||||||
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
||||||
|
|
||||||
|
@ -31,6 +37,27 @@ public class ExampleModule extends ModuleBase {
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<KeyBinding, ModuleFunction> getDefaultBindings() {
|
||||||
|
Map<KeyBinding, ModuleFunction> binds = new HashMap<>();
|
||||||
|
|
||||||
|
KeyBinding openMenu = new KeyBinding(
|
||||||
|
"key.kerosene.example",
|
||||||
|
InputUtil.Type.KEYSYM,
|
||||||
|
GLFW.GLFW_KEY_P,
|
||||||
|
"category.kerosene"
|
||||||
|
);
|
||||||
|
ModuleFunction exampleFunc = new ModuleFunction("kerosene.example.test", this) {
|
||||||
|
@Override
|
||||||
|
public void activate(MinecraftClient client) {
|
||||||
|
System.out.println("example function activated");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
binds.put(openMenu, exampleFunc);
|
||||||
|
|
||||||
|
return binds;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void activate(MinecraftClient client) {
|
public void activate(MinecraftClient client) {
|
||||||
System.out.println("Example Mod Keybind Activate");
|
System.out.println("Example Mod Keybind Activate");
|
||||||
|
|
|
@ -27,4 +27,6 @@ public class BindingInfo {
|
||||||
* The Attached function id.
|
* The Attached function id.
|
||||||
*/
|
*/
|
||||||
public String attachedModuleName;
|
public String attachedModuleName;
|
||||||
|
|
||||||
|
public String attachedFunctionName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,13 @@ package pm.j4.kerosene.modules.bindings;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import net.minecraft.client.options.KeyBinding;
|
import net.minecraft.client.options.KeyBinding;
|
||||||
import net.minecraft.client.util.InputUtil;
|
|
||||||
import net.minecraft.text.TranslatableText;
|
import net.minecraft.text.TranslatableText;
|
||||||
import org.lwjgl.glfw.GLFW;
|
|
||||||
import pm.j4.kerosene.gui.PModuleConfigEntry;
|
import pm.j4.kerosene.gui.PModuleConfigEntry;
|
||||||
import pm.j4.kerosene.gui.PModuleConfigPane;
|
import pm.j4.kerosene.gui.PModuleConfigPane;
|
||||||
import pm.j4.kerosene.util.config.ConfigHolder;
|
import pm.j4.kerosene.util.config.ConfigHolder;
|
||||||
import pm.j4.kerosene.util.config.ConfigManager;
|
import pm.j4.kerosene.util.config.ConfigManager;
|
||||||
import pm.j4.kerosene.util.config.GlobalConfig;
|
|
||||||
import pm.j4.kerosene.util.data.ModInfoProvider;
|
|
||||||
import pm.j4.kerosene.util.module.ModuleBase;
|
import pm.j4.kerosene.util.module.ModuleBase;
|
||||||
|
import pm.j4.kerosene.util.module.ModuleFunction;
|
||||||
import pm.j4.kerosene.util.module.option.KeybindOption;
|
import pm.j4.kerosene.util.module.option.KeybindOption;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,7 +30,6 @@ public class BindingManager extends ModuleBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
registerBindings();
|
|
||||||
super.init();
|
super.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,18 +41,21 @@ public class BindingManager extends ModuleBase {
|
||||||
// which replace the ModuleBase in bindings?
|
// which replace the ModuleBase in bindings?
|
||||||
List<PModuleConfigEntry> entries = new ArrayList<>();
|
List<PModuleConfigEntry> entries = new ArrayList<>();
|
||||||
|
|
||||||
Map<KeybindOption, ModuleBase> mapped = new HashMap<>();
|
Map<KeybindOption, ModuleFunction> mapped = new HashMap<>();
|
||||||
Optional<ConfigHolder> config = ConfigManager.getConfig(this.getParent());
|
Map<String, ConfigHolder> configs = ConfigManager.getAllConfigs();
|
||||||
if (config.isPresent()) {
|
configs.forEach((modName, config) -> {
|
||||||
Map<KeyBinding, ModuleBase> binds = config.get().globalConfig.bindings;
|
if (config.globalConfig != null) {
|
||||||
|
Map<KeyBinding, ModuleFunction> binds = config.globalConfig.bindings;
|
||||||
binds.forEach((key, func) -> {
|
binds.forEach((key, func) -> {
|
||||||
KeybindOption option = new KeybindOption(func.getModuleName() + " " + func.getCategory(), func.getModuleName() + " " + func.getCategory(), func);
|
ModuleBase parent = func.getParent();
|
||||||
|
KeybindOption option = new KeybindOption(parent.getModuleName() + " " + parent.getCategory(), parent.getModuleName() + " " + parent.getCategory(), parent);
|
||||||
option.fromKeybind(key, func);
|
option.fromKeybind(key, func);
|
||||||
mapped.put(option, func);
|
mapped.put(option, func);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
mapped.forEach((configEntry, module) -> {
|
});
|
||||||
|
mapped.forEach((configEntry, func) -> {
|
||||||
|
ModuleBase module = func.getParent();
|
||||||
PModuleConfigEntry entry = new PModuleConfigEntry(configEntry, new TranslatableText(module.getModuleName()), sourcePane);
|
PModuleConfigEntry entry = new PModuleConfigEntry(configEntry, new TranslatableText(module.getModuleName()), sourcePane);
|
||||||
entries.add(entry);
|
entries.add(entry);
|
||||||
});
|
});
|
||||||
|
@ -66,40 +65,35 @@ public class BindingManager extends ModuleBase {
|
||||||
/**
|
/**
|
||||||
* Register bindings.
|
* Register bindings.
|
||||||
*/
|
*/
|
||||||
private void registerBindings() {
|
public static void registerBindings(ModuleBase module) {
|
||||||
Optional<ConfigHolder> config = ConfigManager.getConfig(this.getParent());
|
System.out.println("registering bindings for " + module.getModuleName());
|
||||||
if (!config.isPresent()) {
|
Optional<ConfigHolder> config = ConfigManager.getConfig(module.getParent());
|
||||||
return;
|
Map<KeyBinding, ModuleFunction> defaults = module.getDefaultBindings();
|
||||||
}
|
config.ifPresent((holder) -> {
|
||||||
GlobalConfig c = config.get().globalConfig;
|
System.out.println("config present");
|
||||||
Optional<ModuleBase> mod = ModInfoProvider.getMod("petroleum.modmenu");
|
defaults.forEach((bind, func) -> {
|
||||||
if (mod.isPresent() && !c.isBound(mod.get())) {
|
System.out.println("default value:" + bind.getTranslationKey() + " bound to " + func.getFunctionName());
|
||||||
//TODO
|
if(!holder.globalConfig.isBound(func)) {
|
||||||
// the only explicit keybinding (for now.)
|
System.out.println("no value set, using default");
|
||||||
// once the binding manager has been completed,
|
holder.globalConfig.setBinding(bind, func);
|
||||||
// this should be migrated there, as a default binding
|
|
||||||
KeyBinding binding = new KeyBinding(
|
|
||||||
"key.petroleum.togglemodmenu",
|
|
||||||
InputUtil.Type.KEYSYM,
|
|
||||||
GLFW.GLFW_KEY_RIGHT_CONTROL,
|
|
||||||
"category.petroleum"
|
|
||||||
);
|
|
||||||
config.get().globalConfig.setBinding(binding, mod.get());
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constant registeredBinds.
|
* The constant registeredBinds.
|
||||||
*/
|
*/
|
||||||
private static final List<KeyBinding> registeredBinds = new ArrayList<>();
|
private static final Map<KeyBinding, ModuleFunction> registeredBinds = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add bind.
|
* Add bind.
|
||||||
*
|
*
|
||||||
* @param b the b
|
* @param bind the bind
|
||||||
|
* @param function the function
|
||||||
*/
|
*/
|
||||||
public static void addBind(KeyBinding b) {
|
public static void addBind(KeyBinding bind, ModuleFunction function) {
|
||||||
registeredBinds.add(b);
|
registeredBinds.put(bind, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -116,7 +110,7 @@ public class BindingManager extends ModuleBase {
|
||||||
*
|
*
|
||||||
* @return the active keybinds
|
* @return the active keybinds
|
||||||
*/
|
*/
|
||||||
public static List<KeyBinding> getActiveKeybinds() {
|
public static Map<KeyBinding, ModuleFunction> getActiveKeybinds() {
|
||||||
return registeredBinds;
|
return registeredBinds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package pm.j4.kerosene.util.config;
|
package pm.j4.kerosene.util.config;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -13,11 +14,11 @@ public class ConfigHolder {
|
||||||
/**
|
/**
|
||||||
* The Global config.
|
* The Global config.
|
||||||
*/
|
*/
|
||||||
public GlobalConfig globalConfig;
|
public GlobalConfig globalConfig = new GlobalConfig();
|
||||||
/**
|
/**
|
||||||
* The Server configs.
|
* The Server configs.
|
||||||
*/
|
*/
|
||||||
public Map<String, ServerConfig> serverConfigs;
|
public Map<String, ServerConfig> serverConfigs = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is module enabled boolean.
|
* Is module enabled boolean.
|
||||||
|
|
|
@ -4,14 +4,10 @@ import com.google.common.reflect.TypeToken;
|
||||||
import com.google.gson.*;
|
import com.google.gson.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.AnnotatedElement;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import net.fabricmc.api.ModInitializer;
|
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import pm.j4.kerosene.modules.ExampleModule;
|
|
||||||
import pm.j4.kerosene.modules.bindings.BindingInfo;
|
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.ModInfoProvider;
|
||||||
import pm.j4.kerosene.util.data.ModuleConfig;
|
import pm.j4.kerosene.util.data.ModuleConfig;
|
||||||
import pm.j4.kerosene.util.data.OptionSerializiable;
|
import pm.j4.kerosene.util.data.OptionSerializiable;
|
||||||
|
@ -57,7 +53,7 @@ public class ConfigManager {
|
||||||
public static void initConfig(String moduleName, Class parent) {
|
public static void initConfig(String moduleName, Class parent) {
|
||||||
try {
|
try {
|
||||||
Annotation moduleAnnotation = parent.getAnnotation(Modules.class);
|
Annotation moduleAnnotation = parent.getAnnotation(Modules.class);
|
||||||
assert moduleAnnotation != null;
|
if (moduleAnnotation != null) {
|
||||||
Module[] modules = ((Modules)moduleAnnotation).value();
|
Module[] modules = ((Modules)moduleAnnotation).value();
|
||||||
System.out.println(modules);
|
System.out.println(modules);
|
||||||
for (int i = 0; i < modules.length; i++) {
|
for (int i = 0; i < modules.length; i++) {
|
||||||
|
@ -65,6 +61,7 @@ public class ConfigManager {
|
||||||
ModInfoProvider.registerMod(modules[i].value());
|
ModInfoProvider.registerMod(modules[i].value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
System.out.println(e);
|
System.out.println(e);
|
||||||
}
|
}
|
||||||
|
@ -75,7 +72,7 @@ public class ConfigManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigHolder config = new ConfigHolder();
|
ConfigHolder config = new ConfigHolder();
|
||||||
config.globalConfig = new DefaultConfig();
|
config.globalConfig = new GlobalConfig();
|
||||||
config.serverConfigs = new HashMap<>();
|
config.serverConfigs = new HashMap<>();
|
||||||
|
|
||||||
System.out.println("load cfg");
|
System.out.println("load cfg");
|
||||||
|
@ -97,10 +94,12 @@ public class ConfigManager {
|
||||||
System.out.println("initialize " + module.getModuleName());
|
System.out.println("initialize " + module.getModuleName());
|
||||||
if (options != null && options.options != null) {
|
if (options != null && options.options != null) {
|
||||||
options.options.forEach((key, option) -> {
|
options.options.forEach((key, option) -> {
|
||||||
if (module.hasOption(option.key)) {
|
if (module.hasOption(option.key) && option.value != null) {
|
||||||
System.out.println("setting " + option.key + " to " + option.value.getAsString());
|
|
||||||
module.setConfigOption(option.key, option.value);
|
module.setConfigOption(option.key, option.value);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("ignoring unknown option " + option.key);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
module.initialized = true;
|
module.initialized = true;
|
||||||
|
@ -149,11 +148,15 @@ public class ConfigManager {
|
||||||
*
|
*
|
||||||
* @param <T> the type parameter
|
* @param <T> the type parameter
|
||||||
* @param element the element
|
* @param element the element
|
||||||
* @param tClass the t class
|
* @param valueClass the t class
|
||||||
* @return the t
|
* @return the t
|
||||||
*/
|
*/
|
||||||
public static <T> T deserializeElement(JsonElement element, Class<T> tClass) {
|
public static <T> T deserializeElement(JsonElement element, Class<T> valueClass) {
|
||||||
return GSON.fromJson(element, tClass);
|
return GSON.fromJson(element, valueClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> JsonElement serializeElement(T value) {
|
||||||
|
return GSON.toJsonTree(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -226,6 +229,10 @@ public class ConfigManager {
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<String, ConfigHolder> getAllConfigs() {
|
||||||
|
return configs;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
package pm.j4.kerosene.util.config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The type Default config.
|
|
||||||
*/
|
|
||||||
public class DefaultConfig extends GlobalConfig {
|
|
||||||
/**
|
|
||||||
* Instantiates a new Default config.
|
|
||||||
*/
|
|
||||||
public DefaultConfig() {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,6 +10,7 @@ import pm.j4.kerosene.modules.bindings.BindingManager;
|
||||||
import pm.j4.kerosene.util.data.ModInfoProvider;
|
import pm.j4.kerosene.util.data.ModInfoProvider;
|
||||||
import pm.j4.kerosene.util.data.OptionSerializiable;
|
import pm.j4.kerosene.util.data.OptionSerializiable;
|
||||||
import pm.j4.kerosene.util.module.ModuleBase;
|
import pm.j4.kerosene.util.module.ModuleBase;
|
||||||
|
import pm.j4.kerosene.util.module.ModuleFunction;
|
||||||
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,7 +20,7 @@ public class GlobalConfig extends Config {
|
||||||
/**
|
/**
|
||||||
* The Bindings.
|
* The Bindings.
|
||||||
*/
|
*/
|
||||||
public final Map<KeyBinding, ModuleBase> bindings = new HashMap<>();
|
public final Map<KeyBinding, ModuleFunction> bindings = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is bound boolean.
|
* Is bound boolean.
|
||||||
|
@ -27,7 +28,7 @@ public class GlobalConfig extends Config {
|
||||||
* @param func the func
|
* @param func the func
|
||||||
* @return the boolean
|
* @return the boolean
|
||||||
*/
|
*/
|
||||||
public boolean isBound(ModuleBase func) {
|
public boolean isBound(ModuleFunction func) {
|
||||||
AtomicBoolean found = new AtomicBoolean(false);
|
AtomicBoolean found = new AtomicBoolean(false);
|
||||||
bindings.forEach((key, binding) -> {
|
bindings.forEach((key, binding) -> {
|
||||||
if (binding.equals(func)) {
|
if (binding.equals(func)) {
|
||||||
|
@ -43,7 +44,7 @@ public class GlobalConfig extends Config {
|
||||||
* @param bind the bind
|
* @param bind the bind
|
||||||
* @param func the func
|
* @param func the func
|
||||||
*/
|
*/
|
||||||
public void setBinding(KeyBinding bind, ModuleBase func) {
|
public void setBinding(KeyBinding bind, ModuleFunction func) {
|
||||||
AtomicReference<KeyBinding> match = new AtomicReference<>();
|
AtomicReference<KeyBinding> match = new AtomicReference<>();
|
||||||
if (bindings.containsValue(func)) {
|
if (bindings.containsValue(func)) {
|
||||||
bindings.forEach((key, binding) -> {
|
bindings.forEach((key, binding) -> {
|
||||||
|
@ -58,8 +59,9 @@ public class GlobalConfig extends Config {
|
||||||
bindings.remove(match.get());
|
bindings.remove(match.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ModInfoProvider.isActive(func.getModuleName())) {
|
if (ModInfoProvider.isActive(func.getParent().getModuleName())) {
|
||||||
BindingManager.addBind(bind);
|
BindingManager.addBind(bind, func);
|
||||||
|
func.getParent().setBinding(bind, func);
|
||||||
bindings.put(bind, func);
|
bindings.put(bind, func);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,8 +73,12 @@ public class GlobalConfig extends Config {
|
||||||
*/
|
*/
|
||||||
private void convertBinding(BindingInfo info) {
|
private void convertBinding(BindingInfo info) {
|
||||||
Optional<ModuleBase> match = ModInfoProvider.getMod(info.attachedModuleName);
|
Optional<ModuleBase> match = ModInfoProvider.getMod(info.attachedModuleName);
|
||||||
match.ifPresent(moduleBase -> setBinding(reconstructBinding(info),
|
match.ifPresent(moduleBase -> {
|
||||||
moduleBase));
|
List<ModuleFunction> foundBinds = moduleBase.matchBinding(info.attachedFunctionName);
|
||||||
|
if (foundBinds.size() > 0) {
|
||||||
|
setBinding(reconstructBinding(info), foundBinds.get(0));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -97,9 +103,10 @@ public class GlobalConfig extends Config {
|
||||||
* @param f the f
|
* @param f the f
|
||||||
* @return the binding info
|
* @return the binding info
|
||||||
*/
|
*/
|
||||||
public static BindingInfo extractBinding(KeyBinding b, ModuleBase f) {
|
public static BindingInfo extractBinding(KeyBinding b, ModuleFunction f) {
|
||||||
BindingInfo res = new BindingInfo();
|
BindingInfo res = new BindingInfo();
|
||||||
res.attachedModuleName = f.getModuleName();
|
res.attachedModuleName = f.getParent().getModuleName();
|
||||||
|
res.attachedFunctionName = f.getFunctionName();
|
||||||
|
|
||||||
res.translationKey = b.getTranslationKey();
|
res.translationKey = b.getTranslationKey();
|
||||||
InputUtil.Key k = b.getDefaultKey();
|
InputUtil.Key k = b.getDefaultKey();
|
||||||
|
|
|
@ -2,10 +2,13 @@ package pm.j4.kerosene.util.module;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import net.minecraft.client.MinecraftClient;
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import net.minecraft.client.options.KeyBinding;
|
||||||
import net.minecraft.text.TranslatableText;
|
import net.minecraft.text.TranslatableText;
|
||||||
import pm.j4.kerosene.gui.PModuleConfigEntry;
|
import pm.j4.kerosene.gui.PModuleConfigEntry;
|
||||||
import pm.j4.kerosene.gui.PModuleConfigPane;
|
import pm.j4.kerosene.gui.PModuleConfigPane;
|
||||||
|
import pm.j4.kerosene.modules.bindings.BindingManager;
|
||||||
import pm.j4.kerosene.util.config.ConfigHolder;
|
import pm.j4.kerosene.util.config.ConfigHolder;
|
||||||
import pm.j4.kerosene.util.config.ConfigManager;
|
import pm.j4.kerosene.util.config.ConfigManager;
|
||||||
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
||||||
|
@ -41,7 +44,7 @@ public abstract class ModuleBase {
|
||||||
* Init.
|
* Init.
|
||||||
*/
|
*/
|
||||||
public void init() {
|
public void init() {
|
||||||
|
BindingManager.registerBindings(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -250,6 +253,29 @@ public abstract class ModuleBase {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<KeyBinding, ModuleFunction> attachedBinds = new HashMap<>();
|
||||||
|
|
||||||
|
public Map<KeyBinding, ModuleFunction> getDefaultBindings() {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ModuleFunction> matchBinding(String name) {
|
||||||
|
List<ModuleFunction> result = attachedBinds.entrySet().stream()
|
||||||
|
.filter(entry -> entry.getValue().getFunctionName().equals(name))
|
||||||
|
.map(entry -> entry.getValue())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBinding(KeyBinding bind, ModuleFunction function) {
|
||||||
|
if (attachedBinds.containsKey(bind)) {
|
||||||
|
attachedBinds.replace(bind, function);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
attachedBinds.put(bind, function);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets config entries.
|
* Gets config entries.
|
||||||
*
|
*
|
||||||
|
|
24
src/main/java/pm/j4/kerosene/util/module/ModuleFunction.java
Normal file
24
src/main/java/pm/j4/kerosene/util/module/ModuleFunction.java
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package pm.j4.kerosene.util.module;
|
||||||
|
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
|
||||||
|
public class ModuleFunction {
|
||||||
|
public ModuleFunction(String name, ModuleBase parent) {
|
||||||
|
this.name = name;
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
private final ModuleBase parent;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public ModuleBase getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFunctionName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void activate(MinecraftClient client) {
|
||||||
|
this.parent.activate(client);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import pm.j4.kerosene.modules.bindings.BindingInfo;
|
||||||
import pm.j4.kerosene.util.config.ConfigManager;
|
import pm.j4.kerosene.util.config.ConfigManager;
|
||||||
import pm.j4.kerosene.util.config.GlobalConfig;
|
import pm.j4.kerosene.util.config.GlobalConfig;
|
||||||
import pm.j4.kerosene.util.module.ModuleBase;
|
import pm.j4.kerosene.util.module.ModuleBase;
|
||||||
|
import pm.j4.kerosene.util.module.ModuleFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type Keybind value.
|
* The type Keybind value.
|
||||||
|
@ -46,6 +47,10 @@ public class KeybindOption extends ConfigurationOption {
|
||||||
return value.getDefaultKey().getLocalizedText().getString();
|
return value.getDefaultKey().getLocalizedText().getString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BindingInfo getBindingInfo() {
|
||||||
|
return convertedValue;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fromJson(JsonElement e) {
|
public void fromJson(JsonElement e) {
|
||||||
BindingInfo bindingInfo = ConfigManager.deserializeElement(e, BindingInfo.class);
|
BindingInfo bindingInfo = ConfigManager.deserializeElement(e, BindingInfo.class);
|
||||||
|
@ -64,7 +69,7 @@ public class KeybindOption extends ConfigurationOption {
|
||||||
* @param bind the bind
|
* @param bind the bind
|
||||||
* @param base the base
|
* @param base the base
|
||||||
*/
|
*/
|
||||||
public void fromKeybind(KeyBinding bind, ModuleBase base) {
|
public void fromKeybind(KeyBinding bind, ModuleFunction base) {
|
||||||
this.value = bind;
|
this.value = bind;
|
||||||
this.convertedValue = GlobalConfig.extractBinding(bind, base);
|
this.convertedValue = GlobalConfig.extractBinding(bind, base);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
"kerosene.bindings": "Binding Manager",
|
"kerosene.bindings": "Binding Manager",
|
||||||
"kerosene.options": "Kerosene Options"
|
"kerosene.options": "Kerosene Options",
|
||||||
|
"kerosene.example": "Example Module"
|
||||||
}
|
}
|
Loading…
Reference in a new issue