Player#setGameMode doesn't work (update Player connection)? [1.11.2]

Discussion in 'Plugin Development' started by ScrouthTV, Apr 5, 2017.

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

    ScrouthTV

    So I have:
    Code:
    Player p = (Player) sender;
    int gmi = Integer.parseInt(args[0]);
    GameMode gm = gmi == 0 ? GameMode.SURVIVAL : gmi == 1 ? GameMode.CREATIVE : gmi == 2 ? GameMode.ADVENTURE : gmi == 3 ? GameMode.SPECTATOR : GameMode.SURVIVAL;
    p.setGameMode(gmi);
    If I am in survival and use the command to go to Creative (/gm 1), I am creatuve mode (I can fly, middle click blocks), but it shows the lifes from survival and I don't have a creative inventory.
    If the account reconnects, everything works fine...

    So do I have to update the player's connection?
     
  2. Offline

    Zombie_Striker

    Or, you can yse
    Code:
    GameMode gm = GameMode.getByValue(gmi%4);
    Do you have any other plugins that may interfere with this? If so, remove them and try it again.
     
  3. Offline

    ScrouthTV

    Yes, but it's @deprecated, and I hate this.

    Only plugin is mine, which doesn't do anything else with the gamemode. The gamemode also gets updated with the players death.
     
  4. Offline

    Zombie_Striker

    Don't. Deprecation is just there to bring awareness to something. For this, it is deprecated because it uses a magic value.

    What do you mean by this? Does the gamemode revert back to survivial, or does it get updated to createive mode?
     
  5. Offline

    ScrouthTV

    @Zombie_Striker It (the inventory) gets changed to that I typed in the command (creative).
     
    Last edited: Apr 8, 2017
  6. Offline

    ScrouthTV

    ... anybody?
     
  7. Offline

    Zombie_Striker

    @ScrouthTV
    This is strange. The change should be automatic, and I have tried it and it worked.

    Are you sure you are running the newest build of bukkit or spigot?
     
  8. Offline

    ScrouthTV

    @Zombie_Striker
    So just updated it to the latest version (my build was from March).

    At this point I have to run off the topic for a moment:
    I'm running Windows, so as it says here, I download Git Bash and BuildTools (I was using mcadmin.net, but it's currently down) and ran BuildTools. But for what do you need Git Bash here? It (java -jar BuildTools.jar) works with the normal windows command line, too...

    However, an update doesn't fix the thing; my code:
    Code:
    @EventHandler
    public void onChat(AsyncPlayerChatEvent ev) {
        String[] msgs = ev.getMessage().split(" ");
        if(msgs[0].equalsIgnoreCase("gamemode")) {
            int gmi = Integer.parseInt(msgs[1]);
            GameMode gm = gmi == 0 ? GameMode.SURVIVAL : gmi == 1 ? GameMode.CREATIVE : gmi == 2 ? GameMode.ADVENTURE : gmi == 3 ? GameMode.SPECTATOR : GameMode.SURVIVAL;
            ev.setCancelled(true);
            ev.setMessage(null);
            ev.getPlayer().setGameMode(gm);
            ev.getPlayer().sendMessage("ยง3You are now " + gm + ".");
            ev.getPlayer().updateInventory();
            }
        }
    }
    I use it as player like: gamemode 1

    When I execute it the first time, I have this error in the console:
    Line 513 is this:
    The second time I run it, it says, it has worked fine, but I am still survival mode
     
  9. Offline

    Zombie_Striker

    @ScrouthTV
    Just noticed you are using asyncplayerchatEvent. All non-chat related code should be done synchronously. Either create a sync delayed task for 0 ticks (essentially, running the code the next tick) or use just playerchatevent.
     
  10. @ScrouthTV
    It would be better to have onCommand and register your commands, rather then listen for them to put it in the chat.
     
  11. Offline

    ScrouthTV

    I'm using onCommand, but the problem is, that I want to make a command, completely invisible to other users (autocomplete on commands).
     
  12. Online

    timtower Administrator Administrator Moderator

    @ScrouthTV Why? This plugin seems like a backdoor to me.
    No permissions, no checks at all, invisible.
     
Thread Status:
Not open for further replies.

Share This Page