package niket.ratna.listener; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.TextComponent; import niket.ratna.Ratna; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class DhoomAstraListener implements Listener { private static final long COOLDOWN_TIME = 20; // 40 seconds in ticks private static final long ACTION_BAR_REFRESH_TIME = 20; // 1 second in ticks private final Map cooldowns = new HashMap<>(); private Set dashAbilityPlayers = new HashSet<>(); @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); if (hasDhoomAstraItem(player)) { startActionBarTimer(player); } } @EventHandler public void onClick(final PlayerInteractEvent event) { if (event.getHand() != EquipmentSlot.HAND || event.getAction() != Action.RIGHT_CLICK_AIR) return; Player player = event.getPlayer(); ItemStack hand = player.getInventory().getItemInMainHand(); if (hand.hasItemMeta() && hand.getItemMeta().getDisplayName().equals(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Dhoom Astra")) { if (isOnCooldown(player)) { long remainingTime = getRemainingTime(player); player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Please wait, the Astra is still in cooldown"); } else { // Trigger dash ability triggerDash(player); // Set cooldown setCooldown(player); // Start the action bar timer startActionBarTimer(player); } } } @EventHandler public void onEntityDamage(EntityDamageEvent event) { if (event.getEntity() instanceof Player) { Player player = (Player) event.getEntity(); if (dashAbilityPlayers.contains(player)) { // Cancel fall damage for players with the dash ability if (event.getCause() == EntityDamageEvent.DamageCause.FALL) { event.setCancelled(true); } } } } private void triggerDash(Player player) { // Get the block location from where the player is standing Location blockLocation = player.getLocation().getBlock().getLocation(); // Implement dash ability with initial upward velocity Vector upwardVelocity = new Vector(0, 1, 0).multiply(2); player.setVelocity(upwardVelocity); // Add player to the set to track players with the dash ability dashAbilityPlayers.add(player); // Schedule a task to remove the player from the set after a delay Bukkit.getScheduler().runTaskLater(Ratna.getInstance(), () -> { dashAbilityPlayers.remove(player); }, 20 * 5); // Remove the player from the set after 5 seconds (adjust the time as needed) // Schedule a task to check for reaching 5 blocks height and apply downward velocity int taskId = Bukkit.getScheduler().runTaskTimer(Ratna.getInstance(), () -> { if (player.getLocation().getY() >= blockLocation.getY() + 5) { // After reaching 5 blocks height, apply downward velocity Vector downwardVelocity = new Vector(0, -1, 0).multiply(1.5); player.setVelocity(downwardVelocity); } }, 0, 1).getTaskId(); // Check every tick // Schedule a task to cancel the block height check after 3 seconds Bukkit.getScheduler().runTaskLater(Ratna.getInstance(), () -> { Bukkit.getScheduler().cancelTask(taskId); }, 20 * 3); // Cancel the task after 3 seconds // Schedule a task to check for entities in a 2-block radius upon landing Bukkit.getScheduler().runTaskLater(Ratna.getInstance(), () -> { checkForEntities(player); }, 20 * 2); // Delay of 2 seconds before checking for entities // Send a chat message with the block location information sendChatMessage(player, "You clicked Dhoom Astra at block location: " + blockLocation.getBlockX() + ", " + blockLocation.getBlockY() + ", " + blockLocation.getBlockZ()); // Spawn a particle circle around the block location spawnParticleCircle(blockLocation, 2.0, 20); } private void spawnParticleCircle(Location center, double radius, int points) { // Implementation of particle spawning remains unchanged // (Use the same spawnParticleCircle method as provided earlier) } private void sendChatMessage(Player player, String message) { player.sendMessage(ChatColor.GREEN + message); } private void checkForEntities(Player player) { Location playerLocation = player.getLocation(); double radius = 2.0; // Iterate through entities in the specified radius for (Entity entity : player.getNearbyEntities(radius, radius, radius)) { if (entity instanceof LivingEntity) { // Damage the entity LivingEntity livingEntity = (LivingEntity) entity; livingEntity.damage(5.0); // Adjust the damage value as needed } } // Make the player immune to fall damage player.setNoDamageTicks(40); // 2 seconds immunity (adjust ticks as needed) } private boolean hasDhoomAstraItem(Player player) { for (ItemStack item : player.getInventory().getContents()) { if (item != null && item.hasItemMeta() && item.getItemMeta().getDisplayName().equals(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Dhoom Astra")) { return true; } } return false; } private void startActionBarTimer(Player player) { Bukkit.getScheduler().runTaskTimer(Ratna.getInstance(), () -> { if (hasDhoomAstraItem(player) && isOnCooldown(player)) { long remainingTime = getRemainingTime(player); sendActionBar(player, ChatColor.WHITE + "" + "💎 " + ChatColor.YELLOW + formatTime(remainingTime) + "s"); } else { sendActionBar(player, ChatColor.WHITE + "💎 " + ChatColor.GREEN + "ready!"); } }, 0, ACTION_BAR_REFRESH_TIME); } private void sendActionBar(Player player, String message) { TextComponent textComponent = new TextComponent(message); player.spigot().sendMessage(net.md_5.bungee.api.ChatMessageType.ACTION_BAR, textComponent); } private boolean isOnCooldown(Player player) { return cooldowns.containsKey(player.getName()) && System.currentTimeMillis() < cooldowns.get(player.getName()); } private long getRemainingTime(Player player) { long remainingTime = cooldowns.get(player.getName()) - System.currentTimeMillis(); return Math.max(0, Math.round(remainingTime / 1000.0)); // Round to the nearest second } private void setCooldown(Player player) { long cooldownEnd = System.currentTimeMillis() + COOLDOWN_TIME; cooldowns.put(player.getName(), cooldownEnd); } private String formatTime(long time) { long minutes = time / 60; long seconds = time % 60; return String.format("%02d:%02d", minutes, seconds); } }