Created
March 27, 2016 23:43
-
-
Save kinggoesgaming/4900bfefecc9c01a88ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinggoesgaming.foundations.api.configuration.abstraction.concrete; | |
import com.kinggoesgaming.foundations.api.configuration.Configuration; | |
import ninja.leaping.configurate.ConfigurationNode; | |
import ninja.leaping.configurate.loader.ConfigurationLoader; | |
import java.io.IOException; | |
import java.nio.file.Path; | |
/** | |
* Base template for all concrete configuration files. | |
* | |
* <p> | |
* This class narrows down the {@link Configuration} interface for use with configuration files that can be stored on disk. | |
* </p> | |
* | |
* @param <N> The {@link ConfigurationNode} type. | |
* @param <L> The {@link ConfigurationLoader} type. | |
*/ | |
public abstract class ConcreteConfiguration<N extends ConfigurationNode, L extends ConfigurationLoader<N>> implements Configuration<N, L> { | |
protected Path file; | |
protected N node; | |
protected L loader; | |
protected Class<? extends ConcreteConfiguration<N, L>> clazz; | |
protected String header = ""; | |
@Override | |
public final void save() throws IOException { | |
loader.save(node); | |
} | |
@Override | |
public final N get() { | |
return node; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinggoesgaming.foundations.api.configuration.abstraction.concrete; | |
import com.kinggoesgaming.foundations.api.configuration.annotation.ConcreteConfigAsset; | |
import com.kinggoesgaming.foundations.api.configuration.annotation.OptionalConfigAsset; | |
import ninja.leaping.configurate.ConfigurationOptions; | |
import ninja.leaping.configurate.commented.CommentedConfigurationNode; | |
import ninja.leaping.configurate.hocon.HoconConfigurationLoader; | |
import org.spongepowered.api.Sponge; | |
import org.spongepowered.api.asset.Asset; | |
import org.spongepowered.api.plugin.PluginContainer; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.Optional; | |
/** | |
* Base template for all concrete HOCON configuration files. | |
* | |
* <p> | |
* This class narrows down the {@link ConcreteConfiguration} for use with configuration files that can be stored on disk as HOCON. | |
* </p> | |
*/ | |
public abstract class ConcreteHoconConfiguration extends ConcreteConfiguration<CommentedConfigurationNode, HoconConfigurationLoader> { | |
@Override | |
public void load() throws IOException { | |
if (!Files.exists(file)) { | |
create(); | |
save(); | |
return; | |
} | |
upgrade(); | |
save(); | |
} | |
private void create() throws IOException { | |
Files.createFile(file); | |
upgrade(); | |
if (clazz.isAnnotationPresent(OptionalConfigAsset.class)) { | |
OptionalConfigAsset optionalConfigAsset = clazz.getAnnotation(OptionalConfigAsset.class); | |
Optional<PluginContainer> pluginContainer = Sponge.getPluginManager().getPlugin(optionalConfigAsset.plugin()); | |
if (pluginContainer.isPresent()) { | |
Optional<Asset> optionalAsset; | |
HoconConfigurationLoader tempLoader; | |
for (String asset : optionalConfigAsset.assets()) { | |
optionalAsset = pluginContainer.get().getAsset(asset); | |
if (optionalAsset.isPresent()) { | |
tempLoader = HoconConfigurationLoader.builder().setURL(optionalAsset.get().getUrl()).build(); | |
node.mergeValuesFrom(tempLoader.load()); | |
} | |
} | |
} | |
} | |
} | |
private void upgrade() throws IOException { | |
loader = HoconConfigurationLoader.builder().setPath(file).build(); | |
node = loader.load(ConfigurationOptions.defaults().setHeader(header)); | |
if (clazz.isAnnotationPresent(OptionalConfigAsset.class)) { | |
ConcreteConfigAsset optionalConfigAsset = clazz.getAnnotation(ConcreteConfigAsset.class); | |
Optional<PluginContainer> pluginContainer = Sponge.getPluginManager().getPlugin(optionalConfigAsset.plugin()); | |
if (pluginContainer.isPresent()) { | |
Optional<Asset> optionalAsset; | |
HoconConfigurationLoader tempLoader; | |
for (String asset : optionalConfigAsset.assets()) { | |
optionalAsset = Sponge.getAssetManager().getAsset(asset); | |
if (optionalAsset.isPresent()) { | |
tempLoader = HoconConfigurationLoader.builder().setURL(optionalAsset.get().getUrl()).build(); | |
node.mergeValuesFrom(tempLoader.load()); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kinggoesgaming.foundations.core.configs; | |
import com.kinggoesgaming.foundations.api.configuration.abstraction.concrete.ConcreteHoconConfiguration; | |
import com.kinggoesgaming.foundations.api.configuration.annotation.ConcreteConfigAsset; | |
import java.nio.file.Paths; | |
import static com.kinggoesgaming.foundations.core.FoundationsCore.getHeaders; | |
import static com.kinggoesgaming.foundations.core.FoundationsCore.getTargets; | |
/** | |
* | |
*/ | |
@ConcreteConfigAsset( | |
plugin = "com.kingoesgaming.foundations.core", | |
assets = "config/concrete/global.conf" | |
) | |
public class GlobalConfig extends ConcreteHoconConfiguration { | |
public GlobalConfig() { | |
file = Paths.get(getTargets().getNode("configs", "global").getString()); | |
header = getHeaders().getNode("configs", "global").getString(); | |
clazz = getClass(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[16:24:23] [Server thread/ERROR] [Sponge]: Could not pass FMLInitializationEvent to org.spongepowered.mod.plugin.SpongeModPluginContainer@26e8ac89 | |
java.lang.NullPointerException | |
at com.kinggoesgaming.foundations.api.configuration.abstraction.concrete.ConcreteHoconConfiguration.load(ConcreteHoconConfiguration.java:51) ~[ConcreteHoconConfiguration.class:0.1.0] | |
at com.kinggoesgaming.foundations.core.FoundationsCore.onInitialization(FoundationsCore.java:96) ~[FoundationsCore.class:?] | |
at org.spongepowered.common.event.listener.GameInitializationEventListener_FoundationsCore_onInitialization1.handle(Unknown Source) ~[?:?] | |
at org.spongepowered.common.event.RegisteredListener.handle(RegisteredListener.java:86) ~[RegisteredListener.class:1.8.9-1763-4.1.0-BETA-1212] | |
at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:239) [SpongeModEventManager.class:1.8.9-1763-4.1.0-BETA-1212] | |
at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:283) [SpongeModEventManager.class:1.8.9-1763-4.1.0-BETA-1212] | |
at org.spongepowered.mod.SpongeMod.onStateEvent(SpongeMod.java:183) [SpongeMod.class:1.8.9-1763-4.1.0-BETA-1212] | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_74] | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_74] | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_74] | |
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_74] | |
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.EventBus.post(EventBus.java:275) [minecraft_server.1.8.9.jar:?] | |
at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:211) [LoadController.class:?] | |
at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:189) [LoadController.class:?] | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_74] | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_74] | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_74] | |
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_74] | |
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) [minecraft_server.1.8.9.jar:?] | |
at com.google.common.eventbus.EventBus.post(EventBus.java:275) [minecraft_server.1.8.9.jar:?] | |
at net.minecraftforge.fml.common.LoadController.redirect$onPost$0(LoadController.java:53) [LoadController.class:?] | |
at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:118) [LoadController.class:?] | |
at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:737) [Loader.class:?] | |
at net.minecraftforge.fml.server.FMLServerHandler.finishServerLoading(FMLServerHandler.java:97) [FMLServerHandler.class:?] | |
at net.minecraftforge.fml.common.FMLCommonHandler.onServerStarted(FMLCommonHandler.java:325) [FMLCommonHandler.class:?] | |
at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:205) [ko.class:?] | |
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:441) [MinecraftServer.class:?] | |
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_74] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment