Solved HashMap help

Discussion in 'Plugin Development' started by Kassestral, Jan 31, 2015.

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

    Kassestral

    The problem I'm having is that the HashMap doesn't seem to be keeping it's value, any help?
    (For instance when I use /tpaccept it keeps returning as that the player isn't in the hasmap)

    Code:
    package com.kassestral.plugins.imperium.commands;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import com.kassestral.plugins.imperium.Message;
    import com.kassestral.plugins.imperium.Plugin;
    
    public class Teleport implements CommandExecutor {
    
        Plugin plugin = Plugin.plugin;
        Message message = new Message();
        HashMap<String,String> requests = new HashMap<String, String>();
       
       
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
           
            if(command.getName().equalsIgnoreCase("tpa")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                   
                    if(args.length == 1) {
                        Player target = Bukkit.getServer().getPlayerExact(args[0]);
                       
                        if(target != null) {
                            if(requests.containsValue(target.getName().toLowerCase())) {
                                requests.remove(target.getName().toLowerCase());
                                requests.put(target.getName().toLowerCase(), player.getName().toLowerCase());
                                player.sendMessage(requests.toString());
                                target.sendMessage(ChatColor.LIGHT_PURPLE + "You have recieved a teleport request from " + ChatColor.WHITE + player.getName());
                            }
                            else {
                                requests.put(target.getName().toLowerCase(), player.getName().toLowerCase());
                                player.sendMessage(requests.toString());
                                target.sendMessage(ChatColor.LIGHT_PURPLE + "You have recieved a teleport request from " + ChatColor.WHITE + player.getName());
                            }
                        }
                        else {
                            player.sendMessage(message.offlinePlayer());
                        }
                    }
                    else {
                        player.sendMessage(ChatColor.RED + "Usage: /tpa <playername>");
                    }
                }
                else {
                    plugin.Log("This command cannot be executed from the console");
                }
            }
           
            else if(command.getName().equalsIgnoreCase("tpaccept")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                   
                    if(requests.containsKey(player.getName().toLowerCase())) {
                        Player target = (Player) Bukkit.getServer().getPlayerExact(requests.get(player.getName().toLowerCase()));
                       
                        if(target != null) {
                            if(target.isOnline()) {
                                target.teleport(player);
                                requests.remove(player.getName().toLowerCase());
                                target.sendMessage(ChatColor.WHITE + player.getName() + ChatColor.GREEN +" has accepted your teleport request");
                            }
                            else {
                                player.sendMessage(message.offlinePlayer());
                            }
                        }
                        else {
                            player.sendMessage(message.offlinePlayer());
                        }
                    }
                    else {
                        player.sendMessage(ChatColor.RED + "You do not have any teleport requests");
                        player.sendMessage(requests.toString());
                    }
                }
                else {
                    plugin.Log("This command cannot be executed from the console");
                }
            }
           
            else if(command.getName().equalsIgnoreCase("tpdeny")) {
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                   
                    if(requests.containsKey(player.getName().toLowerCase())) {
                        Player target = (Player) Bukkit.getServer().getPlayerExact(requests.get(player.getName().toLowerCase()));
                       
                        if(target != null) {
                            if(target.isOnline()) {
                                requests.remove(player.getName().toLowerCase());
                                target.sendMessage(ChatColor.WHITE + player.getName() + ChatColor.RED +" has denied your teleport request");
                            }
                            else {
                                player.sendMessage(message.offlinePlayer());
                            }
                        }
                        else {
                            player.sendMessage(message.offlinePlayer());
                        }
                    }
                }
            }
           
            return false;
        }
       
    }
     
  2. Offline

    mythbusterma

  3. Offline

    Kassestral

    Alright, I won't question why you need it, but the problem I am having doesn't come from the main class
    Code:
    package com.kassestral.plugins.imperium;
    
    import java.io.File;
    
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.kassestral.plugins.imperium.commands.*;
    import com.kassestral.plugins.imperium.events.EntityDeath;
    import com.kassestral.plugins.imperium.events.JoinAndLeave;
    
    public class Plugin extends JavaPlugin implements Listener{
      
        private File file_configuration = new File(this.getDataFolder() + File.separator + "config.yml");
        private YamlConfiguration configuration;
      
        public static Plugin plugin;
      
        public void onEnable() {
            plugin = this;
            cleanStartup();
            cleanListeners();
            cleanCommands();
        }
      
        public void onDisable() {
            plugin = null;
        }
      
        public void Log(String message) {
            this.getLogger().info(message);
        }
      
        public void cleanStartup() {
            File directory = new File(this.getDataFolder() + File.separator + "accounts" + File.separator);
            if(!directory.exists()) {
                directory.mkdirs();
            }
            if(!file_configuration.exists()) {
                this.saveDefaultConfig();
            }
        }
      
        private void cleanCommands() {
            getCommand("msg").setExecutor(new Msg());
            getCommand("pay").setExecutor(new Pay());
            getCommand("tpa").setExecutor(new Teleport());
            getCommand("tpaccept").setExecutor(new Teleport());
            getCommand("tpdeny").setExecutor(new Teleport());
        }
        private void cleanListeners() {
            this.getServer().getPluginManager().registerEvents(new JoinAndLeave(), this);
            this.getServer().getPluginManager().registerEvents(new EntityDeath(), this);
        }
    
        public YamlConfiguration getConfiguration() {
            this.configuration = YamlConfiguration.loadConfiguration(file_configuration);
            return configuration;
        }
    }
    @mythbusterma
     
  4. Offline

    mythbusterma


    Just as I expected. Do you see the issue here?
     
  5. Offline

    Kassestral

    @mythbusterma
    No, however I wasn't too sure if this would cause a problem, would you mind explaining to me what is wrong?
     
  6. Offline

    mythbusterma

    @Kassestral

    You're creating three different instances of your CommandExectuor class, each of which has their own HashMap.
     
  7. Offline

    Kassestral

    @mythbusterma
    How would you recommend I go about fixing this?
     
  8. Offline

    mythbusterma

    @Kassestral

    Create one instance of it and have it be the executor for all three of those commands.
     
    Kassestral likes this.
  9. Offline

    Kassestral

    @mythbusterma
    Thanks, sorted it now :) Cheers for the help
     
    mythbusterma likes this.
  10. Offline

    teej107

    @Kassestral I was going to help you but I then realized that my help wasn't going to be needed. Please mark this thread as solved!
     
  11. Offline

    Kassestral

    @teej107
    It's been such a long time since I used these forums, how do I change it to Solved?
     
  12. Offline

    nverdier

    @Kassestral Thread Tools (top right corner of the thread box) > Edit Title > Click on the little box on the left side of the text field > Change it!
     
Thread Status:
Not open for further replies.

Share This Page