Uh, you’re not supposed to use the + operator on Paths. There is no guarantee that configDir + “/config.conf” resolves to anything sane. You should instead use private Path configFile = configDir.resolve(“config.conf”);
Now I’m getting an error about not being able to inject something though :S
Error injecting constructor, java.lang.NullPointerException
at com.patey07.NTCCore.NTCCore.(NTCCore.java:38)
at org.spongepowered.common.guice.SpongePluginGuiceModule.configure(SpongePluginGuiceModule.java:68)
while locating com.patey07.NTCCore.NTCCore
1 error
at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at org.spongepowered.mod.plugin.SpongeModPluginContainer.constructMod(SpongeModPluginContainer.java:288) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_141]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_141]
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[minecraft_server.1.10.2.jar:?]
at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:243) ~[LoadController.class:?]
at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:221) ~[LoadController.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_141]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_141]
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[minecraft_server.1.10.2.jar:?]
at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[minecraft_server.1.10.2.jar:?]
at net.minecraftforge.fml.common.LoadController.redirect$onPost$zzb000(LoadController.java:561) ~[LoadController.class:?]
at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:145) [LoadController.class:?]
at net.minecraftforge.fml.common.Loader.loadMods(Loader.java:549) [Loader.class:?]
at net.minecraftforge.fml.server.FMLServerHandler.beginServerLoading(FMLServerHandler.java:97) [FMLServerHandler.class:?]
at net.minecraftforge.fml.common.FMLCommonHandler.onServerStart(FMLCommonHandler.java:328) [FMLCommonHandler.class:?]
at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:121) [ld.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:431) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_141]
Caused by: java.lang.NullPointerException
at com.patey07.NTCCore.NTCCore.(NTCCore.java:48) ~[NTCCore.class:?]
at com.patey07.NTCCore.NTCCore$$FastClassByGuice$$1c96668d.newInstance() ~[$ReflectUtils.class:?]
at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:61) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:105) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:267) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:145) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.InjectorImpl$2$1.call(InjectorImpl.java:1016) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1012) ~[spongeforge-1.10.2-2281-5.2.0-BETA-2433.jar:1.10.2-2281-5.2.0-BETA-2433]
… 30 more
entire main class code:
@Plugin(id = “ntccore”, name = “NTC-Core”, version = “0.0.1”)
public class NTCCore {
private static NTCCore instance;
public static NTCCore getInstance() {
return instance;
}
private final Logger logger;
private final Game game;
public Logger getLogger() {
return this.logger;
}
@Inject
public NTCCore(Logger logger, Game game) {
instance = this;
this.logger = logger;
this.game = game;
}
@Inject
@ConfigDir(sharedRoot = false)
private Path configDir;
private Path configFile = configDir.resolve("config.conf");
private ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder().setPath(configFile).build();
private CommentedConfigurationNode configNode;
@Listener
public void onPreInit(GamePreInitializationEvent e){
configsetup();
}
public void configsetup(){
if(!Files.exists(configDir)){
try{
Files.createDirectories(configDir);
}catch(IOException io){
io.printStackTrace();
}
}
if(!Files.exists(configFile)){
try{
Files.createFile(configFile);
}catch(IOException e){
e.printStackTrace();
}
}else{
load();
}
}
//Loads config file if it already exists
public void load(){
try{
configNode = configLoader.load();
}catch(IOException e){
e.printStackTrace();
}
}
//And obviously saves config whenever needed.
public void save(){
try{
configLoader.save(configNode);
}catch(IOException e){
e.printStackTrace();
}
}
@Listener
public void onServerStart(GameStartedServerEvent event) {
getLogger().info("NTC-Core module initialized!");
}
@Listener
public void onInitialize(GameInitializationEvent event) {
RegisterEvents();
}
public void RegisterEvents() {
game.getEventManager().registerListeners(this, new PlayerJoin(this));
}
public Path getConfigDir(){
return configDir;
}
public CommentedConfigurationNode get(){
return configNode;
}
You’re not supposed to use injected fields in direct initializers for other fields. When configFile is initialized, configDir is still null because it hasn’t been injected yet. You could move configDir to another constructor parameter and initialize it in the constructor, or you could initialize it in onPreInit and leave configDir as an injected field. Your choice.
This is probably a stupid question, but how do I initialize it? when I move it to a constructor eclipse’s initialize suggestion is Path configDir = null;
I know I’m like a months late to this but I should’ve stated in my example that you shouldn’t use the + “/config.conf” unless you actually can guarantee that you have that file like in your resources folder or such. Thanks for clearing that up to him for me.
What? No. You’d use the AssetManager for resources like that. We’re talking about physical file locations, in the config/ directory. Completely unrelated to resources folders.