Solved Canceling PortalCreateEvent

Discussion in 'Plugin Development' started by Alster551, Jun 5, 2014.

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

    Alster551

    So i'm making a plugin and part of it is meant to cancel the PortalCreateEvent unless they have a permission, but when i test it, it doesn't work. Any ideas what i need to do for it to work? Do i need a listener or something?

    Current Code:
    Code:java
    1. package me.alster551.PermissionPortals;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerEvent;
    10. import org.bukkit.event.world.PortalCreateEvent;
    11. import org.bukkit.plugin.PluginManager;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin implements Listener{
    15.  
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17.  
    18. @Override
    19. public void onEnable()
    20. {
    21. getLogger().info("Permission Portals disabled");
    22. PluginManager pm = Bukkit.getServer().getPluginManager();
    23. pm.registerEvents(this, this);
    24. }
    25.  
    26. @Override
    27. public void onDisable()
    28. {
    29. getLogger().info("Permission Portals disabled");
    30. }
    31.  
    32. @EventHandler
    33. public void onPortalCreate(PortalCreateEvent event, PlayerEvent player){
    34.  
    35. Player p = player.getPlayer();
    36.  
    37. if(p.hasPermission(("permissionportals.create"))){
    38. event.setCancelled(false);
    39. }else
    40. event.setCancelled(true);
    41. }
    42. }
     
  2. This is a very helpful statement.
    Explicitly setting cancelled to false is a very good idea.

    Seriously, put in some debug code.
     
  3. Offline

    Deleted user

    Alster551
    That's not how you register an event

    AdamQpzm
    Event handlers don't take two arguments. The player event is not supposed to be there

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

    teej107

    Alster551 Can you have 2 events in the same parameters for an event? And also your hasPermission if test look backwards. The event will be cancelled if the player has the permission.
     
  5. zombiekiller753 Ah, you meant the method header. Good spot. And I thought you meant the registerEvents() call - that's what people usually mean when they use the term "register events" ;)

    teej107 Nope, it's the right way around.

    Alster551 Best solution is to read some plugin tutorials. :)
     
  6. Offline

    Deleted user

    Alster551
    Honestly there are so many mistakes in your code.

    The permission checking is backwards, as well as having an extra set of brackets.

    The unused logger suggests you're a beginner. Am I right?

    If I'm wrong, then please take a look at the API. If I am, still take a look please.

    EDIT: lol. The onenable method logs a disabled message.
     
  7. Offline

    L33m4n123

    by learning java and look how If-statements work correctly and then look at your code again
     
    teej107 likes this.
  8. Offline

    teej107

    Alster551 Learn Java first (and know it well). It will make using the Bukkit API a lot easier since it is in Java. Learning the Bukkit API for me was a snap since I knew Java when I first started making plugins.
     
    L33m4n123 likes this.
  9. Offline

    fireblast709

    Logger.getLogger("Minecraft") is a no no. Use JavaPlugin#getLogger(), like you did in onEnable()
     
  10. teej107 zombiekiller753 L33m4n123
    PHP:
    if the player has the "permissionportals.create" permission
        set cancelled to false
    otherwise
        set cancelled to true
    How the hell is that backwards?! I agree that he hasn't coded this very well but he doesn't need to be incorrectly told 3 times that his if statement is backwards, okay?
     
  11. Offline

    L33m4n123

  12. Offline

    teej107

    AdamQpzm Ok I did get a bit mixed up but you don't need to setCancelled() to false. The event is already not going to be cancelled unless you did cancel it.
     
  13. Offline

    Deleted user

    AdamQpzm
    Basically, if the player has the create permission, he sets it to not cancelled, which is backwards because he does not set ignoreCancelled to true. Any cancelled events won't make it to his handler.

    What he should have done is

    if (!hasPermission(perm)) //Also note the brackets. He's got one extra set
    setCancelled(true)
     
  14. Offline

    L33m4n123

    the extra bracket does not matter tooo much as he closes it also


    And WHAT is backwards on following statement

    Code:
    If the player HAS the create permission
      event is NOT cancelled
    this basicly passes the event on if the player HAS the permission node
     
  15. Offline

    Deleted user

    L33m4n123
    Because that's useless. If the event isn't cancelled, no need to do that. If it is cancelled, he won't even get the event in the first place
     
  16. Offline

    L33m4n123

    If he ignoreCanceled = true to make sure his event gets called under all circumstances
     
  17. L33m4n123 Fair enough, reasonable to assume that they were correct I guess. :)
    teej107 In some cases it is necessary - if you have specific reason to override another plugin's decision to cancel the event. But he would have used a higher priority if he wanted that, so you're right that it's useless doing it here. In some cases it may even be harmful, as it could conflict with other plugins needlessly.
    zombiekiller753 Erm, what? He doesn't set ignoreCancelled, so he will be passed the event. You're only not passed cancelled events if you ignore them.
     
  18. Offline

    breezeyboy

    They do take two args. The class where the listener is, and the plugin class.
     
  19. Offline

    Onlineids

    Thats registering the events not the actual eventhandler
     
  20. Offline

    Deleted user

    AdamQpzm
    Ugh. You're just not understanding.

    The permission checks whether the player has 'x' permission. If he does, the event is allowed. If he doesn't, it's cancelled.
    That's stupid. That's wrong. That's backwards.

    Why?
    Let's consider the two possible scenarios. Either the event was cancelled before this, or it wasn't.

    If it was: Well, he doesn't declare ignoreCancelled to true, so he doesn't get the event. Useless code
    If it wasn't: Then what's the point of setting cancelled to false? That's like saying if (someBoolean != true)

    tl;dr Should have done

    if (!player.hasPermission("blah")) event.setCancelled(true)

    Or even event.setCancelled(!player.hasPermission("blah"))
     
  21. zombiekiller753 No, you're the one that doesn't understand. Firstly, it's not backwards at all. Did you actually read what he wanted to do? I don't think you did. Maybe you should, that would be helpful.

    Just to clear it up for you, he says "it is meant to cancel the PortalCreateEvent unless they have a permission". So why the hell would you cancel the event if they have the permission. He does this:

    PHP:
    if(p.hasPermission(("permissionportals.create"))){
                
    event.setCancelled(false);
            }else
            
    event.setCancelled(true);
    And you say that's backwards. Is he meant to do this, then?

    PHP:
    if(p.hasPermission(("permissionportals.create"))){
                
    event.setCancelled(true);
            }else
     
            
    event.setCancelled(false);
    Because that makes absolutely no sense at all. If the player has the "permissionportals.create permission, cancel the event"?! Because that's what you're suggesting he do. I agree that he shouldn't be setting cancelled to false, and that the following is the best solution
    PHP:
    if(!player.hasPermission("p")) event.setCancelled(true);
    But under no circumstances is what he's doing at the moment backwards.

    And now onto your second, incredibly confusing comment. "he doesn't declare ignoreCancelled to true, so he doesn't get the event" What? Seriously, what? What is it that you think "ignoreCancelled" does when it's set to true?

    I'll tell you what it does. When it's set to true, it ignores cancelled events. I know that may be confusing and seem quite strange, considering that's exactly what its name implies it does, but there you go. When it's not set to true, you get the events. If you don't believe me, go test it. Actually, don't. You probably don't actually have any idea how basic logic works, so there's no point.
     
  22. Offline

    Deleted user

    AdamQpzm
    (All swears are replaced with 'gorilla')
    I swear to god, you're such a gorilla idiot. Take a look at my tldr. Now take a look at your solution. It's the same.

    Now, granted, I goofed about ignoreCancelled, but please don't tell me you didn't know what I meant.
     
  23. zombiekiller753 There's a huge gorilla-sized difference between "backwards" and redundant. And there's also a difference between goofed and got it completely wrong. Now stop calling me an idiot or gorilla off.
     
Thread Status:
Not open for further replies.

Share This Page