Solved Need help with timer

Discussion in 'Plugin Development' started by Arachiss, Nov 8, 2020.

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

    Arachiss

    Hello everyone!
    How do I get a timer to start when I click on an item?

    I need an example of how to make it so that when you constantly press the mouse button
    the chat displayed something like "you pressed the button (...) number of times"

    But if you stop at a certain number of clicks, and do not press the mouse button for more than 1 second, the click counter is fixed, and if you do the second click, the number of TNT corresponding to the number of mouse clicks will spawn.

    To paraphrase: I need something like my GIF

    It would be nice if the answer was code straight away. :D
     

    Attached Files:

  2. Offline

    Kars

    1. Catch PlayerInteractEvent or inventory move event or whichever one you mean by 'clicking the mouse'
    2. Have a HashMap<UUID, custom object with long and times clicked> -> save player id and a custom object containing time milliseconds of the last time they clicked and amount of times they have clicked.
    3. When they click, schedule a delayed task that compares the last time they clicked to the current time. If > 1 second, remove entry from HashMap and do what you have to do.
     
    Arachiss likes this.
  3. Offline

    Mathias Eklund

    I would personally create a task that gets executed and locks the counting of clicks with every click, and make it so that the next click cancels the task if it hasn't been processed yet.

    Just make a String list called "locked" or something that you add a players name to when the task is processed and stop the code if the list contains the players name before executing the click code etc.

    I'm currently on laptop and can't give you a real code example, but something like this is very easy to do.

    Interact Called > Check if "locked" contains the players name > proceed if it doesn't > check if the lock task exists and hasn't been processed yet > cancel the task > create a new lock task that will run in X ticks.
     
  4. Offline

    Arachiss

    What do you mean by "custom object with long and times clicked"? I'm new to bukkit, and doesn't know about how hashmaps work. And how do i can compare it to the current time?
     
  5. Offline

    Kars

    That would be neater but would require a scheduler, future and cancellation etc.. I figured that would be out of reach from the OP here.

    @Arachiss
    make a class called Something (name doesn't matter) and give it two properties and a constructor:
    • property long timeMillis;
    • property int timesClicked;
    • Constructor with timeMillis as only argument
    Make a HashMap<UUID, Something> (Something being the name/type of your newly made class) and call it hashmap (or something)
    https://www.w3schools.com/java/java_hashmap.asp

    • hashmap.put(player.getUniqueId, new Something(System.currentTimeMillis());
    • hashmap.get(player.getUniqueId) will return your Something object. Just read out the properties and compare to the new System.currentTimeMillis
     
    Arachiss likes this.
  6. Offline

    Arachiss

    Okay, I still haven't figured out a word of this. Where should I write all this? Yes, I'm dumb. I guess after all this I will never make any plugins.

    I have that code in the Main class:

    Code:
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Main extends JavaPlugin implements Listener
    {
        int count;
      
        public Main() {
            this.count = 0;
        }
      
        public void onEnable() {
        this.getLogger().info("Cringe was loaded.");
            this.getServer().getPluginManager().registerEvents((Listener)this, (Plugin)this);
            this.getConfig().addDefault("use-permission", (Object)"item.use");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
      
        public void onDisable() {
        this.getLogger().info("Cringe was unloaded.");
    }
      
        @EventHandler
        public void onClick(final PlayerInteractEvent e) {
            final Player p = e.getPlayer();       
            final ItemStack i = p.getItemInHand();
            final Material m = i.getType();
          
            if (m == Material.LEVER && (e.getAction() == Action.RIGHT_CLICK_AIR && p.hasPermission(this.getConfig().getString("use-permission"))))
            {
                    ++this.count; 
                    p.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "You clicked the button " + count + " times.");
                    }
            }
    }
     
  7. Offline

    Kars

  8. Offline

    Arachiss

    @Kars I created a class, and put the hashmap and properties into it. But how and where do i put the "hashmap.put(player.getUniqueId, new Something(System.currentTimeMillis());"? When i try to paste it somewhere, after a compile and test, I get an error saying "Abnormal plugin type". Sorry for stupid questions...
     
  9. Offline

    Kars

    @Arachiss you don't put the HashMap in the class. The class is simply a container for the two properties (timeMillis and timesClicked).

    From your Main, you can now create instances of the class Something (or whatever you called it).
    Create a hashmap like so:
    PHP:
    HashMap<UUIDSomethinghashmap = new HashMap<UUIDSomething>();
    You can now add instances of Something (or whatever you called it) into the hashmap.
    PHP:
    Something something = new Something(System.currentTimeMillis());
    hashmap.put(player.getUniqueId(), something);

    // above is the same as

    hashmap.put(player.getUniqueId(), new Something(System.currentTimeMillis()));
    If you still don't get it i suggest you brush up on your basic Java.
     
  10. Offline

    jonthe445

    @Arachiss
    You created the class, great! That is the first step, what the thread is suggesting is you then USE your newly formed class as part of the hash map in Main. The class can be simple and should look like the following. NOTE: The hashmap is NOT in the class for my 'timeContainer'

    Code:
    public class timeContainer {
       
        Private long timeMillis;
        Private int timesClicked;
    
       public timeContainer(long tm) {
            this.timeMillis = tm;
            this.timesClicked = 1;
       }
    
    //getters and setters
    
    }
    
    

    Think of your newly created class as a special container to hold two things, timeMillis and timesClicked.

    In Main you could use
    Map<UUID, timeContainer> myNewMap = new HashMap<UUID,timeContainer>();
    //Use myNewMap #put or myNewMap #get to access the map
    // then use the instance of timeContainer with your getters to access the two variable.

    Hope this helps explain it a bit better!
     
    Last edited: Nov 18, 2020
  11. Offline

    Arachiss

    @jonthe445
    I already got it, lol.
    If you want to help - give the complete code.
     
  12. Offline

    Kars

    You mean do your work for you?
     
  13. Offline

    Arachiss

    @Kars If you think it's that easy, then - yes.
    Also, this thread is dead.
     
  14. Offline

    Kars

    It is easy. You just need to learn basic Java. Spoonfeeding is not going to help in this case.
     
Thread Status:
Not open for further replies.

Share This Page