package pm.j4.kerosene.util.config; 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; import pm.j4.kerosene.KeroseneMod; import pm.j4.kerosene.modules.bindings.BindingInfo; import pm.j4.kerosene.util.data.OptionSerializiable; import pm.j4.kerosene.util.module.ModuleBase; import pm.j4.kerosene.util.module.option.ConfigurationOption; /** * The type Global config. */ public class GlobalConfig extends Config { /** * The Bindings. */ public final Map bindings = new HashMap<>(); /** * Is bound boolean. * * @param func the func * @return the boolean */ public boolean isBound(ModuleBase func) { 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 */ public void setBinding(KeyBinding bind, ModuleBase func) { AtomicReference match = new AtomicReference<>(); if (bindings.containsValue(func)) { bindings.forEach((key, binding) -> { if (binding.equals(func)) { KeroseneMod.removeBind(key); match.set(key); } }); } if (match.get() != null) { bindings.remove(match.get()); } if (KeroseneMod.isActive(func.getModuleName())) { KeroseneMod.addBind(bind); bindings.put(bind, func); } } /** * Convert binding. * * @param info the info */ private void convertBinding(BindingInfo info) { Optional match = KeroseneMod.getMod(info.attachedModuleName); match.ifPresent(moduleBase -> setBinding(reconstructBinding(info), moduleBase)); } /** * 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 */ public static BindingInfo extractBinding(KeyBinding b, ModuleBase f) { BindingInfo res = new BindingInfo(); res.attachedModuleName = f.getModuleName(); 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 serializeBindings() { List b = new ArrayList<>(); bindings.forEach((k, f) -> b.add(extractBinding(k, f))); return b; } /** * Deserialize bindings. * * @param info the info */ public void deserializeBindings(List info) { info.forEach(this::convertBinding); } /** * Serialize module configuration list. * * @param module the module * @return the list */ public static Map serializeModuleConfiguration(ModuleBase module) { Map opts = new HashMap<>(); Map configuration = module.getModuleConfiguration(); configuration.forEach((key, value) -> { opts.put(key, new OptionSerializiable(key, value.toJson())); }); return opts; } /** * Deserialize module configuration. * * @param opts the opts * @param module the module */ public void deserializeModuleConfiguration(List opts, ModuleBase module) { opts.forEach(option -> { if (module.hasOption(option.key)) { module.setConfigOption(option.key, option.value); } }); } }