Best way to charge

Discussion in 'Plugin Development' started by 97WaterPolo, Dec 11, 2014.

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

    97WaterPolo

    So I have been thinking up of some ideas for some random things to make, and one is a charge of a sorts. Basically the longer you hold down the right click button on the mouse, the stronger something happens. What would be the best way to detect and handle constant right-clicking? I am assuming one would use PlayerInteractEvent, but I am not sure how to track/handle constant right clicking. Any ideas would be greatly appreciated.
     
  2. Offline

    teej107

    AFAIK, PlayerInteractEvent fires constantly (I heard it's every 4 ticks, don't hold me to that) when you perform an interaction. Just increment a number when a player holds down right mouse button and when the number reaches a certain point, do something.
     
  3. Offline

    97WaterPolo

    @teej107

    I gave it a bit more thought, thoughts on this?

    PHP:
        List<Stringcharge = new ArrayList<String>();
        @
    EventHandler
        
    public void onInteract(PlayerInteractEvent event){
            final 
    Player player event.getPlayer();
            if (
    event.getAction() != Action.RIGHT_CLICK_AIR || event.getAction() != Action.RIGHT_CLICK_BLOCK)
                return;
            if (
    player.getItemInHand().getType().toString().contains("SWORD") && !charge.contains(player.getUniqueId().toString())){
                
    charge.add(player.getUniqueId().toString());
                new 
    BukkitRunnable()
                {
                    
    int counter 0;
                    @
    Override
                    
    public void run()
                    {
                        if (
    player.isBlocking()){
                            
    counter++;
                        }else{
                            
    Bukkit.broadcastMessage("You blocked for: " counter/2);
                            
    charge.remove(player.getUniqueId().toString());
                            
    this.cancel();
                        }
                       
                    }
                }.
    runTaskTimer(this010);
            }
        }
     
  4. Offline

    teej107

    Yes I do.
    • Why are you converting a UUID to a String? Just use a UUID for the type parameter.
    • You don't need to use a Bukkit Runnable. Worst comes to worst, you can schedule a delayed task for the check.
    • Also the player's item in hand can return null.
     
  5. Offline

    97WaterPolo

    @teej107
    1. I personally like to convert UUID's to strings, but either would work.
    2. While you can just increase it each time, there is no real way to check when they stop blocking, or is what you meant add a delay after the event and see if they aren't blocking?
    3. Thanks, forgot about that.
     
  6. @97WaterPolo
    1. The UUID -> String has no real advantages, but does have a few disadvantages including wasted resources and reduces readability. Unless you genuinely have a reason other than "I like it" I really suggest that you don't do it :)
    2. Yeah, I think that's what he means, a delayed task. Personally I think the timer is fine for this, since the alternative of a delayed task would end up with a lot more tasks (1 every 4 ticks?)
     
    97WaterPolo and teej107 like this.
Thread Status:
Not open for further replies.

Share This Page