Posted October 16, 201212 yr 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
October 17, 201212 yr Author 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; } }
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.