Solved If a player toggles sneak, allow him to get a "powerup" for X seconds...

Discussion in 'Plugin Development' started by treestompz, Feb 24, 2013.

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

    treestompz

    Hello everyone,

    I am trying to make a plugin that if a player toggles sneak (taps shift,) he can get a "powerup" with PlayerInteractEvent for X amount of time. For example, if the player taps sneak, he will enter into a powered up mode and be able to fire multiple snowballs (as opposed to only 1) for 5 seconds.

    Here is what I have been goofing around with:

    Did the player sneak?
    Code:
        @EventHandler
        public void onSneak(PlayerToggleSneakEvent event)
        {
            if(event.getPlayer().isSneaking())
            {
                isSneak = true;
                event.getPlayer().sendMessage("isSneak = true;");
            }
            if(!event.getPlayer().isSneaking())
            {
                isSneak = false;
                event.getPlayer().sendMessage("isSneak = false;");
            }
        }
    If the player tapped sneak, he can has the powerup for 5 seconds:

    Code:
    @EventHandler
    public void activatePowerupIfToggledSneak(PlayerInteractEvent event)
        {
            if(!isSneak)  /* isSneaking() returns false if the player is crouched..I think..
                                    this is where the main issue is, because as soon as a player unsneaks,
                                    this is set to true. At the same time I can't do a while(!isSneaking()) because
                                    I want just tapping sneak to activate this, not holding it down
            {
                //Activate a "powerup"
                if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getTypeId() == 332)
                {
                    Player player = event.getPlayer();
                    //some code to shoot 10 snowballs at once instead of 1
                }
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    public void run()
                    {               
                        //after 5 seconds disable this "powerup"
                    } }, 20 * 5);
            }
        }
        



    Any idea on how to go about this? I don't know if it is blatantly obvious and I am just really tired...I can't figure out how to make tapping sneak activate the ability without having to hold down sneak. I suppose I would also need some scheduler to allow the player to use this ability AGAIN after X seconds, but I can figure that out most likely.

    Thanks in advance!
     
  2. Offline

    Superckl1

    You're on the right track, but I wrote an example of this:
    Code:
        public static Map<String, Integer> sneaks = new HashMap<String, Integer>();
        public static Set<String> cantsneak = new HashSet<String>();
        @EventHandler
        public void onPlayerSneak(final PlayerToggleSneakEvent e){
            if(e.isSneaking()) return;
            if(sneaks.containsKey(e.getPlayer().getName())){
                Bukkit.getScheduler().cancelTask(sneaks.get(e.getPlayer().getName()));
                sneaks.remove(e.getPlayer().getName());
                e.getPlayer().sendMessage(ChatColor.GOLD+"Powerup deactivated!");
                cantsneak.add(e.getPlayer().getName());
                Bukkit.getScheduler().scheduleSyncDelayedTask(new Main(), new Runnable(){
                    @Override
                    public void run() {
                        Listeners.cantsneak.remove(e.getPlayer().getName());
                    }
                }, 100L);
                return;
            }
            if(cantsneak.contains(e.getPlayer().getName())){
                e.getPlayer().sendMessage(ChatColor.RED+"Your cooldown isnt done yet!");
                return;
            }
            sneaks.put(e.getPlayer().getName(), Bukkit.getScheduler().scheduleSyncDelayedTask(new Main(), new Runnable(){
                @Override
                public void run() {
                    Listeners.sneaks.remove(e.getPlayer().getName());
                    e.getPlayer().sendMessage(ChatColor.GOLD+"Powerup deactivated!");
                    cantsneak.add(e.getPlayer().getName());
                    Bukkit.getScheduler().scheduleSyncDelayedTask(new Main(), new Runnable(){
                        @Override
                        public void run() {
                            Listeners.cantsneak.remove(e.getPlayer().getName());
                        }
                    }, 100L);
                }
            }, 100L));
            e.getPlayer().sendMessage(ChatColor.GOLD+"Powerup activated!");
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            if(sneaks.containsKey(e.getPlayer().getName()) && e.getAction() == Action.RIGHT_CLICK_AIR && e.getPlayer().getItemInHand().getTypeId() == 332){
                for(int i=0;i<4;i++) e.getPlayer().throwSnowball();
            }
        }
    You can follow the process to see how it works. As far as I tested, it works just fine. I set the cool-down of the power-up to 5 seconds.
     
  3. Offline

    Tirelessly

    Don't use a runnable, capture system time with System.currentTimeMillis(), add to a map, compare.
     
  4. Offline

    Superckl1

    That would work too.
     
  5. Offline

    treestompz

    Superckl1 Wow! Thanks a ton man! Sorry for my delayed reply. I got 1 question I am going to PM you about if you don't mind.

    Tirelessly Why is it that you recommend System.currentTimeMillis()? Just wondering, I am new to Java and programming as a whole :)
     
  6. Offline

    Tirelessly

    Less overhead than creating a bunch of tasks.
     
Thread Status:
Not open for further replies.

Share This Page