Can't set gamemode after teleporting

Discussion in 'Plugin Development' started by ScrouthTV, Mar 26, 2021.

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

    ScrouthTV

    Hello. I think I found a bug and would like some ideas on it:

    me.scrouthtv.main.Main.java (open)

    Code:
    package me.scrouthtv.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
       
       public void onEnable() {
         System.out.println("Hello!");
       }
       
       @Override
       public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
         Player p = (Player) sender;
         Location target = Bukkit.getWorld("world_nether").getSpawnLocation();
         p.teleport(target);
         p.setGameMode(GameMode.CREATIVE);
         return true;
       }
    }
    


    plugin.yml (open)

    Code:
    name: TestPlugin
    version: 1
    main: me.scrouthtv.main.Main
    commands:
      creative-nether:
      description: Teleports the player to the nether and sets them to creative mode.
    


    This creates the command `creative-nether` which teleports the player to the nether and sets the player into creative mode.

    Now when I use this plugin, it works as expected.

    However, if I set my server to offline mode, the setGameMode does not work (I am in survival mode).

    If you're going to tell me to simply buy Minecraft, don't worry. If you look up my account, you'll find out that I've owned the game for over 6 years. I have to use offline mode at this point of development because I have to test something with multiple players, but don't own enough accounts. Also offline-mode really helps to avoid "try restarting your game" every other hour.

    If I replace the
    Code:
    p.setGameMode(GameMode.CREATIVE)
    with
    Code:
    Bukkit.getScheduler().runTaskLater(this, () -> p.setGameMode(GameMode.CREATIVE), 1);
    
    it works as expected.

    This suggests that the teleporting might be done asynchronously, and only in offline mode the teleporting happens fast enough to trigger this bug. Is there any way to first wait for the teleporting to finish instead of waiting an arbitrary amount of time?

    Thanks in advance.
     
  2. Offline

    KarimAKL

    @ScrouthTV Have you tried setting the gamemode before teleporting the player?

    A fix for this would be a simple online or offline mode check, and then use a runnable if the server is in offline mode.
     
  3. Offline

    timtower Administrator Administrator Moderator

    Locked
    Issue is caused by offline mode which is not supported anyways here.
     
Thread Status:
Not open for further replies.

Share This Page