Check if a player is op?

Discussion in 'Plugin Development' started by h4344, Nov 23, 2011.

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

    h4344

    Is it possible to do an if statement like if (player != op) {whatever} ? If not how could i check for if a player is an op so they can do stuff others cant. Ty for any help :)
     
  2. Offline

    nala3

    I am not really good at these kinds of things, but I think it is somewhere along the lines of:
    Correction.


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  3. Offline

    h4344

    Im not sure if im not using the code right or if im just missing something but it dosent seem to work for me :/
     
  4. Offline

    Perdog

    Code:java
    1. Player player = event.getPlayer;
    2. if (player.isOp()) {
    3. //Do the things you wanna do
    4. }
     
  5. Offline

    h4344

    Code:java
    1.  
    2.  
    3. what if im checking for if the player is not op?
     
  6. Offline

    Perdog

    Then you either do, if (!player.isOp()) { or if (player.isOp() == false) {

    Either of those should work, but the first one is probably your best option
     
  7. Offline

    Jaker232

    You'll need to import Player variable then do the following operation.
    Code:java
    1.  
    2. if(player.isOp()) {
    3. // Execute a body
    4. }
    5.  
    6. // If not..
    7. if(!player.isOp()){
    8. // Execute a body
    9. }
    10.  
     
  8. Offline

    Perdog

    So exactly what I said :p

    Where exactly are you trying to do this? In a listener or a command oorrr?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 9, 2018
  9. Offline

    h4344

    I keep getting an error for this. Do i have to have some kind of listener? Something is wrong with the "player" part for me.

    ya im trying to put it inside a listener for whenever a player interacts with flint and steel.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 10, 2018
  10. Offline

    Jaker232

    Have you imported from this way:

    Listener
    Code:java
    1.  
    2. Player player = event.getPlayer();
    3.  
     
  11. Offline

    Perdog

    Alright ... If you don't mind, would you be able to post your code here? To see how you're doing it? I have to actually see what you're doing to be able to help :p
     
  12. Offline

    h4344

    public class MyPlayerListener extends PlayerListener {
    @Override
    public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.getItem().getType() == Material.FLINT_AND_STEEL) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    Player player = event.getPlayer();
    player.getInventory().remove(new ItemStack(Material.FLINT_AND_STEEL,1));
    event.setCancelled(true);
    getServer().broadcastMessage(ChatColor.GREEN + player.getName() + "'s Fire Was Suppressed!");
    }
    }
    }
    }
     
  13. Offline

    Jaker232

    If you want to place a block somewhere in the middle of the code, you have to build it on top of the other blocks then add a new ending curly bracket for it. I can place the OP checking block. Where do you want me to place it?
     
  14. Offline

    Perdog

    Do you have the listener in a new class or in the main class, along with your onEnable and stuff
     
  15. Offline

    h4344

    There is actually another part. I wanted this whole part to only happen if the player is not op so im not sure if you would have to do it once for each class

    public class MyBlockListener extends BlockListener {
    @Override
    public void onBlockIgnite(BlockIgniteEvent event)
    {
    event.setCancelled(true);
    }
    }

    public class MyPlayerListener extends PlayerListener {
    @Override
    public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.getItem().getType() == Material.FLINT_AND_STEEL) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    Player player = event.getPlayer();
    player.getInventory().remove(new ItemStack(Material.FLINT_AND_STEEL,1));
    event.setCancelled(true);
    getServer().broadcastMessage(ChatColor.GREEN + player.getName() + "'s Fire Was Suppressed!");
    }
    }
    }
    }
    }

    Ya the other part that says which event too look for is under onEnable

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  16. Offline

    Perdog

    Try replacing your onInteract with this one:
    Code:java
    1. public void onPlayerInteract(PlayerInteractEvent event) {
    2. Player player = event.getPlayer();
    3. ItemStack held = player.getItemInHand();
    4. Action action = event.getAction();
    5. if (held.getType() == Material.FLINT_AND_STEEL) {
    6. if (action == Action.RIGHT_CLICK_BLOCK) {
    7. player.getInventory().remove(new ItemStack(Material.FLINT_AND_STEEL,1));
    8. event.setCancelled(true);
    9. this.getServer().broadcastMessage(ChatColor.GREEN + player.getName() + "'s Fire Was Suppressed!");
    10. }
    11. }
    12. }
     
  17. Offline

    h4344

    The way i have it now stops all fire. That one only prevents flint and steel from being used.
     
  18. Offline

    Perdog

    That's just moving your code around and cleaning it, you can still keep the onBlockIgnite method
     
  19. Offline

    Jaker232

    You would cancel it.
     
  20. Offline

    Perdog

    Cancel what?
     
  21. Offline

    h4344

    So what about the op issue? Any ideas what im not doing right when trying to use it?
     
  22. Offline

    Perdog

    What isn't working?
     
  23. Offline

    Jaker232

    Explain in-depth what you're trying to do?
     
    Perdog likes this.
  24. Offline

    h4344

    Never mind i fixed it.
     
  25. Offline

    Perdog

    Good to hear :) What was the issue?
     
  26. Offline

    h4344

    Just forgot () in a section lol
     
Thread Status:
Not open for further replies.

Share This Page