[Unsolved]How to get online players then give them items

Discussion in 'Plugin Development' started by ASHninja1997, Jul 31, 2013.

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

    ASHninja1997

    I tried several ways to do this but I can't seem to find a way that works. So this code saves the player who executed the command in a hashmap with a item.
    Code:
        final Player p = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("WoodHoe")){
            for(Player player: getServer().getOnlinePlayers()) {
                if (!player.hasPermission("Snipecraft.WoodHoe")){player.sendMessage(ChatColor.BLUE + "You have to buy this gun!");return true;}
              if (fweapon.containsKey(p.getName())) {
                    player.sendMessage(ChatColor.AQUA + "You have already selected a" + ChatColor.RED + " Primary " + ChatColor.AQUA + "weapon");
                    } else {
              List<String> ls = new ArrayList<String>();
              ls.add(ChatColor.BOLD + "SnipeCraft");
              ls.add(ChatColor.GREEN + "Shoots a arrow at 1.5x volcity");
              ItemStack b = cname(new ItemStack(Material.WOOD_HOE), ChatColor.GOLD + "Wood_Hoe", ls);
              fweapon.put(p.getName(), b);
              player.sendMessage(ChatColor.GREEN + "Secondary Weapon:" + ChatColor.RED + " Wood_Hoe");
              return true;
            }}}
    And this code retrives the item
    Code:
    ItemStack retrieve = fweapon.get(p.getName());
    and this code gives the player the item in their inventory
    Code:
    p.getInventory().addItem(new ItemStack(retrieve));
    What could I define as p so bukkit go through all online players and gets their name?
     
  2. Offline

    xTrollxDudex

    ASHninja1997
    Haven't you already solved this with your above code?
     
  3. Offline

    Pizza371

    xTrollxDudex this is what i thought too lol.. but then I thought I must be missing the question. xD
    for(Player p : Bukkit.getOnlinePlayers()) {
    //stuff?? but you already did this xD
    }
     
    xTrollxDudex likes this.
  4. Offline

    ASHninja1997

    Pizza371 xTrollxDudex
    I must be stupid of something xD.....So you just use for(Player p : Bukkit.getOnlinePlayers()){ } to get all online players and their name's so I can give them their item

    I thought you have to go futher with the code
    like
    Code:
            for(Player po = Bukkit.getOnlinePlayers())
                                    if(!(po.getPlayer().isOnline() || po.getPlayer() == null)) return;
                                    ItemStack retrieve = fweapon.get(po.getPlayer().getName());
                                    po.getPlayer().getName().getInventory().addItem(new ItemStack(retrieve));
    I know this doesn't work :p

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

    Pizza371

    ASHninja1997 xD but it already makes sure they're online players (getOnlinePlayers()) and it wont be null so yea your making it more difficult for yourself xD
     
  6. Just an idea, when you execute the command it will get the online players and give them a specific item. I personally think this is better and simpler than adding the players to a hashmap and then retrieving them.
     
  7. Offline

    Pizza371

  8. Nvm lol. Now I see what he's trying to do xD
     
  9. Offline

    xTrollxDudex

  10. Offline

    JPG2000

    ASHninja1997 I think I know what you trying to do. Why dont you make a Players. Player players = Bukkit.getOnlinePlayers();
    . Then check if players,hasPermission. Finnaly, create a ItemStack. ItemStack woodhoe = new ItemStack(Material.WOOD_HOE);. Then do players.getInventory().add(woodhoe);.

    Checking back, im not sure if Player players = bukkit.getOnlinePlayers() will work. I think it will, but not positive.
     
  11. Offline

    ASHninja1997

    JPG2000 xTrollxDudex Pizza371
    For a more in depth description on what I am trying to do is....I am trying to make it on my server for players to select a item through command then once a countdown has ended it gives them their items. So lets say I type in the command woodhoe.....That command would put me in the hashmap with a wood hoe, Then a countdown starts from 60 and when it gets to 0 they get their item's. The only problem I am using this
    Code:
                                      for (Player po : Bukkit.getOnlinePlayers()){
                                            if(!(po.isOnline() || po.getPlayer() == null)) return;
                                          ItemStack retrieve = fweapon.get(po);
                                          po.getInventory().addItem(retrieve);}
    as my code and it still doesn't work it gives me a "Item can't be null".......So I guess I didn't register the item Stack correctly, can anyone help me with that. Look at my first post for the code on how I registered the Item Stack.
     
  12. Offline

    xTrollxDudex

    ASHninja1997
    Is fweapon a HashMap<Player, ItemStack>

    Show entire code/any errors.
     
  13. Offline

    ASHninja1997

    xTrollxDudex
    This Is my hashmap for fweapon
    Code:
    HashMap<String, ItemStack> fweapon = new HashMap<String, ItemStack>();
     
  14. Offline

    Samthelord1

    ; in your if
    if (!player.hasPermission("Snipecraft.WoodHoe")){player.sendMessage(ChatColor.BLUE + "You have to buy this gun!");return true;}
     
  15. Offline

    xTrollxDudex

    ASHninja1997
    You have your problem. ItemStack retrieve would be fweapon.get(po.getName())
     
    ASHninja1997 likes this.
  16. Offline

    Samthelord1

    Oh sorry. Your format kinda messed though.
     
  17. Offline

    ASHninja1997

    xTrollxDudex
    Thank you I will try this and tell you what happens
    ??????? PlayerjoinEvent then set them banned :p ?????????
     
  18. Offline

    xTrollxDudex

  19. Offline

    Samthelord1

  20. Offline

    ASHninja1997

    xTrollxDudex
    you are awesome!! it worked!!

    Samthelord1
    Awesome signature :D

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

    Samthelord1

    ASHninja1997 you're forgetting my thanks? Remember.. I told you how to make the best plugin known to man..
     
  22. Offline

    ASHninja1997

    xD, thank you
     
  23. Offline

    xTrollxDudex

    Samthelord1
    You're like the third person to get ideas from my sig :D
     
  24. Offline

    Samthelord1

Thread Status:
Not open for further replies.

Share This Page