hey i am developing a anti-swear plugin but i keep getting an error "Type mismatch: cannot convert from void to boolean" so i did but i keep getting the error i tried to find someone that could help me but i diden't so i went here please help me with this error main code: @EventHandler public boolean dontswear(AsyncPlayerChatEvent event) { Player player = event.getPlayer(); String swear = configGetter.getConfig().getString("swear words"); if (player.sendMessage(swear)) { event.setCancelled(true); player.sendMessage("do not use swear words it is not nice!"); } }
Here are the two things that error can come from. Unless you are actually returning something, methods like this should be "public void don....." sendMessage does not return anything. If statements can only take in booleans. I honestly don't know what this line is trying to accomplish.
@GrimEpp Your problem is on this line: Code: if (player.sendMessage(swear)) { sendMessage() returns void, not a boolean, and you are attempting to pass it to an if statement, which accepts only booleans.
Code: @EventHandler public void onChat(AsyncPlayerChatEvent e) { Player p = e.getPlayer(); List<String> list; String string = e.getMessage(); String[] parts = string.split(" "); for (Object obj : configGetter.getConfig().getList("SwearWords") { list.add(obj.toString().toLowerCase()); } for (String word : list) { for(i = 0; parts.size() > i; i++) { if(parts[1].equalsIgnoreCase(word)) { event.setCancelled(true); player.sendMessage("do not use swear words it is not nice!"); } } } } }
@Maxx_Qc That's incredibly inefficient, O(n^2) time. The config already gets you a list, why are you looping through it and adding it to another list? Use the provided list.contains(string) method, that'll make it so much cleaner.
@ShowbizLocket61 While his code is demonstrably awful, it is not O(n*m) as you suggested. Any naive String search operation is O(n*m), while his is in fact on the order of O(n^2*m), not that such a small inefficiency matters anyway.