Stopping players from using all caps in a message.

Discussion in 'Plugin Development' started by Sabersamus, Aug 28, 2012.

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

    Sabersamus

    Note: This may or may not belong in resources? Oh well.

    This code is for people who want to check if a player used x amount of all capital words in a message.
    And then do something else.

    Code:java
    1.  
    2. @EventHandler
    3. public void onChat(PlayerChatEvent event){
    4. String m = event.getMessage();
    5. String[] message = m.split(" ");
    6. if(message.length < 2)return;
    7. int x = (YOUR INT HERE);
    8. if(getCapsSize(message) > x){
    9. //DO STUFF
    10. //ex:
    11. event.getPlayer().sendMessage(ChatColor.RED + "Please don't use so many caps");
    12. event.setMessage(m.toLowerCase());
    13. }
    14. }
    15.  
    16. private int getCapsSize(String[] args){
    17. int i = 0;
    18. for(String string: args){
    19. if(string.equals(new String(string.toUpperCase()))){
    20. i++;
    21. }
    22. }
    23. return i;
    24. }
    25.  
     
  2. Offline

    Barinade

  3. Offline

    Giant

    Your code could actually be optimized as well! :)
    Code:
    private int getCapsSize(String... args) { // This allows both String[] and arg1, arg2, arg3, arg4
        int i = 0; // Just use a counter instead of a list, as this takes less resources
        for(String s : args) {
            if(string.equals(string.toUpperCase())) // Shouldn't need "new String()"
                i++;
        }
       
        return i;
    }
    
     
  4. Offline

    Sabersamus

    You actually do need new String() because i tried
    Code:java
    1.  
    2. if(string.eqauls(string.toUpperCase()){
    3. getLogger().info("true");
    4. }else{
    5. getLogger().info("false");
    6. }


    and it said false, so i tried new String(string.toUpperCase()); and it said true
     
  5. Offline

    Giant

    Interesting... Doesn't mean my code is not slightly more optimized though! :D
     
  6. Offline

    Sabersamus

    it is, i didn't think about that, im gonna edit the post so people dont need to scroll down to see it :) Thanks Giant
     
  7. Offline

    amitlin14

    AsyncPlayerChatEvent event

    made something similar 2 days ago:

    Code:java
    1. public boolean capsCheck(String message, AsyncPlayerChatEvent evt,
    2. String player) {
    3. boolean capCheck = false;
    4. int counter = 0;
    5.  
    6. if(message.length() > 3) {
    7.  
    8. for(int i = 3; i<message.length(); i++) {
    9. if(Character.isUpperCase(message.charAt(i)) && Character.isLetter(message.charAt(i))) {
    10. counter++;
    11. }
    12. }
    13.  
    14. if(counter > (message.length()*0.30)) {
    15. capCheck = true;
    16. evt.getPlayer().sendMessage(
    17. ChatColor.DARK_RED + "You are now muted");
    18. message = message.toLowerCase();
    19. char[] msg = message.toCharArray();
    20. msg[0] = Character.toUpperCase(msg[0]);
    21. msg.toString();
    22. message = new String(msg);
    23. evt.setMessage(message);
    24. }
    25. }
    26.  
    27. return capCheck;
    28. }


    checks if there is more then 30% caps in the String and returns if the player used more caps then the limit and mutes him for 10 seconds on another class(that manages all my 'guards'[curses,links, etc]). Also prints the message, just lowercased and first letter capitalized.

    btw, are there any better ways to do it?
     
  8. Offline

    Giant

    This part seems quite useless to me... What .toString() does is return a String, not change something internally into a String.
    Therefore doing "message = msg.toString();" should work just as fine :)
     
  9. Offline

    amitlin14

    Yeah someone told me this yesterday, i forgot to change it
     
  10. Offline

    Giant

    That would've been me :D
     
Thread Status:
Not open for further replies.

Share This Page