[SOLVED]Stopping gamemode from being reset on changing worlds - how?

Discussion in 'Plugin Development' started by phondeux, Nov 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    phondeux

    It's really annoying for our ops to change world and have to set their gamemode all over again. I thought I'd write a little plugin to grab the teleport and join events and have all ops be automatically set to creative.

    Code:
        public void onPlayerJoin(PlayerJoinEvent event) {
            if (event.getPlayer().isOp()) {
                event.getPlayer().setGameMode(GameMode.CREATIVE);
            }
        }
    
        public void onPlayerTeleport(PlayerTeleportEvent event) {
            if (event.getPlayer().isOp()) {
                event.getPlayer().setGameMode(GameMode.CREATIVE);
            }
        }
    It doesn't work. What happens is the player logs in or changes worlds, my code above executes and sets their mode ... and then the server switches their mode back to survival. What am I missing?
     
  2. Offline

    coldandtired

    Isn't it easier to set the whole server to Creative?
     
  3. Offline

    phondeux

    No, because it's a PvP server. I just want the admins to be in creative to do whatever they want.
     
  4. Offline

    Rednax_

    Is it possible the reset takes place in an event with a higher priority?
    You could test this by giving your plugin the hisgtest priority.
    Somewhere in the stickies this is explained better.
     
  5. Offline

    scranner

    made a slight fork to your code to stop some frustration
    Code:
    public void onPlayerJoin(PlayerJoinEvent event)
    {
        if (event.getPlayer().isOp())
        {
            if(event.getPlayer().getGameMode() == GameMode.CREATIVE)
            {
            event.getPlayer().setGameMode(GameMode.CREATIVE);
            }
            else if(event.getPlayer().getGameMode() == GameMode.SURVIVAL)
            {
                event.getPlayer().setGameMode(GameMode.SURVIVAL);
            }
        }
    }
    
    public void onPlayerTeleport(PlayerTeleportEvent event)
    {
        if (event.getPlayer().isOp())
        {
            if(event.getPlayer().getGameMode() == GameMode.CREATIVE)
            {
            event.getPlayer().setGameMode(GameMode.CREATIVE);
            }
            else if(event.getPlayer().getGameMode() == GameMode.SURVIVAL)
            {
                event.getPlayer().setGameMode(GameMode.SURVIVAL);
            }
        }
    }
     
  6. Offline

    Baummann

    Maybe add a delay?
    Code:
    Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
            public void run() {
                    event.getPlayer().setGameMode(GameMode.CREATIVE);
            }
    }, 20l);
     
  7. Offline

    Sleaker

    setting the gamemode before the event resolves doesn't do anything - that's why it's not working. As Baummann suggested - you'll need to add a scheduled task to set the gamemode after the event finishes - his example shows 20 ticks, 0 or 1 will work also as the task wont fire until the event has finished processing anyhow.
     
  8. Offline

    phondeux

    The delay worked!

    So ... I just noticed a new problem - when a player takes a nether portal they get reset again. :( What event is governing this? Is that PlayerChangedWorldEvent?
     
  9. I think it was onPlayerPortal or something similar

    EDIT: Yes its PLAYER_PORTAL event : JavaDocs
     
  10. Offline

    phondeux

    Thanks, I just farted around with my code and that was indeed it!
     
Thread Status:
Not open for further replies.

Share This Page