This commit is contained in:
jane 2020-12-22 19:51:20 -05:00
parent 7b41f24211
commit 2ea888ecaa
16 changed files with 222 additions and 112 deletions

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.10.8
# Mod Properties
mod_version = 0.1.6-SNAPSHOT
mod_version = 0.1.6
maven_group = pm.j4
archives_base_name = kerosene

View File

@ -1,5 +1,6 @@
package pm.j4.kerosene;
import java.util.Map;
import java.util.Optional;
import net.fabricmc.api.ModInitializer;
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.data.ModInfoProvider;
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);
//initialize keybind handler
conf.ifPresent(configHolder -> ClientTickEvents.END_CLIENT_TICK.register(client -> {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (ModInfoProvider.client != client) {
ModInfoProvider.client = client;
}
for (KeyBinding b : BindingManager.getActiveKeybinds()) {
while (b.wasPressed()) {
configHolder.globalConfig.bindings.get(b).activate(client);
Map<KeyBinding, ModuleFunction> binds = BindingManager.getActiveKeybinds();
binds.forEach((bind, func) -> {
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();
}
}

View File

@ -14,6 +14,7 @@ import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import pm.j4.kerosene.util.config.ConfigHolder;
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.ConfigurationOption;
import pm.j4.kerosene.util.module.option.KeybindOption;
@ -136,7 +137,6 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
if (this.displayText != null) {
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, displayText, x, y, 0xAAAAAA);
}
System.out.println(option);
if (this.option != null) {
//TODO option text box (?)
// option should be centered or otherwise offset
@ -145,12 +145,9 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
//TODO use TranslatableText instead of LiteralText
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) {
String className = option.getClass().toString();
System.out.println(className);
if (className.equals(BooleanOption.class.toString())) {
System.out.println("boolean");
elements.add(new ButtonWidget(x, y + (int)(fontHeight * 2.5),
entryWidth,
fontHeight * 2,
@ -165,10 +162,9 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
else if (className.equals(ListOption.class.toString())) {
// TODO: determine whether list options are viable,
// 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())) {
System.out.println("keybind");
ButtonWidget bindButton = new ButtonWidget(x, y + (int)(fontHeight * 2.5),
entryWidth,
fontHeight * 2,
@ -180,14 +176,27 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
if (keyCode != 257 && keyCode != 32 && keyCode != 335) {
KeybindOption newValue = new KeybindOption(option.getConfigKey(), option.getDescription(), option.getParent());
KeyBinding bind = new KeyBinding(((KeybindOption)option).getTranslationKey(), keyCode, "category." + option.getParent().getParent());
newValue.fromKeybind(bind, option.getParent());
Optional<ConfigHolder> config = ConfigManager.getConfig(option.getParent().getParent());
assert config.isPresent();
config.get().globalConfig.setBinding(bind, option.getParent());
option = newValue;
this.setMessage(new LiteralText(option.getStringValue().toUpperCase()));
selected = null;
return false;
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());
if (config.isPresent()) {
config.get().globalConfig.setBinding(bind, foundBinds.get(0));
option = newValue;
this.setMessage(new LiteralText(option.getStringValue().toUpperCase()));
}
selected = null;
return false;
}
else {
System.out.println("no binds found to set!");
selected = null;
return true;
}
} else {
this.playDownSound(MinecraftClient.getInstance().getSoundManager());
this.onPress();
@ -201,7 +210,7 @@ public class PModuleConfigEntry extends ElementListWidget.Entry<PModuleConfigEnt
elements.add(bindButton);
}
else {
System.out.println("other/string");
System.out.println("NYI other/string");
//TODO
}
}

View File

@ -73,8 +73,9 @@ public class POptionsScreen extends Screen {
@Override
public void onClose() {
super.onClose();
assert this.client != null;
this.client.openScreen(previousScreen);
if (this.client != null) {
this.client.openScreen(previousScreen);
}
}
protected void init() {
@ -105,8 +106,9 @@ public class POptionsScreen extends Screen {
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.saveEverything();
assert this.client != null;
this.client.openScreen(this.previousScreen);
if (this.client != null) {
this.client.openScreen(this.previousScreen);
}
}));
}

View File

@ -35,8 +35,9 @@ public class OptionsMenuMixin extends Screen {
method = "init()V")
protected void init(CallbackInfo ci) {
this.addButton(new ButtonWidget(this.width / 2 - 75, this.height / 6 + 140, 150, 20, new TranslatableText("kerosene.options"), (buttonWidget) -> {
assert this.client != null;
this.client.openScreen(new POptionsScreen(this));
if (this.client != null) {
this.client.openScreen(new POptionsScreen(this));
}
}));
}
}

View File

@ -1,9 +1,15 @@
package pm.j4.kerosene.modules;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.ModuleFunction;
import pm.j4.kerosene.util.module.option.BooleanOption;
import pm.j4.kerosene.util.module.option.ConfigurationOption;
@ -31,6 +37,27 @@ public class ExampleModule extends ModuleBase {
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
public void activate(MinecraftClient client) {
System.out.println("Example Mod Keybind Activate");

View File

@ -27,4 +27,6 @@ public class BindingInfo {
* The Attached function id.
*/
public String attachedModuleName;
public String attachedFunctionName;
}

View File

@ -2,16 +2,13 @@ package pm.j4.kerosene.modules.bindings;
import java.util.*;
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.gui.PModuleConfigEntry;
import pm.j4.kerosene.gui.PModuleConfigPane;
import pm.j4.kerosene.util.config.ConfigHolder;
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.ModuleFunction;
import pm.j4.kerosene.util.module.option.KeybindOption;
/**
@ -33,7 +30,6 @@ public class BindingManager extends ModuleBase {
@Override
public void init() {
registerBindings();
super.init();
}
@ -45,18 +41,21 @@ public class BindingManager extends ModuleBase {
// which replace the ModuleBase in bindings?
List<PModuleConfigEntry> entries = new ArrayList<>();
Map<KeybindOption, ModuleBase> mapped = new HashMap<>();
Optional<ConfigHolder> config = ConfigManager.getConfig(this.getParent());
if (config.isPresent()) {
Map<KeyBinding, ModuleBase> binds = config.get().globalConfig.bindings;
binds.forEach((key, func) -> {
KeybindOption option = new KeybindOption(func.getModuleName() + " " + func.getCategory(), func.getModuleName() + " " + func.getCategory(), func);
option.fromKeybind(key, func);
mapped.put(option, func);
});
}
mapped.forEach((configEntry, module) -> {
Map<KeybindOption, ModuleFunction> mapped = new HashMap<>();
Map<String, ConfigHolder> configs = ConfigManager.getAllConfigs();
configs.forEach((modName, config) -> {
if (config.globalConfig != null) {
Map<KeyBinding, ModuleFunction> binds = config.globalConfig.bindings;
binds.forEach((key, func) -> {
ModuleBase parent = func.getParent();
KeybindOption option = new KeybindOption(parent.getModuleName() + " " + parent.getCategory(), parent.getModuleName() + " " + parent.getCategory(), parent);
option.fromKeybind(key, func);
mapped.put(option, func);
});
}
});
mapped.forEach((configEntry, func) -> {
ModuleBase module = func.getParent();
PModuleConfigEntry entry = new PModuleConfigEntry(configEntry, new TranslatableText(module.getModuleName()), sourcePane);
entries.add(entry);
});
@ -66,40 +65,35 @@ public class BindingManager extends ModuleBase {
/**
* Register bindings.
*/
private void registerBindings() {
Optional<ConfigHolder> config = ConfigManager.getConfig(this.getParent());
if (!config.isPresent()) {
return;
}
GlobalConfig c = config.get().globalConfig;
Optional<ModuleBase> mod = ModInfoProvider.getMod("petroleum.modmenu");
if (mod.isPresent() && !c.isBound(mod.get())) {
//TODO
// the only explicit keybinding (for now.)
// once the binding manager has been completed,
// 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());
}
public static void registerBindings(ModuleBase module) {
System.out.println("registering bindings for " + module.getModuleName());
Optional<ConfigHolder> config = ConfigManager.getConfig(module.getParent());
Map<KeyBinding, ModuleFunction> defaults = module.getDefaultBindings();
config.ifPresent((holder) -> {
System.out.println("config present");
defaults.forEach((bind, func) -> {
System.out.println("default value:" + bind.getTranslationKey() + " bound to " + func.getFunctionName());
if(!holder.globalConfig.isBound(func)) {
System.out.println("no value set, using default");
holder.globalConfig.setBinding(bind, func);
}
});
});
}
/**
* The constant registeredBinds.
*/
private static final List<KeyBinding> registeredBinds = new ArrayList<>();
private static final Map<KeyBinding, ModuleFunction> registeredBinds = new HashMap<>();
/**
* Add bind.
*
* @param b the b
* @param bind the bind
* @param function the function
*/
public static void addBind(KeyBinding b) {
registeredBinds.add(b);
public static void addBind(KeyBinding bind, ModuleFunction function) {
registeredBinds.put(bind, function);
}
/**
@ -116,7 +110,7 @@ public class BindingManager extends ModuleBase {
*
* @return the active keybinds
*/
public static List<KeyBinding> getActiveKeybinds() {
public static Map<KeyBinding, ModuleFunction> getActiveKeybinds() {
return registeredBinds;
}
}

View File

@ -1,5 +1,6 @@
package pm.j4.kerosene.util.config;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -13,11 +14,11 @@ public class ConfigHolder {
/**
* The Global config.
*/
public GlobalConfig globalConfig;
public GlobalConfig globalConfig = new GlobalConfig();
/**
* The Server configs.
*/
public Map<String, ServerConfig> serverConfigs;
public Map<String, ServerConfig> serverConfigs = new HashMap<>();
/**
* Is module enabled boolean.

View File

@ -4,14 +4,10 @@ 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.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;
@ -57,12 +53,13 @@ public class ConfigManager {
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());
if (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) {
@ -75,7 +72,7 @@ public class ConfigManager {
}
ConfigHolder config = new ConfigHolder();
config.globalConfig = new DefaultConfig();
config.globalConfig = new GlobalConfig();
config.serverConfigs = new HashMap<>();
System.out.println("load cfg");
@ -97,10 +94,12 @@ public class ConfigManager {
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());
if (module.hasOption(option.key) && option.value != null) {
module.setConfigOption(option.key, option.value);
}
else {
System.out.println("ignoring unknown option " + option.key);
}
});
}
module.initialized = true;
@ -149,11 +148,15 @@ public class ConfigManager {
*
* @param <T> the type parameter
* @param element the element
* @param tClass the t class
* @param valueClass the t class
* @return the t
*/
public static <T> T deserializeElement(JsonElement element, Class<T> tClass) {
return GSON.fromJson(element, tClass);
public static <T> T deserializeElement(JsonElement element, Class<T> valueClass) {
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();
}
public static Map<String, ConfigHolder> getAllConfigs() {
return configs;
}
}
/**

View File

@ -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() {
}
}

View File

@ -10,6 +10,7 @@ import pm.j4.kerosene.modules.bindings.BindingManager;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.data.OptionSerializiable;
import pm.j4.kerosene.util.module.ModuleBase;
import pm.j4.kerosene.util.module.ModuleFunction;
import pm.j4.kerosene.util.module.option.ConfigurationOption;
/**
@ -19,7 +20,7 @@ public class GlobalConfig extends Config {
/**
* The Bindings.
*/
public final Map<KeyBinding, ModuleBase> bindings = new HashMap<>();
public final Map<KeyBinding, ModuleFunction> bindings = new HashMap<>();
/**
* Is bound boolean.
@ -27,7 +28,7 @@ public class GlobalConfig extends Config {
* @param func the func
* @return the boolean
*/
public boolean isBound(ModuleBase func) {
public boolean isBound(ModuleFunction func) {
AtomicBoolean found = new AtomicBoolean(false);
bindings.forEach((key, binding) -> {
if (binding.equals(func)) {
@ -43,7 +44,7 @@ public class GlobalConfig extends Config {
* @param bind the bind
* @param func the func
*/
public void setBinding(KeyBinding bind, ModuleBase func) {
public void setBinding(KeyBinding bind, ModuleFunction func) {
AtomicReference<KeyBinding> match = new AtomicReference<>();
if (bindings.containsValue(func)) {
bindings.forEach((key, binding) -> {
@ -58,8 +59,9 @@ public class GlobalConfig extends Config {
bindings.remove(match.get());
}
if (ModInfoProvider.isActive(func.getModuleName())) {
BindingManager.addBind(bind);
if (ModInfoProvider.isActive(func.getParent().getModuleName())) {
BindingManager.addBind(bind, func);
func.getParent().setBinding(bind, func);
bindings.put(bind, func);
}
}
@ -71,8 +73,12 @@ public class GlobalConfig extends Config {
*/
private void convertBinding(BindingInfo info) {
Optional<ModuleBase> match = ModInfoProvider.getMod(info.attachedModuleName);
match.ifPresent(moduleBase -> setBinding(reconstructBinding(info),
moduleBase));
match.ifPresent(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
* @return the binding info
*/
public static BindingInfo extractBinding(KeyBinding b, ModuleBase f) {
public static BindingInfo extractBinding(KeyBinding b, ModuleFunction f) {
BindingInfo res = new BindingInfo();
res.attachedModuleName = f.getModuleName();
res.attachedModuleName = f.getParent().getModuleName();
res.attachedFunctionName = f.getFunctionName();
res.translationKey = b.getTranslationKey();
InputUtil.Key k = b.getDefaultKey();

View File

@ -2,10 +2,13 @@ package pm.j4.kerosene.util.module;
import com.google.gson.JsonElement;
import java.util.*;
import java.util.stream.Collectors;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.text.TranslatableText;
import pm.j4.kerosene.gui.PModuleConfigEntry;
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.ConfigManager;
import pm.j4.kerosene.util.module.option.ConfigurationOption;
@ -41,7 +44,7 @@ public abstract class ModuleBase {
* Init.
*/
public void init() {
BindingManager.registerBindings(this);
}
/**
@ -250,6 +253,29 @@ public abstract class ModuleBase {
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.
*

View 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);
}
}

View File

@ -6,6 +6,7 @@ import pm.j4.kerosene.modules.bindings.BindingInfo;
import pm.j4.kerosene.util.config.ConfigManager;
import pm.j4.kerosene.util.config.GlobalConfig;
import pm.j4.kerosene.util.module.ModuleBase;
import pm.j4.kerosene.util.module.ModuleFunction;
/**
* The type Keybind value.
@ -46,6 +47,10 @@ public class KeybindOption extends ConfigurationOption {
return value.getDefaultKey().getLocalizedText().getString();
}
public BindingInfo getBindingInfo() {
return convertedValue;
}
@Override
public void fromJson(JsonElement e) {
BindingInfo bindingInfo = ConfigManager.deserializeElement(e, BindingInfo.class);
@ -64,7 +69,7 @@ public class KeybindOption extends ConfigurationOption {
* @param bind the bind
* @param base the base
*/
public void fromKeybind(KeyBinding bind, ModuleBase base) {
public void fromKeybind(KeyBinding bind, ModuleFunction base) {
this.value = bind;
this.convertedValue = GlobalConfig.extractBinding(bind, base);
}

View File

@ -1,4 +1,5 @@
{
"kerosene.bindings": "Binding Manager",
"kerosene.options": "Kerosene Options"
"kerosene.options": "Kerosene Options",
"kerosene.example": "Example Module"
}