Delayed Task Help

Discussion in 'Plugin Development' started by Creeperzombi3, Jul 11, 2015.

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

    Creeperzombi3

    So I have this code that is supposed to not allow someone to use a command when they are in the ArrayList 'inPvpList'. But when I use the code I currently have, it stops all running tasks, so when people are for example, running away, and trying to get out of PvP, it will glitch when someone else starts to pvp. Anyone know a way to make it so it will start a delayed task for only 1 person?
    Code:
    public void playerPvp(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
                Player hitPlayer  =  (Player) event.getEntity();
                String hitName = hitPlayer.getName();
                Player hittingPlayer = (Player) event.getDamager();
                String hittingName = hittingPlayer.getName();
                if(!pvpList.contains(hittingName) && !pvpList.contains(hitName)) {
                    event.setCancelled(true);
                    hittingPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                            + ChatColor.RED + "PvP is not enabled for you and " + ChatColor.GOLD + hitName + ChatColor.RED + "!");
                } else if(!pvpList.contains(hittingName)) {
                    event.setCancelled(true);
                    hittingPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                            + ChatColor.RED + "PvP is not enabled for you!");
                } else if(!pvpList.contains(hitName)) {
                    event.setCancelled(true);
                    hittingPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                            + ChatColor.RED + "PvP is not enabled for " + ChatColor.GOLD + hitName + ChatColor.RED + "!");
                } else if(inPvpList.contains(hittingName) && inPvpList.contains(hitName)) {
                    Bukkit.getScheduler().cancelAllTasks();
                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                            inPvpList.remove(hitName);
                            inPvpList.remove(hittingName);
                            hitPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                            hittingPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                        }
                    }, (getConfig().getInt("pvpTimer") * 20));
                    event.setCancelled(false);
                } else if(inPvpList.contains(hitName)) {
                    inPvpList.add(hittingName);
                    Bukkit.getScheduler().cancelAllTasks();
                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                            inPvpList.remove(hitName);
                            inPvpList.remove(hittingName);
                            hitPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                            hittingPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                        }
                    }, (getConfig().getInt("pvpTimer") * 20));
                    event.setCancelled(false);
                } else if(inPvpList.contains(hittingName)) {
                    inPvpList.add(hitName);
                    Bukkit.getScheduler().cancelAllTasks();
                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                            inPvpList.remove(hitName);
                            inPvpList.remove(hittingName);
                            hitPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                            hittingPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                        }
                    }, (getConfig().getInt("pvpTimer") * 20));
                    event.setCancelled(false);
                } else {
                    inPvpList.add(hitName);
                    inPvpList.add(hittingName);
                    event.setCancelled(false);
                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                            inPvpList.remove(hitName);
                            inPvpList.remove(hittingName);
                            hitPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                            hittingPlayer.sendMessage(ChatColor.GREEN + "[" + ChatColor.BLUE + "PvPToggle" + ChatColor.GREEN + "] "
                                    + ChatColor.YELLOW + "You are no longer in combat!");
                        }
                    }, (getConfig().getInt("pvpTimer") * 20));
                }
            }
        }
     
  2. Offline

    Burn38

    Put your taks in a class extending Runnable, and add in the constructor the Player. You'll have a task that you can schedule (delayed if you want) working only for one person. Don't forget to get and save the ids of your tasks into an int array or in an hashmap<String playername, int runnableid> to save the ids of the runnable you want to cancel.
     
Thread Status:
Not open for further replies.

Share This Page