[Solved] 1.1RB - PlayerListener is deprecated - help!

Discussion in 'Plugin Development' started by ChuckHunky, Jan 26, 2012.

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

    ChuckHunky

    Hi

    I've only produced one plugin so far, but it's been well received :) I'm still a relative newbie though, so be gentle with me!

    I updated my Bukkit version to bukkit-1.1-R2-SNAPSHOT.jar on my dev machine today before doing some work on my plugin. However, Eclipse is now complaining that "PlayerListener is deprecated".

    I make fairly extensive use of org.bukkit.event.player.PlayerListener and am at a bit of a loss as to what I should replace it with, can anyone help me?

    I've attached some code where I import the PlayerListener package then define a class extending it:

    Code:
    package uk.co.warmlight.andrew.BadWords;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.ChatColor;
     
    public class ServerJoinPlayerListener extends PlayerListener {
        public static BadWords plugin;
        
    Thanks in anticipation of your help!
     
  2. Offline

    ArcheCane

  3. Offline

    ArcheCane

  4. Offline

    ChuckHunky

    Thanks to both ArcheCane and w00tklumpWn, I'll check it out!

    Right, changed for the new event system. Seems I still have two problems, if anyone can help I'd be grateful!

    1.
    Code:
    @EventHandler
    is resulting in the error "EventHandler cannot be resolved to a type"

    2.
    Code:
    List<String> badW = getConfig().getList("BannedWords");
    results in the error "Type mismatch: cannot convert from List<Object> to List<String>". I never had any problems with this line pre-1.1, does getList now return an List<Object> rather than List<String>?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  5. Offline

    RROD

    Does your Listener class implement Listener?
    Eg.:
    Code:Java
    1. public class MyListener implements Listener {
    2.  
    3. // Example Event
    4. @EventHandler(priority = EventPriority.NORMAL)
    5. public void onPlayerJoin(PlayerJoinEvent event) {
    6. // Event code here
    7. }
    8.  
    9. }
     
  6. Offline

    ChuckHunky

    Yes, here's a bigger snippet:
    Code:Java
    1. ...
    2.  
    3. import org.bukkit.event.EventPriority;
    4. import org.bukkit.event.Listener;
    5.  
    6. public class ServerChatPlayerListener implements Listener {
    7. public static BadWords plugin;
    8.  
    9. public ServerChatPlayerListener(BadWords instance) {
    10. plugin = instance;
    11. }
    12.  
    13. @EventHandler
    14. public void onPlayerChat(PlayerChatEvent chat) {
    15. Player p = chat.getPlayer();
    16. String message = chat.getMessage();
    17.  
    18. ...
     
  7. Offline

    RROD

    Could I see your Main Class, plus the whole of this Listener class. Would be more useful so I can see what the issue is.
    Thanks.
     
  8. Ehr... do you have the newest Bukkit-jar version? Currently no idea what the real name is! ;)

    €dit: hmm.. screw this. If you can implementListener, you have the newset...
     
  9. Offline

    ChuckHunky

    Main class:

    Code:
    package uk.co.warmlight.andrew.BadWords;
     
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    import java.util.logging.Logger;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class BadWords extends JavaPlugin {
     
        public static BadWords plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public final ServerChatPlayerListener playerChatListener = new ServerChatPlayerListener(this);
        public final ServerJoinPlayerListener playerJoinListener = new ServerJoinPlayerListener(this);
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdffile = this.getDescription();
            this.logger.info(pdffile.getName()+" "+pdffile.getVersion()+" is now disabled");
        }
     
        @Override
        public void onEnable() {
            loadConfiguration();
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(playerChatListener, this);
            pm.registerEvents(playerJoinListener, this);
            getCommand("bw").setExecutor(new ServerCommandExecutor(this));
            PluginDescriptionFile pdffile = this.getDescription();
            this.logger.info(pdffile.getName()+" "+pdffile.getVersion()+" is enabled");
            ServerUpdateCheck su = new ServerUpdateCheck();
            if (su.isUpdated(pdffile.getVersion())) {
                this.logger.info("[BadWords] There is a NEW VERSION of BadWords available");
                this.logger.info("[BadWords] Please update at BukkitDev");
            }
        }
     
        public void loadConfiguration() {
            String badWords[] = {"bum","idiot","you are crap"};
            getConfig().addDefault("Warnings.default", 3);
            getConfig().addDefault("Action.ban", true);
            getConfig().addDefault("Log.swear", true);
            getConfig().addDefault("Notify.player", true);
            getConfig().addDefault("Notify.others", true);
            getConfig().addDefault("BannedWords",Arrays.asList(badWords));
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
        public Integer getDefaultWarn() {
            Integer defaultWarnings = getConfig().getInt("Warnings.default", 3);
            return defaultWarnings;
        }
     
        public Integer getRemWarn(String uName) {
            Integer warnRemaining = getConfig().getInt("Warnings.warned."+uName.toLowerCase(), getDefaultWarn());
            return warnRemaining;
        }
     
        public Boolean getBanAction() {
            return getConfig().getBoolean("Action.ban");
        }
     
        public void setRemWarn(String uName, Integer warnRemaining) {
            getConfig().set("Warnings.warned."+uName.toLowerCase(), warnRemaining);
            saveConfig();
        }
     
        public void removeBanned(String uName) {
            getConfig().set("Warnings.warned."+uName.toLowerCase(), null);
            saveConfig();
        }
     
        public boolean getNotifyPlayer() {
            return getConfig().getBoolean("Notify.player");
        }
     
        public boolean getNotifyOthers() {
            return getConfig().getBoolean("Notify.others");
        }
     
        public boolean getLogSwear() {
            return getConfig().getBoolean("Log.swear");
        }
     
        @SuppressWarnings("unchecked")
        public boolean addBannedWord(String badWord) {
            if (getConfig().getList("BannedWords").contains(badWord)) {
                return false;
            } else {
                getConfig().getList("BannedWords").add(badWord);
                saveConfig();
                return true;
            }
        }
     
        public boolean delBannedWord(String badWord) {
            if (getConfig().getList("BannedWords").contains(badWord)) {
                getConfig().getList("BannedWords").remove(badWord);
                saveConfig();
                return true;
            } else {
                return false;
            }
        }
     
        @SuppressWarnings("unchecked")
        public List<String> getBannedWords() {
            List<String> badW = getConfig().getList("BannedWords");
            return badW;
        }
     
        public boolean didTheySwear(String message) {
            boolean found = false;
            List<String> badW = getBannedWords();
            Iterator<String> i = badW.iterator();
            while (i.hasNext()) {
                String thisWord = i.next();
                String thisRegex = "\\b"+thisWord+"\\b";
                Pattern patt = Pattern.compile(thisRegex, Pattern.CASE_INSENSITIVE);
                Matcher m = patt.matcher(message);
                if (m.find()) {
                    found = true;
                }
            }
            return found;
        }
     
     
    }
    
    Listener class:

    Code:
    package uk.co.warmlight.andrew.BadWords;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
     
    public class ServerChatPlayerListener implements Listener {
        public static BadWords plugin;
     
        public ServerChatPlayerListener(BadWords instance) {
            plugin = instance;
        }
     
        @EventHandler
        public void onPlayerChat(PlayerChatEvent chat) {
            Player p = chat.getPlayer();
            String message = chat.getMessage();
            ChatColor RED = ChatColor.RED;
            ChatColor WHITE = ChatColor.WHITE;
                 
            if (plugin.didTheySwear(message)) {
                String uName = p.getName();
                Integer warnRemaining = plugin.getRemWarn(uName);
                warnRemaining--;
                plugin.setRemWarn(uName, warnRemaining);
                chat.setCancelled(true);
                if (warnRemaining >= 0) {
                    if (plugin.getLogSwear()) {
                        plugin.logger.info("[BadWords] "+uName+" just said '"+message+"'");
                    }
                    String s = (warnRemaining == 1) ? " warning" : " warnings";
                    if (plugin.getNotifyPlayer()) {
                        p.sendMessage(RED + "[BadWords] " + WHITE + "You cannot say that word, " + warnRemaining + s + " left!");
                    }
                    if (plugin.getNotifyOthers()) {
                        plugin.getServer().broadcastMessage(RED + "[BadWords] " + WHITE + uName + " just said a banned word and received a warning, "+ warnRemaining + s + " left");
                    }
                } else {
                    String action = (plugin.getBanAction()) ? "banned" : "kicked";
                    p.kickPlayer("You have been "+action+" for repeated bad language");
                    if (plugin.getNotifyOthers()) {
                        plugin.getServer().broadcastMessage(RED + "[BadWords] " + WHITE + uName + " has been "+action+" for repeated bad language");
                    }
                    // Remove the player from the config file
                    // if they're banned. So if they're unbanned
                    // they have another 3 warnings
                    if (plugin.getBanAction()) {
                        plugin.removeBanned(uName);
                        p.setBanned(true);
                    } else {
                        // Set their remaining warnings back to 0
                        // so they'll get kicked again next time
                        plugin.setRemWarn(uName, 0);
                    }
                }
            }
        }
    }
     
    
    I've got bukkit-1.1-R2-SNAPSHOT.jar. Which is not an RB, could that be the problem?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  10. do you really need
    Code:
    @EventHandler(priority = Priority.NORMAL)
    when the priority is normal?
    did you tried just @EventHandler ?
     
  11. Offline

    RROD

    Code:Java
    1. package uk.co.warmlight.andrew.BadWords;
    2.  
    3. import java.util.Arrays;
    4. import java.util.Iterator;
    5. import java.util.List;
    6. import java.util.logging.Logger;
    7. import java.util.regex.Matcher;
    8. import java.util.regex.Pattern;
    9.  
    10. import org.bukkit.event.Event;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12. import org.bukkit.plugin.PluginManager;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15.  
    16. public class BadWords extends JavaPlugin {
    17.  
    18. public static BadWords plugin;
    19. public final Logger logger = Logger.getLogger("Minecraft");
    20.  
    21. @Override
    22. public void onDisable() {
    23. PluginDescriptionFile pdffile = this.getDescription();
    24. this.logger.info(pdffile.getName()+" "+pdffile.getVersion()+" is now disabled");
    25. }
    26.  
    27. @Override
    28. public void onEnable() {
    29. loadConfiguration();
    30. getServer().getPluginManager().registerEvents(new BWListener(this), this);
    31. getCommand("bw").setExecutor(new ServerCommandExecutor(this));
    32. PluginDescriptionFile pdffile = this.getDescription();
    33. this.logger.info(pdffile.getName()+" "+pdffile.getVersion()+" is enabled");
    34. ServerUpdateCheck su = new ServerUpdateCheck();
    35. if (su.isUpdated(pdffile.getVersion())) {
    36. this.logger.info("[BadWords] There is a NEW VERSION of BadWords available");
    37. this.logger.info("[BadWords] Please update at BukkitDev");
    38. }
    39. }
    40.  
    41. public void loadConfiguration() {
    42. String badWords[] = {"bum","idiot","you are crap"};
    43. getConfig().addDefault("Warnings.default", 3);
    44. getConfig().addDefault("Action.ban", true);
    45. getConfig().addDefault("Log.swear", true);
    46. getConfig().addDefault("Notify.player", true);
    47. getConfig().addDefault("Notify.others", true);
    48. getConfig().addDefault("BannedWords",Arrays.asList(badWords));
    49. getConfig().options().copyDefaults(true);
    50. saveConfig();
    51. }
    52.  
    53. public Integer getDefaultWarn() {
    54. Integer defaultWarnings = getConfig().getInt("Warnings.default", 3);
    55. return defaultWarnings;
    56. }
    57.  
    58. public Integer getRemWarn(String uName) {
    59. Integer warnRemaining = getConfig().getInt("Warnings.warned."+uName.toLowerCase(), getDefaultWarn());
    60. return warnRemaining;
    61. }
    62.  
    63. public Boolean getBanAction() {
    64. return getConfig().getBoolean("Action.ban");
    65. }
    66.  
    67. public void setRemWarn(String uName, Integer warnRemaining) {
    68. getConfig().set("Warnings.warned."+uName.toLowerCase(), warnRemaining);
    69. saveConfig();
    70. }
    71.  
    72. public void removeBanned(String uName) {
    73. getConfig().set("Warnings.warned."+uName.toLowerCase(), null);
    74. saveConfig();
    75. }
    76.  
    77. public boolean getNotifyPlayer() {
    78. return getConfig().getBoolean("Notify.player");
    79. }
    80.  
    81. public boolean getNotifyOthers() {
    82. return getConfig().getBoolean("Notify.others");
    83. }
    84.  
    85. public boolean getLogSwear() {
    86. return getConfig().getBoolean("Log.swear");
    87. }
    88.  
    89. public boolean addBannedWord(String badWord) {
    90. if (getConfig().getList("BannedWords").contains(badWord)) {
    91. return false;
    92. } else {
    93. getConfig().getList("BannedWords").add(badWord);
    94. saveConfig();
    95. return true;
    96. }
    97. }
    98.  
    99. public boolean delBannedWord(String badWord) {
    100. if (getConfig().getList("BannedWords").contains(badWord)) {
    101. getConfig().getList("BannedWords").remove(badWord);
    102. saveConfig();
    103. return true;
    104. } else {
    105. return false;
    106. }
    107. }
    108.  
    109. @SuppressWarnings("unchecked")
    110. public List getBannedWords() {
    111. return getConfig().getList("BannedWords");
    112. }
    113.  
    114. public boolean didTheySwear(String message) {
    115. boolean found = false;
    116. List badW = getBannedWords();
    117. Iterator<String> i = badW.iterator();
    118. while (i.hasNext()) {
    119. String thisWord = i.next();
    120. String thisRegex = "\\b"+thisWord+"\\b";
    121. Pattern patt = Pattern.compile(thisRegex, Pattern.CASE_INSENSITIVE);
    122. Matcher m = patt.matcher(message);
    123. if (m.find()) {
    124. found = true;
    125. }
    126. }
    127. return found;
    128. }
    129.  
    130.  
    131. }
    132.  


    You only need one listener class for all. Combine them into a new one called BWListener. I have started you off using the events from this one.

    Code:Java
    1. package uk.co.warmlight.andrew.BadWords;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.player.PlayerChatEvent;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.Listener;
    8.  
    9. public class ServerChatPlayerListener implements Listener {
    10. public static BadWords plugin;
    11.  
    12. public BWListener(BadWords plugin) {
    13. this.plugin = plugin;
    14. }
    15.  
    16. @EventHandler(priority = EventPriority.NORMAL)
    17. public void onPlayerChat(PlayerChatEvent chat) {
    18. Player p = chat.getPlayer();
    19. String message = chat.getMessage();
    20.  
    21. if (plugin.didTheySwear(message)) {
    22. String uName = p.getName();
    23. Integer warnRemaining = plugin.getRemWarn(uName);
    24. warnRemaining--;
    25. plugin.setRemWarn(uName, warnRemaining);
    26. chat.setCancelled(true);
    27. if (warnRemaining >= 0) {
    28. if (plugin.getLogSwear()) {
    29. plugin.logger.info("[BadWords] "+uName+" just said '"+message+"'");
    30. }
    31. String s = (warnRemaining == 1) ? " warning" : " warnings";
    32. if (plugin.getNotifyPlayer()) {
    33. p.sendMessage(RED + "[BadWords] " + WHITE + "You cannot say that word, " + warnRemaining + s + " left!");
    34. }
    35. if (plugin.getNotifyOthers()) {
    36. plugin.getServer().broadcastMessage(RED + "[BadWords] " + WHITE + uName + " just said a banned word and received a warning, "+ warnRemaining + s + " left");
    37. }
    38. } else {
    39. String action = (plugin.getBanAction()) ? "banned" : "kicked";
    40. p.kickPlayer("You have been "+action+" for repeated bad language");
    41. if (plugin.getNotifyOthers()) {
    42. plugin.getServer().broadcastMessage(ChatColor.RED + "[BadWords] " + ChatColor.WHITE + uName + " has been "+action+" for repeated bad language");
    43. }
    44. // Remove the player from the config file
    45. // if they're banned. So if they're unbanned
    46. // they have another 3 warnings
    47. if (plugin.getBanAction()) {
    48. plugin.removeBanned(uName);
    49. p.setBanned(true);
    50. } else {
    51. // Set their remaining warnings back to 0
    52. // so they'll get kicked again next time
    53. plugin.setRemWarn(uName, 0);
    54. }
    55. }
    56. }
    57. }
    58. }
    59.  


    The code may, in parts, be wrong as I'm not actually in eclipse atm.
     
  12. Offline

    ChuckHunky

    Yes, I did, same problem. I stuck in the (priority = Priority.NORMAL) in case it was required, then forgot to remove it when I discovered it wasn't.

    Really? What would be advantage of that? It seems to me that having separate listener classes for different types of event helps keep things nicely separated. For example, as well as my player chat listener class, I have a class that handles a player join event.

    As I said previously, I'm really still learning Java (and OO programming), having done procedural programming for ~10 years, so please bear with me! :)

    Oh, I think I understand now. I still retain separate listener classes for each type of event, they just all refer to the same overall listener?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  13. Offline

    RROD

    It's just my style of coding. If I see multiple classes of the same function, I'd want to try and combine them. Overall, it works out cleaner and should in theory reduce server load(probably very, very small difference).

    You can continue to keep your classes separate if you want, but to me it just looks messy.

    As for ~10 years in, Java I'm the same. I started just six months ago, but have learnt a heck of a lot. Before I knew just HTML and CSS. Now Java has opened me to other object-orientated languages such as JS, PHP, C++ and C# - which all of them I am learning as I continue to expand on my Java.

    Don't worry, you'll get the hang of Java soon I'm sure. Good luck ;)
     
  14. Offline

    ChuckHunky

    I'm getting there, thanks!

    So just to clarify, I'm still having the problems detailed in Reply #6 - does anyone have any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  15. Offline

    mindless728

    Did you import org.bukkit.event.EventHandler
     
  16. Offline

    mmiillkkaa

    1.
    Import org.bukkit.event.EventHandler

    2. change .getList to getStringList
     
  17. Offline

    ChuckHunky

    Ding ding - we have a winner!

    Thanks to everyone for walking me through this :)
     
  18. Offline

    mmiillkkaa

    Happy to help :D
     
Thread Status:
Not open for further replies.

Share This Page