My TPA Command doesn't work when removing static keyword from hashmap

Discussion in 'Plugin Development' started by RoundStrider, Oct 28, 2021.

  1. Offline

    RoundStrider

    I'm new to spigot plugin development. I was watching Kody Simpson's tutorial of fly plugin in youtube.


    In that video, in 7:02 he makes an arraylist which stores players who have flight. I tried to make a Tpa plugin in spigot. I used a static hashmap which stores sender and target player names. But in that video he didn't use static so I removed the static from mine. But mine doesn't work. I used a lot of system.out.print to keep track of the hashmap contents, but when the command is tpaccept(when the command re executes) the map content is empty

    Code (Java):
    Code:
    package me.ihsan.myfirstplugin.commands;
    
    import net.md_5.bungee.api.chat.BaseComponent;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    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 java.util.HashMap;
    import java.util.Map;
    
    public class TPACommands implements CommandExecutor
    {
       public HashMap<String,String> tplist = new HashMap<>();
    
        @Override
       public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
           if (sender instanceof Player p){
               System.out.println("[TPACommand] Sender is instance of player");
               if(command.getName().equalsIgnoreCase("tpa")){
                   System.out.println("[TPACommand] Command name is tpa");
                   if(args.length == 0){
                        p.sendMessage(ChatColor.RED + "Please specify a player to teleport to!");
                   }
                   else if(args.length == 1){
                       System.out.println("[TPACommand] Argument length is 1");
                        Player target = Bukkit.getPlayerExact(args[0]);
                       if (target != null){
                           System.out.println("[TPACommand] Player not null");
                            target.sendMessage(p.getDisplayName() + " wants to teleport to you!");
                          
                            BaseComponent[] component = new ComponentBuilder("[ACCEPT]")
                                    .color(net.md_5.bungee.api.ChatColor.GREEN)
                                    .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tpaccept"))
                                    .append("   ")
                                    .append("[DECLINE]")
                                    .color(net.md_5.bungee.api.ChatColor.RED)
                                    .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tpdeny"))
                                    .create();
                            target.spigot().sendMessage(component);
                            tplist.put(p.getName(), target.getName());
                           System.out.println("[TPACommand] Added player to hashmap tplist");
                           for(Map.Entry<String,String> entry: tplist.entrySet()){
                               System.out.println("[MAP] " + entry.getKey() + " => " + entry.getValue());
                           }
                       }
                       else{
                           System.out.println("[TPACommand] Player was null");
                            p.sendMessage(ChatColor.RED + "Could not find an online player named " + args[0] + "!");
                       }
                   }
                   else{
                       System.out.println("[TPACommand] Argument length was greater than 1");
                        p.sendMessage(ChatColor.RED + "Usage: /tpa <target player>");
                   }
               }
               if (command.getName().equalsIgnoreCase("tpaccept")){
                   System.out.println("[TPACommand] Command name is tpaccept");
                   System.out.println("[TPACommand] Map contains: ");
                   for (Map.Entry<String,String> entry: tplist.entrySet()){
                       System.out.println("[MAP] " + entry.getKey() + " => " + entry.getValue());
                   //here in the for loop it is supossed to print some data, but the map was empty
                   }
                   if (args.length == 0){
                       System.out.println("[TPAccept] argument length was 0");
                       if(tplist.containsValue(p.getName())){
                           for(Map.Entry<String, String> entry: tplist.entrySet()){
    
                               if(entry.getValue().equalsIgnoreCase(p.getName())){
                                    Player tper = Bukkit.getPlayerExact(entry.getKey());
                                   if(tper != null){
                                        tper.sendMessage("Teleporting you to " + p.getDisplayName());
                                        tper.teleport(p.getLocation());
                                        tplist.remove(tper.getName());
                                   }
                               }
                           }
                       }else{
                            p.sendMessage(ChatColor.RED + "You do not have any teleport requests!");
                       }
    
                   }
                   else{
                        p.sendMessage(ChatColor.RED + "Usage: /tpaccept");
                   }
               }
           }
           return true;
       }
    }
    Thats all the code in the commandexecuter class. Ill also show the main class

    Code (Java):
    Code:
    package me.ihsan.myfirstplugin;
    
    import me.ihsan.myfirstplugin.commands.*;
    import me.ihsan.myfirstplugin.listeners.*;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class MyFirstPlugin extends JavaPlugin{
        @Override
       public void onEnable() {
           // Plugin startup logic
           System.out.println("[My First Plugin] Plugin loaded successfullt");
            getServer().getPluginManager().registerEvents(new PlayerJoinEventListener(),this);
            getServer().getPluginManager().registerEvents(new ChatListener(), this);
            getServer().getPluginManager().registerEvents(new MenuClickedEvent(), this);
            getServer().getPluginManager().registerEvents(new GamemodeChangeEvent(), this);
            getCommand("die").setExecutor(new DieCommand());
            getCommand("about").setExecutor(new AboutCommand());
            getCommand("tpa").setExecutor(new TPACommands());
            getCommand("tpaccept").setExecutor(new TPACommands());
            getCommand("tpdeny").setExecutor(new TPACommands());
            getCommand("fly").setExecutor(new FlyCommand());
            getCommand("menu").setExecutor(new VaultCommand());
       }
    
    
    }
    
    Please someone help me, I'm scratching my head all day long for this. It works when the map is static but not now. It is supposed to work now isn't it. then how did the youtbuer's work!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @RoundStrider Because you have multiple instances of the same class.
    Make it a single instance and you don't need the static.
    Or you put the map in the main class and pass it along to the other classes.
     
  3. Offline

    RoundStrider

    Is there some way to make one instance of the command and register it for all 3
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    RoundStrider

    how to pass it from main please explain
     
  6. Offline

    timtower Administrator Administrator Moderator

    You make a constructor for it.
     
  7. Offline

    RoundStrider

    Thank you so much
     

Share This Page