Toggle on/off with command HELP!

Discussion in 'Plugin Development' started by PeterXonwiiXx, Nov 21, 2015.

Thread Status:
Not open for further replies.
  1. i made a plugin but i want when a player type a command it will turn on,and if he type it again it will turn off... but the problem is that i know how to toggle but its a
    @EventHandler
    public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {

    event..and i cant figure out how it works...
     
  2. Offline

    Xerox262

    Code:
    if sender is player
      if player can fly
        disable fly
      else
        enable fly
    There, toggle flight command
     
  3. can i pm my class?
    would be easier for you to see what i make
     
  4. Offline

    Xerox262

    Why pm it? Post it here, trust me, no one is going to steal your original idea of a toggle fly command.
     
    PeterXonwiiXx likes this.
  5. haha i know but still tho :p

    Code:
        @EventHandler
        public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
            final Player player = event.getPlayer();
    
            if (player.getGameMode().equals(GameMode.CREATIVE))
                return;
            if (!(player.hasPermission("dj.doublejump")))
                return;
            if (cooldownTime.containsKey(player.getName())) {
                event.setCancelled(true);
                return;
            }
    
            event.setCancelled(true);
            player.setAllowFlight(true);
            player.setFlying(false);
            player.setVelocity(player.getLocation().getDirection().multiply(1.5).setY(1));
            event.getPlayer().setNoDamageTicks(100);
    
            cooldownTime.put(player.getName(), getConfig().getInt("cooldown") * 20);
            if (getConfig().getBoolean("messageadd"))
                player.sendMessage(format(getConfig().getString("cooldownadd")));
            player.setAllowFlight(false);
    
            cooldownTask.put(player.getName(), Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                public void run() {
                    int time = cooldownTime.get(player.getName());
                    time -= 1;
                    if (time <= 0) {
                        cooldownTime.remove(player.getName());
                        if (getConfig().getBoolean("messageremove")) {
                            player.sendMessage(format(getConfig().getString("cooldownremove")));
                        }
                        BukkitTask task = cooldownTask.get(player.getName());
                        cooldownTask.remove(player.getName());
                        task.cancel();
                    } else {
                        cooldownTime.put(player.getName(), time);
                    }
                }
            }, 1l, 1l));
    here ya go,its not like you think it it,it has more in the class itself :p but this is the part where it has to be
     
    Last edited by a moderator: Nov 21, 2015
  6. Offline

    Zombie_Striker

    @PeterXonwiiXx
    Instead of creating a variable, and then going through THREE checks to see if you can even use it, check first, and then create the player variable.

    Why do you give the player flight, and then NANOSECONDS later you take it away?

    Instead of -=1; just use --;

    Why are you going to repeat that task every tick?
     
  7. its for double jump plugin when a player has permission,what i have here works fine...i only need something that i can toggle this in a command
     
  8. Offline

    Xerox262

    Create a collection of the players that are exempt from double jumping, in the command add/remove them depending on what they need and in the event check if they're in the collection.
     
Thread Status:
Not open for further replies.

Share This Page