1.14.4 First click: code 1, second click: code 2

Discussion in 'Spigot Plugin Development' started by eralysium, Jan 26, 2020.

  1. I would like that when I click on an object, the first time, it does code 1, and if I click on that item again, it does code 2, and if I click on it a third time, it does code 1, a fourth time, code 2, etc. If I click a third time, it does code 1, a fourth time, code 2, and so on.

    Code (Text):
        public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        First click:
            make 1
       Second click
           make 2
    I had made a system with a yml config, but it's not very good I think.

    Code (Text):
        if (main.getConfig().getBoolean((player.getName() + ".actionbar"), true)){
            main.getConfig().set(player.getName() + ".actionbar", false);
            player.sendMessage("§cLa distance n'est plus affichée.");
         } else {
            main.getConfig().set(player.getName() + ".actionbar", true);
            player.sendMessage("§aLa distance est affichée.");
    I want to make it clear that I'm a beginner :)
  2. Use a Map. player -> clicks of player
  3. Doesn't even have to be an int, could also be a boolean
    Code (Java):
    Map<Player,Boolean> clicks = new WeakHashMap<>();

    final Boolean value = clicks.get(player);
    if (value == null || value) {
        clicks.put(player, false);
        // code 1
    } else {
       clicks.put(player, true);
       // code 2
  4. Or even better, have a HashSet and add/remove them.
  5. Insertion should always have a higher cost (at least O(n) ) than getting an item (Hopefully O(log n), if not even O(1) for special cases), so I doubt it.
  6. I could never understand what does that O(something) mean (Probably didn't get to it yet in math class), do you mind explaning or linking a resource?
  7. Don't have a resource at hand, unfortunately... This -> O(something) is called the Big O notation and denotes the complexity of a certain algorithm. It's a worst-case scenario and explains "how much time" is needed. I put that in quotes because there's not an actual way of computing a real time-unit like 5 seconds or something out of that. It just denotes the time a certain operation takes if there is a lot of data to be considered. Again, magic number; cannot be expressed in means of byte or anything. how much time will it take to perform a certain algorithm?
    Suppose an ArrayList. If you want to get an element at position i, you will always only have to do one step. Obviously, if you look closer, it will be more than one step; for example, range-checking has to be performed and such. But this actually doesn't matter on a big scale; it's just an approximate measure. Now take an insertion on the other hand. If you want to insert an element to an ArrayList, you might have to increase the Capacity (the underlying array) and therefore might have to copy an array of size n. So the complexity increases as n increases.
    Now take an algorithm that changes every block in a chunk if that block is of type stone. This algorithm would loop over 3 loops, resulting in a complexity of O(N^3).
    Hope, that made it a little clearer :giggle:
    • Informative Informative x 1