Solved Help with coding arguments

Discussion in 'Plugin Development' started by DragonSlayer1920, Jul 17, 2013.

Thread Status:
Not open for further replies.
  1. Hi, I am trying to make a command that will send both the sender and target a message. Like /test the sender would see "test" the target might see "also a test" but if the target cannot be found I would like it to say [Test]Player not found!



    Here is my code

    Code:
    package me.DragonSlayer1920.Test;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
     
    public class Test extends JavaPlugin {
     
    public final Logger logger = Logger.getLogger("Minecraft");
     
    @Override
    public void onEnable() {
     
    }
     
    @Override
    public void onDisable() {
     
    }
     
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
     
    if(!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.DARK_RED + "[Test]" + ChatColor.RED + "This command is for players only!" );
    return true;
    }
    Player p = (Player) sender;
    if (commandLabel.equalsIgnoreCase("test")) {
    Player target = (Bukkit.getServer().getPlayer(args[0]));
    if (target == null) {
    p.sendMessage(ChatColor.DARK_RED + "[Test]" + ChatColor.RED + "Usage:/c <player> <reason>");
    return true;
     
    }
    }
     
     
    return false;
    }
     
    }
    Here is my consle

    Code:
    2013-07-17 15:29:12 [INFO] DragonSlayer1920 issued server command: /test
    2013-07-17 15:29:12 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin Test v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
    at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:523)
    at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:965)
    at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:883)
    at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:840)
    at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:109)
    at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
    at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.DragonSlayer1920.Test.Test.onCommand(Test.java:38)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more
     
    }
    Please Help Me :)
     
  2. Offline

    HackintoshMan

    First, check for args.

    Code:
    Player p = (Player) sender;
    if (commandLabel.equalsIgnoreCase("test")) {
        if(args.length != 1){
            return false;
        }
        else if(args.length == 1){
            try{
                Player target = args[0].getPlayer;
                p.sendMessage("Test message to the sender");
                target.sendMessage("Test message to the target");
            }catch(Exception e){
                p.sendMessage("Please type in a players name!");
            }
        }
    Done without an IDE, so it may not work, but you get the gist...
     
    DragonSlayer1920 likes this.
  3. HackintoshMan thanks for the help code you supplied worked with one minor change here is my final code

    Code:
    Player p = (Player) sender;
            if (commandLabel.equalsIgnoreCase("test")) {
                if(args.length != 1){
                    p.sendMessage("Need A Player!");
                    return false;
                }
                else if(args.length == 1){
                    try{
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        p.sendMessage("Test message to the sender");
                        target.sendMessage("Test message to the target");
     
  4. Offline

    HackintoshMan

    @DragonSlayer19200

    You are very welcome. Do you understand what all of that means now?
     
  5. HackintoshMan yes now I do you solved my one error in my plugin.

    HackintoshMan for some reason your code stopped working, there is no error in the consle or anything. Never mind I just noticed I wasn't exporting it to the right place.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    xTrollxDudex

    DragonSlayer1920
    Code? Please put it in pastebin or surround in syntax for cleanliness this time.
    syntax=java]Your code here[/syntax]
     
  7. xTrollxDudex Just put it in Code:


    Anyways nice signature
     
  8. Offline

    xTrollxDudex

  9. Offline

    tamajpm

    Code:
    package me.DragonSlayer1920.src;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
        public static Main plugin;
        Logger logger = Logger.getLogger("Minecraft");
        public Player player;
        public Player target;
       
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " has been enabled.");
        }
       
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " has been disabled.");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            this.player = (Player) sender;
            this.target = Bukkit.getServer().getPlayer(args[0]);
            if(commandLabel.equalsIgnoreCase("test")) {
                if(args.length < 1 || args.length > 1) {
                    player.sendMessage(ChatColor.RED + "Please use /test <Target username>");
                } else if(args.length == 1) {
                    if(target != null) {
                        player.sendMessage(ChatColor.GREEN + "Test for player!!!");
                        target.sendMessage(ChatColor.GREEN + "Test for target!!!");
                    } else if(target == null) {
                        player.sendMessage(ChatColor.RED + "This player isn't online!");
                    }
                }
            }
           
            return false;
        }
    }
    
     
  10. Offline

    xTrollxDudex

    DragonSlayer1920
    Lol
    <----stupid

    tamajpm wrote something, try that before I show anything.
    But otherwise, did you fix it?
     
  11. Offline

    tamajpm

  12. Offline

    xTrollxDudex

  13. @xTeollxDudex I think he was meaning to say he didn't see that post
    whatever you say

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  14. Offline

    xTrollxDudex

  15. xTrollxDudex likes this.
Thread Status:
Not open for further replies.

Share This Page