Jump to content

Calling function on game start OUTSIDE the main mod class


Dawars

Recommended Posts

But here is how I would attempt to go about it:

import net.minecraftforge.event.ForgeSubscribe;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

public class EventHookContainerClass {

@ForgeSubscribe
public void FunctionNameHere(FMLPreInitializationEvent event) {
	//What you want to do here.               
}

}

 

And then in your mods Main class add:

	@Init
public void load(FMLInitializationEvent event) {
	MinecraftForge.EVENT_BUS.register(new EventHookContainerClass());

}

Link to comment
Share on other sites

Hello,

I'd like to call a function from outside the main mod class when for example the game starts.

It'd be in the main mod class like this:

@PreInit
public static void preInit(FMLPreInitializationEvent event){
function();
}

 

I hope I explained well if not please tell me!

 

Thank you in advance,

Dawars

 

You mean like calling a function from an other class in your main mod file?

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Yes I meant class.

 

I tried it and it crashed:

package.package.FunctionNameHere(cpw.mods.fml.common.event.FMLInitializationEvent) has @ForgeSubscribe annotation, but takes a argument that is not a Event class cpw.mods.fml.common.event.FMLInitializationEvent

 

What I'd like to do is a program that modders can put into their mods and register in their mod file maximum once.

It'd send data to my website and they could see theirs stats there...

I don't want it to be to complicated for them.

Link to comment
Share on other sites

2013-05-20 10:09:04 [sEVERE] [ForgeModLoader] Fatal errors were detected during the transition from PREINITIALIZATION to INITIALIZATION. Loading cannot continue
2013-05-20 10:09:04 [sEVERE] [ForgeModLoader] 
mcp [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized
FML [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized
Forge [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized
Suggestions [suggestions mod] (bin) Unloaded->Constructed->Errored
2013-05-20 10:09:04 [sEVERE] [ForgeModLoader] The following problems were captured during this phase
2013-05-20 10:09:04 [sEVERE] [ForgeModLoader] Caught exception from Suggestions
java.lang.IllegalArgumentException: Method public void mods.suggestions.webstats.WebStats.FunctionNameHere(cpw.mods.fml.common.event.FMLInitializationEvent) has @ForgeSubscribe annotation, but takes a argument that is not a Event class cpw.mods.fml.common.event.FMLInitializationEvent
at net.minecraftforge.event.EventBus.register(EventBus.java:50)
at mods.suggestions.Suggestions.preInit(Suggestions.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:515)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)
at com.google.common.eventbus.EventBus.post(EventBus.java:267)
at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:165)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)
at com.google.common.eventbus.EventBus.post(EventBus.java:267)
at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:98)
at cpw.mods.fml.common.Loader.loadMods(Loader.java:514)
at cpw.mods.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:160)
at net.minecraft.client.Minecraft.startGame(Minecraft.java:407)
at net.minecraft.client.MinecraftAppletImpl.startGame(MinecraftAppletImpl.java:44)
at net.minecraft.client.Minecraft.run(Minecraft.java:729)
at java.lang.Thread.run(Unknown Source)

Link to comment
Share on other sites

FML's events are not part of Forge's event bus as Forge's event bus is highly specialized for efficiency.

Your best bet is just do do things normaly.

Have a 'bouncer' method in your main class that just has a 'OtherClass.onInit(event)'

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.