kerosene/src/main/java/pm/j4/kerosene/modules/bindings/BindingManager.java

91 lines
2.7 KiB
Java

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.KeroseneMod;
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.module.ModuleBase;
import pm.j4.kerosene.util.module.option.KeybindOption;
/**
* The type Binding manager.
*/
public class BindingManager extends ModuleBase {
/**
* Instantiates a new Module base.
* Parameters should be constant across restarts.
*/
public BindingManager() {
super("kerosene",
"kerosene.bindings",
"kerosene.misc",
false,
true,
true);
}
@Override
public void init() {
registerBindings();
super.init();
}
@Override
public List<PModuleConfigEntry> getConfigEntries(PModuleConfigPane sourcePane) {
//TODO multiple binds per module
// thoughts: have modules include a list of module triggers/functions
// 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) -> {
PModuleConfigEntry entry = new PModuleConfigEntry(configEntry, new TranslatableText(module.getModuleName()), sourcePane);
entries.add(entry);
});
return entries;
}
/**
* Register bindings.
*/
private void registerBindings() {
Optional<ConfigHolder> config = ConfigManager.getConfig(this.getParent());
if (!config.isPresent()) {
return;
}
GlobalConfig c = config.get().globalConfig;
Optional<ModuleBase> mod = KeroseneMod.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());
}
}
}