Simply autokick a user...

Discussion in 'Plugin Development' started by warpie, Apr 20, 2012.

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

    warpie

    Hi.
    I've been trying to make a simple script, but it's this one part i just can't get figured out. While trying to kick a player on join..:
    Code:
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
     
    public class testsys implements Listener {
        
        @EventHandler
        public void normalLogin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            player.kickPlayer("Kicked due to something...");
        }
    }
    But when I logg in to my server, nothing happens - I don't get kicked.

    Full code is something similar to (constantly editing it), but not important:
    Show Spoiler
    Code:
    package me.warpie.PlayerTiming;
     
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.logging.Logger;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
     
     
    public class PlayerTiming extends JavaPlugin implements Listener {
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        Logger log = Logger.getLogger("Minecraft");
     
        @EventHandler
        public void normalLogin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            String nick = player.getDisplayName();
     
            log = this.getLogger();
            log.info(nick +" has joined us!");   
       
            String isRegisterd = isUser(nick.toLowerCase()).toString();
            System.out.println(isRegisterd);
       
            if(isRegisterd.equals("True")) {
                log.info(nick +" is registerd in our DB!");
            } else {
                //long unixTime = System.currentTimeMillis() / 1000L;
                //long twenty = unixTime+1200;
                //String kickTime = Long.toString(twenty);
           
                //Start taking the time...     
                //log.info(Long.toString(unixTime));
                //log.info("He will be kicked at Unix:"+ kickTime);
                player.kickPlayer("Kicked due to something!");
           
            }
     
        }
     
        public static StringBuilder isUser(String username){
            StringBuilder falsify = new StringBuilder();
     
            try {
                Runtime r = Runtime.getRuntime();
                Process p = r.exec("python C:\\python\\xxx.py "+ username);
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                p.waitFor();
                while (br.ready()) {
                    falsify.append(br.readLine());
                }
     
            } catch (Exception e) {
                String cause = e.getMessage();
                if (cause.equals("python: not found"))
                    System.out.println("No python interpreter found.");
            }
            return falsify;
        }
    }


    Any idèas on what is wrong?
     
  2. Offline

    Alectriciti

    Did you put the
    @EventHandler
    annotation before your method? If that's not it, I'm not sure. I noticed PlayerLoginEvent is really weird for me, and doesn't allow me to send messages to logged in players, though I've never really needed it for my plugins, I do want to figure it out eventually.
     
  3. Offline

    warpie

    Yes, I acutally did. Was only my mistake that it was missing in the first code-snip (fixed, and tested again). But still - nothing.

    EDIT: My mistake. I just used PlayerJoinEvent instead :)

    But i found another problem I wish to descuss here, how can I run a long loop, or do a "sleep();" within this script, without affecting/"crashing" the server?
    EG:
    Code:
                player.sendMessage(ChatColor.RED + "Writing something...");
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException ie) {
                    System.out.println(ie.getMessage());
                }
                player.sendMessage(ChatColor.RED + "Writing something more afther a few secound...");
     
  4. Offline

    SpartaMercenary

    You could spin off a delayed task on the main thread with a delay of whatever amount of milliseconds you want.

    Example:
    Code:java
    1.  
    2. getServer().getScheduler().scheduleSyncDelayedTask(this, new DelayedMessage(player), 4000);
    3.  

    In this example, it would create a new DelayedMessage task, passing the player object to it as a constructor, and putting that object into a field in the task. In the run() method of this task, you would put the player.sendMessage() line.

    EDIT: If you sleep the main server thread, you will lock the server up.
     
  5. Offline

    warpie

    Yes, I figured that.

    I will take a peak at the code you're showing, and hopefully learn something.
    Thank you very much! :)

    Edit:
    So I did not manage to pass player in to the Schduler, I'm not really sure what went wrong... But i had an error with "DelayedMessage" and (player); would not seem to work:
    Code:
                getServer().getScheduler().scheduleSyncDelayedTask(this, new DelayedMessage(player) {
                    public void run() {
                        player.sendMessage(ChatColor.RED + "some text..");
                    }
                }, 100L);
    When I changed DelayedMessage(); to Runnable(); it seemd somewhat better, but I still could not pass player as a consturctor.

    [​IMG]
     
  6. Offline

    dillyg10

    yeah your code should look something like this..

    Code:java
    1.  
    2. //im assuming this is in you PlayerJoin thing..
    3. //player MUST be final!
    4. final Player player;
    5.  
    6. getServer().getScheduler().scheduleSyncDelayedTask(this,new Runnable() {
    7. public void run() {
    8. //code...
    9. }
    10. },400L);
     
  7. Offline

    warpie

    Well, as I'm sure you already know, I will state it anyway: That did the trick!
     
  8. Offline

    dillyg10

    :p. NP bro :D
     
Thread Status:
Not open for further replies.

Share This Page