Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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. :)

  • Author

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. :)

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.