Return command (teleport to last location)

Discussion in 'Plugin Development' started by Maxx_Qc, Jun 22, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Maxx_Qc

    Hi guys, I'm trying to make a Return command.
    Of course, this command will teleport you back to your last location.
    The problem is everytime I do the command, it says that I have no previous location.
    Code:
    HashMap<String, Location> lastLoc = new HashMap<String, Location>();
    @EventHandler
        public void PlayerTeleport(PlayerTeleportEvent event)
        {
            Player p = event.getPlayer();
            String name = event.getPlayer().getName();
            lastLoc.remove(name);
            lastLoc.put(name, p.getLocation());
        }
    @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(cmd.getName().equalsIgnoreCase("return")) {
                Player player = (Player) sender;
                if (lastLoc.get(player.getName()) != null)
                {
                    Location loc = lastLoc.get(player.getName());
                    player.teleport(loc);
                    if(player != sender) {
                        player.sendMessage(colorize(Main.getPlugin().getConfig().getString("returnedyou").replaceAll("%player", sender.getName())));
                    }
                  
                    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                        if(p.isOp() || p.hasPermission("commands.teleport")) {
                            String returnmessage = Main.getPlugin().getConfig().getString("hasreturned");
                            String returnmsg = returnmessage.replaceAll("%admin", String.valueOf(sender.getName())).replaceAll("%player", "lui-même");
                            p.sendMessage(colorize(returnmsg));
                        }
                    }
                  
                    lastLoc.remove(player.getName());
                } else {
                    sender.sendMessage(colorize(Main.getPlugin().getConfig().getString("newlocfailed")));
                }
            }
            return true;
    }
    
    Can someone help me plz?
     
  2. Offline

    Evaluations

    Is the class implementing listener? Are you registering your events?
     
  3. Offline

    Garnetty

    Make sure you register events at the onEnable method. In order to do so your class must implement Listener

    Also, keep in mind that you are casting the CommandSender variable to the Player class when it could be the console sending it. Make sure you do an instanceof check before casting things like that.
     
  4. Offline

    Maxx_Qc

    @Evaluations
    Code:
    public class Commands implements Listener, CommandExecutor
    @Garnetty
    Code:
    public void onEnable()
        {
            saveDefaultConfig();
           plugin = this;
           getServer().getPluginManager().registerEvents(this, this);
            getServer().getPluginManager().registerEvents(new onPlayerChat(), this);
            getServer().getPluginManager().registerEvents(new ExpMultiplier(), this);
            getServer().getPluginManager().registerEvents(new Commands(), this);
            getServer().getPluginManager().registerEvents(new Backpack(), this);
            getServer().getPluginManager().registerEvents(new MySQL(), this);
            getServer().getPluginManager().registerEvents(new WorldEdit(), this);
            getServer().getPluginManager().registerEvents(new Donjons(), this);
            getCommand("mcreload").setExecutor(new Commands());
            getCommand("goto").setExecutor(new Commands());
            getCommand("return").setExecutor(new Commands());
            getCommand("bring").setExecutor(new Commands());
            getCommand("tp").setExecutor(new Commands());
            getCommand("tokens").setExecutor(new MySQL());
            getCommand("paytokens").setExecutor(new MySQL());
            getCommand("confirmer").setExecutor(new MySQL());
            getCommand("boutique").setExecutor(new Commands());
            getCommand("backpack").setExecutor(new Backpack());
            getCommand("gm").setExecutor(new Commands());
            getCommand("say").setExecutor(new Commands());
            getCommand("gamemode").setExecutor(new Commands());
            getCommand("setarea").setExecutor(new WorldEdit());
            getCommand("dj").setExecutor(new Donjons());
            MySQL.connect();
            Silenced = false;
           
            File file = new File(plugin.getDataFolder()+File.separator+"players");
            if(!file.exists()) {
                file.mkdirs();
            }
           
            FileConfiguration config2 = null;
            File file3 = new File(plugin.getDataFolder()+File.separator+"donjons.yml");
            if(!file3.exists()){
                   try {
                      file3.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                   config2 = YamlConfiguration.loadConfiguration(file3);
                   try {
                      config2.save(file3);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
           
            FileConfiguration config = null;
            File file1 = new File(plugin.getDataFolder()+File.separator+"area.yml");
            if(!file1.exists()){
                   try {
                        file1.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                   config = YamlConfiguration.loadConfiguration(file1);
                   config.createSection("area");
                   try {
                        config.save(file1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    
    And yeah I will do that but this is not the problem.
     
  5. Offline

    Garnetty

    I never said that it was the problem, I'm giving you advice
     
Thread Status:
Not open for further replies.

Share This Page