Jump to content

[SOLVED]How to access player's onUpdate()


bltpyro

Recommended Posts

I want to run some code while the player is sleeping. I had tried using the PlayerSleepInBedEvent, but that seems to only be useful for before a player goes to sleep. Searching around I noticed the onUpdate() increments the sleep timer and at the beginning of the function there is this:

FMLCommonHandler.instance().onPlayerPreTick(this);

I have looked through the FMLCommonHandler and tried serching online to figure out how use this but can't seem to figure it out. Does anyone know how to use these?

 

Thanks

Link to comment
Share on other sites

OK I got it figured out. I never used the tickHandlers before, but thankfully got it working. First I needed to register the handler in my CommonProxy since I need my code serverside

public void registerServerTickHandler()
{
TickRegistry.registerTickHandler(new ServerTickHandler(), Side.SERVER);
}

 

Then I had to set up the handler. I make sure that the tick types I want are the Player ticks for the server, which have the player as the object. Using the player I could access the sleepTimer and do stuff when I wanted to based on the timer.

public class ServerTickHandler implements ITickHandler
{
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData)
{
if (type.equals(EnumSet.of(TickType.PLAYER)))
     {
         onPlayerTick((EntityPlayer)tickData[0]);
     }
}
private void onPlayerTick(EntityPlayer player)
{
int sleepTicks = player.getSleepTimer();
if(sleepTicks == 99)
{
System.out.println("about to fall asleep");
//dostuff
}

}
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
// TODO Auto-generated method stub
}
@Override
public EnumSet<TickType> ticks()
{
     return EnumSet.of(TickType.PLAYER, TickType.SERVER);
}
@Override
public String getLabel() {
// TODO Auto-generated method stub
return null;
}
}

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.