Solved Loop to kill players in an arraylist

Discussion in 'Plugin Development' started by iLiveorLose, Feb 26, 2016.

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

    iLiveorLose

    Hi, I need to find out how to make a loop that kills a player in an arraylist and keeps killing them until they are manually removed from the arraylist (Like when my friends decide to mess around, I can use this magic).
    In simpler words: /loopkill <playername> will add them to an arraylist, and players in that arraylist will be killed over and over until they are removed (ill make a command to remove them). If it would be easier I could make a scheduled task that kills players in the arraylist every certain ticks, but I dont know how to do this.
    Any help? Thanks in advance.
     
  2. Offline

    Zombie_Striker

  3. Offline

    iLiveorLose

    @Zombie_Striker
    I read the scheduler programming tutorial many times after you replied and tried to make my own, but none of them worked or had any errors. I might need a little help with this whole scheduling tasks thing, I know I'm an idiot and I completely understand if you are agitated
     
  4. Offline

    Zombie_Striker

    @iLiveorLose
    Don't be hard on yourself. Let me explain:

    There are two ways to create a scheduler:
    1. Create an entirely new class
    2. Create a sub class within another class.
    Because I normally use sub-class scheduled tasks, I will explain how you would go about using them.
    The structure follows the following structure:
    Code:
    //I'm going to break the bracket up into multiple lines to make it easier to explain.
    
    Bukkit.getScheduler().scheduleSycnRepeatingTask(
    JavaPluginInstance,// This is the main class defined in the plugin.yml. If you put this inside the class that extends JavaPlugin, replace "JavaPluginInstance" with "this". If this is in another class, pass the main instance through the constructor and reference that instance.
    new Runnable(){// This creates a new thread. If you ever dealt with threads this should be nothing new.
    public void run(){
    
       /**
       *Inside here you put all the stuff you want to happen every time the task is "triggered".
       **/
    
      }
    },
    Initial Delay, //When this task has first been created, how long will it take before it get triggered for the [I]first[/I] time. (Note this is in ticks. 20 ticks = 1 second)
    Delay between repeats // How long it will take between tasks [I]after[/I] it has been triggered once. (Note: this is in ticks. 20 Ticks = 1 second)
    ); //Close the bracket
    In case you don't know how to pass objects through constructors:
    http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
     
  5. Offline

    Swakiny

    Do you have done any code yet?
     
    teej107 likes this.
  6. Offline

    iLiveorLose

    @Zombie_Striker
    Thank you... That alone made it much, much easier. If I put that in my subclass, then iin my main class I can make a PlayerJoinEvent and call the run method inside the event? Basically, like this?:
    Code:
    public void onJoin(PlayerJoinEvent e){
         run();
    }
    Does that work? If not, what would I put in my main class to run the run part?

    @Swakiny
    I will start once I get out of bed :p
     
  7. Offline

    Swakiny

    Of course not, why are you trying to run without extending bukkitrunnable?
    Code:
    public void onJoin(PlayerJoinEvent e){
    new BukkitRunnable() {
                           public void run()
    {
                                      //do what you want
    }                            
                            }.runTaskTimer(this, 20l, 20l);
    
    }
    Beware, I think this will creat a new runnable for every player that join in the server...
    If you wan't to prevent this, set int x = 0; and before creating a new runnable check if int x == 0, if so, after creating a runnable set int x to 1
     
  8. Offline

    iLiveorLose

    Ok, and do I put that in the sub or main class?
     
  9. Offline

    Swakiny

    lol. Where you want the loop goes on?
     
  10. Offline

    iLiveorLose

    @Swakiny
    What? I want the loop to kill players in an arraylist, but I dont know which class to put what you just said in.
     
  11. Offline

    JoaoBM

  12. Offline

    iLiveorLose

    @JoaoBM @Zombie_Striker @Swakiny
    Code:
    package me.LiveOrLose.Loopkill;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        ArrayList<String> loopkill = new ArrayList<String>();
    
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            /*
            * I couldn't think of another event to use to kill the player if the
            * arraylist contains them
            */
            for (Player online : Bukkit.getServer().getOnlinePlayers()){
                if (loopkill.contains(online)){
                    online.setHealth(0);
                }
            }
        }
    
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("loopkill")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED
                            + "Correct usage: /loopkill <player>");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.GOLD + args[0] + ChatColor.RED
                            + " is not online.");
                    return true;
                }
                if (loopkill.contains(target.getName())) {
                    loopkill.remove(target.getName());
                    sender.sendMessage(ChatColor.GOLD + target.getName()
                            + ChatColor.GREEN + " has stopped being loop killed!");
                    return true;
                }
                loopkill.add(target.getName());
                sender.sendMessage(ChatColor.GOLD + target.getName()
                        + ChatColor.GREEN + " is now being loop killed!");
            }
            return true;
        }
    }
    
    It doesn't work, but how do I make it work? And preferably without the PlayerMoveEvent so that it always kills them even if they don't move. Thanks in advance.

    @JoaoBM @Zombie_Striker
    Ok, I fixed it so that when the player moves just a little bit (even the mouse) they die if they are in the arraylist. This is amazing, as it's what I wanted to do. However, the chat spams with tons of death messages. I tried to mute the death messages by doing this:
    Code:
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            Entity entity = e.getEntity();
            if (entity instanceof Player) {
                if (loopkill.contains(entity)) {
                    e.setDeathMessage(null);
                }
            }
        }
    
    But it didnt work. Any help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 27, 2016
  13. Offline

    Zombie_Striker

    @iLiveorLose
    ProTip: Setting Strings to "null" most likely never works. Either set the death message to "" (nothing) or try to cancel the event.
     
  14. Offline

    Doomfighter

    I mean I would look on a PlayerRespawnEvent. If he respawns, look if he´s in the List and them kill him again..
     
  15. Offline

    iLiveorLose

    Wow, thanks! That's really helpful :p

    @Doomfighter
    Nvm... doesn't work... no errors or anything, just keeps me alive on respawn until I move (which was allready defined earlier) Heres my code for the event:
    Code:
        @EventHandler
        public void onRespawn(PlayerRespawnEvent e) {
            Player p = e.getPlayer();
            if (loopkill.contains(p.getName())) {
                p.getPlayer().setHealth(0);
            }
        }
    Thanks.

    @Zombie_Striker
    Also, e.setDeathMessage(null) worked perfectly, the only problem was that I forgot to add the .getName() to entity. Just letting you know because it may be helpful to you :)

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

    teej107

    @iLiveorLose Run a scheduled task in the respawn event.
     
  17. Offline

    Zombie_Striker

    @iLiveorLose
    As @teej107 said, you should create a task to delay killing the player. Look into "Delayed Tasks" which are almost identical to "Repeating tasks", but lack the "delay between repeats" variables (as posted in my previous post).
     
  18. Offline

    iLiveorLose

    @Zombie_Striker @teej107
    Thanks so much guys! It worked very well, now when they respawn after dying from the loopkill, they will just die again! This will show my friends when they decide to be annoying.... Marking as solved.... Thanks so much guys!
     
  19. Offline

    teej107

Thread Status:
Not open for further replies.

Share This Page