Kick player when they join server if not OP.

Discussion in 'Plugin Development' started by dakoslug, Nov 26, 2011.

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

    dakoslug

    Hi, I'm creating a plugin called "EmergencyDowntime" yet I have a unusual problem.
    Here's what my code does now, if IsServerAlreadyOnDowntime = true then when a non-op joins it allows them to enter (This is NOT what I want, I want my code to kick them). Then in my main class (not in my playerlistener class) I wrote, when a OP does /downtimefull then kick all NON-OP players out, it does this fine. However when a OP joins the server when server on downtime it kicks them!! :S.
    Here's my code in the playerlistener (I dont think main class code is needed but just ask. )
    Code:
    //Solved
    //NVM
    
     
  2. Offline

    Jogy34

    I doubt this will do anything but try doing this
    Code:
    if (!(event.getPlayer().isOp()) != true)
     
  3. Offline

    dakoslug

    For both playerJoin and playerLogin events?
     
  4. Offline

    Jogy34

  5. Offline

    dakoslug

    Weird it does the exact same thing. (Meaning nothings changed. )
     
  6. Offline

    Jogy34

    You could constantly check all of the players to see if they are OPs and if they aren't then kick them.
     
  7. Offline

    dakoslug

    SO by this you'd set a timer and it'd check maybe every minute and get a list of players online then check if they're OPs?

    Is they're really no way to block them from entering :p ?
     
  8. Offline

    Jogy34

    You could use an onPlayerMove listener instead so that if they move they get kicked.
     
  9. Offline

    dakoslug

    Wow nvm! My playerlistner works! I guess what you said in you're first post was correct! Thankkkksss sooo much!
     
  10. Offline

    Jogy34

    That has happened to me a few times before and I have no clue why that is.
     
  11. Offline

    dakoslug

    Crap not sure what happened but its allowing Non-ops in.
    Code:
    package me.dakoslug.EmergencyDowntime;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerLoginEvent;
    
    public class EmergencyDowntimeplayerListener extends PlayerListener
    {
      private EmergencyDowntime plugin;
    
      public void onPlayerLogin(PlayerLoginEvent event)
      {
        if (!EmergencyDowntimeCheck.IsServerAlreadyOnDowntime() == false )
        {
          event.allow();
        }
        else if (event.getPlayer().isOp())
        {
          event.allow();
        }
        else
        {
          event.getPlayer().kickPlayer("Sorry server needs downtime! Come back later!");
        }
      }
    
      public EmergencyDowntimeplayerListener(EmergencyDowntime plugin)
      {
        this.plugin = plugin;
      }
    
      public void onPlayerJoin(PlayerJoinEvent event) {
        if (EmergencyDowntimeCheck.IsServerAlreadyOnDowntime())
        {
          if (event.getPlayer().isOp())
          {
            event.getPlayer().sendMessage("Warning: Server in Downtime!");
          }
          else
          {
            event.getPlayer().kickPlayer("Sorry server needs downtime! Come back later!");
          }
        }
      }
    }
    
     
  12. Offline

    Jogy34

    instead of this:
    Code:
    event.getPlayer().kickPlayer("Sorry server needs downtime! Come back later!");
    
    try this:
    Code:
    event.setCancled(true);
     
  13. Offline

    dakoslug

    Getting errors ;/
    what about event.disallow
     
  14. Offline

    Jogy34

    what errors are you getting?

    EDIT:
    Also when you are testing it are you making sure that you aren't an OP
     
  15. Offline

    dakoslug

    Description Resource Path Location Type
    The method setCancled(boolean) is undefined for the type PlayerJoinEvent EmergencyDowntimeplayerListener.java /EmergencyDowntime/src/me/dakoslug/EmergencyDowntime line 42 Java Problem
     
  16. Offline

    Jogy34

    I looked at the javaDocs and it would be something like
    Code:
     event.disallow(Result.KICK_OTHER, "Sorry server needs downtime! Come back later!");
    atleast for onPlayerLogin

    EDIT:
    As far as I can see there isn't a method like that for onPlayerJoin
     
  17. Offline

    dakoslug

    Ok i imported
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerLoginEvent.Result;

    And since I have a config file that has a custom kick message my code now looks like this
    Code:
      public void onPlayerLogin(PlayerLoginEvent event)
      {
        if (!EmergencyDowntimeCheck.IsServerAlreadyOnDowntime() == false )
        {
          event.allow();
        }
        else if (event.getPlayer().isOp())
        {
          event.allow();
        }
        else
        {
        	event.disallow(Result.KICK_OTHER, (plugin.getConfig().getString("Kickmessage")));
        }
      }
    
    And do i get rid of the onPlayerJoin method then?
     
  18. Offline

    Jogy34

    Might as well. There isn't really much of a point in keeping it
     
  19. Offline

    dakoslug

    Ok testing wish me luck!
     
  20. Offline

    Jogy34

    good luck:D
     
  21. Offline

    dakoslug

    *Sighs* I really can't figure out whats wrong.
    And I'm checking if they're OP in these statements;

    Code:
    if (!EmergencyDowntimeCheck.IsServerAlreadyOnDowntime() == false )
        {
          event.allow();
        }
        else if (event.getPlayer().isOp())
        {
          event.allow();
    wait should I do (event.getPlayer()isOp() == true)
    ?
     
  22. Offline

    Jogy34

    Just as a precaution change it to
    Code:
    if (!(EmergencyDowntimeCheck.IsServerAlreadyOnDowntime()) == false )
    
    Because with java it sometimes acts weird and you may end up checking it like this:
    Code:
    if ((!EmergencyDowntimeCheck).IsServerAlreadyOnDowntime() == false )
    EDIT:
    Although I still doubt it would actually do anything but who knows

    I'll try to help you some more tomorrow but as for now I have to sleep.

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

    dakoslug


    No work :/
    Why java why u hate me.
    You want the code on IsServerAlreadyOnDowntime? WIll that help?

    Code:
    package me.dakoslug.EmergencyDowntime;
    public class EmergencyDowntimeCheck
    {
    public static boolean DowntimeFull = false;
    
        public static boolean IsServerOnDowntimeFull(boolean status) //well is it?
                {
                    boolean DowntimeFull = status;
                    return true;
                }
    
        public static boolean IsServerAlreadyOnDowntime()
                {
                    return DowntimeFull;
                }
    }
    And you no need to reply now, Go get sleep.

    And in the main class when somebody does /downtimefull it sets DowntimeFull = true
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("downtimefull")){ // If the player typed /downtime then do the following...
                  for (Player player : getServer().getOnlinePlayers())
                  {
                    if (player.isOp() == true)
                    {
                      player.sendMessage("Server is now on downtime!");
                      EmergencyDowntimeCheck.DowntimeFull = true ;
                      loginlistener = new EmergencyDowntimeplayerListener(this);
                      getServer().getPluginManager().registerEvent(Type.PLAYER_LOGIN, this.loginlistener,Priority.Monitor, this);
    
                    }
                    else
                    {
                      player.kickPlayer(getConfig().getString("Kickmessage"));
    
                    }
    
                  }
    
            }
                return false;
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  24. What's this ? It should be
    Code:
    if (EmergencyDowntimeCheck.IsServerAlreadyOnDowntime())
    or
    Code:
    if (!EmergencyDowntimeCheck.IsServerAlreadyOnDowntime())
    But not at all what you wrote.

    You should know that
    Code:
    if (myBool == true)
    should be
    Code:
    if (myBool)
     
  25. Offline

    dakoslug

    I had that before, but as a precaution i changed it to that .
     
  26. This is false.
    It's like doing :
    Code:
    if (myBool == true) {
        return true;
    } else {
        return false;
    }
    Where it should be :
    Code:
    return myBool;
     
  27. Offline

    dakoslug

    So if I did what you told me to which was;
    Code:
    if (EmergencyDowntimeCheck.IsServerAlreadyOnDowntime()) 
    I am doing it right? or are u telling me to do another thing?



    Ugh! For some reason it works the first time but not the second!

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

    Jogy34

    1.that doesn't really matter that much but it does save space and in the long run time.

    2.Post your .jar file I want to try a few things with it.
     
  29. Offline

    dakoslug

    You think it's something wrong with my boolean checks?

    pm'd

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

    Jogy34

    So I de-complied your .jar file and saw a few problems with it.
    1. you don't need 5 onCommand methods(onCommand, onCommand2, onCommand3, etc...) you can put all of the commands in one of them.
    2. when you are chacking this: if (!EmergencyDowntimeCheck.DowntimeFull()) - EmergencyDowntimeCheck.DowntimeFull() ALWAYS returns false so if you say !EmergencyDowntimeCheck.DowntimeFull() it will always be true
    3. I also noticed that you used the Priority Moderator. Moderator isn't for acting on an event it would be for something like if you were making a log program. It just reports the event's outcome
     
Thread Status:
Not open for further replies.

Share This Page