Solved sendMessage() has extra char

Discussion in 'Plugin Development' started by DatScreamer, Mar 16, 2016.

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

    DatScreamer

    Hi. I have code to enable something and send the player a confimation but it has an extra word in it that I did not put there. http://prntscr.com/ag64mz When I enter /deathsound enable there should be no extra /deathsound printed. Why is it there? Can anyone see something I can't? Also if I change the command to /ds the message with contain /ds. It should'nt.
    Code:
    package com.screamer;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class plugin extends JavaPlugin implements CommandExecutor, Listener {
    
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
    
            Logger logger = getLogger();
            logger.info(pdfFile.getName() + " V." + pdfFile.getVersion() + ")" + " had been Enabled!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
    
            logger.info(pdfFile.getName() + " (V." + pdfFile.getVersion() + ")" + " had been disabled!");
        }
    
        public boolean deathsoundenabled;
      
        @EventHandler
        public void onDeath(PlayerDeathEvent event) { // e (or event)
            if (deathsoundenabled) {
                event.getEntity().getWorld().playSound(event.getEntity().getLocation(), Sound.AMBIENT_CAVE, 2, 1);
            }
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("deathsound")) {
                if (args.length == 0) { // Sender only typed the base command and nothing else.
                    sender.sendMessage("To use this command you need to enter either" + ChatColor.GREEN + " enable" + ChatColor.WHITE + " or" + ChatColor.GREEN + " disable.");
                } else {// Sender typed more than 1 word, so args[0] can't be null.
                    if (args[0].equalsIgnoreCase("enable")) {
                        deathsoundenabled = true;
                        // Send player a message
                        sender.sendMessage(
                                "You have" + ChatColor.GREEN + " enabled" + ChatColor.WHITE + " the DeathSound.");
                    } else {
                        if (args[0].equalsIgnoreCase("disable")) {
                            deathsoundenabled = false;
                            // Send player a message
                            sender.sendMessage("You have" + ChatColor.GREEN + " disabled" + ChatColor.WHITE + " the DeathSound.");
                        }
                    }
                }
            }
            return false;
        }
    }
    
     
    Last edited: Mar 16, 2016
  2. Offline

    blablubbabc

    Change your last line in the onCommand-method from 'return false;' to 'return true;'.

    If you return false, bukkit will send the command usage (which you can define in the plugin.yml).
     
  3. Offline

    DatScreamer

    Oh, thanks. I was debugging and changed that, forgot about it! Sweet.
     
Thread Status:
Not open for further replies.

Share This Page