Solved Reply cmd not working

Discussion in 'Plugin Development' started by johnnyD, Feb 5, 2021.

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

    johnnyD

    My msg works fine, but when trying to reply it always says there is no one to reply to. Im new to hashmaps so im not to sure how to figure this out. The hashmap returns null for some reason, so im not sure why that is.

    Code:
    public class messageCommand implements CommandExecutor{
    
       
        HashMap<Player,Player> conversations = new HashMap<Player,Player>();
       
        String abc = "EDBCAF";
       
        public void setReplyTarget(Player messager, Player reciever) {
            conversations.put(messager, reciever);
            conversations.put(reciever, messager);
        }
        public Player getReplyTarget(Player messager) {
            return conversations.get(messager);
        }
       
       
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("msg")) {
                if(sender instanceof Player) {
                    switch(args.length) {
                    case 0:
                        sender.sendMessage(ChatColor.GRAY + "Usage: " + ChatColor.GOLD + "/msg (player) (message)");
                        break;
                    case 1:
                        sender.sendMessage(ChatColor.GRAY + "Usage: " + ChatColor.GOLD + "/msg (player) (message)");
                        break;
    
    
    
                    }
                    if(args.length>1) {
                        if(Bukkit.getOfflinePlayer(args[0]).getPlayer() != null) {
                            Player messager = (Player) sender;
                            Player reciever = (Player) Bukkit.getOfflinePlayer(args[0]).getPlayer();
                            //int f = Main.getInstance().RConfigP.getInt("Users."+reciever.getUniqueId().toString()+".Rank");
                            //String letter = Character.toString(abc.charAt(f));
    
                            setReplyTarget(messager, reciever);
    
                            StringBuilder sb = new StringBuilder();
                            for(int i = 1; i < args.length; i++) {
                                sb.append(args[i]).append(" ");
                            }
                            String txt = sb.toString().trim();
                            //String txt = args.toString().substring(p.getUniqueId().le, args.length);
                            messager.sendMessage(ChatColor.GRAY + "(To " + reciever.getCustomName() +ChatColor.GRAY+ "): " +txt);
                            reciever.sendMessage(ChatColor.GRAY + "(From " + messager.getCustomName() +ChatColor.GRAY+ "): " +txt);
    
                        }else {
                            sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
                        }
                    }
                   
                   
                }
    
            }
           
            if(cmd.getName().equalsIgnoreCase("reply")) {
                if(sender instanceof Player) {
                    Player messager = (Player) sender;
                    messager.sendMessage(""+getReplyTarget(messager));
                    if(getReplyTarget(messager) == null) {
                        messager.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "You have no one to reply to!");
                        return true;
                    }
                    Player reciever = getReplyTarget(messager);
                    StringBuilder sb = new StringBuilder();
                    for(int i = 0; i < args.length; i++) {
                        sb.append(args[i]).append(" ");
                    }
                    String txt = sb.toString().trim();
                   
                    messager.sendMessage(ChatColor.GRAY + "(To " + reciever.getCustomName() +ChatColor.GRAY+ "): " +txt);
                    reciever.sendMessage(ChatColor.GRAY + "(From " + messager.getCustomName() +ChatColor.GRAY+ "): " +txt);
                   
                }
            }
           
           
           
           
           
           
            return true;
        }
    
    }
    
     
  2. Offline

    Kars

    It might be because the instance of Player in your HashMap is not the same instance as the Player 'sender'.
    This is why you should always save UUID's in the map, instead of Player objects.
     
  3. Offline

    johnnyD

    I figured it out, I had to create the hashmap out of the cmd class since everytime i would run the reply cmd it would reset it. I will start using uuids instead now though, thx.
     
  4. Offline

    Strahan

    Glad to hear you got it. Couple thoughts...
    Code:
    public class messageCommand implements CommandExecutor{
    That should be MessageCommand to follow Java conventions
    Code:
    HashMap<Player,Player> conversations = new HashMap<Player,Player>();
    That should be Map not HashMap on the left to follow SOLID design principles

    Code:
    public void setReplyTarget(Player messager, Player reciever) {
    Just to be pedantic, heh, that should be receiver not reciever.
    Code:
    if(sender instanceof Player) {
    You should consider negative check and return, it would save some indentation
    Code:
    if(args.length>1) {
    It's weird to combine an if statement that examines something you already switched on. Just use default. The default case is for anything that wasn't matched in the switch.
    Code:
    StringBuilder sb = new StringBuilder();
    for(int i = 1; i < args.length; i++) {
      sb.append(args[i]).append(" ");
    }
    String txt = sb.toString().trim();
    Spigot shades in Apache Commons, so this could be condensed to just:
    Code:
    String txt = StringUtils.join(args, " ", 1, args.length);
     
  5. Offline

    Kars

    Not in your example it wouldn't. That is weird.
     
  6. Offline

    johnnyD

    Okay thanks for the tips! While your here, Im having a problem with calling a method from another plugin.

    Main class of the plugin that I want to call it to
    Code:
    public BlockGen rgb;
       
        @Override
        public void onEnable() {
           
            Bukkit.getServer().getPluginManager().registerEvents(new HandlerEvents(), this);
           
            rgb = (BlockGen)Bukkit.getPluginManager().getPlugin("RandomBlockGen");
           
           
            if(rgb == null) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "RANDOMBLOCKGEN NOT WORK");
            }
    
    Events class calling the method
    Code:
    Handler.getInstance().rgb.GenBlocks();

    If you want me to post the code of the class of the plugin being inherited let me know, but its just a general method that does nothing but broadcast a method..

    ALSO heres the error I get in console, ive done my best to try to read through it since the stack usually tells me the problem but I have had no luck with this one.

    STACK:
    Code:
    [21:23:40 ERROR]: Could not pass event PlayerInteractEvent to ArenaHandler v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:226) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:724) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:80) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_251]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_251]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    Caused by: java.lang.NullPointerException
            at me.Jake.ArenaHandler.HandlerEvents.onSign(HandlerEvents.java:53) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot1.8.jar:git-Spigot-c3c767f-33d5de3]
            ... 16 more
    
    Thanks
     
Thread Status:
Not open for further replies.

Share This Page