Giving items through console?

Discussion in 'Plugin Development' started by JustinsCool15, Jul 29, 2014.

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

    JustinsCool15

    So I'm making a custom little item that I want people to get through voting. The votifier listener using a command based system so I need to set it up so my plugin can give an item through console pretty much.

    I know how to give an item stack using a command with a target. But I don't know how to setup the target variable when console runs the command.

    If someone could just point me in the right direction that would be great. If you need any further info I will be happy to oblige.
     
  2. Offline

    LCastr0

    You would need a new argument to get the player/target of the command
    E.g:
    /give LCastr0 1 64
    Would give me 64 stones
     
  3. Offline

    JustinsCool15

    LCastr0

    I tried
    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[0]);

    that didn't seem to work with console.
     
  4. Offline

    LCastr0

    You don't need to get the player from the server, try doing this:
    Code:java
    1. Player target = Bukkit.getPlayer(args[0]);
     
  5. Offline

    JustinsCool15

    LCastr0
    Thanks a lot. That worked. Is there any sort of checks I should do to make sure I don't get exceptions later on?
     
  6. Offline

    LCastr0

    @JustinsCool
    Yes, you need to check if the player you are trying to get is online.
    You can do it by 2 ways:
    Code:java
    1. if(Bukkit.getOnlinePlayers().contains(targetPlayer)){
    2. //the player is online
    3. } else {
    4. //the player is offline
    5. }

    (This one is not recommended though)
    (Note: I think getOnlinePlayers is an array/collection, so it won't have .contains method. I'm in my phone so I cannot check it now)
    Or
    Code:java
    1. if(targetPlayer!=null){
    2. //the player is online
    3. } else {
    4. //the player is offline
    5. }
    6.  

    (This one is more recommended, and smaller :p)
    If you still need anything, send me a PM or tahg me C:
     
  7. Offline

    ZodiacTheories

Thread Status:
Not open for further replies.

Share This Page