Scheduler and command

Discussion in 'Plugin Development' started by slater96, Apr 18, 2012.

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

    slater96

    Hi, I have a message which broadcasts every hour (configurable in config) and when that msg broadcasts, I want people to be allowed to do the command /pvp join which is only allowed to be used for say, 5 mins and then it comes up with another msg if it's not allowed at that time. Any help appreciated. Thanks

    bump, please anyone help?

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

    Alectriciti

    Hey there. I'd try looking at http://wiki.bukkit.org/Scheduler_Programming

    To save you some time, here's how you'd go about using repeating tasks.

    Code:
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
     
      public void run() {
          getServer().broadcastMessage("This message is broadcast by the main thread");
      }
    }, 60L);

    As far as the command "/pvp join" goes, since you want a delay, I may recommend using a HashMap for the player who entered the command, then remove them from it after 5 minutes. Here's a quick example of what the command may look like.

    Code:
    if (HashMapExample.get(p) == false){
      player.sendMessage("This is the message that would appear to the player.");
      HashMapExample.put(player, true); //This would prevent them from typing it again.
      main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
     
     
                      public void run() {
                              HashMapExample.put(player, false);
                      }
                  }, 6000L); //6000/20 = 300 seconds = 5 minutes.
    }else{
      player.sendMessage("You cannot use that command right now.");
    }
    

    I hope this helped somehow.
     
    slater96 likes this.
  3. Offline

    slater96

    Thanks, it helped alot :D
    However on the word 'player' in pvp.put(player, false); there is an error saying change modifier of player to final.
    Code:
                    if(pvpjoin.get(player) == false){
                        player.sendMessage("PVP is not on!");
                        pvpjoin.put(player, true);
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            public void run() {
                                pvpjoin.put(player, false);
                            }
                        }, 6000L);
                    } else {
                        player.sendMessage("TEST");
                    }
                }
            }
            return true;
        }
    Does that look right otherwise?

    Bump. Please anyone, could do with getting this sorted tonight.

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

    Alectriciti

    Setting the modifier of player to final should work fine. Give it a shot. I recommend just playing around with the code. Is everything working out alright?
     
  5. Offline

    slater96

    No, sadly it's still not working :(
    I changed the player to final, but still no luck. Here's my whole command code.
    Can you see where i've gone wrong as I'm new to HashMaps?
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,String[] args) {
            PluginDescriptionFile pdfFile = this.getDescription();
            if (!(sender instanceof Player)) {
                sender.sendMessage("[PVPAnnouncer] Version " + pdfFile.getVersion() + " by slater96");
                sender.sendMessage("/pvp join - Teleport to PVP World");
                return false;
            }
            final Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("pvp")) {
                if(args.length == 0) {
                    player.sendMessage(ChatColor.GREEN + "[PVPAnnouncer]" + ChatColor.GOLD + " Version " + pdfFile.getVersion() + ChatColor.AQUA + " by slater96");
                    player.sendMessage(ChatColor.GOLD + "/pvp join" + ChatColor.AQUA + " - Teleport to PVP World");
                } else if(args[0].equalsIgnoreCase("join")) {
                    double radius = 20;
                    Location spawn = Bukkit.getServer().getWorld(getConfig().getString("World")).getSpawnLocation().add((2*Math.random() - 1)*radius, 0.0, (2*Math.random() - 1)*radius);
                    player.teleport(spawn);
                    if(pvpjoin.get(player) == false){
                        player.sendMessage("PVP is not on yet!");
                        pvpjoin.put(player, true);
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            public void run() {
                                pvpjoin.put(player, false);
                            }
                        }, 6000L);
                    } else {
                        player.sendMessage("TEST");
                    }
                }
            }
            return true;
        }
    Thanks

    bump

    Please, any help?

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

    Alectriciti

    I scanned over your code, and if this doesn't work, I'll put it into Eclipse myself until we get to the bottom of this.

    At


    PHP:
     Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            public 
    void run() {
                                
    pvpjoin.put(playerfalse);
                            }
                        }, 
    6000L);
    Try removing the very first
    "Bukkit."
    and just leave it at this:
    "getServer().getScheduler().scheduleSync..." and so on.

    Your result should simply be:

    PHP:
    getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            public 
    void run() {
                                
    pvpjoin.put(playerfalse);
                            }
                        }, 
    6000L);
     
    slater96 likes this.
  7. Offline

    slater96

    Thanks for the help, but it's still not working right. It gives me this error?
    http://pastebin.com/XrGfABQY
     
  8. Offline

    Alectriciti

    I think I found it. Since you're getting a null pointer exception, the only thing I can think of is the HashMap, which I've had trouble with before. The thing about HashMaps, is that they are variables. They need to be defined first. The error appears to be here:
    pvpjoin.get(player) == false


    It's trying to get a value where it's currently null.

    What you might want to do is make a listener for player logins. When a play logs into the server, add him to the pvpjoin HashMap as either true or false, depending on what you want it to be to start with.
     
    slater96 likes this.
  9. Offline

    slater96

    Ok, thanks. When I set the HashMap as true, does that mean they can do the command then?
     
  10. Offline

    Neodork

    The one you set it for will be allowed.
     
    slater96 likes this.
  11. Offline

    ImminentFate

    adding to this, is it possible to schedule a scheduled task?
     
  12. Offline

    Neodork

    You can activate and de-activate is so I would say yes.
     
  13. Offline

    slater96

    So is it simply like this?
    Code:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            pvpjoin.put(player, true);
        }
    Then in my onCommand, I do that to false when they do the command?
     
  14. Offline

    Neodork

    Yep so:

    Code:
    pvpjoin.put(player,false);
     
    or:
     
    if(pvpjoin.get(player)==true){
        pvpjoin.set(player,false) 
    }else{
    if(pvpjoin.get(player)==false){
        pvpjoin.set(player,true) 
    }
    }
    
    if you got some events bound by:

    Code:
    if(pvpjoin.get(player)==true){
     
     
     
    }

    They will react if the value is true for the player.
     
  15. Offline

    slater96

    Could it not be working because people teleport first and then there's the HashMap code?
    Like here:
    Code:
    Location spawn = Bukkit.getServer().getWorld(getConfig().getString("World")).getSpawnLocation().add((2*Math.random() - 1)*radius, 0.0, (2*Math.random() - 1)*radius);
                    player.teleport(spawn);
                    if(pvpjoin.get(player) == false){
                        player.sendMessage("PVP is not on yet!");
                        pvpjoin.put(player, true);
                        getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            public void run() {
                                pvpjoin.put(player, false);
                            }
                        }, 6000L);
                    } else {
                        player.sendMessage("TEST");
                    }
                }
            }
            return true;
        }
    bump

    Any help?

    Bump

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

    slater96

    Bump. Come on, someone must be able to help??
     
Thread Status:
Not open for further replies.

Share This Page