Two 'BukkitRunnable()'s' in the same event

Discussion in 'Plugin Development' started by caledonian26, Feb 20, 2023.

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

    caledonian26

    Hey all,

    I have the following code.

    The idea here is that when the player interacts with the lever, it records:

    1. The name of the player/time they did this (this part of the code works)
    2. Assigns items to the player's inventory every minute (this part of the code works)
    3. Kills the player after 195 seconds (3900 ticks) (this part of the code does not work)

    However, the final aim - where the player is killed, does not seem to work! I put this final objective in a separate 'bukkitrunnable()', but I believe the issue may lie in the fact that you cannot have two bukkitrunnable()'s in the same event.

    I am not sure how to rectify this!

    Would be so grateful for a helping hand?

    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
         if (event.getClickedBlock().getType() == Material.LEVER) {
          try {
             String content = String.valueOf(new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date())) + " Player: " + event.getPlayer().getName() + " Material: " + event.getClickedBlock().getType() + "\n";
             if (!getDataFolder().exists()) {
                   getDataFolder().mkdirs();
                 }
             final File file = new File(dataFolder, "eventswayfind" + event.getPlayer().getName() + String.valueOf(new SimpleDateFormat("dd-MM-yyyy").format(new Date())) + ".log");
             if (!file.exists()) {
                 file.createNewFile();
             }
             Files.write(file.toPath(),content.getBytes(StandardCharsets.UTF_8),StandardOpenOption.APPEND);
          } catch(Exception e) {
                 e.printStackTrace();
             }
            
          new BukkitRunnable() {
            int count = 0;
            Material[] listofitems = {Material.SPONGE, Material.COBWEB, Material.CAKE, Material.RED_WOOL};
    
            public void run() {
              if(!event.getPlayer().isOnline()) {
                    this.cancel();
                }
              if (count == listofitems.length-1) cancel();
              Material nextItem = listofitems[count];
              ItemStack item = new ItemStack(nextItem);
              event.getPlayer().getInventory().addItem(item);    
              count++;
            }
          }.runTaskTimer(this, 0, 900);
         
          new BukkitRunnable() {
              int count = 0;
    
              public void run() {
                if(!event.getPlayer().isOnline()) {
                      this.cancel();
                  }
                if (count == 1) cancel();
                event.getPlayer().setHealth(0);    
                count++;
              }
            }.runTaskTimer(this, 0, 3900);
        }
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @caledonian26 You can have as many runnables in there as you want.
    Why isn't that second one a runTaskLater?
     
  3. Offline

    caledonian26

    This seems to work! :)

    Code:
     new BukkitRunnable() {
            int count = 0;
            Material[] listofitems = {Material.SPONGE, Material.COBWEB, Material.CAKE, Material.RED_WOOL};
    
            public void run() {
              if(!event.getPlayer().isOnline()) {
                    this.cancel();
                }
              if (count == listofitems.length-1) cancel();
              Material nextItem = listofitems[count];
              ItemStack item = new ItemStack(nextItem);
              event.getPlayer().getInventory().addItem(item);  
              count++;
            }
          }.runTaskTimer(this, 0, 900);
       
          final BukkitRunnable Br = new BukkitRunnable() {
              int count = 0;
    
              public void run() {
                if(!event.getPlayer().isOnline()) {
                      this.cancel();
                  }
                if (count == 0) cancel();
                event.getPlayer().setHealth(0);  
                count++;
              }
            };
          Br.runTaskTimer(this, 3900, 3900);
        }
     
    Last edited: Feb 20, 2023
  4. Offline

    timtower Administrator Administrator Moderator

    @caledonian26 Why are you still calling runTaskTimer?
    And why is it still using a count variable?
     
Thread Status:
Not open for further replies.

Share This Page