BlockPlace runnable error help

Discussion in 'Spigot Plugin Development' started by ElbestHD, Sep 25, 2018.

  1. Hello, I have a problem that I did this code and it does not work and I do not get an error in the console because it will be?

    Code (Text):
        private Main main;
        int count = 60;
         
        @EventHandler
          public void onPlaceFlin(BlockPlaceEvent e)
          {
            if (e.getItemInHand().getType().equals(Material.COBBLESTONE)) {
              new BukkitRunnable()
              {
                public void run()
                {
                   
                    if (count == 0) {
                       
                         if (e.getBlock().getType().equals(Material.COBBLESTONE)) {
                             e.getBlock().setType(Material.AIR);
                       
                        Bukkit.broadcastMessage("§6Removing blocks...");
                        cancel();
                 
                  }else {
                     
                      count--;
                      Bukkit.broadcastMessage("§6Removing blocks in §e" + count + "§6 seconds..");
                  }
                }
              }
            }.runTaskLater(this.main, 100L);
          }
         }  
         
    }
     
    • Like Like x 1
  2. SteelPhoenix

    Moderator

    Maybe provide the actual error?
     
  3. #getItemInHand is deprecated and you should be comparing enum values with == or !=. Also, you are using a delayed task and not a repeating task. Meaning that the code in the event will run 5 seconds later and that's it. Apart from that, you're calling #cancel on a delayed task which is foolish to do so...

    A conclusion:
    • You should initialize your int localy and not have it set as a "global" variable.
    • You should be taking a look on repeating tasks.
    • Comparing enum types should be done with == or !=
    • You needn't cancel a delayed task as it is only run once and this can't be stopped by cancelling.
    • #getItemInHand is deprecated. You'll have to specify which hand to get. (Main or offHand)
     
    #3 darklazerog, Sep 25, 2018
    Last edited: Sep 25, 2018
    • Like Like x 1

  4. I already understand a little of what you are saying, but can you give me some examples or help me?
     
  5. Code (Java):


    final Block block = e.getBlock();

    // TODO do your block type checks?

    new BukkitRunnable() {
    int num = 60;
         @Override
        public void run() {
       
            if (num-- == 0) {
              // TODO you've reached 0
             this.cancel();
             return;  
            }  

        // TODO You've not reached 0, what should happen?

        }
    }.runTaskTimer(JavaPluginClass, time untill the task starts, time untill it repeats);
     
     
    • Like Like x 1
  6. and if I just want to remove a single material from a block every 20s
     
  7. Change the num to 20 and do your block type check up there, where I added the comment.
     
    • Like Like x 1
  8. Well bro thanks for the help like: D