Jump to content

Triggering code once world has loaded/OnLoad event?


GeoffNukem

Recommended Posts

I was wondering if there was an OnLoad event/method anywhere for when the player loads a new/saved world. For example, once the user enters a world, an 'onLoad' method or similar is called. I've been looking around the files and had no luck. So I thought I'd better ask here, just in case I've missed it.

 

If not, is there any way of adding one myself or at least a work around? I have a feeling it'll need one of the orginal classes to do so. I'd rather not edit any orginal classes, as that'll obviously decrease the chances of mod compatibility with other mods.

 

I did try the Load event class inside the World class, but had no luck with that. I also tried the IPlayerTracker Interface and tried that with little luck. Unless i'm missing something or doing something wrong with them.

 

The reason I ask is I want to trigger some code once a world has been loaded or when the player has entered the world. Just so I could do some GUI changes and perhaps a welcome message and things once the game starts, but also avoid editing orginal classes as much as possible. Maybe there is another way of doing this?

 

Thanks in advance. :)

Link to comment
Share on other sites

Okay. After a spark of insipration (and a night sleep), I think I've done it. The idea of it was to use a tick method and basically have a boolean flag indicating if a world has be loaded.

 

How I did was I made myself a TickHandler class using the example on the wiki. I then just made the boolean variable and checked it along with checking the MC.theWorld whether it is null.

 

The code for what I've done is below if anyone is curious. The key methods are the OnTickInGame() and the OnTickInGUI(). I've also added an Unload method while I was at it.

 

public class MyTickHandler implements ITickHandler {

boolean isLoaded = false;

@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) { }

@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
	GuiScreen guiscreen = Minecraft.getMinecraft().currentScreen;
        	if (guiscreen != null) {
                	onTickInGUI(guiscreen);
        } else {
        	        onTickInGame();
        }	
}

@Override
public EnumSet<TickType> ticks() {
	EnumSet<TickType> wantedTicks = EnumSet.of(TickType.CLIENT);
        	wantedTicks.add(TickType.CLIENTGUI);
        return wantedTicks;
}

@Override
public String getLabel() {
	return null;
}

public void onTickInGame() {

	if(!isLoaded && FMLClientHandler.instance().getClient().theWorld != null)
	{
		isLoaded = true;
		onLoad();
		System.out.println("World loaded: " + isLoaded);
	}

}

public void onTickInGUI(GuiScreen guiscreen) {

	if (isLoaded && FMLClientHandler.instance().getClient().theWorld == null)
	{
		isLoaded = false;
		onUnload();
		System.out.println("World loaded: " + isLoaded);
	}
}
public void onLoad() { }

public void onUnload() { }

}

 

The code might need some tweaking, so if anyone has any ideas then please say so. Also does anyone mind if I add this to the wiki? If so then where do you want it?

 

Hope this helps for others. :)

Link to comment
Share on other sites

I don't see any issues, but then again I'm basically doing the same thing  :)

 

 

That reminds me, I should update that code with my findings.

 

This can be cleaned up:

  @Override
  public EnumSet<TickType> ticks() {
    EnumSet<TickType> wantedTicks = EnumSet.of(TickType.CLIENT);
    wantedTicks.add(TickType.CLIENTGUI);
    return wantedTicks;
  }

 

To look like this:

  @Override
  public EnumSet<TickType> ticks() {
    return EnumSet.of(TickType.CLIENT, TickType.CLIENTGUI);
  }

I have no idea what I was thinking before.

 

I've also tried every TickType and only Render, Client, & Player seemed to work on the client side.

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.