2020-12-20 04:19:55 +00:00
|
|
|
package pm.j4.kerosene.util.config;
|
2020-12-19 23:30:50 +00:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
import net.minecraft.client.options.KeyBinding;
|
|
|
|
import net.minecraft.client.util.InputUtil;
|
2020-12-20 04:19:55 +00:00
|
|
|
import pm.j4.kerosene.modules.bindings.BindingInfo;
|
2020-12-22 19:32:27 +00:00
|
|
|
import pm.j4.kerosene.modules.bindings.BindingManager;
|
|
|
|
import pm.j4.kerosene.util.data.ModInfoProvider;
|
2020-12-20 04:19:55 +00:00
|
|
|
import pm.j4.kerosene.util.data.OptionSerializiable;
|
|
|
|
import pm.j4.kerosene.util.module.ModuleBase;
|
2020-12-23 00:51:20 +00:00
|
|
|
import pm.j4.kerosene.util.module.ModuleFunction;
|
2020-12-20 04:19:55 +00:00
|
|
|
import pm.j4.kerosene.util.module.option.ConfigurationOption;
|
2020-12-19 23:30:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type Global config.
|
|
|
|
*/
|
|
|
|
public class GlobalConfig extends Config {
|
|
|
|
/**
|
|
|
|
* The Bindings.
|
|
|
|
*/
|
2020-12-23 00:51:20 +00:00
|
|
|
public final Map<KeyBinding, ModuleFunction> bindings = new HashMap<>();
|
2020-12-19 23:30:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is bound boolean.
|
|
|
|
*
|
|
|
|
* @param func the func
|
|
|
|
* @return the boolean
|
|
|
|
*/
|
2020-12-23 00:51:20 +00:00
|
|
|
public boolean isBound(ModuleFunction func) {
|
2020-12-19 23:30:50 +00:00
|
|
|
AtomicBoolean found = new AtomicBoolean(false);
|
|
|
|
bindings.forEach((key, binding) -> {
|
|
|
|
if (binding.equals(func)) {
|
|
|
|
found.set(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return found.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets binding.
|
|
|
|
*
|
|
|
|
* @param bind the bind
|
|
|
|
* @param func the func
|
|
|
|
*/
|
2020-12-23 00:51:20 +00:00
|
|
|
public void setBinding(KeyBinding bind, ModuleFunction func) {
|
2020-12-19 23:30:50 +00:00
|
|
|
AtomicReference<KeyBinding> match = new AtomicReference<>();
|
|
|
|
if (bindings.containsValue(func)) {
|
|
|
|
bindings.forEach((key, binding) -> {
|
|
|
|
if (binding.equals(func)) {
|
2020-12-22 19:32:27 +00:00
|
|
|
BindingManager.removeBind(key);
|
2020-12-19 23:30:50 +00:00
|
|
|
match.set(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match.get() != null) {
|
|
|
|
bindings.remove(match.get());
|
|
|
|
}
|
|
|
|
|
2020-12-23 00:51:20 +00:00
|
|
|
if (ModInfoProvider.isActive(func.getParent().getModuleName())) {
|
|
|
|
BindingManager.addBind(bind, func);
|
|
|
|
func.getParent().setBinding(bind, func);
|
2020-12-19 23:30:50 +00:00
|
|
|
bindings.put(bind, func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert binding.
|
|
|
|
*
|
|
|
|
* @param info the info
|
|
|
|
*/
|
|
|
|
private void convertBinding(BindingInfo info) {
|
2020-12-22 19:32:27 +00:00
|
|
|
Optional<ModuleBase> match = ModInfoProvider.getMod(info.attachedModuleName);
|
2020-12-23 00:51:20 +00:00
|
|
|
match.ifPresent(moduleBase -> {
|
|
|
|
List<ModuleFunction> foundBinds = moduleBase.matchBinding(info.attachedFunctionName);
|
|
|
|
if (foundBinds.size() > 0) {
|
|
|
|
setBinding(reconstructBinding(info), foundBinds.get(0));
|
|
|
|
}
|
|
|
|
});
|
2020-12-19 23:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconstruct binding key binding.
|
|
|
|
*
|
|
|
|
* @param info the info
|
|
|
|
* @return the key binding
|
|
|
|
*/
|
|
|
|
public static KeyBinding reconstructBinding(BindingInfo info) {
|
|
|
|
return new KeyBinding(
|
|
|
|
info.translationKey,
|
|
|
|
info.type,
|
|
|
|
info.key,
|
|
|
|
info.category
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract binding info.
|
|
|
|
*
|
|
|
|
* @param b the b
|
|
|
|
* @param f the f
|
|
|
|
* @return the binding info
|
|
|
|
*/
|
2020-12-23 00:51:20 +00:00
|
|
|
public static BindingInfo extractBinding(KeyBinding b, ModuleFunction f) {
|
2020-12-19 23:30:50 +00:00
|
|
|
BindingInfo res = new BindingInfo();
|
2020-12-23 00:51:20 +00:00
|
|
|
res.attachedModuleName = f.getParent().getModuleName();
|
|
|
|
res.attachedFunctionName = f.getFunctionName();
|
2020-12-19 23:30:50 +00:00
|
|
|
|
|
|
|
res.translationKey = b.getTranslationKey();
|
|
|
|
InputUtil.Key k = b.getDefaultKey();
|
|
|
|
res.type = k.getCategory();
|
|
|
|
res.key = k.getCode();
|
|
|
|
res.category = b.getCategory();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize bindings list.
|
|
|
|
*
|
|
|
|
* @return the list
|
|
|
|
*/
|
|
|
|
public List<BindingInfo> serializeBindings() {
|
|
|
|
List<BindingInfo> b = new ArrayList<>();
|
|
|
|
bindings.forEach((k, f) -> b.add(extractBinding(k, f)));
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize bindings.
|
|
|
|
*
|
|
|
|
* @param info the info
|
|
|
|
*/
|
|
|
|
public void deserializeBindings(List<BindingInfo> info) {
|
|
|
|
info.forEach(this::convertBinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize module configuration list.
|
|
|
|
*
|
|
|
|
* @param module the module
|
|
|
|
* @return the list
|
|
|
|
*/
|
|
|
|
public static Map<String, OptionSerializiable> serializeModuleConfiguration(ModuleBase module) {
|
|
|
|
Map<String, OptionSerializiable> opts = new HashMap<>();
|
|
|
|
Map<String, ConfigurationOption> configuration = module.getModuleConfiguration();
|
2020-12-22 21:19:02 +00:00
|
|
|
configuration.forEach((key, value) -> opts.put(key, new OptionSerializiable(key, value.toJson())));
|
2020-12-19 23:30:50 +00:00
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize module configuration.
|
|
|
|
*
|
|
|
|
* @param opts the opts
|
|
|
|
* @param module the module
|
|
|
|
*/
|
|
|
|
public void deserializeModuleConfiguration(List<OptionSerializiable> opts, ModuleBase module) {
|
|
|
|
opts.forEach(option -> {
|
|
|
|
if (module.hasOption(option.key)) {
|
|
|
|
module.setConfigOption(option.key, option.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|