[Player Heads]

Discussion in 'Plugin Development' started by DigitalCookie, Mar 24, 2014.

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

    DigitalCookie

    I am now making a player heads plugin. I have all the meta set up but I need how to do /head (playername) and getting that player
     
  2. Offline

    ImPhantom

    DigitalCookie
    Like how to make the command? Or how to adapt the meta to the command?
     
  3. Offline

    DigitalCookie

    both ImPhantom

    This is what I have so far, head code from pogostick29dev's tutorials, thanks pogo!
    Code:java
    1. publicboolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2.  
    3. //console sender
    4. if(!(sender instanceof Player)) {
    5. sender.sendMessage(prefix + ChatColor.RED + "Only players can use this command! nice try, yours truley - Zanilla");
    6. return true;
    7. }
    8.  
    9. //commands
    10.  
    11. Player p = (Player) sender;
    12.  
    13. if (cmd.getName().equalsIgnoreCase("head")) {
    14. if (args.length == 0) {
    15. p.sendMessage(prefix + ChatColor.RED + "Please specify a player.");
    16. return true;
    17.  
    18. Player target = Bukkit.getServer().getPlayer(args[0]);
    19. if (target == null) {
    20. p.sendMessage(prefix + ChatColor.RED + "Could not find player " + args[0] + "!");
    21. return true;
    22. }
    23. }
    24.  
    25.  
    26.  
    27.  
    28. ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
    29.  
    30. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    31. meta.setOwner(p.getTarget().getName());
    32. meta.setDisplayName(ChatColor.LIGHT_PURPLE + p.getTarget().getName());
    33. skull.setItemMeta(meta);
    34.  
    35. p.getPlayer().getInventory().addItem(skull);
    36. p.getPlayer().sendMessage(prefix + ChatColor.GOLD + "You got " + ChatColor.GREEN + p.getTarget().getName() + "'s" + ChatColor.GOLD + "head!");
    37.  
    38. }
    39. }
    40. returntrue;
    41. }
    42. }
    43. }


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

    ShadowLAX

    DigitalCookie By checking if the sender is a player before the actual command check, you just denied console access to every command...

    You also don't need to check if the player is online either. Just set a new skull ItemStack's owner args[0].
     
    badboysteee98 likes this.
  5. Offline

    DigitalCookie

    ok I did that all, now I am getting errors in the code in this line
    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[0]);
     
  6. Offline

    ShadowLAX

    ... You don't need to actually get the player, as I said above.
     
  7. Offline

    DigitalCookie

    ShadowLAX , sorry im a new dev anyways, if I remove that line how would I get the target?
    Code:java
    1. if (target == null) {
    2. p.sendMessage(prefix + ChatColor.RED + "Could not find player " + args[0] + "!");
    3. return true;
    4. }
    5.  
    6.  
    7. ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
    8.  
    9. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    10. meta.setOwner(target.getName());
    11. meta.setDisplayName(ChatColor.LIGHT_PURPLE + target.getName());
    12. skull.setItemMeta(meta);
    13.  
    14. p.getPlayer().getInventory().addItem(skull);
    15. p.getPlayer().sendMessage(prefix + ChatColor.GOLD + "You got " + ChatColor.GREEN + target.getName() + "'s" + ChatColor.GOLD + "head!");

    I get errors all over the targets
     
  8. Offline

    ShadowLAX

    DigitalCookie You don't get a player. You get the first argument (args[0]) and parse that as the skull's owner.
     
  9. Offline

    DigitalCookie

    ShadowLAX how would I do that? I watched TheNewBostons tutorials on parsing and I dont understand how to apply it to this project
     
  10. Offline

    ShadowLAX

  11. Offline

    DigitalCookie

    ShadowLAX still receiving errors on | String target = args[0] | I feel im doing something wrong
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2.  
    3. Player p = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("head")) {
    6. if (args.length == 0) {
    7. p.sendMessage(prefix + ChatColor.RED + "Please specify a player.");
    8. return true;
    9.  
    10. String target = args[0];
    11. if (target == null) {
    12. p.sendMessage(prefix + ChatColor.RED + "Could not find player " + args[0] + "!");
    13. return true;
    14. }
    15.  
    16. ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
    17. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    18. skull.setOwner(args[0]);
    19.  
    20. meta.setOwner(target.getName());
    21. meta.setDisplayName(ChatColor.LIGHT_PURPLE + target.getName());
    22. skull.setItemMeta(meta);
    23.  
    24. p.getPlayer().getInventory().addItem(skull);
    25. p.getPlayer().sendMessage(prefix + ChatColor.GOLD + "You got " + ChatColor.GREEN + target.getName() + "'s" + ChatColor.GOLD + "head!");
    26.  
    27. }
    28. }
    29.  
    30. if(args[0].equalsIgnoreCase("help")) {
    31. p.sendMessage(ChatColor.GRAY + "=====" + prefix + ChatColor.GRAY + "=====");
    32. p.sendMessage(ChatColor.GREEN + "/" + ChatColor.GRAY + "head <playername>" + ChatColor.GREEN + " | " + ChatColor.LIGHT_PURPLE + "Spawns in a head with that players skin :p");
    33. p.sendMessage(ChatColor.GREEN + "/" + ChatColor.GRAY + "head help" + ChatColor.GREEN + " | " + ChatColor.LIGHT_PURPLE + "Shows this dialog.");
    34. }
    35. return true;
    36. }
    37. }


    bump

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

    badboysteee98

    Just going over the code :)

    **Bump Once Every 24hours**
     
  13. Offline

    ShadowLAX

    DigitalCookie First, you didn't close the if/else statement on line 6 correctly, so everything after the return true; will throw an error. Add a } on line 9. Second, remove the if target is equal to null check, I already said you could remove that. Third, you don't need line 18. That is unnecessary. Fourth, you can't get the name of a string... Fix those, and it should work.
     
  14. Offline

    FabeGabeMC

    DigitalCookie Why not do this.
    Code:java
    1. ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3);
    2. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    3.  
    4. meta.setOwner(args[0]);
    5. meta.setDisplayName(ChatColor.LIGHT_PURPLE + args[0]);
    6. skull.setItemMeta(meta);
    7.  
    8. p.getPlayer().getInventory().addItem(skull);
    9. p.getPlayer().sendMessage(prefix + ChatColor.GOLD + "You got " + ChatColor.GREEN + args[0] + "'s" + ChatColor.GOLD + "head!");
    10.  


    Because there is no such skull.setOwner();
    Also if you want to do it for any player (online or not), do this:

    Code:java
    1. //Fixed code
    2. if (cmd.getName().equalsIgnoreCase("head")) {
    3. if (args.length == 0) {
    4. p.sendMessage(prefix + ChatColor.RED + "Please specify a player.");
    5. return true;
    6. }else if(args.length == 1){
    7. ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
    8. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    9.  
    10. meta.setOwner(args[0]);
    11. meta.setDisplayName(ChatColor.LIGHT_PURPLE + target.getName());
    12. skull.setItemMeta(meta);
    13.  
    14. p.getPlayer().getInventory().addItem(skull);
    15. p.getPlayer().sendMessage(prefix + ChatColor.GOLD + "You got " + ChatColor.GREEN + args[0] + "'s" + ChatColor.GOLD + "head!");
    16.  
    17. }
    18. }


    Make sure you add if(args.length == 1)
    if you want to make a sub-command (example: /head Notch, "Notch" is argument 2, which is 1 in Java. It goes like 0,1,2,3,4,5 and so on).

    Hope this helps.

    ~Gabe
     
Thread Status:
Not open for further replies.

Share This Page