java bukkit double jump code example

Example: java bukkit double jump

import java.util.ArrayList;
import java.util.List;

import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;

public class DoubleJump implements Listener {

	private List<Player> doubleJump;
	
	public DoubleJump() {
		doubleJump = new ArrayList<>();
	}

	@EventHandler
	public void onJoin(PlayerJoinEvent event) {
		event.getPlayer().setAllowFlight(true);
	}

	@EventHandler
	public void onQuit(PlayerQuitEvent event) {
		doubleJump.remove(event.getPlayer());
	}

	@EventHandler
	public void onToggleFlight(PlayerToggleFlightEvent event) {
		Player player = event.getPlayer();
		GameMode gameMode = player.getGameMode();
		
		if(gameMode == GameMode.CREATIVE || gameMode == GameMode.SPECTATOR || player.isFlying()) {
			return;
		}

		event.setCancelled(true);
		doubleJump.add(player);

		player.setAllowFlight(false);
		player.setFlying(false);
		player.setVelocity(player.getLocation().getDirection().multiply(1.5).setY(1));
	}

	@EventHandler
	public void onHitGround(PlayerMoveEvent event) {
		Player player = event.getPlayer();

		if ((player.isOnGround() || event.getTo().getBlock().isLiquid()) && doubleJump.remove(player)) {
			player.setAllowFlight(true);
		}
	}
}

Tags:

Java Example