move static info out of mod file

This commit is contained in:
jane 2020-12-22 14:32:27 -05:00
parent 3db9cf6ccd
commit 2600f92b2a
10 changed files with 146 additions and 131 deletions

View file

@ -1,20 +1,16 @@
package pm.j4.kerosene; package pm.j4.kerosene;
import java.util.ArrayList;
import java.util.List;
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;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer; import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding; import net.minecraft.client.options.KeyBinding;
import net.minecraft.server.integrated.IntegratedServer;
import pm.j4.kerosene.modules.ExampleModule; import pm.j4.kerosene.modules.ExampleModule;
import pm.j4.kerosene.modules.bindings.BindingManager; 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.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase; import pm.j4.kerosene.util.module.ModuleBase;
@ -23,107 +19,6 @@ import pm.j4.kerosene.util.module.ModuleBase;
* The type Kerosene mod. * The type Kerosene mod.
*/ */
public class KeroseneMod implements ModInitializer { public class KeroseneMod implements ModInitializer {
/**
* The Mod data.
*/
public static ModMetadata modData = null;
/**
* The constant client.
*/
private static MinecraftClient client;
/**
* The constant activeMods.
*/
private static List<ModuleBase> registeredMods = new ArrayList<>();
public static void registerMod(Class<? extends ModuleBase> mod) throws IllegalAccessException, InstantiationException {
ModuleBase base = mod.newInstance();
if(!registeredMods.contains(base)) {
registeredMods.add(base);
}
}
/**
* Is active boolean.
*
* @param modName the mod name
* @return the boolean
*/
public static boolean isActive(String modName) {
return registeredMods.stream().anyMatch(mod -> mod.getModuleName().equals(modName));
}
/**
* Gets mod.
*
* @param modName the mod name
* @return the mod
*/
public static Optional<ModuleBase> getMod(String modName) {
return registeredMods.stream().filter(mod -> mod.getModuleName().equals(modName)).findFirst();
}
/**
* Gets active mods.
*
* @return the active mods
*/
public static List<ModuleBase> getRegisteredMods() {
return registeredMods;
}
/**
* The constant registeredBinds.
*/
private static final List<KeyBinding> registeredBinds = new ArrayList<>();
/**
* Add bind.
*
* @param b the b
*/
public static void addBind(KeyBinding b) {
registeredBinds.add(b);
}
/**
* Remove bind.
*
* @param b the b
*/
public static void removeBind(KeyBinding b) {
if(registeredBinds.contains(b)) {
registeredBinds.remove(b);
}
}
/**
* Gets active keybinds.
*
* @return the active keybinds
*/
public static List<KeyBinding> getActiveKeybinds() {
return registeredBinds;
}
/**
* Gets server address.
*
* @return the server address
*/
public static String getServerAddress() {
if (client != null && client.getServer() != null) {
IntegratedServer server = client.getServer();
if (!server.isRemote()) {
return "localhost";
}
if (server.isRemote() && !server.getServerIp().isEmpty()) {
return server.getServerIp();
}
}
return null;
}
@Override @Override
public void onInitialize() { public void onInitialize() {
@ -131,13 +26,13 @@ public class KeroseneMod implements ModInitializer {
// always update mod data // always update mod data
Optional<ModContainer> modContainer = FabricLoader.getInstance().getModContainer("kerosene"); Optional<ModContainer> modContainer = FabricLoader.getInstance().getModContainer("kerosene");
modContainer.ifPresent(container -> modData = container.getMetadata()); modContainer.ifPresent(container -> ModInfoProvider.modData = container.getMetadata());
Optional<ConfigHolder> conf = ConfigManager.getConfig("kerosene"); Optional<ConfigHolder> conf = ConfigManager.getConfig("kerosene");
try { try {
this.registerMod(ExampleModule.class); ModInfoProvider.registerMod(ExampleModule.class);
this.registerMod(BindingManager.class); ModInfoProvider.registerMod(BindingManager.class);
} }
catch (Exception e) { catch (Exception e) {
System.out.println(e); System.out.println(e);
@ -145,14 +40,14 @@ public class KeroseneMod implements ModInitializer {
//initialize any keybinds, data, etc. //initialize any keybinds, data, etc.
registeredMods.forEach(ModuleBase::init); ModInfoProvider.getRegisteredMods().forEach(ModuleBase::init);
//initialize keybind handler //initialize keybind handler
conf.ifPresent(configHolder -> ClientTickEvents.END_CLIENT_TICK.register(client -> { conf.ifPresent(configHolder -> ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (KeroseneMod.client != client) { if (ModInfoProvider.client != client) {
KeroseneMod.client = client; ModInfoProvider.client = client;
} }
for (KeyBinding b : KeroseneMod.getActiveKeybinds()) { for (KeyBinding b : BindingManager.getActiveKeybinds()) {
while (b.wasPressed()) { while (b.wasPressed()) {
configHolder.globalConfig.bindings.get(b).activate(client); configHolder.globalConfig.bindings.get(b).activate(client);
} }

View file

@ -17,6 +17,7 @@ import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.*; import net.minecraft.text.*;
import pm.j4.kerosene.KeroseneMod; import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.util.config.ConfigManager; import pm.j4.kerosene.util.config.ConfigManager;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase; import pm.j4.kerosene.util.module.ModuleBase;
/** /**
@ -99,7 +100,7 @@ public class POptionsScreen extends Screen {
this.configPane.setLeftPos(paneWidth); this.configPane.setLeftPos(paneWidth);
this.children.add(this.configPane); this.children.add(this.configPane);
List<ModuleBase> configurableModules = new ArrayList<>(); List<ModuleBase> configurableModules = new ArrayList<>();
configurableModules.addAll(KeroseneMod.getRegisteredMods() configurableModules.addAll(ModInfoProvider.getRegisteredMods()
.stream().filter(ModuleBase::configurable) .stream().filter(ModuleBase::configurable)
.collect(Collectors.toList())); .collect(Collectors.toList()));
configurableModules.forEach(module -> this.modules.addEntry(new POptionEntry(module, this.modules))); configurableModules.forEach(module -> this.modules.addEntry(new POptionEntry(module, this.modules)));

View file

@ -11,6 +11,7 @@ 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.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.option.KeybindOption; import pm.j4.kerosene.util.module.option.KeybindOption;
@ -72,7 +73,7 @@ public class BindingManager extends ModuleBase {
return; return;
} }
GlobalConfig c = config.get().globalConfig; GlobalConfig c = config.get().globalConfig;
Optional<ModuleBase> mod = KeroseneMod.getMod("petroleum.modmenu"); Optional<ModuleBase> mod = ModInfoProvider.getMod("petroleum.modmenu");
if (mod.isPresent() && !c.isBound(mod.get())) { if (mod.isPresent() && !c.isBound(mod.get())) {
//TODO //TODO
// the only explicit keybinding (for now.) // the only explicit keybinding (for now.)
@ -87,4 +88,38 @@ public class BindingManager extends ModuleBase {
config.get().globalConfig.setBinding(binding, mod.get()); config.get().globalConfig.setBinding(binding, mod.get());
} }
} }
/**
* The constant registeredBinds.
*/
private static final List<KeyBinding> registeredBinds = new ArrayList<>();
/**
* Add bind.
*
* @param b the b
*/
public static void addBind(KeyBinding b) {
registeredBinds.add(b);
}
/**
* Remove bind.
*
* @param b the b
*/
public static void removeBind(KeyBinding b) {
if(registeredBinds.contains(b)) {
registeredBinds.remove(b);
}
}
/**
* Gets active keybinds.
*
* @return the active keybinds
*/
public static List<KeyBinding> getActiveKeybinds() {
return registeredBinds;
}
} }

View file

@ -3,6 +3,7 @@ package pm.j4.kerosene.util.config;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import pm.j4.kerosene.KeroseneMod; import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase; import pm.j4.kerosene.util.module.ModuleBase;
/** /**
@ -31,8 +32,8 @@ public abstract class Config {
* @param mod the mod * @param mod the mod
*/ */
public void disableModule(String mod) { public void disableModule(String mod) {
if (isEnabled(mod) && KeroseneMod.isActive(mod) && KeroseneMod.getMod(mod).isPresent()) { if (isEnabled(mod) && ModInfoProvider.isActive(mod) && ModInfoProvider.getMod(mod).isPresent()) {
ModuleBase moduleInfo = KeroseneMod.getMod(mod).get(); ModuleBase moduleInfo = ModInfoProvider.getMod(mod).get();
if (moduleInfo.isActivatable()) { if (moduleInfo.isActivatable()) {
enabledModules.remove(mod); enabledModules.remove(mod);
} }
@ -45,8 +46,8 @@ public abstract class Config {
* @param mod the mod * @param mod the mod
*/ */
public void toggleModule(String mod) { public void toggleModule(String mod) {
if (KeroseneMod.isActive(mod) && KeroseneMod.getMod(mod).isPresent()) { if (ModInfoProvider.isActive(mod) && ModInfoProvider.getMod(mod).isPresent()) {
ModuleBase moduleInfo = KeroseneMod.getMod(mod).get(); ModuleBase moduleInfo = ModInfoProvider.getMod(mod).get();
if (moduleInfo.isActivatable()) { if (moduleInfo.isActivatable()) {
if (isEnabled(mod)) { if (isEnabled(mod)) {
enabledModules.remove(mod); enabledModules.remove(mod);

View file

@ -4,6 +4,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import pm.j4.kerosene.KeroseneMod; import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.util.data.ModInfoProvider;
import pm.j4.kerosene.util.module.ModuleBase; import pm.j4.kerosene.util.module.ModuleBase;
/** /**
@ -27,7 +28,7 @@ public class ConfigHolder {
*/ */
public boolean isModuleEnabled(String module) { public boolean isModuleEnabled(String module) {
if (!KeroseneMod.isActive(module)) { if (!ModInfoProvider.isActive(module)) {
return false; return false;
} }
if (globalConfig.isEnabled(module)) { if (globalConfig.isEnabled(module)) {
@ -46,7 +47,7 @@ public class ConfigHolder {
* @return the enabled modules * @return the enabled modules
*/ */
public List<ModuleBase> getEnabledModules() { public List<ModuleBase> getEnabledModules() {
List<ModuleBase> modules = KeroseneMod.getRegisteredMods(); List<ModuleBase> modules = ModInfoProvider.getRegisteredMods();
return modules.stream().filter(module -> return modules.stream().filter(module ->
isModuleEnabled(module.getModuleName()) isModuleEnabled(module.getModuleName())
).collect(Collectors.toList()); ).collect(Collectors.toList());
@ -58,7 +59,7 @@ public class ConfigHolder {
* @return the server * @return the server
*/ */
public String getServer() { public String getServer() {
return KeroseneMod.getServerAddress(); return ModInfoProvider.getServerAddress();
} }
/** /**

View file

@ -8,6 +8,7 @@ import java.util.*;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import pm.j4.kerosene.KeroseneMod; import pm.j4.kerosene.KeroseneMod;
import pm.j4.kerosene.modules.bindings.BindingInfo; import pm.j4.kerosene.modules.bindings.BindingInfo;
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;
import pm.j4.kerosene.util.module.ModuleBase; import pm.j4.kerosene.util.module.ModuleBase;
@ -63,7 +64,7 @@ public class ConfigManager {
* Init modules. * Init modules.
*/ */
public static void initModules(String moduleName) { public static void initModules(String moduleName) {
KeroseneMod.getRegisteredMods().forEach(module -> { ModInfoProvider.getRegisteredMods().forEach(module -> {
ModuleConfig options = load(moduleName + "/modules/", module.getModuleName() + ".json", ModuleConfig.class); ModuleConfig options = load(moduleName + "/modules/", module.getModuleName() + ".json", ModuleConfig.class);
if (options != null && options.options != null) { if (options != null && options.options != null) {
options.options.forEach((key, option) -> { options.options.forEach((key, option) -> {
@ -158,7 +159,7 @@ public class ConfigManager {
* Save all modules. * Save all modules.
*/ */
public static void saveAllModules() { public static void saveAllModules() {
List<ModuleBase> mods = KeroseneMod.getRegisteredMods(); List<ModuleBase> mods = ModInfoProvider.getRegisteredMods();
mods.forEach((module) -> { mods.forEach((module) -> {
ConfigManager.saveModule(module.getParent(), module); ConfigManager.saveModule(module.getParent(), module);
}); });
@ -229,7 +230,7 @@ class SerializationHelper {
} else { } else {
options = new HashMap<>(); options = new HashMap<>();
} }
KeroseneMod.getRegisteredMods().forEach(module -> { ModInfoProvider.getRegisteredMods().forEach(module -> {
if (options.containsKey(module.getModuleName())) { if (options.containsKey(module.getModuleName())) {
cfg.deserializeModuleConfiguration(options.get(module.getModuleName()), module); cfg.deserializeModuleConfiguration(options.get(module.getModuleName()), module);
} }

View file

@ -10,6 +10,5 @@ public class DefaultConfig extends GlobalConfig {
* Instantiates a new Default config. * Instantiates a new Default config.
*/ */
public DefaultConfig() { public DefaultConfig() {
this.enabledModules = Collections.singletonList("petroleum.splashtext");
} }
} }

View file

@ -7,6 +7,8 @@ import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil; import net.minecraft.client.util.InputUtil;
import pm.j4.kerosene.KeroseneMod; import pm.j4.kerosene.KeroseneMod;
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.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.option.ConfigurationOption; import pm.j4.kerosene.util.module.option.ConfigurationOption;
@ -47,7 +49,7 @@ public class GlobalConfig extends Config {
if (bindings.containsValue(func)) { if (bindings.containsValue(func)) {
bindings.forEach((key, binding) -> { bindings.forEach((key, binding) -> {
if (binding.equals(func)) { if (binding.equals(func)) {
KeroseneMod.removeBind(key); BindingManager.removeBind(key);
match.set(key); match.set(key);
} }
}); });
@ -57,8 +59,8 @@ public class GlobalConfig extends Config {
bindings.remove(match.get()); bindings.remove(match.get());
} }
if (KeroseneMod.isActive(func.getModuleName())) { if (ModInfoProvider.isActive(func.getModuleName())) {
KeroseneMod.addBind(bind); BindingManager.addBind(bind);
bindings.put(bind, func); bindings.put(bind, func);
} }
} }
@ -69,7 +71,7 @@ public class GlobalConfig extends Config {
* @param info the info * @param info the info
*/ */
private void convertBinding(BindingInfo info) { private void convertBinding(BindingInfo info) {
Optional<ModuleBase> match = KeroseneMod.getMod(info.attachedModuleName); Optional<ModuleBase> match = ModInfoProvider.getMod(info.attachedModuleName);
match.ifPresent(moduleBase -> setBinding(reconstructBinding(info), match.ifPresent(moduleBase -> setBinding(reconstructBinding(info),
moduleBase)); moduleBase));
} }

View file

@ -17,7 +17,7 @@ public class Category {
* @return the category map * @return the category map
*/ */
public static Map<String, List<ModuleBase>> getCategoryMap() { public static Map<String, List<ModuleBase>> getCategoryMap() {
List<ModuleBase> modules = KeroseneMod.getRegisteredMods(); List<ModuleBase> modules = ModInfoProvider.getRegisteredMods();
Map<String, List<ModuleBase>> categoryMap = new HashMap<>(); Map<String, List<ModuleBase>> categoryMap = new HashMap<>();
modules.forEach(module -> { modules.forEach(module -> {
if (!categoryMap.containsKey(module.getCategory())) { if (!categoryMap.containsKey(module.getCategory())) {

View file

@ -0,0 +1,80 @@
package pm.j4.kerosene.util.data;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.server.integrated.IntegratedServer;
import pm.j4.kerosene.util.module.ModuleBase;
//TODO refactor into multiple data holders
public class ModInfoProvider {
/**
* The Mod data.
*/
public static ModMetadata modData = null;
/**
* The constant client.
*/
public static MinecraftClient client;
/**
* The constant activeMods.
*/
private static List<ModuleBase> registeredMods = new ArrayList<>();
public static void registerMod(Class<? extends ModuleBase> mod) throws IllegalAccessException, InstantiationException {
ModuleBase base = mod.newInstance();
if(!registeredMods.contains(base)) {
registeredMods.add(base);
}
}
/**
* Is active boolean.
*
* @param modName the mod name
* @return the boolean
*/
public static boolean isActive(String modName) {
return registeredMods.stream().anyMatch(mod -> mod.getModuleName().equals(modName));
}
/**
* Gets mod.
*
* @param modName the mod name
* @return the mod
*/
public static Optional<ModuleBase> getMod(String modName) {
return registeredMods.stream().filter(mod -> mod.getModuleName().equals(modName)).findFirst();
}
/**
* Gets active mods.
*
* @return the active mods
*/
public static List<ModuleBase> getRegisteredMods() {
return registeredMods;
}
/**
* Gets server address.
*
* @return the server address
*/
public static String getServerAddress() {
if (client != null && client.getServer() != null) {
IntegratedServer server = client.getServer();
if (!server.isRemote()) {
return "localhost";
}
if (server.isRemote() && !server.getServerIp().isEmpty()) {
return server.getServerIp();
}
}
return null;
}
}