Getting All Online Ops

Discussion in 'Plugin Development' started by wptcraft, Sep 4, 2014.

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

    wptcraft

    okay so i need to get all online ops and send them a message can anyone help me out with parts of code that may help? Thanks
     
  2. wptcraft What have you tried so far?
     
  3. Offline

    wptcraft

    AdamQpzm I havent Tried anything i dont know where to start :/
     
  4. Offline

    mythbusterma

    wptcraft

    Perhaps iterate over every Player and check their OP status?
     
    Datdenkikniet likes this.
  5. wptcraft The Bukkit API has a method to get all online players. The Bukkit API has a method to check whether a Player is an op. The Bukkit API has Javadocs. If you're not able to piece anything together, I'd recommend you first learn Java from the oracle tutorials or a Java book, and then read the plugin tutorial found on the wiki
     
    xTrollxDudex and Garris0n like this.
  6. Offline

    teej107

    That's what I would do first, however I thought there would be a better alternative method to use. After searching through google, I came across this thread which suggested using this:
    Code:java
    1. Bukkit.broadcast(message, Server.BROADCAST_CHANNEL_ADMINISTRATIVE);


    wptcraft Took like 10 seconds to find that. Google is your friend.
     
  7. Offline

    wptcraft

    Just tried that, I get no errors or anything here is my code
    Code:java
    1. package me.t0mmy66.adminip;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Server;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerJoinEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin implements Listener{
    13.  
    14. public void onEnable() {
    15. getServer().getPluginManager().registerEvents(this, this);
    16. final java.util.logging.Logger Logger = java.util.logging.Logger
    17. .getLogger("Minecraft");
    18.  
    19. }
    20.  
    21. @EventHandler
    22. public void onPlayerJoinEvent(PlayerJoinEvent e) {
    23. Player player = e.getPlayer();
    24. String IP = player.getName() + " " + e.getPlayer().getAddress().getAddress().getHostAddress();
    25.  
    26. Bukkit.broadcast(IP, Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
    27. }
    28.  
    29. }
     
  8. Offline

    teej107

  9. If you really want a logger use getLogger();

    Also 700 messages on Bukkit!!!!!
     
  10. Offline

    wptcraft

    teej107 No clue what that is or how to add that? can i have a little code to show me? Thanks
     
  11. wptcraft

    Code:java
    1. public void onEnable(){
    2. getLogger().info("Hello"); //Prints Hello to console when the plugin is enabled.
    3. }
     
  12. Offline

    wptcraft

    bwfcwalshy what? i dont think thats the right thing?
     
  13. wptcraft You do not need a Logger especially what you did. It is not needed at all!
     
  14. Offline

    teej107

  15. Offline

    mythbusterma

    wptcraft

    That is the "right thing," might I suggest learning Java before you try to program for Bukkit.
     
  16. Offline

    Reddeh

    Code:java
    1. for(Player p : Bukkit.getServer().getOnlinePlayers())
    2. {
    3. if(p.isOp())
    4. {
    5. //Your code here
    6. }
    7. }

    Put that where your command is run.
    Although seeing the other comments, you won't know what any of this is.
    Protip: Learn java before bukkit or forge or anything like that.
     
  17. Offline

    wptcraft

    No i do know java the basics its just im getting confused by alot of things been shot at me at once.
     
  18. Offline

    ColonelHedgehog

    Not really a lot of things.

    System.out.println(); has been my best friend for...


    days.


    *Ahem*

    I use it, but you can use the stock Bukkit logger rather than the Java logger. Not sure if there's any "best" logger, but that's my choice.
     
  19. Offline

    Reddeh

    Learn more than the basics I'd say, since it looks live you've learnt quite little. A lot of this is basic stuff. Mine for example goes in a command, which is basic bukkit. Then you can execute your code on every op. If you want to execute on non ops for example, just put an exclamation mark at the beginning of the if statement conditions.
     
  20. Offline

    teej107

    Programming is very versatlie. There are many ways you can achieve the same effect. I posted a method that takes full advantage of the Bukkit API. Others have posted another way that you have more control over.
     
  21. Offline

    fireblast709

    'Bukkit logger' would be a 'Java logger'. The Logger you should be using (in reply to the second part of the post) is the Logger object that you get from calling 'getLogger()' in the main class.
     
  22. Offline

    wptcraft

    fixing it dont know why all you guys are ranting on this thread, 20 replies
     
  23. wptcraft If you've got it, please mark as solved.
     
  24. Offline

    Dudemister1999

    If you don't want to go through the playerlist, here's a solution I devised:

    Show Spoiler

    Code:java
    1. import java.io.File;
    2. import java.io.FileReader;
    3. import java.io.IOException;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.UUID;
    7. import org.json.simple.JSONArray;
    8. import org.json.simple.JSONObject;
    9. import org.json.simple.parser.JSONParser;
    10. import org.json.simple.parser.ParseException;
    11.  
    12. public class PlayerManager
    13. {
    14. public static List<User> getOperators() throws IOException, ParseException
    15. {
    16. List<User> ops = new ArrayList<User>();
    17.  
    18. File opsFile = new File("ops.json");
    19. FileReader reader = new FileReader(opsFile.getPath());
    20. JSONParser jsonParser = new JSONParser();
    21. JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
    22.  
    23. JSONArray array = (JSONArray) jsonParser.parse(reader);
    24.  
    25. for(Object obj : array)
    26. {
    27. JSONObject user = (JSONObject) obj;
    28.  
    29. String name = (String) user.get("name");
    30. UUID uuid = (UUID) user.get("uuid");
    31. int opLevel = (int) user.get("level");
    32.  
    33. ops.add(new User(uuid, name, opLevel));
    34. }
    35.  
    36. return ops;
    37. }
    38. }
    39.  
    40. class User
    41. {
    42. public UUID uuid;
    43. public String username;
    44. public int opLevel;
    45.  
    46. public User(UUID uuid, String playerName, int level)
    47. {
    48. this.uuid = uuid;
    49. this.username = playerName;
    50. this.opLevel = level;
    51. }
    52. }



    Dudemister1999 Whoops, forgot the @. See my above post.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  25. Dudemister1999 No offence, but that's a pretty bad way to get the ops, especially if you are only concerned about the online ones.
     
  26. Offline

    octoshrimpy

    Is there any other way without iterating through players? (asking with curiosity, not spite)
     
  27. Offline

    Necrodoom

    mythbusterma likes this.
  28. Offline

    octoshrimpy

    Necrodoom :eek: I need to do my daily adventure through the javadocs still. I'll make sure to stop on getOperators()!
     
  29. Offline

    Dudemister1999

    Necrodoom I always seem to miss the obvious. Thanks for pointing it out!

    AdamQpzm It was only a suggestion, I did not notice he was looking for online operators. :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
    octoshrimpy likes this.
  30. Offline

    DusRonald

    You can use this code for your question.
    Code:java
    1. for(Player on : Bukkit.getOnlinePlayers()){ // Get all players.
    2. if(on.isOp()){ // Checks of the players is OP.
    3. // Do Stuff
    4. }
    5. }
     
Thread Status:
Not open for further replies.

Share This Page