Solved Add player in whitelist when a not be connected at the server

Discussion in 'Plugin Development' started by MrSolarius, Jun 20, 2020.

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

    MrSolarius

    Hey,
    I want to create a discord command that can add a player to the whitelist. But the problem is I don't know how to get the Player object if the player is not connected... So how I can get the Player object whiteout player connection required?
    Or there is another way to add a player in whitelist than :
    Code:
    player.setWhitelisted(true);
    Cheers ;)
     
  2. Online

    timtower Administrator Administrator Moderator

    @MrSolarius Get the offlineplayer based on the uuid
     
  3. Offline

    Machine Maker

    I'm guessing you want to use the UUID of the player in the discord command, so I would just use Bukkit.getOfflinePlayer(username).setWhitelisted(boolean). getOfflinePlayer is deprecated, but that's how you would do it.
     
    MrSolarius likes this.
  4. Offline

    Zxoir

    Yes, getting the offline player through a string is deprecated though since it can be null. If you get it with a uuid it shouldn't be depricated
     
  5. Offline

    Machine Maker

    @Zxoir I think he HAS to use the playername. He is trying to whitelist players through a discord command, having the player supply their UUID to that command is a massive pain. It'll be fine, I was just telling him that it will show it as deprecated.
     
  6. Offline

    Zxoir

    Oh, then you're right
     
  7. Offline

    MrSolarius

    Thanks for your response,
    I have tried to do Bukkit.getOfflinePlayer(UUID).setWhitelisted(true).
    But that doesn't work correctly when I try to add the player to whitelist, I just have his UUID in the whitelist.json. And because of that when I try to connect it's like I'm not whitelisted.

    here is my whitelist.json after add player from discord :
    Code:
    [
      {
    //normal player whitelisted
        "uuid": "ad73bd22-bfae-43f1-8517-fbe19c1dff49",
        "name": "MrLunarius"
      },
      {
    //whitelisted with my command
        "uuid": "3f94e55a-a73a-4efc-ad66-a5d17c6e5789"
      }
    ]
    and here is my code to add player :
    Code:
        public void addToWhiteList(String uuid){
            System.out.println(uuid);
            OfflinePlayer p = Bukkit.getOfflinePlayer(UUID.fromString(uuid));
            p.setWhitelisted(true);
            Bukkit.reloadWhitelist();
        }
     
  8. Online

    timtower Administrator Administrator Moderator

    @MrSolarius It should not look at a name though (as it can change a lot)
    And you can always just run a whitelist command in the console.
     
  9. Offline

    MrSolarius

    Oh!
    It's possible to just run a console command :D
    How I can do that?
    (but yes in my case just UUID don't work for whitelist)
     
  10. Online

    timtower Administrator Administrator Moderator

  11. Offline

    MrSolarius

    That's works!
    Realy thanks ^^.
    But the code is weird cause of an async event. So if that can help someone here is my code :
    Code:
    public void addToWhiteList(String Nickname) throws CommandException, ExecutionException, interruptedException {
            Bukkit.getScheduler().callSyncMethod( this, () -> Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(),"whitelist add "+Nickname)).get();
            Bukkit.reloadWhitelist();
    }
    
     
Thread Status:
Not open for further replies.

Share This Page