Toggle armour with command

Discussion in 'Plugin Development' started by glasseater, Sep 25, 2014.

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

    glasseater

    Hey guys,
    Today I have a question with toggling my disco armour with a command such as /disco on and /disco off. However, it does not seem to be working. Here is my code.

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3. Player p = event.getPlayer();
    4. ItemStack blaze = new ItemStack(Material.BLAZE_POWDER);
    5. ItemMeta im = blaze.getItemMeta();
    6. im.setDisplayName("§a§lDisco Armour");
    7. blaze.setItemMeta(im);
    8. p.getInventory().addItem(blaze);
    9. p.updateInventory();
    10.  
    11. }
    12.  
    13. @EventHandler
    14. public void onPlayerQuit(PlayerQuitEvent event) {
    15. Player p = event.getPlayer();
    16. p.getInventory().remove(Material.BLAZE_POWDER);
    17. disco.remove(p.getName());
    18.  
    19. }
    20.  
    21. @EventHandler(priority = EventPriority.HIGH)
    22. public void onPlayerUse(PlayerInteractEvent event) {
    23. Player o = event.getPlayer();
    24.  
    25. Action a = event.getAction();
    26. if (a == Action.RIGHT_CLICK_AIR) {
    27. if (o.getItemInHand().getType() == Material.BLAZE_POWDER
    28. && o.hasPermission("disco.use")) {
    29. if (o.getItemInHand().getItemMeta().getDisplayName()
    30. .equals("§a§lDisco Armour")) {
    31.  
    32. if (disco.contains(o.getName())) {
    33. o.sendMessage(prefix
    34. + "Your disco armour has been disabled!");
    35. disco.remove(o.getName());
    36. o.getInventory().setHelmet(
    37. new ItemStack(Material.AIR, 1));
    38. o.getInventory().setChestplate(
    39. new ItemStack(Material.AIR, 1));
    40. o.getInventory().setLeggings(
    41. new ItemStack(Material.AIR, 1));
    42. o.getInventory().setBoots(
    43. new ItemStack(Material.AIR, 1));
    44. o.updateInventory();
    45. } else if (!disco.contains(o.getName())) {
    46. disco.add(o.getName());
    47. o.sendMessage(prefix
    48. + "Your disco armour has been enabled!");
    49. o.getInventory().setHelmet(
    50. new ItemStack(Material.LEATHER_HELMET));
    51. o.getInventory().setChestplate(
    52. new ItemStack(Material.LEATHER_CHESTPLATE));
    53. o.getInventory().setLeggings(
    54. new ItemStack(Material.LEATHER_LEGGINGS));
    55. o.getInventory().setBoots(
    56. new ItemStack(Material.LEATHER_BOOTS));
    57. Bukkit.getServer()
    58. .getScheduler()
    59. .scheduleSyncRepeatingTask(this,
    60. new Runnable() {
    61. public Random r = new Random();
    62.  
    63. public void run() {
    64. Color c = Color.fromRGB(
    65. r.nextInt(255),
    66. r.nextInt(255),
    67. r.nextInt(255));
    68.  
    69. for (Player p : Bukkit
    70. .getServer()
    71. .getOnlinePlayers()) {
    72. if (p.getInventory()
    73. .getHelmet() != null
    74. && p.getInventory()
    75. .getHelmet()
    76. .getType() == Material.LEATHER_HELMET) {
    77. p.getInventory()
    78. .setHelmet(
    79. getColorArmor(
    80. Material.LEATHER_HELMET,
    81. c));
    82. }
    83.  
    84. if (p.getInventory()
    85. .getChestplate() != null
    86. && p.getInventory()
    87. .getChestplate()
    88. .getType() == Material.LEATHER_CHESTPLATE) {
    89. p.getInventory()
    90. .setChestplate(
    91. getColorArmor(
    92. Material.LEATHER_CHESTPLATE,
    93. c));
    94. }
    95.  
    96. if (p.getInventory()
    97. .getLeggings() != null
    98. && p.getInventory()
    99. .getLeggings()
    100. .getType() == Material.LEATHER_LEGGINGS) {
    101. p.getInventory()
    102. .setLeggings(
    103. getColorArmor(
    104. Material.LEATHER_LEGGINGS,
    105. c));
    106. }
    107.  
    108. if (p.getInventory()
    109. .getBoots() != null
    110. && p.getInventory()
    111. .getBoots()
    112. .getType() == Material.LEATHER_BOOTS) {
    113. p.getInventory()
    114. .setBoots(
    115. getColorArmor(
    116. Material.LEATHER_BOOTS,
    117. c));
    118. }
    119. }
    120. }
    121. }, 0, 1);
    122. }
    123. }
    124. }
    125. }
    126. }
    127.  
    128. public void onEnable() {
    129. getServer().getPluginManager().registerEvents(this, this);
    130. }
    131.  
    132. public boolean onCommand(CommandSender sender, Command cmd,
    133. String commandLabel, String[] args) {
    134. Player player = (Player) sender;
    135. if (commandLabel.equalsIgnoreCase("disco")) {
    136. if (args.length == 0) {
    137. sender.sendMessage(ChatColor.BLUE
    138. + "==========================================");
    139. sender.sendMessage(ChatColor.GOLD
    140. + "This plugin was developed by Glasseater84");
    141. sender.sendMessage(ChatColor.BLUE
    142. + "==========================================");
    143. }
    144. if (args.length == 1) {
    145. if (args[0].equalsIgnoreCase("on")
    146. || args[0].equalsIgnoreCase("enable")) {
    147. if (disco.contains(player.getName())) {
    148. player.sendMessage(prefix
    149. + "Your disco armour is already enabled!");
    150. return false;
    151. }
    152. if (!disco.contains(player.getName())) {
    153. disco.add(player.getName());
    154. player.sendMessage(prefix
    155. + "Your disco armour is now enabled.");
    156.  
    157. }
    158.  
    159. }
    160. if (args[0].equalsIgnoreCase("off")
    161. || args[0].equalsIgnoreCase("disable")) {
    162. if (!disco.contains(player.getName())) {
    163. player.sendMessage(prefix
    164. + "Your disco armour is already disabled!");
    165. return false;
    166. }
    167. if (disco.contains(player.getName())) {
    168. disco.add(player.getName());
    169. player.sendMessage(prefix
    170. + "Your disco armour is now disabled.");
    171.  
    172. }
    173.  
    174. }
    175. }
    176. return false;
    177.  
    178. }
    179. return false;
    180. }
    181.  
    182. public ItemStack getColorArmor(Material m, Color c) {
    183. ItemStack i = new ItemStack(m, 1);
    184. LeatherArmorMeta meta = (LeatherArmorMeta) i.getItemMeta();
    185. meta.setColor(c);
    186. i.setItemMeta(meta);
    187. return i;
    188. }
    189. }
    190.  


    So the only problem is disabling and enabling with the command! Thank you in advance!
    P.S I still want the item to work when you click it!
     
  2. Offline

    CoolGuy2001

    You are not doing anything when you run the command! :p Make a method for equipping the armor! :)
     
  3. Offline

    glasseater

    CoolGuy2001 Ok, but, I equip the armor from line 87 down - So what would I do - re-type everything from there?

    bump?

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

    CoolGuy2001

    Nope, cut the equipping from the event. Then make a new method like equipArmor(). Paste the code in the method. Call the method equipArmor() in the event, AND the command! :)
     
  5. Offline

    CraftCreeper6

  6. Offline

    glasseater

    CraftCreeper6 Yes, my events are registered.

    CoolGuy2001 CraftCreeper6 Ok, so I have created my method here:
    Code:java
    1. public void setArmor(PlayerInteractEvent e){
    2. Player p = e.getPlayer();
    3. p.sendMessage(prefix + "Your disco armour has been enabled!");
    4. p.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    5. p.getInventory().setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
    6. p.getInventory().setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
    7. p.getInventory().setBoots(new ItemStack(Material.LEATHER_BOOTS));
    8. }
    9.  

    But the method cannot be called to 'o' because we use that in the event.

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

    CoolGuy2001

    Look at some basic Java tutorials. Do not put the event in the method. You should not be basing the event off your method. Here is a hint:
    Code:
    public void equipArmor(Player p)
    {
        //Do stuff here
    }
     
Thread Status:
Not open for further replies.

Share This Page