How do I make this case-insensitive

Discussion in 'Plugin Development' started by football70500, Jul 31, 2012.

  1. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I can't seem to figure out how ot make it so that this is case insensitive, here is my code:


    PHP:
    package me.football7500.FakeOp;
     
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class 
    MyPluginListener implements Listener {
     
        @
    EventHandler
        
    public void onPlayerChat(PlayerChatEvent event) {
           if(
    event.getMessage().contains("can i have op?"))
                
    event.getPlayer().sendMessage(ChatColor.YELLOW "You are now Op!");
        }
     
    }
    The canI have op part. I need it case insensitve, i tried equalsIgnoreCase with .contains, didint work

    This post has been edited 3 times. It was last edited by football70500 Jul 31, 2012.
  2. Offline

    jazpermo

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
     @EventHandler
        public void onPlayerChat(PlayerChatEvent event) {
          if(event.getMessage().toLowerCase().contains("can i have op?"))
                event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
        }
    Its not case insensitive, but will always force it lowercase no matter the input, and if you keep your check lower case you shouldn't have any problems.

    This post has been edited 1 time. It was last edited by jazpermo Jul 31, 2012.
  3. Offline

    sayaad

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Well, here is something totally unrelated to your problem but can be added to your plugin.

    Code:java
    1. public class MyPluginListener implements Listener {
    2.  
    3. public ArrayList<String> fakeOps = new ArrayList<String>();
    4.  
    5. @EventHandler
    6. public void onPlayerChat(PlayerChatEvent event) {
    7.  
    8. if(event.getMessage().toLowerCase().contains("can i have op?")){
    9.  
    10. event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");
    11. fakeOps.add(event.getPlayer().getName());
    12. }
    13. }
    14. @EventHandler
    15. public void onCommand(PlayerCommandPreprocessEvent event) {
    16.  
    17. if(fakeOps.contains(event.getPlayer.getName())){
    18.  
    19. event.setCancelled(true);
    20. event.getPlayer().sendMessage("What makes you think your allowed to use commands?");
    21. event.getPlayer().setBanned(true);
    22. event.getPlayer().kickPlayer("Bye Op!");
    23. }
    24. }
  4. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    SAH WEET, just one more thing, how would I put a delay on the You are now op! message, for example a 5 second delay after you ask for op
  5. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    With a Delayed Task Scheduler.
  6. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  7. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @football70500
    Why doesn't that make sense? All you have to do is copy and paste the code with slight modifications and change the delay length.
    ZeusAllMighty11 likes this.
  8. Offline

    Scizzr

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Untested but should work.

    Code:
    final Player player = event.getPlayer();
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
        new Runnable() {
            public void run() {
                if (player.isOnline()) {
                    player.sendMessage(ChatColor.YELLOW + "You are now an op.");
                }
            }
        }, 100L);
    

    This post has been edited 4 times. It was last edited by Scizzr Aug 1, 2012.
  9. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    and would I put that all in the onPlayerChat? and replace my old code with it?
  10. Offline

    HON95

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Replace
    Code:JAVA
    1. event.getPlayer().sendMessage(ChatColor.YELLOW + "You are now Op!");

    with it.

    EDIT: WTF format codes??
    EDIT II: Fixed, LOL :)

    This post has been edited 4 times. It was last edited by HON95 Aug 1, 2012.
  11. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I did that, now I have plugin underlined it says plugin cannot be resolved to a variable.

    Here is mah code
    Code:JAVA
    1. package me.football7500.FakeOp;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerChatEvent;
    11. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    12.  
    13. public class MyPluginListener implements Listener {
    14.  
    15. public ArrayList<String> fakeOps = new ArrayList<String>();
    16.  
    17. @EventHandler
    18. public void onPlayerChat(PlayerChatEvent event)
    19. {
    20. if(event.getMessage().toLowerCase().contains("can i have op?"))
    21. final Player player = event.getPlayer();
    22. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
    23. new Runnable() {
    24. public void run() {
    25. if (player.isOnline()) {
    26. player.sendMessage(ChatColor.YELLOW + "You are now an op.");
    27. }
    28. }
    29. }, 100L);
    30.  
    31. fakeOps.add(event.getPlayer().getName());
    32.  
    33. }
    34. @EventHandler
    35. public void onCommand(PlayerCommandPreprocessEvent event) {
    36.  
    37. if(fakeOps.contains(event.getPlayer().getName())){
    38.  
    39. event.setCancelled(true);
    40. event.getPlayer().sendMessage("What makes you think your allowed to use commands?");
    41. event.getPlayer().setBanned(true);
    42. event.getPlayer().kickPlayer("Bye Op!");
    43. }
    44. }
    45.  
    46. }
    47.  
  12. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    The variable plugin represents the class that is your plugin. If this is in your main class, just do "this"
    Scizzr likes this.
  13. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    My main class that has onEnable and onDisable is ChatMessage.java
  14. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Then this class has to have its constructor accept your plugin as a parameter.
  15. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    My code in ChatMessage.java is:

    Code:JAVA
    1. package me.football7500.FakeOp;
    2.  
    3. import java.awt.Color;
    4.  
    5.  
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.player.PlayerChatEvent;
    13. import org.bukkit.plugin.PluginDescriptionFile;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.Bukkit;
    16. import org.bukkit.ChatColor;
    17.  
    18.  
    19. public class ChatMessage extends JavaPlugin{
    20. public static ChatMessage plugin;
    21. public final Logger logger = Logger.getLogger("Minecraft");
    22.  
    23. @Override
    24. public void onEnable() {
    25. Bukkit.getPluginManager().registerEvents(new MyPluginListener(), this);
    26. PluginDescriptionFile pdfFile = this.getDescription();
    27. this.logger.info(pdfFile.getName() + "version" + pdfFile.getVersion() + " is now enabled.");
    28. }
    29. @Override
    30. public void onDisable() {
    31. PluginDescriptionFile pdfFile = this.getDescription();
    32. this.logger.info(pdfFile.getName() + "version" + pdfFile.getVersion() + " is now disabled.");
    33. }
    34.  
    35.  
    36. }
    37.  

    Is my chatMessage plugin; right? that what I gotta use?
  16. Offline

    HON95

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Just a few tips: Use "getLogger()" instead of "Logger.getLogger("Minecraft")", don't print "ChatMessage ver x is now dis/enabled" to reduce console spam. And you most likely don't need your "plugin" variable.

    Again, they're just tips. Nothing more.

    EDIT: And yeah, that's your plugin class. Add a constructor in your other class to take an instance of this one as a parameter, so you change (in this class) "new MyPluginListener()" to "new MyPluginListener(this)". And you probably want to store the instance in your other class.

    This post has been edited 1 time. It was last edited by HON95 Aug 2, 2012.
  17. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Constructor? Instance? Im sorry, i have no clue what those tw owords mean, I just started watching newboston tutorials, so I haven't gotten that far
  18. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @football70500
    Constructor is a class's main method that defines what arguments are required when you create a new instance of it.
    Instance is when you create a new object... every object is in instances, the "new ClassName()" code creates a new instance of ClassName, while calling its constructor with no arguments.

    Constructor is not required but if you specify one with arguments, the one without arguments won't be available if you don't specify one without arguments as well.

    A constructor would look like this:
    Code:
    public class YourClass
    {
        public YourClass() // mind that it doesn't have any return value !
        {
             // code executed when you call "new YourClass()"
        }
    }
    So for your needs, you need to use the "Plugin plugin" argument and create a field (field = variable global to the entire class, before any method is defined) private Plugin plugin; and then asign your this.plugin = plugin in the constructor.... example:
    Code:
    public class YourClass
    {
        private Plugin plugin;
    
        public YourClass(Plugin plugin)
        {
             this.plugin = plugin;
        }
    }
    Then just use new YourClass(this) to parse the main class into that class.

    This post has been edited 1 time. It was last edited by Digi Aug 2, 2012.
  19. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So do I put this in ChatMessage or MyPluginListener, ChatMessage has my onEnable and Disable
  20. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Here is my code in Chat mesage.java,
    Code:JAVA
    1. import java.awt.Color;
    2.  
    3.  
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.player.PlayerChatEvent;
    11. import org.bukkit.plugin.Plugin;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.Bukkit;
    15. import org.bukkit.ChatColor;
    16.  
    17.  
    18. public class ChatMessage extends JavaPlugin{
    19. private Plugin plugin;
    20. public final Logger logger = Logger.getLogger("Minecraft");
    21.  
    22. public ChatMessage(Plugin plugin)
    23. {
    24. this.plugin = plugin;
    25. }
    26.  
    27. @Override
    28. public void onEnable() {
    29. Bukkit.getPluginManager().registerEvents(new MyPluginListener(), this);
    30. PluginDescriptionFile pdfFile = this.getDescription();
    31. this.logger.info(pdfFile.getName() + " is now enabled.");
    32. }
    33. @Override
    34. public void onDisable() {
    35. PluginDescriptionFile pdfFile = this.getDescription();
    36. this.logger.info(pdfFile.getName() + " is now disabled.");
    37. }
    38.  
    39.  
    40. }
    41.  
    42.  
    43.  


    Here is my code in MyPluginListener:

    Code:JAVA
    1. package me.football7500.FakeOp;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerChatEvent;
    11. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    12.  
    13. public class MyPluginListener implements Listener {
    14.  
    15. public ArrayList<String> fakeOps = new ArrayList<String>();
    16.  
    17. @EventHandler
    18. public void onPlayerChat(PlayerChatEvent event)
    19. {
    20. if(event.getMessage().toLowerCase().contains("can i have op?")){
    21. final Player player = event.getPlayer();
    22. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
    23. new Runnable() {
    24. public void run() {
    25. if (player.isOnline()) {
    26. player.sendMessage(ChatColor.YELLOW + "You are now an op.");
    27. }
    28. }
    29. }, 100L);
    30.  
    31. }
    32.  
    33. fakeOps.add(event.getPlayer().getName());
    34.  
    35. }
    36. @EventHandler
    37. public void onPlayerChat1(PlayerChatEvent event)
    38. {
    39. if(event.getMessage().toLowerCase().contains("I am from planetminecraft")){
    40. event.getPlayer().setBanned(true);
    41. event.getPlayer().kickPlayer("Impersonating Planetminecraft staff.");
    42. }
    43. }
    44. @EventHandler
    45. public void onCommand(PlayerCommandPreprocessEvent event) {
    46.  
    47. if(fakeOps.contains(event.getPlayer().getName())){
    48.  
    49. event.setCancelled(true);
    50. event.getPlayer().sendMessage("What makes you think your allowed to use commands?");
    51. event.getPlayer().setBanned(true);
    52. event.getPlayer().kickPlayer("Bye Op!");
    53. }
    54. }
    55.  
    56. }
    57.  

    Which one do I have to put that code into? and where would it go?
  21. Offline

    HON95

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    put
    Code:JAVA
    1. private final ChatMessage PLUGIN;
    2. public MyPluginListener(ChatMessage instance) {
    3. PLUGIN = instance;
    4. }

    in your MyPluginListener class, and
    Code:JAVA
    1. private final MyPluginListener LISTENER;
    2. public ChatMessage() {
    3. LISTENER = new MyPluginListener(this);
    4. }

    in your ChatMessage class. Now you can use methods and fields in one of the classes from the other one, by using E.G. "PLUGIN.getLogger()" or you can put PLUGIN in your "Bukkit.getScheduler.scheduleSyncDelayedTask(PLUGIN, new Runnable() {}, 100L);"

    Note that they are final fields, so you can only set them to something once, either in your constructor, when you declare them, or in some code block right after they are declared. By that last, I mean
    Code:JAVA
    1. final string S; // Not inside a method or anything...
    2. {S = "some string"}

    Assuming you care ofc.
  22. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    In which class?
  23. Offline

    HON95

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    No no, you don't need that in your classes, I just felt like telling you that finals are awesome.
  24. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    mY PLugin isnt working here is my code::

    MYPLUGINLISTENER:
    Code:JAVA
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerChatEvent;
    7. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    8.  
    9. public class MyPluginListener implements Listener {
    10.  
    11.  
    12. private final ChatMessage PLUGIN;
    13. public MyPluginListener(ChatMessage instance) {
    14. PLUGIN = instance;
    15. }
    16. public ArrayList<String> fakeOps = new ArrayList<String>();
    17.  
    18. @EventHandler
    19. public void onPlayerChat(PlayerChatEvent event)
    20. {
    21. if(event.getMessage().toLowerCase().contains("can i have op?")){
    22. final Player player = event.getPlayer();
    23. Bukkit.getScheduler().scheduleSyncDelayedTask(PLUGIN,
    24. new Runnable() {
    25. public void run() {
    26. if (player.isOnline()) {
    27. player.sendMessage(ChatColor.YELLOW + "You are now an op.");
    28. }
    29. }
    30. }, 100L);
    31.  
    32. }
    33.  
    34. fakeOps.add(event.getPlayer().getName());
    35.  
    36. }
    37. @EventHandler
    38. public void onPlayerChat1(PlayerChatEvent event)
    39. {
    40. if(event.getMessage().toLowerCase().contains("I am from planetminecraft")){
    41. event.getPlayer().setBanned(true);
    42. event.getPlayer().kickPlayer("Impersonating Planetminecraft staff.");
    43. }
    44. }
    45. @EventHandler
    46. public void onCommand(PlayerCommandPreprocessEvent event) {
    47.  
    48. if(fakeOps.contains(event.getPlayer().getName())){
    49.  
    50. event.setCancelled(true);
    51. event.getPlayer().sendMessage("What makes you think your allowed to use commands?");
    52. event.getPlayer().setBanned(true);
    53. event.getPlayer().kickPlayer("Bye Op!");
    54. }
    55. }
    56.  
    57. }
    58.  

    ChatMessage:
    Code:JAVA
    1. package me.football7500.FakeOp;
    2. import java.util.logging.Logger;
    3. import org.bukkit.plugin.PluginDescriptionFile;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5. public class ChatMessage extends JavaPlugin{
    6. public final Logger logger = Logger.getLogger("Minecraft");
    7.  
    8. private final MyPluginListener LISTENER;
    9. public ChatMessage() {
    10. LISTENER = new MyPluginListener(this);
    11. }
    12.  
    13.  
    14. @Override
    15. public void onEnable() {
    16. PluginDescriptionFile pdfFile = this.getDescription();
    17. this.logger.info(pdfFile.getName() + " is now enabled.");
    18. }
    19. @Override
    20. public void onDisable() {
    21. PluginDescriptionFile pdfFile = this.getDescription();
    22. this.logger.info(pdfFile.getName() + " is now disabled.");
    23. }
    24.  
    25. }
    26.  

    What's wrong?

    This post has been edited 1 time. It was last edited by football70500 Aug 2, 2012.
  25. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    1.
    Code:
    public ChatMessage() {
        LISTENER = new MyPluginListener(this);
    }
    No.

    2. You didn't register the event in onEnable()
  26. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I registered it in onEnable, I get an error on (new MyPluginListener(), this) It says, the constructor is unidentified then it gives me 3 options: Add argument to match 'MyPlugnListener'(ChatMessage)
    Change Constructor 'MyPluginListener(ChatMessage)': Remove parameter ChatMessage
    Create Constructor 'MyPluginListener()'

    Code:JAVA
    1. @Override
    2. public void onEnable() {
    3. Bukkit.getPluginManager().registerEvents(new MyPluginListener(), this);
    4. PluginDescriptionFile pdfFile = this.getDescription();
    5. this.logger.info(pdfFile.getName() + " is now enabled.");
    6. }
  27. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You didn't in the previous code:

    Code:
    @Override
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " is now enabled.");
    }
    Your MyPluginListener's constructor has your plugin as a parameter. Therefore do this: MyPluginListener(this) instead of MyPluginListener()

    This post has been edited 1 time. It was last edited by Firefly Aug 2, 2012.
  28. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Now registerEvents has an error on it
    The method registerEvents(Listener, Plugin) in the type PluginManager is not applicable for the arguments (MyPluginListener)

    What to do there?
    Code:
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(new MyPluginListener(this) ;
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is now enabled.");
        }
  29. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I said replace MyPluginListener(this) with only MyPluginListener

    you still need to have this completely: MyPluginListener(this), this
  30. Offline

    football70500

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Got it now how do I put a scheduler on here

    Code:
        @EventHandler
        public void onCommand(PlayerCommandPreprocessEvent event) {
     
            if(fakeOps.contains(event.getPlayer().getName())){
     
            event.setCancelled(true);
            event.getPlayer().sendMessage("What makes you think your allowed to use commands?");
            event.getPlayer().setBanned(true);
            event.getPlayer().kickPlayer("Bye Op!");
            }
        }

Share This Page