Not getting the string that I want

Discussion in 'Spigot Plugin Development' started by Ghose, May 17, 2016.

Thread Status:
Not open for further replies.
  1. Ok so basically I'm currently attempting to make a accurate cps detection. Basically a problem I am encountering is I don't receive the actual wanted string/ Im stuck and idk what to do.

    Code (Text):
    package me.ghose.sprint.event;


    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    import me.ghose.sprint.Sprint;

    public class Playerleftclick implements Listener {

        @EventHandler
        public void onLeftClick(PlayerInteractEvent event){
           
            Action action = event.getAction();
           
           
            if(action == Action.LEFT_CLICK_AIR){
               
                Player player = event.getPlayer();
               
                long log = System.currentTimeMillis();
               
                           
                Sprint.clickms.put(player.getName(), log);  
               
                long lastclick = Sprint.clickms.get(player.getName());
               
                String clickms = Long.toString(log - lastclick);
               
                player.sendMessage(clickms);
            }
        }
    }
     
  2. Sprint.clickms.put(player.getName(), log);
    long lastclick = Sprint.clickms.get(player.getName());

    What looks wrong here?
     
    • Like Like x 1
  3. So what are you getting?
    What are you expecting?
     
  4. Im expecting the delay between 2 clicks
     
  5. Ok, that's the second question answered.

    What are you getting instead? debugs? outputs? stack traces?
     
    • Useful Useful x 1
  6. Except you're overriding the last value before you even use it.
     
    • Agree Agree x 1
  7. Oh i'm actually dumb I save it and just load it right after, but then I'm stuck like should I put it after or something?
     
  8. I'm getting 0 every single time I left click
     
  9. Oh there are two lines you need to switch around...

    Code (Text):
    Sprint.clickms.put(player.getName(), log);   //write new time to variable
             
    long lastclick = Sprint.clickms.get(player.getName()); //lastclick now contains this very moment...
     
  10. Tried it now I'm not getting anything in chat
     
  11. That's generally not supposed to break chat... (unless that's what happens when it works :p)

    How about placing

    Code (Text):
     Sprint.clickms.put(player.getName(), log);  
    under your sendMessage code.

    Pretty much what is happening is "log" and "lastclick" are the same value for you to be getting 0 as a result

    This indicates you are not storing "lastclick" correctly.

    put a whole heap of

    Code (Text):
    System.out.Print("Lastclick= " + lastclick + "\nLog= " + log);
    all over the joint. That way you can see how data is being used
     
  12. Oh I just looked and im getting errors everytime I click
     
  13. What are those errors? it would be greatly appreciated :)
     

  14.  
  15. Code (Text):
    long lastclick = Sprint.clickms.get(player.getName());
    This is what I have on my line 26
     
  16. Another technique to hell you break down your coding is to do inline comments.
    I know it kinda wastes time but it really helps *especially* when returning to old code...

    Code (Text):
             Player player = event.getPlayer(); //Get the player
         
             long log = System.currentTimeMillis();//get the current time
       
                   
             Sprint.clickms.put(player.getName(), log); //store the current time
       
             long lastclick = Sprint.clickms.get(player.getName()); //retrieve the current time
       
             String clickms = Long.toString(log - lastclick); //get the difference between the current time....and the current time
       
             player.sendMessage(clickms); //Tell the user that x - x = 0


    So you're getting an NPE.... because that value doesn't exist.... try initialising with like -1

    Then say that
    Code (Text):

    If left click{
        IF (currentTime == -1){ //user has not done a previous click since last log on

            set current time in hash, but don't process further.
        }
        ELSE
        {
           //original leftclick code
        }

    }
     
    #16 Emxm3, May 17, 2016
    Last edited: May 17, 2016
  17. clickms is null. That's the only thing that can be null on that line.
     
    • Agree Agree x 1
  18. Aha, only just saw this.

    That's pretty much it. It just needs to be handled so that the *very first time* it is used, it is initialised so that it is never null afterwards.


    I'm assuming clickms is some kind of HashMap?


    Code (Text):
    If (clickms.containsKey(player)) //entry in hashmap found
    {
    //do your code
    } else { //Entry isn't found.
    clickms.put(player,currenttime); //Initialise with current time.
    }
     
    #18 Emxm3, May 17, 2016
    Last edited: May 17, 2016
  19. Yes Clickms is a hashmap that stores player name and Long data
     
  20. I'm sort of confused could you like type all of what you just said in like my code the proper way to do it ?
     
Thread Status:
Not open for further replies.