Solved Dead code?

Discussion in 'Spigot Plugin Development' started by mlgcraftnetwork, May 17, 2016.

  1. So I'm creating a plugin that stops players from spamming, it first checks if the message is simular to the previouw message and then adds the new message as last message. but the last message never gets added to the HasMap for some reason, this is my code:
    Code (Text):
        Map<UUID, String> lastmsg = new HashMap<UUID, String>();
       
        @EventHandler
        public void Chat(AsyncPlayerChatEvent e) {
        Player p = e.getPlayer();
        String msg = e.getMessage().toLowerCase();
        if (msg.contains(lastmsg.get(p.getUniqueId())) || msg == lastmsg.get(p.getUniqueId())) {
        p.sendMessage(ChatColor.RED + "Please don't spam the chat!");
        p.playSound(p.getLocation(), Sound.NOTE_BASS, 2.0F, 1.0F);
        e.setCancelled(true);
        }
        lastmsg.put(p.getUniqueId(), msg);
        }
    Thanks for your help!
     
  2. Indent your code properly first.
     
  3. When you compare strings, don't use == but in this case msg.equals()
     
  4. The person will never get added to the map because it will throw nullpointerexceptions as you try to get something from the map which isn't there yet.
     
    • Agree Agree x 1
  5. That seems like a very legit reason xD, will take a look at that right away.
     
  6. Will change it, thanks!