Commands functions

Discussion in 'Plugin Development' started by luckie12, Feb 29, 2012.

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

    luckie12

    Hey all!

    i have a question i want to make some /kill and /setxp commands but i get a error here:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args);
    if(cmd.getName().equalsIgnoreCase("Kill"){
    Player player = (Player)sender;
    Player target = player.getWorld().getPlayer(args[0]);
    target.setHealth(0);
    }
    return false;
    }
    
    this line
    Code:
    Player target = player.getWorld().getPlayerargs[0]);
    
    the .getPlayer(args[0]);

    gives a error
    Code:
    The method getPlayer(String) is undefined for the type World
    
    And for the XP thing
    i want to have /setxp <name> <amount of xp>
    but i only know how to do /setxp <name>
    not how to add the amount thing :/
    maybe someone can help with that to thanx

    someone knows what is wrong?
     
  2. Offline

    dead4y

    Use this : Player target = player.getServer().getPlayer(args[0]);
    oh and in your command type return true; this tell that the command is successful.
     
  3. Offline

    luckie12

    not working :/
    it says Unknown command blablabla

    And how to do that with /setexp and then the name and amound? :eek:
     
  4. Offline

    dead4y

    and you must have in your plugin.yml this:
    commands:
    kill:
    description: Destroys a human bean.
    usage: /<command> player
    Edit:
    player.giveExp(amount of exp); I think this is used to give player exp.
     
  5. Offline

    luckie12

    i mean they can insert in chat how much xp ;)

    and it still says unknown command :/

    this is what i have:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if(cmd.getName().equalsIgnoreCase("Murd")){
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            target.setHealth(0);
        }
              if(cmd.getName().equalsIgnoreCase("setexp")){
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            target.giveExp(arg0);
        }
        return false;
    }
    
    and my plugin.yml
    Code:
    name: TestMPlugin
    main: me.ha.ha.Main
    version: 1.1
    commands:
    Murd:
    description: Destroys a human bean.
    usage: /<command> player
    setexp:
    description: Sets a player XP.
    usage: /<command> player amount
    
     
  6. Offline

    LinkterSHD

    you need to import bukkit in the external jars and then import all the errors. As i cant see any import.(name) in the code
     
  7. Offline

    luckie12

    its not the full code? o.o
    its a part of it.. its only the oncommand...
    eclipse doesnt give any errors so...
     
  8. Offline

    dead4y

    i will try to make it and to give you the code w8 some seconds
     
  9. Offline

    luckie12

    ok if u need the full code just ask me ;)
     
  10. Offline

    dead4y

    Look this :
    if (sender instanceof Player) // this is optinal...
    { int amount;
    if (command.getName().equalsIgnoreCase("expgive"))
    {
    Player target = player.getServer().getPlayer(args[1]);
    amount = Integer.parseInt(args[2]);
    target.giveExp(amount);
    }
    } else
    {
    System.out.println("You must use this command in game!");
    }
    try this and tell us the results
     
  11. Offline

    luckie12

    hmmm
    still not working still saying unknown blabla
    this is my plugin.yml now
    Code:
    name: TestMPlugin
    main: me.ha.ha.Main
    version: 1.1
    commands:
    expgive:
    description: Destroys a human bean.
    usage: /<command> player amount
    
    someone ? :( Sorry for bump oo.oo

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

    Njol

    Is this really how your plugin.yml looks like? It should be indented like this:

    Code:
    name: TestMPlugin
    main: me.ha.ha.Main
    version: 1.1
    commands:
        expgive:
            description: Destroys a human bean.
            usage: /<command> player amount
    
     
  13. Offline

    luckie12

    thanx but everytime i do like /expgive luckie12 1000
    it just says in chat /expgive player amount
    same with /Murd it just says /Murd player

    it doesnt give anthing :O ?
     
  14. because you return false even if the command went like expected.
     
  15. Offline

    luckie12

    So what do i have to change? return false to true? :O or remove it?
     
  16. do not remove it. neither change it to true IN ALL cases, because sometimes you need it.
    if everything went fine, return true, else false.
     
  17. Offline

    luckie12

    so .. what causes that problem that it doesnt do the function?
    it still says like /expgive player amount
    it doesnt give anything
     
  18. What is the command you use in game to set the exp? Is it /setexp myplayername 10000?

    If so, thats not working! Your code looks for the args [0], which in the example above is "myplayername", not 10000! You dont even need to type the player name ingame, you already made the code check who is typing the command!

    Try this command ingame:

    /setexp 1000

    This should set your exp to the amount 1000! If not, let me know!
     
  19. Offline

    luckie12

    .. i mean i can set the other players XP not my own...
     
  20. Offline

    Sorroko

    luckie12 hey there, could you post your full code? and the commands you are typing in game?
     
  21. Offline

    luckie12

    lol xD i told that like 100times
    but here:
    Code:
    package me.ha.ha;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Chicken;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Sheep;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    public class MyBlockListener implements Listener {
    @EventHandler
    public void blockBreaking(BlockBreakEvent event){
    if (event.getBlock().getType() == Material.NETHER_BRICK_STAIRS)
      event.getPlayer().setHealth(2);//2 half hearts = 1 heart
      if (event.getBlock().getType() == Material.NETHER_BRICK)
      event.getPlayer().setHealth(2);//2 half hearts = 1 heart
      if (event.getBlock().getType() == Material.DIRT)
      event.getPlayer().getInventory();
      if (event.getBlock().getType() == Material.COBBLESTONE)
      event.getBlock().getDrops();
    }
    @EventHandler
    public void blockPlacing(BlockPlaceEvent event){
      if (event.getBlock().getType() == Material.TNT)
      event.getPlayer().setHealth(0);
    }
    @EventHandler
        public void onEntityDeath(EntityDeathEvent e) {
            Entity entity = e.getEntity();
            if (entity instanceof Creeper) {
                e.setDroppedExp(10000);
            }
            if(entity instanceof Sheep) {
              e.setDroppedExp(100000);
            }
            if(entity instanceof Chicken) {
            e.setDroppedExp(1000000000);
            }
            if(entity instanceof Zombie) {
            e.setDroppedExp(-999999999);
            }
        }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if(cmd.getName().equalsIgnoreCase("Murd")){
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            target.setHealth(0);
        }
              if(cmd.getName().equalsIgnoreCase("setexp")){
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            target.giveExp(8888);
        }
              if (sender instanceof Player) // this is optinal...
              { int amount;
              if (cmd.getName().equalsIgnoreCase("expgive"))
              {
                Player player = (Player)sender;
              Player target = player.getServer().getPlayer(args[1]);
              amount = Integer.parseInt(args[2]);
              target.giveExp(amount);
              }
              } else
              {
              System.out.println("You must use this command in game!");
              }
              return true;
    }
    }
     
    
    this is just a apart class
    i also have a Main class thing
     
  22. ah, got. The command isn't doing anything because the class you're using it in, doesn't implements commandexecutor and depending on your code, you might have also forgot to register it (I don't know.)
     
  23. Offline

    luckie12

    the Main.java

    Code:
    package me.ha.ha;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin {
    public final MyBlockListener blockListener = new MyBlockListener();
    public void onDisable() {
    System.out.println("TESTMPLUGIN DISABLEDEEDD!");
    }
    public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    System.out.println("TESTMPLUGIN ENABLEDEEDD!");
    pm.registerEvents(this.blockListener, this);
    }
     
    }
    
    someone can fix it for me then please?

    EDIT:Added commandexecutor
    still not working ...
    Tanx in advance!
     
  24. you added the command executor but did you set it to the command?
     
  25. Offline

    Njol

    add getCommand("setexp").setExecutor(blockListener); to your onEnable().
    (repeat for all commands, i.e. "Murd" and "expgive")
     
  26. Offline

    luckie12

    Hey ive added that to my onEnable but when i typ in gamce /Murd it says internal error occurred... and more
    and i get this in my console:
    Code:
    16:51:32 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'murd
    ' in plugin TestMPlugin v1.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:3
    86)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:777)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:737)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:725)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:536)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.ha.ha.MyBlockListener.onCommand(MyBlockListener.java:54)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 12 more
    
    i get this also with /expgive

    But i have 2 classes Main.java and MyBlockListener.java
    onEnable is in Main.java
    But the oncommand is in MyBlockListener.java
    So... do i have to add something to the .setExecutor?
     
  27. Offline

    Njol

    see that error?
    Code:
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at me.ha.ha.MyBlockListener.onCommand(MyBlockListener.java:54)
    I guess it's because you use args[0] without checking whether there is an argument specified. Add this everywhere where you expect one argument or more, or right at the beginning of onCommand if all commands at least require one argument:
    Code:java
    1. if (args.length == 0) {
    2. return false;
    3. }
     
  28. Offline

    luckie12

    hmm like this?

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
      if (args.length == 0) {
      return false;
      }
        if(cmd.getName().equalsIgnoreCase("Murd")){
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            target.setHealth(0);
        }
              if(cmd.getName().equalsIgnoreCase("setexp")){
            Player player = (Player)sender;
            Player target = player.getServer().getPlayer(args[0]);
            target.giveExp(8888);
        }
              if (sender instanceof Player) // this is optinal...
              { int amount;
              if (cmd.getName().equalsIgnoreCase("expgive"))
              {
                Player player = (Player)sender;
              Player target = player.getServer().getPlayer(args[1]);
              amount = Integer.parseInt(args[2]);
              target.giveExp(amount);
              }
              } else
              {
              System.out.println("You must use this command in game!");
              }
              return false;
    }
    
    If that has to be good this error comes up ingame i can do /expgive
    it says use /expgive player amount but if i do like /expgive luckie12 1000 i get same error as before
    But it says now outofboundsexception 2
    hmm


    Code:
    17:06:17 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'expg
    ive' in plugin TestMPlugin v1.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:3
    86)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:777)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:737)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:725)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:536)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
            at me.ha.ha.MyBlockListener.onCommand(MyBlockListener.java:71)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 12 more
    
    Error also on /expgive luckie12
    but nothing on /expgive

    EDIT: /murd player Works good
    only /expgive not

    Someone?:p
    [[srry for bump]][diamond]

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

    Sodapop98

    in Player target = player.getServer().getPlayer(args[1]); change that args[1] to args[0] and in amount = Integer.parseInt(args[2]); change args[2] to args[1].
    Hope this helped.
     
  30. Offline

    JayEffKay

    @lucky12 Sure, I'll give you another push :p

    ArrayIndexOutOfBoundsException usually gets thrown when you try to get an element that doesn't exist from an array.

    I think this line:
    Code:
    amount =  Integer.parseInt(args[2])
    
    First of all always when you access something from an array[123] make damn sure the array contains that element:
    Code:
    if (args.length() > 2) {
    //ok now go
        amount =  Integer.parseInt(args[2])
    }
    
    But you got to keep in mind, with the following command:
    /expgive luckie12 1000
    expgive is the command.
    luckie12 is the first argument, that's args[0].
    1000 is the second argument, and that's args[1].
    An array always start at index 0, you got that right with the other two commands though.

    edit: someone beat me to it :p
     
Thread Status:
Not open for further replies.

Share This Page