Setting Armor

Discussion in 'Plugin Development' started by MrLizardDogMan, Feb 20, 2014.

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

    MrLizardDogMan

    Ok, so I've been looking around for awhile and i can't find an answer on how would i set a player's armor.

    Code:java
    1. else if(cmd.getName().equalsIgnoreCase("class")){
    2. if(args[1] == "knight"){
    3. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + " Choosing Knight Class..");
    4. }
    5.  


    what would i have to add to do something like set the helmet to a leather helmet?
     
  2. Offline

    gyroninja

  3. Offline

    Compressions

    MrLizardDogMan You can set each armor piece individually or add all armor pieces as an array.
    Array index Armor piece
    0 Boots
    1 Leggings
    2 Chestplate
    3 Helmet
     
    MrLizardDogMan likes this.
  4. Offline

    MrLizardDogMan

    Ok thanks, could you give me example code?
     
  5. Offline

    Compressions

    MrLizardDogMan
    Code:
    ItemStack[] armor = {};
    armor[0] = null;
    armor[1] = null;
    armor[2] = null;
    armor[3] = new ItemStack(Material.LEATHER_HELMET, 1);
     
    player.getInventory().setArmorContents(armor);
     
  6. Offline

    MrLizardDogMan

    Mhh, when i do that i get this,

    Code:java
    1. [21:12:03] [Server thread/INFO]: MrLizardDogMan issued server command: /join mall archer
    2. [21:12:03] [Server thread/ERROR]: null
    3. org.bukkit.command.CommandException: Unhandled exception executing command 'join' in plugin KitPvP v0.0.9
    4. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1237]
    5. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) ~[spigot.jar:git-Spigot-1237]
    6. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:546) ~[spigot.jar:git-Spigot-1237]
    7. at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:962) [spigot.jar:git-Spigot-1237]
    8. at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:817) [spigot.jar:git-Spigot-1237]
    9. at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1237]
    10. at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1237]
    11. at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:147) [spigot.jar:git-Spigot-1237]
    12. at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [spigot.jar:git-Spigot-1237]
    13. at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:679) [spigot.jar:git-Spigot-1237]
    14. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:273) [spigot.jar:git-Spigot-1237]
    15. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:540) [spigot.jar:git-Spigot-1237]
    16. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:446) [spigot.jar:git-Spigot-1237]
    17. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [spigot.jar:git-Spigot-1237]
    18. Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    19. at me.MrLizard.KitPvP.KitPvP.onCommand(KitPvP.java:79) ~[?:?]
    20. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1237]
    21. ... 13 more
    22.  


    Code
    Code:java
    1. package me.MrLizard.KitPvP;
    2.  
    3. import net.minecraft.server.v1_7_R1.NBTTagList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17. import org.bukkit.potion.PotionEffect;
    18. import org.bukkit.potion.PotionEffectType;
    19.  
    20.  
    21. public class KitPvP extends JavaPlugin{
    22.  
    23. public static KitPvP plugin;
    24.  
    25. @Override
    26. public void onDisable() {
    27. }
    28.  
    29. @Override
    30. public void onEnable() {
    31. //Register Events
    32. plugin.getServer().getPluginManager().registerEvents((Listener) this, this);
    33. }
    34.  
    35. //Commands
    36. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    37. if (!(sender instanceof Player)) {
    38. sender.sendMessage("You cannot run this command through the console!");
    39. return false;
    40. }
    41. Player player = (Player) sender;
    42. if(cmd.getName().equalsIgnoreCase("Lobby")){
    43. //Reset Player to Default
    44. player.setFoodLevel(20);
    45. player.setHealth(20.0);
    46. player.setGameMode(GameMode.SURVIVAL);
    47. player.setMaxHealth((double)20);
    48. ((CraftPlayer)player).getHandle().inventory.b(new NBTTagList());
    49. //Sends Player Joining Message
    50. player.sendMessage(ChatColor.GOLD + "Joining Lobby...");
    51. //Teleporting Player
    52. player.teleport(new Location(Bukkit.getWorld("world"), -785, 27, 195, 180, 2));
    53. }
    54.  
    55. //Classes
    56. else if(cmd.getName().equalsIgnoreCase("join")){
    57. if (args.length == 0){
    58. // Showing the usage of the command here would be good.
    59. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Usage: /join [ARENA] [CLASS]");
    60. }
    61. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("knight")){
    62. // Player chose knight!
    63. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Choosing Knight Class...");
    64. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Joining Mall...");
    65. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    66. ItemStack[] armor = {};
    67. armor[0] = new ItemStack(Material.CHAINMAIL_BOOTS, 1);
    68. armor[1] = new ItemStack(Material.CHAINMAIL_LEGGINGS, 1);
    69. armor[2] = new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1);
    70. armor[3] = new ItemStack(Material.CHAINMAIL_HELMET, 1);
    71. player.getInventory().setArmorContents(armor);
    72. }
    73. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("archer")){
    74. // Player chose archer!
    75. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Choosing Archer Class...");
    76. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Joining Mall...");
    77. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    78. ItemStack[] armor = {};
    79. armor[0] = new ItemStack(Material.LEATHER_BOOTS, 1);
    80. armor[1] = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    81. armor[2] = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    82. armor[3] = new ItemStack(Material.LEATHER_HELMET, 1);
    83. player.getInventory().setArmorContents(armor);
    84. }
    85. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("tank")){
    86. // Player chose Tank!
    87. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Choosing Tank Class...");
    88. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Joining Mall...");
    89. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    90. }
    91. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("scout")){
    92. // Player chose Scout!
    93. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Choosing Scout Class...");
    94. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + "Joining Mall...");
    95. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    96. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 0, 1));
    97. }
    98. }
    99. return false;
    100. }
    101. }
    102.  
    103.  
    104.  


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

    werter318

    MrLizardDogMan Replace ItemStack[] armor = {} With: ItemStack[] armor = new ItemStack[4];
     
Thread Status:
Not open for further replies.

Share This Page