Issue with Relogging

Discussion in 'Plugin Development' started by Aegislock, Jul 17, 2024.

  1. Offline

    Aegislock

    Code:
    Code:
    Main Class:
    package me.aegislock.bettermovement;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class BetterMovement extends JavaPlugin {
    
        @Override
        public void onEnable() {
            // Plugin startup logic
            BetterMovementListener movementListener = new BetterMovementListener();
            BetterMovementCommand command = new BetterMovementCommand(movementListener);
            BetterMovementAntiCommand antiCommand = new BetterMovementAntiCommand(movementListener);
    
            getServer().getPluginManager().registerEvents(movementListener, this);
            getCommand("bettermovement").setExecutor(command);
            getCommand("bettermovementstop").setExecutor(antiCommand);
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
            for (Player player : Bukkit.getOnlinePlayers()) {
                player.setAllowFlight(false);
            }
        }
    
        public static BetterMovement getInstance() {
            return getPlugin(BetterMovement.class);
        }
    }
    
    Listener:
    package me.aegislock.bettermovement;
    
    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.boss.BarColor;
    import org.bukkit.boss.BarStyle;
    import org.bukkit.boss.BossBar;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.*;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;
    
    import java.util.*;
    
    public class BetterMovementListener implements Listener {
        private boolean isActive = false;
        private Set<UUID> dashCooldown = new HashSet<>();
        private Set<UUID> dashing = new HashSet<>();
        private Map<UUID, BossBar> sprintBars = new HashMap<>();
        @EventHandler
        public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
            if (!isActive) {
                return;
            }
            if (dashCooldown.contains(event.getPlayer().getUniqueId())) {
                return;
            }
            if (dashing.contains(event.getPlayer().getUniqueId())) {
                return;
            }
            if (event.getPlayer().getGameMode() == GameMode.CREATIVE || event.getPlayer().getGameMode() == GameMode.SPECTATOR) {
                return;
            }
            if (event.getPlayer().getGameMode() == GameMode.SURVIVAL) {
                event.getPlayer().setAllowFlight(true);
            }
            event.setCancelled(true);
            Player player = event.getPlayer();
            UUID playerUUID = player.getUniqueId();
    
            Vector dashDirection = player.getLocation().getDirection();
            player.setVelocity(dashDirection.multiply(2.0));
            dashCooldown.add(playerUUID);
            dashing.add(playerUUID);
            Bukkit.getLogger().info("Player added to dashing");
            player.setAllowFlight(false);
    
            sprintBars.get(player.getUniqueId()).setProgress(0.0);
    
            BukkitRunnable flightTask = new BukkitRunnable() {
                @Override
                public void run() {
                    player.getServer().getScheduler().runTask(BetterMovement.getInstance(), () -> {
                        dashCooldown.remove(playerUUID);
                        player.setAllowFlight(true);
                        this.cancel();
                    });
                }
            };
    
            flightTask.runTaskLater(BetterMovement.getInstance(), 100L);
    
            BukkitRunnable sprintTask = new BukkitRunnable() {
                @Override
                public void run() {
                    // Ensure operations on player are done on the main thread
                    player.getServer().getScheduler().runTask(BetterMovement.getInstance(), () -> {
                        double currentSprint = sprintBars.get(player.getUniqueId()).getProgress();
                        double newSprint = currentSprint + 0.008333333333;
                        if (newSprint > 1.0) {
                            newSprint = 1.0;
                            this.cancel(); //
                        }
                        BossBar playerSprint = sprintBars.get(player.getUniqueId());
                        playerSprint.setProgress(newSprint);
                    });
                }
            };
            sprintTask.runTaskTimer(BetterMovement.getInstance(), 0L, 1L); // 20 ticks = 1 second
    
            BukkitRunnable noFall = new BukkitRunnable() {
                @Override
                public void run() {
                    player.getServer().getScheduler().runTask(BetterMovement.getInstance(), () -> {
                        if (player.isOnGround()) {
                            dashing.remove(player.getUniqueId());
                        }
                    });
                }
            };
            noFall.runTaskTimer(BetterMovement.getInstance(), 0L, 1L);
        }
        @EventHandler
        public void onPlayerDamageEvent(EntityDamageEvent event) {
            if (event.getEntity() instanceof Player && event.getCause() == EntityDamageEvent.DamageCause.FALL) {
                Player player = (Player) event.getEntity();
                UUID playerID = player.getUniqueId();
                if (dashing.contains(playerID)) {
                    event.setCancelled(true);
                    Bukkit.getLogger().info("Player " + player.getName() + " took fall damage but is dashing, damage cancelled.");
                }
            }
        }
        @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent event) {
            // Remove player from cooldown and dashing sets on respawn
            UUID playerUUID = event.getPlayer().getUniqueId();
            dashCooldown.remove(playerUUID);
            dashing.remove(playerUUID);
        }
        @EventHandler
        public void onPlayerLogOut(PlayerQuitEvent event) {
            Player logoutPlayer = event.getPlayer();
            UUID logoutPlayerID = logoutPlayer.getUniqueId();
            dashCooldown.remove(logoutPlayerID);
            dashing.remove(logoutPlayerID);
        }
        @EventHandler
        public void onPlayerLogin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            player.setAllowFlight(true);
    
            // Create a new BossBar if it doesn't exist for the player
            if (!sprintBars.containsKey(player.getUniqueId())) {
                BossBar bossBar = Bukkit.createBossBar("Dash", BarColor.BLUE, BarStyle.SOLID);
                bossBar.setProgress(1.0f);
                bossBar.addPlayer(player);
                this.sprintBars.put(player.getUniqueId(), bossBar);
            } else {
                // Add the player to the existing BossBar
                sprintBars.get(player.getUniqueId()).addPlayer(player);
            }
        }
    
        public void makeActive() {
            this.isActive = true;
        }
        public void makeInactive() {
            this.isActive = false;
        }
        public void addSprint(UUID playerID, BossBar sprintBar) {
            this.sprintBars.put(playerID, sprintBar);
        }
        public void removeSprint(UUID playerID) {
            this.sprintBars.remove(playerID);
        }
    }
    
    
    Problem: I'm trying to make a dash mechanic that cancels out fall damage. On first enabling of the plugin, the fall damage cancelling works, however, when the player logs out and logs back in, the dash still works but the fall damage is not cancelled. I've confirmed with print statements that the onPlayerDamageEvent is still firing after relogging, and that the player is being added to the "dashing" set. I'm not sure what could be wrong.
     

Share This Page