anti swear problem

Discussion in 'Plugin Development' started by GrimEpp, Jan 19, 2016.

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

    GrimEpp

    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!");
    }
    }
     
  2. Offline

    Zombie_Striker

    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.
     
    boomboompower likes this.
  3. Offline

    boomboompower

  4. Offline

    ShowbizLocket61

    @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.
     
  5. Offline

    GrimEpp

    Last edited by a moderator: Jan 20, 2016
  6. Offline

    ShowbizLocket61

    @GrimEpp
    You're welcome. If you will, please mark the thread as solved :p
     
  7. Offline

    Maxx_Qc

    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!");
    }
    }
    }
    }
    } 
     
  8. Offline

    ShowbizLocket61

    @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.
     
    Maxx_Qc and WolfMage1 like this.
  9. Offline

    mythbusterma

    @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.
     
Thread Status:
Not open for further replies.

Share This Page