Solved Plugin prints command as message

Discussion in 'Plugin Development' started by blue1, Jan 21, 2016.

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

    ShowbizLocket61

    @Konato_K
    Yes, it terminates it if the thrown exception is not handled. Thus, it prevents your plugin from breaking, by halting the execution in case the exception is not handled.
     
  2. Offline

    Konato_K

    @ShowbizLocket61 I'm not entirely sure if halting execution can be considered "prevent from breaking", technically it breaks it, in some way
     
  3. Offline

    blue1

    The plugin works beautiously now, and I shall post the code here so that it might help someone else later. I don't know if there's anything else I should change. I know I could check for length of args, but seeing as this is all I wanted out of the plugin, I think I'll leave it as-is.
    Main class (open)

    Code:
    package me.blue1.skyemusic;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.blue1.skyemusic.cmds.Music;
    
    public class Main extends JavaPlugin{
        @Override
        public void onEnable(){
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN+"Music has been Enabled successfully");
            getCommand("play").setExecutor(new Music());
            getCommand("sm").setExecutor(new Music());
            getCommand("skyemusic").setExecutor(new Music());
        }
    }
    

    Music Class (open)

    Code:
    package me.blue1.skyemusic.cmds;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Music implements CommandExecutor {
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player p = (Player) sender;
            String prefix = new String(ChatColor.GRAY+"["+ChatColor.DARK_AQUA+"Music"+ChatColor.GRAY+"] ");
            if(cmd.getName().equalsIgnoreCase("play")){
               
                 if(args[0].equalsIgnoreCase("13")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Bells");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.GOLD_RECORD.getId());
                 }
                 if(args[0].equalsIgnoreCase("blocks")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Blocks");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_3.getId());
                 }
                 if(args[0].equalsIgnoreCase("cat")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Cat");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.GREEN_RECORD.getId());
                 }
                 if(args[0].equalsIgnoreCase("chirp")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Chirp");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_4.getId());
                 }
                 if(args[0].equalsIgnoreCase("far")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Far");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_5.getId());
                 }
                 if(args[0].equalsIgnoreCase("mall")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Mall");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_6.getId());
                 }
                 if(args[0].equalsIgnoreCase("mellohi")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Mellohi");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_7.getId());
                 }
                 if(args[0].equalsIgnoreCase("stal")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Stal");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_8.getId());
                 }
                 if(args[0].equalsIgnoreCase("strad")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Strad");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_9.getId());
                 }
                 if(args[0].equalsIgnoreCase("ward")){
                     p.sendMessage(prefix+ChatColor.GOLD+"You have selected music disc Ward");
                     p.getLocation().getWorld().playEffect(p.getLocation(), Effect.RECORD_PLAY, Material.RECORD_10.getId());
                 }
            }
            if((cmd.getName().equalsIgnoreCase("sm"))||(cmd.getName().equalsIgnoreCase("skyemusic"))){
                 p.sendMessage(ChatColor.LIGHT_PURPLE+"SkyeMusic Help:");
                 p.sendMessage(ChatColor.GOLD+"Just type /play <discName> to play some music!");
                 p.sendMessage(ChatColor.LIGHT_PURPLE+"Discs:");
                 p.sendMessage(ChatColor.GOLD+"13");
                 p.sendMessage(ChatColor.GOLD+"blocks");
                 p.sendMessage(ChatColor.GOLD+"cat");
                 p.sendMessage(ChatColor.GOLD+"chirp");
                 p.sendMessage(ChatColor.GOLD+"far");
                 p.sendMessage(ChatColor.GOLD+"mall");
                 p.sendMessage(ChatColor.GOLD+"mellohi");
                 p.sendMessage(ChatColor.GOLD+"stal");
                 p.sendMessage(ChatColor.GOLD+"strad");
                 p.sendMessage(ChatColor.GOLD+"ward");
            }
            return true;
        }
    }

    Hope this helps someone. If there's something I can do to improve it, let me know. If I should add this as a plugin of mine to be downloaded, also let me know.
     
Thread Status:
Not open for further replies.

Share This Page