How do I change arguments of a command with CommandPreProcessEvent (spigot 1.8.8)

Discussion in 'Plugin Development' started by Neyuux_, Jun 18, 2022.

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

    Neyuux_

    Hello, I code for fun and recently I wanted to code a UHC. I am currently coding an Anonymous scenario (which changes the skin and nickname of all players). The scenario works well but I wanted to make sure that players can put the player's initial nickname in the commands (/msg Neyuux_ rather than /msg Anonymous1) for example. But using CommandPreProcessEvent the result is not good and shows "player not found" in console.

    Code :

    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onCommand(PlayerCommandPreprocessEvent ev) {
            String message = ev.getMessage();
            String[] messageWords = message.split(" ");
            List<String> broadcastAliases = new ArrayList<>(Arrays.asList("/b", "/broadcast", "/bd", "/bc", "/annonce", "/announce", "/ann", "/say"));
            List<String> directMessageAliases = new ArrayList<>(Arrays.asList("/msg", "/m", "/message", "/tell"));
    
            if (messageWords.length <= 1) return;
    
            if (directMessageAliases.contains(messageWords[0])) {
                String receiver = messageWords[1];
    
                for (Map.Entry<PlayerUHC, String> name : realName.entrySet())
                    if (receiver.equalsIgnoreCase(name.getValue()))
                        message = message.replace(receiver, name.getKey().getPlayer().getName());
    
            } else if (!broadcastAliases.contains(messageWords[0])) {
    
                for (Map.Entry<PlayerUHC, String> name : realName.entrySet())
                    message = message.replaceAll("(?i)" + name.getValue(), name.getKey().getPlayer().getName());
            }
    
            ev.setMessage(message);
           
            Bukkit.broadcastMessage(message.replace("§", "&")); //this
            for (Player player : Bukkit.getOnlinePlayers()) { // is
                Bukkit.broadcastMessage(player.getName().replace("§", "&")); //just
                Bukkit.broadcastMessage(String.valueOf(message.split(" ")[1].equalsIgnoreCase(player.getName()))); //for testing
            }
        }
    Console :

    (2nd line in console is a broadcast in chat to announce that a player types a command)
     
  2. Offline

    Strahan

    Hmm. Well, I played around to see if I could swap a nick for a proper username in commands in preprocess, worked for me doing this:
    Code:
    Map<String, String> nicks = new HashMap<>();
    nicks.put("Lupu", "Lupusregina_Beta");
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onPCPE(PlayerCommandPreprocessEvent e) {
      List<String> segments = Arrays.asList(e.getMessage().split(" "));
    
      for (Map.Entry<String, String> nick : nicks.entrySet()) {
        if (!segments.stream().anyMatch(nick.getKey()::equalsIgnoreCase)) continue;
      
        for (int x=0; x<segments.size(); x++) {
          if (!segments.get(x).equalsIgnoreCase(nick.getKey())) continue;
        
          segments.set(x, nick.getValue());
        }
      }
    
      e.setMessage(StringUtils.join(segments.toArray(), " ", 0, segments.size()));
    }
    I went in game and did /give lupu stone and my Lupusregina_Beta character got a stack of stone. No errors in console or client. Also notice I changed your priority out for lowest - it'd be best to do the swap as early as possible before other plugins interfere.

    Not sure how this would really integrate to what you are doing, but figured I'd post it since it worked for me.
     
Thread Status:
Not open for further replies.

Share This Page