Semi Automatic Right-Click

Discussion in 'Resources' started by Freelix2000, Jun 8, 2014.

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

    Freelix2000

    I have noticed that a lot of gun plugins that include semi automatic weapons are often limited with their options of how to create a semi automatic weapon. Obviously, this is because PlayerInteractEvent is passed a million times as a player holds their right mouse button down. Some plugins add a delay so that it is more likely to be used in semi auto, but still fully automatic, and some plugin use left clicks for semi automatic but the problem with that is it displays the punching animation each time a player shoots and the player has to get used to different shooting controls for different weapons. So, while I was working on a mini-game plugin that does include a semi automatic weapon, I came up with a pretty simple solution that will detect right clicks semi-automatically. Here's the code:
    Code:
    public HashMap<UUID, Integer> ids = new HashMap<UUID, Integer>();
    @EventHandler
        public void shoot(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(p.getItemInHand().equals(Main.gun) && (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))){
                e.setCancelled(true);
                if(ids.containsKey(p.getUniqueId())){
                    Bukkit.getScheduler().cancelTask(ids.get(p.getUniqueId()));
                    final UUID uid = p.getUniqueId();
                    BukkitTask id = new BukkitRunnable(){
                        @Override
                        public void run(){
                            ids.remove(uid);
                        }
                    }.runTaskLater(pl, 7);
                    ids.put(uid, id.getTaskId());
                    return;
                }
                //Input shoot code here!
                final UUID uid = p.getUniqueId();
                BukkitTask id = new BukkitRunnable(){
                    @Override
                    public void run(){
                        ids.remove(uid);
                    }
                }.runTaskLater(pl, 7);
                ids.put(uid, id.getTaskId());
            }
        }
    This idea works by making a player wait 7 ticks before shooting after their first shot, but then resetting that time each time they try to shoot before 7 ticks. While this may seem like a very flawed and inconvenient method, it actually seems to work really well.
     
  2. Offline

    ResultStatic

    Freelix2000 try comparing system time instead of using task its more efficient
     
Thread Status:
Not open for further replies.

Share This Page