Solved Cancel a task started from a command, on PlayerMove event.

Discussion in 'Plugin Development' started by Manole7, Jun 7, 2022.

  1. Offline

    Manole7

    Hey,

    Title says it all. I want to do /<cmd> and have a cooldown till it's done n then player teleports to a place. What I do is the following:

    Making a map to store UUID of player and task:
    Code:
    Map<UUID, BukkitTask> tasks = new HashMap<>();
    In the command I got this:
    Code:
    if (!tasks.containsKey(p.getUniqueId())) {
                            tasks.put(p.getUniqueId(), (new BukkitRunnable() {
                                public void run() {
                                    p.teleport(loc);
                                    }
                            }).runTaskLater(plugin, 20L * plugin.getConfig().getInt("Cooldown")));
                            return true;
                        }
    And finally in the Move event I got this:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            Player p = e.getPlayer();
            int taskId = tasks.get(p.getUniqueId()).getTaskId();
    
            if (tasks.containsKey(p.getUniqueId())) {
                if (e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockY() != e.getTo().getBlockY() ||
                        e.getFrom().getBlockZ() != e.getTo().getBlockZ()) {
                    if (plugin.getConfig().getBoolean("Cancel-On-Move.Enabled") && tasks != null) {
                        Bukkit.getScheduler().cancelTask(taskId);
                        tasks.remove(p.getUniqueId());
                    }
                }
            }
        }
    Any ideas? I've been stuck over it 'n been looking up online for help but none seems to help my case, or at least that's what I think

    Cheers!
     
  2. Online

    timtower Administrator Administrator Moderator

    @Manole7 Can you post the full classes?
     
  3. Offline

    Manole7

    I have it all in the command class so I can get it to work and then split the event in a different one.

    @timtower

    Command:
    Code:
    if (command.getName().equalsIgnoreCase("spawn"))
                if (args.length == 0) {
                    if (spawnCoords.getConfig().getConfigurationSection("spawn") == null) {
                        p.sendMessage(
                                ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Spawn-Message")));
                        return true;
                    }
                    if (plugin.getConfig().getBoolean("Teleport-Message.Enabled"))
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Teleport-Message.Message")));
                    World w = Bukkit.getServer().getWorld(spawnCoords.getConfig().getString("spawn.world"));
                    double x = spawnCoords.getConfig().getDouble("spawn.x");
                    double y = spawnCoords.getConfig().getDouble("spawn.y");
                    double z = spawnCoords.getConfig().getDouble("spawn.z");
                    float yaw = (float)spawnCoords.getConfig().getDouble("spawn.yaw");
                    float pitch = (float)spawnCoords.getConfig().getDouble("spawn.pitch");
                    final Location loc = new Location(w, x, y, z, yaw, pitch);
                    if (!p.hasPermission("spawn.bypass")) {
                        if (!tasks.containsKey(p.getUniqueId())) {
                            tasks.put(p.getUniqueId(), (new BukkitRunnable() {
                                public void run() {
                                        p.teleport(loc);
                                        if (plugin.getConfig().getBoolean("Spawn-Message.Enabled"))
                                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                                    plugin.getConfig().getString("Spawn-Message.Message")));
                                        if (plugin.getConfig().getBoolean("Spawn-Effect.Enabled")) {
                                            p.getWorld().playEffect(p.getLocation(),
                                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                    0);
                                            p.getWorld().playEffect(p.getLocation(),
                                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                    0);
                                            p.getWorld().playEffect(p.getLocation(),
                                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                    0);
                                        }
                                        if (plugin.getConfig().getBoolean("Spawn-Sound.Enabled"))
                                            p.getWorld().playSound(p.getLocation(),
                                                Sound.valueOf(plugin.getConfig().getString("Spawn-Sound.Sound-Name")),
                                                1.0F, 1.0F);
                                    tasks.remove(p.getUniqueId());
                                    }
                            }).runTaskLater(plugin, 20L * plugin.getConfig().getInt("Cooldown")));
                            return true;
                        }
    Event is exactly as shown above:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            Player p = e.getPlayer();
            int taskId = tasks.get(p.getUniqueId()).getTaskId();
    
            if (tasks.containsKey(p.getUniqueId())) {
                if (e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockY() != e.getTo().getBlockY() ||
                        e.getFrom().getBlockZ() != e.getTo().getBlockZ()) {
                    if (plugin.getConfig().getBoolean("Cancel-On-Move.Enabled") && tasks != null) {
                        Bukkit.getScheduler().cancelTask(taskId);
                        tasks.remove(p.getUniqueId());
                    }
                }
            }
        }
     
  4. Online

    timtower Administrator Administrator Moderator

    @Manole7 Still want the full class, not just methods.
    I expect that you have 2 instances of the class.
     
  5. Offline

    Manole7

    @timtower

    Code:
    package me.ij0hny.setspawn.Commands;
    
    import me.ij0hny.setspawn.Others.LocationManager;
    import me.ij0hny.setspawn.SetSpawn;
    import org.bukkit.*;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    
    import java.util.HashMap;
    import java.util.UUID;
    
    public class SpawnCmd implements CommandExecutor, Listener {
    
        private final SetSpawn plugin = SetSpawn.getPlugin(SetSpawn.class);
    
        public HashMap<UUID, BukkitTask> tasks = new HashMap<>();
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            if (!(sender instanceof Player)) {
                plugin.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Only players can execute commands.");
                return true;
            }
    
            Player p = (Player)sender;
            LocationManager spawnCoords = LocationManager.getManager();
    
            if (!p.hasPermission("spawn.spawn")) {
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Permission-Message")));
                return true;
            }
            if (command.getName().equalsIgnoreCase("spawn"))
                if (args.length == 0) {
                    if (spawnCoords.getConfig().getConfigurationSection("spawn") == null) {
                        p.sendMessage(
                                ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Spawn-Message")));
                        return true;
                    }
                    if (plugin.getConfig().getBoolean("Teleport-Message.Enabled"))
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Teleport-Message.Message")));
                    World w = Bukkit.getServer().getWorld(spawnCoords.getConfig().getString("spawn.world"));
                    double x = spawnCoords.getConfig().getDouble("spawn.x");
                    double y = spawnCoords.getConfig().getDouble("spawn.y");
                    double z = spawnCoords.getConfig().getDouble("spawn.z");
                    float yaw = (float)spawnCoords.getConfig().getDouble("spawn.yaw");
                    float pitch = (float)spawnCoords.getConfig().getDouble("spawn.pitch");
                    final Location loc = new Location(w, x, y, z, yaw, pitch);
                    if (!p.hasPermission("spawn.bypass")) {
                        if (!tasks.containsKey(p.getUniqueId())) {
                            tasks.put(p.getUniqueId(), (new BukkitRunnable() {
                                public void run() {
                                        p.teleport(loc);
                                        if (plugin.getConfig().getBoolean("Spawn-Message.Enabled"))
                                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                                    plugin.getConfig().getString("Spawn-Message.Message")));
                                        if (plugin.getConfig().getBoolean("Spawn-Effect.Enabled")) {
                                            p.getWorld().playEffect(p.getLocation(),
                                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                    0);
                                            p.getWorld().playEffect(p.getLocation(),
                                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                    0);
                                            p.getWorld().playEffect(p.getLocation(),
                                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                    0);
                                        }
                                        if (plugin.getConfig().getBoolean("Spawn-Sound.Enabled"))
                                            p.getWorld().playSound(p.getLocation(),
                                                Sound.valueOf(plugin.getConfig().getString("Spawn-Sound.Sound-Name")),
                                                1.0F, 1.0F);
                                    tasks.remove(p.getUniqueId());
                                    }
                            }).runTaskLater(plugin, 20L * plugin.getConfig().getInt("Cooldown")));
                            return true;
                        }
                    } else {
                        p.teleport(loc);
                        if (plugin.getConfig().getBoolean("Spawn-Message.Enabled"))
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("Spawn-Message.Message")));
                        if (plugin.getConfig().getBoolean("Spawn-Effect.Enabled")) {
                            p.getWorld().playEffect(p.getLocation(),
                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                    0);
                            p.getWorld().playEffect(p.getLocation(),
                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                    0);
                            p.getWorld().playEffect(p.getLocation(),
                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                    0);
                        }
                        if (plugin.getConfig().getBoolean("Spawn-Sound.Enabled"))
                            p.getWorld().playSound(p.getLocation(),
                                    Sound.valueOf(plugin.getConfig().getString("Spawn-Sound.Sound-Name")),
                                    1.0F, 1.0F);
                    }
                } else if (args.length == 1) {
    
                    Player target = Bukkit.getServer().getPlayerExact(args[0]);
    
                    if (args[0].equalsIgnoreCase("set")) {
                        if (!p.hasPermission("spawn.set")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        spawnCoords.getConfig().set("spawn.world", p.getLocation().getWorld().getName());
                        spawnCoords.getConfig().set("spawn.x", Double.valueOf(p.getLocation().getX()));
                        spawnCoords.getConfig().set("spawn.y", Double.valueOf(p.getLocation().getY()));
                        spawnCoords.getConfig().set("spawn.z", Double.valueOf(p.getLocation().getZ()));
                        spawnCoords.getConfig().set("spawn.yaw", Float.valueOf(p.getLocation().getYaw()));
                        spawnCoords.getConfig().set("spawn.pitch", Float.valueOf(p.getLocation().getPitch()));
                        spawnCoords.saveConfig();
                        p.sendMessage(
                                ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Set-Spawn-Message")));
                        return true;
                    }
                    if (args[0].equals("setfirst")) {
                        if (!p.hasPermission("spawn.setfirst")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        spawnCoords.getConfig().set("firstspawn.world", p.getLocation().getWorld().getName());
                        spawnCoords.getConfig().set("firstspawn.x", Double.valueOf(p.getLocation().getX()));
                        spawnCoords.getConfig().set("firstspawn.y", Double.valueOf(p.getLocation().getY()));
                        spawnCoords.getConfig().set("firstspawn.z", Double.valueOf(p.getLocation().getZ()));
                        spawnCoords.getConfig().set("firstspawn.yaw", Float.valueOf(p.getLocation().getYaw()));
                        spawnCoords.getConfig().set("firstspawn.pitch", Float.valueOf(p.getLocation().getPitch()));
                        spawnCoords.saveConfig();
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Set-First-Join-Spawn-Message")));
                        return true;
                    }
                    if (args[0].equalsIgnoreCase("reload")) {
                        if (!p.hasPermission("spawn.reload")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        plugin.reloadConfig();
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Reload-Message")));
                    } else if (args[0].equalsIgnoreCase("help")) {
                        if (!p.hasPermission("spawn.help")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        p.sendMessage(ChatColor.AQUA + "--------------------" + ChatColor.WHITE + ">>SetSpawn<<" +
                                ChatColor.AQUA + "--------------------");
                        p.sendMessage(ChatColor.AQUA + "/spawn || Teleport to the spawn.");
                        p.sendMessage(ChatColor.AQUA + "/spawn set || Set the spawn location.");
                        p.sendMessage(ChatColor.AQUA + "/spawn reload || Reload the config.");
                        p.sendMessage(ChatColor.AQUA + "/spawn help || See help page.");
                        p.sendMessage(ChatColor.AQUA + "--------------------" + ChatColor.WHITE + ">>SetSpawn<<" +
                                ChatColor.AQUA + "--------------------");
                    } else if (p.hasPermission("spawn.others")) {
                        if (target == null) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Force-Spawn.Player-Not-Online-Message")));
                        } else {
                            if (spawnCoords.getConfig().getConfigurationSection("spawn") == null) {
                                p.sendMessage(
                                        ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Spawn-Message")));
                                return true;
                            } else {
                                World w = Bukkit.getServer().getWorld(spawnCoords.getConfig().getString("spawn.world"));
                                double x = spawnCoords.getConfig().getDouble("spawn.x");
                                double y = spawnCoords.getConfig().getDouble("spawn.y");
                                double z = spawnCoords.getConfig().getDouble("spawn.z");
                                float yaw = (float) spawnCoords.getConfig().getDouble("spawn.yaw");
                                float pitch = (float) spawnCoords.getConfig().getDouble("spawn.pitch");
                                final Location loc = new Location(w, x, y, z, yaw, pitch);
                                target.teleport(loc);
                                if (plugin.getConfig().getBoolean("Force-Spawn.Enabled-message-target")) {
                                    target.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Force-Spawn.Message-target").replace("%sendername%", p.getDisplayName())));
                                }
                                if (plugin.getConfig().getBoolean("Force-Spawn.Enabled-message-sender")) {
                                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Force-Spawn.Message-sender").replace("%targetname%", target.getDisplayName())));
                                }
                            }
                        }
                    } else {
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("No-Permission-Message")));
                        return true;
                    }
                }
            return true;
        }
    
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            Player p = e.getPlayer();
            int taskId = tasks.get(p.getUniqueId()).getTaskId();
    
            if (tasks.containsKey(p.getUniqueId())) {
                if (e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockY() != e.getTo().getBlockY() ||
                        e.getFrom().getBlockZ() != e.getTo().getBlockZ()) {
                    if (plugin.getConfig().getBoolean("Cancel-On-Move.Enabled") && tasks != null) {
                        Bukkit.getScheduler().cancelTask(taskId);
                        tasks.remove(p.getUniqueId());
                    }
                }
            }
        }
     
  6. Online

    timtower Administrator Administrator Moderator

    @Manole7 Why do you keep sending separate methods?
    If I ask for the full class I expect a single file.
    How many instances of this class do you make?
     
  7. Offline

    Manole7

    @timtower Sorry about the double code boxes. As of instances you mean when I register it to my main class? EDIT: Also, a mistake to begin with is that I try to get the int right away on Move event which spams HashMap.Object nullexceptions in my console. I moved it below the if tasks != null.

    Code:
    package me.ij0hny.setspawn.Commands;
    
    import me.ij0hny.setspawn.Others.LocationManager;
    import me.ij0hny.setspawn.SetSpawn;
    import org.bukkit.*;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    
    import java.util.HashMap;
    import java.util.UUID;
    
    public class SpawnCmd implements CommandExecutor, Listener {
    
        private final SetSpawn plugin = SetSpawn.getPlugin(SetSpawn.class);
    
        public HashMap<UUID, BukkitTask> tasks = new HashMap<>();
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            if (!(sender instanceof Player)) {
                plugin.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Only players can execute commands.");
                return true;
            }
    
            Player p = (Player) sender;
            LocationManager spawnCoords = LocationManager.getManager();
    
            if (!p.hasPermission("spawn.spawn")) {
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Permission-Message")));
                return true;
            }
            if (command.getName().equalsIgnoreCase("spawn"))
                if (args.length == 0) {
                    if (spawnCoords.getConfig().getConfigurationSection("spawn") == null) {
                        p.sendMessage(
                                ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Spawn-Message")));
                        return true;
                    }
                    if (plugin.getConfig().getBoolean("Teleport-Message.Enabled"))
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Teleport-Message.Message")));
                    World w = Bukkit.getServer().getWorld(spawnCoords.getConfig().getString("spawn.world"));
                    double x = spawnCoords.getConfig().getDouble("spawn.x");
                    double y = spawnCoords.getConfig().getDouble("spawn.y");
                    double z = spawnCoords.getConfig().getDouble("spawn.z");
                    float yaw = (float)spawnCoords.getConfig().getDouble("spawn.yaw");
                    float pitch = (float)spawnCoords.getConfig().getDouble("spawn.pitch");
                    final Location loc = new Location(w, x, y, z, yaw, pitch);
                    if (!p.hasPermission("spawn.bypass")) {
                        if (!tasks.containsKey(p.getUniqueId())) {
                            tasks.put(p.getUniqueId(), (new BukkitRunnable() {
                                public void run() {
                                    p.teleport(loc);
                                    if (plugin.getConfig().getBoolean("Spawn-Message.Enabled"))
                                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                                plugin.getConfig().getString("Spawn-Message.Message")));
                                    if (plugin.getConfig().getBoolean("Spawn-Effect.Enabled")) {
                                        p.getWorld().playEffect(p.getLocation(),
                                                Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                0);
                                        p.getWorld().playEffect(p.getLocation(),
                                                Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                0);
                                        p.getWorld().playEffect(p.getLocation(),
                                                Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                                0);
                                    }
                                    if (plugin.getConfig().getBoolean("Spawn-Sound.Enabled"))
                                        p.getWorld().playSound(p.getLocation(),
                                                Sound.valueOf(plugin.getConfig().getString("Spawn-Sound.Sound-Name")),
                                                1.0F, 1.0F);
                                    tasks.remove(p.getUniqueId());
                                }
                            }).runTaskLater(plugin, 20L * plugin.getConfig().getInt("Cooldown")));
                            return true;
                        }
                    } else {
                        p.teleport(loc);
                        if (plugin.getConfig().getBoolean("Spawn-Message.Enabled"))
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("Spawn-Message.Message")));
                        if (plugin.getConfig().getBoolean("Spawn-Effect.Enabled")) {
                            p.getWorld().playEffect(p.getLocation(),
                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                    0);
                            p.getWorld().playEffect(p.getLocation(),
                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                    0);
                            p.getWorld().playEffect(p.getLocation(),
                                    Effect.valueOf(plugin.getConfig().getString("Spawn-Effect.Effect-Name")),
                                    0);
                        }
                        if (plugin.getConfig().getBoolean("Spawn-Sound.Enabled"))
                            p.getWorld().playSound(p.getLocation(),
                                    Sound.valueOf(plugin.getConfig().getString("Spawn-Sound.Sound-Name")),
                                    1.0F, 1.0F);
                    }
                } else if (args.length == 1) {
    
                    Player target = Bukkit.getServer().getPlayerExact(args[0]);
    
                    if (args[0].equalsIgnoreCase("set")) {
                        if (!p.hasPermission("spawn.set")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        spawnCoords.getConfig().set("spawn.world", p.getLocation().getWorld().getName());
                        spawnCoords.getConfig().set("spawn.x", Double.valueOf(p.getLocation().getX()));
                        spawnCoords.getConfig().set("spawn.y", Double.valueOf(p.getLocation().getY()));
                        spawnCoords.getConfig().set("spawn.z", Double.valueOf(p.getLocation().getZ()));
                        spawnCoords.getConfig().set("spawn.yaw", Float.valueOf(p.getLocation().getYaw()));
                        spawnCoords.getConfig().set("spawn.pitch", Float.valueOf(p.getLocation().getPitch()));
                        spawnCoords.saveConfig();
                        p.sendMessage(
                                ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Set-Spawn-Message")));
                        return true;
                    }
                    if (args[0].equals("setfirst")) {
                        if (!p.hasPermission("spawn.setfirst")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        spawnCoords.getConfig().set("firstspawn.world", p.getLocation().getWorld().getName());
                        spawnCoords.getConfig().set("firstspawn.x", Double.valueOf(p.getLocation().getX()));
                        spawnCoords.getConfig().set("firstspawn.y", Double.valueOf(p.getLocation().getY()));
                        spawnCoords.getConfig().set("firstspawn.z", Double.valueOf(p.getLocation().getZ()));
                        spawnCoords.getConfig().set("firstspawn.yaw", Float.valueOf(p.getLocation().getYaw()));
                        spawnCoords.getConfig().set("firstspawn.pitch", Float.valueOf(p.getLocation().getPitch()));
                        spawnCoords.saveConfig();
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Set-First-Join-Spawn-Message")));
                        return true;
                    }
                    if (args[0].equalsIgnoreCase("reload")) {
                        if (!p.hasPermission("spawn.reload")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        plugin.reloadConfig();
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Reload-Message")));
                    } else if (args[0].equalsIgnoreCase("help")) {
                        if (!p.hasPermission("spawn.help")) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                    plugin.getConfig().getString("No-Permission-Message")));
                            return true;
                        }
                        p.sendMessage(ChatColor.AQUA + "--------------------" + ChatColor.WHITE + ">>SetSpawn<<" +
                                ChatColor.AQUA + "--------------------");
                        p.sendMessage(ChatColor.AQUA + "/spawn || Teleport to the spawn.");
                        p.sendMessage(ChatColor.AQUA + "/spawn set || Set the spawn location.");
                        p.sendMessage(ChatColor.AQUA + "/spawn reload || Reload the config.");
                        p.sendMessage(ChatColor.AQUA + "/spawn help || See help page.");
                        p.sendMessage(ChatColor.AQUA + "--------------------" + ChatColor.WHITE + ">>SetSpawn<<" +
                                ChatColor.AQUA + "--------------------");
                    } else if (p.hasPermission("spawn.others")) {
                        if (target == null) {
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Force-Spawn.Player-Not-Online-Message")));
                        } else {
                            if (spawnCoords.getConfig().getConfigurationSection("spawn") == null) {
                                p.sendMessage(
                                        ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Spawn-Message")));
                                return true;
                            } else {
                                World w = Bukkit.getServer().getWorld(spawnCoords.getConfig().getString("spawn.world"));
                                double x = spawnCoords.getConfig().getDouble("spawn.x");
                                double y = spawnCoords.getConfig().getDouble("spawn.y");
                                double z = spawnCoords.getConfig().getDouble("spawn.z");
                                float yaw = (float) spawnCoords.getConfig().getDouble("spawn.yaw");
                                float pitch = (float) spawnCoords.getConfig().getDouble("spawn.pitch");
                                final Location loc = new Location(w, x, y, z, yaw, pitch);
                                target.teleport(loc);
                                if (plugin.getConfig().getBoolean("Force-Spawn.Enabled-message-target")) {
                                    target.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Force-Spawn.Message-target").replace("%sendername%", p.getDisplayName())));
                                }
                                if (plugin.getConfig().getBoolean("Force-Spawn.Enabled-message-sender")) {
                                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Force-Spawn.Message-sender").replace("%targetname%", target.getDisplayName())));
                                }
                            }
                        }
                    } else {
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("No-Permission-Message")));
                        return true;
                    }
                }
            return true;
        }
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            Player p = e.getPlayer();
            int taskId = tasks.get(p.getUniqueId()).getTaskId();
    
            if (tasks.containsKey(p.getUniqueId())) {
                if (e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockY() != e.getTo().getBlockY() ||
                        e.getFrom().getBlockZ() != e.getTo().getBlockZ()) {
                    if (plugin.getConfig().getBoolean("Cancel-On-Move.Enabled") && tasks != null) {
                        Bukkit.getScheduler().cancelTask(taskId);
                        tasks.remove(p.getUniqueId());
                    }
                }
            }
        }
    }
     
  8. Online

    timtower Administrator Administrator Moderator

    @Manole7 No, if you call "new SpawnCmd" twice.
     
  9. Offline

    Manole7

    @timtower Yes, I do, for registering the Event and Command Executor in main class.
     
  10. Online

    timtower Administrator Administrator Moderator

    Make a single variable, use it in both.
     
  11. Offline

    Manole7

    @timtower I don't really get how to do that and I don't want to get spoon-fed. May I have a hint a little more in depth, please?
     
  12. Online

    timtower Administrator Administrator Moderator

    var instanc = new SpawnCMD
    register it for both methods
     
  13. Offline

    Manole7

    @timtower I suppose you suggest this to make sure both methods work. I tested it and both work because I added test p.sendMessage("Test"); in both MoveEvent and after /<cmd>. The plugin seems to stop @
    Code:
    if (plugin.getConfig().getBoolean("Cancel-On-Move.Enabled") && tasks != null) {
    So I guess UUID or the task doesn't get added in the HashMap? I just don't get why.
     
    Last edited: Jun 9, 2022
  14. Online

    timtower Administrator Administrator Moderator

    @Manole7 The 2 maps are different, it is not the same map.
     
  15. Offline

    Manole7

    So I can't access the task ID through:
    Code:
    int taskId = tasks.get(p.getUniqueId()).getTaskId();
    To be able to cancel it after? What's the way to access it?
     
  16. Online

    timtower Administrator Administrator Moderator

    @Manole7 I told you to make a single instance and use it to register the listener AND the commandexecutor.
     
  17. Offline

    Manole7

    @timtower What I don't get is what type of variable would that be as you mentioned here.
    Instance variable like:
    class SpawnCmd {} ?
     
    Last edited: Jun 9, 2022
  18. Offline

    KarimAKL

    That's a class declaration, not a variable. Declare and initialize a local variable with the datatype being your Listener & CommandExecutor class.
     
  19. Offline

    Manole7

    @KarimAKL So would it be like:
    Code:
    SpawnCmd sp = new SpawnCmd();
    And register like that onEnable()?
    Code:
    Bukkit.getPluginManager().registerEvents(sp, this);
    getCommand("spawn").setExecutor(sp);
    EDIT: I changed the passing of instance of main class to a static getter method as after I added that SpawnCmd instance it didn't start 'coz of a static initializer from my old way of passing instance. Now it's working flawlessly! Thank you very much for your time 'n guidance.
     
    Last edited: Jun 9, 2022
  20. Offline

    KarimAKL

    Yes, exactly.

    That works too, but I would assume the code inside the static initializer block could be moved to a constructor, which would be a better practice.

    I'm glad you got it working, though. :)
     

Share This Page