Disable and enable Death Messages using /dm (on/off)

Discussion in 'Plugin Development' started by rodney12345, Mar 8, 2013.

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

    rodney12345

    Hey, I have just finished all of the thenewboston beginner and intermediate tutorials, so i am not that big of a Java noob. I just started today looking at the bukkit layout, and getting familiar with it the more i play with it.

    Anyways, I was thinking of creating some sort of plugin which disables and enables the death messages of Minecraft using (/dm on) to enable messages, and (/dm off) to disable or hide the messages. What methods would I use?

    Thanks in advance!:)
     
  2. Offline

    Tirelessly

    PlayerDeathEvent, setDeathMessage
     
  3. Offline

    kreashenz

    You should create a permission, or a command to turn on/off the event.. I'm not sure how to use perms to mess around with events, so I'm not really sure.. Just set up permissions, I guess that would be the easiest way. Or you can set it enabled: true/false in a config..
     
  4. Offline

    rodney12345

  5. Offline

    RealDope

    Code:JAVA
    1.  
    2. List<String> messagesOff = new ArrayList<String>();
    3.  
    4. // onCommand
    5. Player p = (Player) sender;
    6. String name = p.getName();
    7. if(messagesOff.contains(name)) {
    8. messagesOff.remove(name);
    9. }
    10. else {
    11. messagesOff.add(name);
    12. }
    13.  
    14. // Events outside of onCommand
    15. @EventHandler
    16. public void onDeath(PlayerDeathEvent event) {
    17. String message = event.getDeathMessage();
    18. event.setDeathMessage(null);
    19.  
    20. for(String s : messagesOff) {
    21. Player p = Bukkit.getServer().getPlayer(s);
    22. p.sendMessage(message);
    23. }
    24. }
    25.  
    26. @EventHandler
    27. public void onLeave(PlayerQuitEvent event) {
    28. if(messagesOff.contains(event.getPlayer().getName()) {
    29. messagesOff.remove(event.getPlayer().getName());
    30. }
    31. }
    32.  
     
  6. Offline

    Wolf7115

    Code:
    if(!deathMessagesEnabled){
        event.setDeathMessage(null);
    }
    Just put that in the PlayerDeathEvent place. You must also just set "deathMessagesEnabled" yourself, but that part's easy.

    Code:
    if(cmd.getName().equalsIgnoreCase("dm")){
        deathMessagesEnabled = !deathMessagesEnabled;
    }
    ^This code (when placed in the onCommand function will just toggly death messages without the use of an argument.
     
  7. Offline

    rodney12345

    Thanks for the help! You guys are the best! So this is what I have so far. Anything I should add/delete?
    Code:
    public class Main extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Main plugin;
     
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
        }
     
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Disabled!");
        }
     
        List<String> messagesOff = new ArrayList <String>();
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args, Object deathMessagesEnabled){
            Player p = (Player) sender;
            if(commandLabel.equalsIgnoreCase("dm")) {
             
                p.sendMessage(ChatColor.GRAY + "Death Messages has been toggled!");
            }
            String name = p.getName();
            if(messagesOff.contains(name)){
                messagesOff.remove(name);
            }
         
            else{
                messagesOff.add(name);
            }
         
            return false;
        }
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
            String message = event.getDeathMessage();
            event.setDeathMessage(null);
         
            for(String s : messagesOff) {
                Player p = Bukkit.getServer().getPlayer(s);
                p.sendMessage(message);
            }
        }
     
        @EventHandler
        public void onLeave(PlayerQuitEvent event) {
            if(messagesOff.remove(event.getPlayer().getName())){
                messagesOff.remove(event.getPlayer().getName());
            }
        }
    }
    
    anyone?:eek:

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

    Minecrell

    I think you should register your events.
     
  9. Offline

    kiwhen

    There is actually quit a bit of code that doesn't make much sense in there.
    • "public static Main plugin" is not used.
    • The logger is not used (for anything useful).
    • Method onDisable() is not used (for anything useful).
    • Classes with event listeners must implement the Listener interface.
    • As pointed out above, event listeners must be registered using registerEvents().
    • There is an illegal cast to player in onCommand(). "sender" may not be a Player object at this point.
    • Don't use the command alias to verify command names. Use Command.getName() instead, otherwise only the specified alias will work, not the actual command.
    • onCommand() takes four arguments; CommandSender, Command, String and String[].
    • If a class is only registered for one command, a name check for commands is not needed, as the method will not trigger on unregistered command calls.
    • onCommand() should always return true when successful. Returning false will cause Bukkit to send the player a message on how this command is used, as specified in your plugin.yml file.
    • "messagesOff" has no defined visibility (package-wide visibility), when it's only ever used in the private scope.
    • PlayerQuitEvent attempts to remove a players name twice. Calling ArrayList.remove() on an element that is not present in the array will not cause any errors.
    • ArrayList.remove() returns true if an element was removed, false otherwise. This can be used to speed up the toggling in onCommand() by attempting to remove the element first, and if this returns false, add the element instead.
    • There is no null check present for "p" in PlayerDeathEvent. NullPointerException errors may occur (bad practice).
    • (Tip) Eventlisteneres are usually named "on", plus the name of the event, minus "Event". PlayerQuitEvent should for instance be named "onPlayerQuit", and so on. Things like that makes it easier for others to read through your code.
    • (Tip) Send status messages after operations are complete, in case the operation crashes. If for some reason the toggling does not work, the player will still receive a completion message.
    I've made the adjustments below, but I haven't tested it yet. These are mostly suggestions, though.

    Code:java
    1.  
    2. public class Main extends JavaPlugin implements Listener {
    3. public void onEnable(){
    4. getServer().getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. private List<String> messagesOff = new ArrayList <String>();
    8.  
    9. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    10. if(!(sender instanceof Player)) {
    11. return true;
    12. }
    13.  
    14. Player p = (Player) sender;
    15.  
    16. String name = p.getName();
    17. if(!messagesOff.remove(name)){
    18. messagesOff.add(name);;
    19. }
    20.  
    21. p.sendMessage(ChatColor.GRAY + "Death Messages has been toggled!");
    22.  
    23. return true;
    24. }
    25.  
    26. @EventHandler
    27. public void onPlayerDeath(PlayerDeathEvent e) {
    28. String message = e.getDeathMessage();
    29. e.setDeathMessage(null);
    30.  
    31. for(String s : messagesOff) {
    32. Player p = Bukkit.getServer().getPlayer(s);
    33. if(p != null) {
    34. p.sendMessage(message);
    35. }
    36. }
    37. }
    38.  
    39. @EventHandler
    40. public void onPlayerLeave(PlayerQuitEvent e) {
    41. messagesOff.remove(e.getPlayer().getName());
    42. }
    43. }
    44.  
     
Thread Status:
Not open for further replies.

Share This Page