OP a player when enable...

Discussion in 'Spigot Plugin Development' started by EyesPlay, Apr 20, 2017.

  1. Hi everyone! I'm new here! I hope that this is the correct section... If not please correct it! ;)

    I'm developing a plugin and i have necessity that it op me when the server enable it...
    I'll explain my question... I've added to my plugin some commands and a join message but i can't find any way to op myself at the plugin "start"... I wish the plugin op me when it is enabling (onEnable)
    There is any way to do it?
    Thanks in advance! And sorry for my bad english but i'm italian and i don't speak english very well...

    edit: if you need it... i can give you my main class!

    Thanks to @2008Choco ! The answer is this:
    Code (Text):
    @Override
    public void onEnable() {
        OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString("yourPlayerUUIDHere"));
        player.setOp(true);
    }

    // Alternatively
    @Override
    public void onEnable() {
        Bukkit.getOfflinePlayer(UUID.fromString("yourPlayerUUIDHere")).setOp(true);
    }
     
    #1 EyesPlay, Apr 20, 2017
    Last edited: Apr 20, 2017
  2. Ciao!
    Vedo che sei italiano e ti scrivero' in italiano :p

    Un modo per far cio' sarebbe instiatare un Player (usare Bukkit.getPlayer(...)) e poi chiamare il metodo #setOp che richiede un valore booleano (true o false), o per controllare se un Player è op puoi usare #isOp

    English:
    Hey!
    I see that you're italian, so i'll write in italian :p

    A way for doing this would be instiating a Player (meant as using Bukkit.getPlayer) and then call the method #setOp that requires a boolean (true or false), also, for checking if a player is op, you can use the #isOp method.
     
    • Like Like x 1
  3. Italiano: ehm... allora... intanto ciao! un'italiano è quello che mi serviva xD
    comunque... praticamente quello che serve a me è che quando il plugin si abilita (quindi sotto onEnable) oppi solo me... il mio nick o il mio uuid
    English: Um ... then ... meanwhile hello! An Italian is what I needed xD
    Anyway ... basically what i need is that when the plugin gets enabled (so under onEnable) just op me ... my nick or my uuid
     
    #4 EyesPlay, Apr 20, 2017
    Last edited: Apr 20, 2017
  4. Choco

    Moderator

    (When speaking another language, please provide an English translation as possible ^-^ Just helps us useless English speakers!)

    If you want to op a player, I recommend getting an OfflinePlayer object and calling #setOp(true). You should be using UUID objects for this because if you so happen to change your name, you will have to modify your plugin. You can get your UUID at http://www.mcuuid.net/ by putting in your username into the text field
    Code (Java):
    @Override
    public void onEnable() {
        OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString("yourPlayerUUIDHere"));
        player.setOp(true);
    }

    // Alternatively
    @Override
    public void onEnable() {
        Bukkit.getOfflinePlayer(UUID.fromString("yourPlayerUUIDHere")).setOp(true);
    }
    Also assure that this does not go in any publicly released resources, otherwise your account will be permanently suspending for malicious code (setting yourself as an operator on a server that you do not own is considered malicious). If it's for personal use, then this is fine :)
     
    • Agree Agree x 3
    • Winner Winner x 1
  5. +1 cuz Italian (don't understand any of it tho :p)

    Correct me if I'm wrong, but I do believe you can use setOp(true) on and OfflinePlayer.
     
  6. oh... sorry for my italian post... anyway... your method seems to be the answer to my question... i go to test it! thanks!
     
  7. Choco

    Moderator

    No no don't worry about posting in Italian! I encourage it! Just also provide an English translation so that those whom do not speak English may also understand and assist you ^-^
     
    • Like Like x 1
  8. don't worry for the malicious code... isn't my intent... just do some jokes to my (noob) friends!
     
    • Like Like x 1
  9. i've traduced it to english!
     
    • Friendly Friendly x 1
  10. yes it's correct! i'll test it
     
  11. @2008Choco just another question... i have to put full uuid or trimmed?
     
  12. Choco

    Moderator

    The full UUID ^-^ Not many people use the trimmed UUID, really. Typically you'll always want to use the full UUID
     
    • Winner Winner x 1
  13. okok thanks! now i'm exporting the jar to test it!
     
  14. yes!! it work!! gg thanks to everyone! specially to @2008Choco
     
  15. uhm... there is a problem... the plugin set me op for ever (when it's enabled) but isn't saved in the ops.json... how can i solve it?
     
  16. the ops.json have this:
    Code (Text):
    [
      {
        "uuid": "f2cf1ae2-9dc1-4d8b-bace-58ecc70922c8",
        "level": 4,
        "bypassesPlayerLimit": false
      }
    ]
    but the name isn't saved and i cannot deop myself in-game...
     
  17. i tried to specify two times the getofflineplayer
    1) with the uuid and another with the name in a single method...
    2) with the uuid and another with the name in two separated method...

    1) gives me an error...
    2) op me two times... one with the name and another with ONLY the uuid
     
  18. For the UUID, use UUID.fromString