KitPvP Golden Apple Drop Not Working

Discussion in 'Plugin Development' started by Camaflicks, May 11, 2014.

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

    Camaflicks

    Hello guys! I recently got help from another member on the forums, and when I tried to do what he told me, it didn't work. I am making a kitpvp plugin for my server, but I want it so that when you die, you drop a golden apple that when you pickup, it automagically gives you the golden apple effects. Here is my code:

    Code:java
    1. package com.invictuspvp.InvictusPvP;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.GameMode;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.enchantments.Enchantment;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.entity.PlayerDeathEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.PlayerInventory;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class Main extends JavaPlugin implements Listener {
    23. public static Main plugin;
    24. public int number = 15;
    25.  
    26. public void onEnable() {
    27. Bukkit.broadcastMessage(ChatColor.GREEN + "UPDATE NOTES");
    28. Bukkit.broadcastMessage(ChatColor.YELLOW
    29. + "In v1.2 of InvictusPvP Kits, by jeezerman and PistonGaming, we have successfully:");
    30. Bukkit.broadcastMessage(ChatColor.WHITE
    31. + "- Added custom names to each item");
    32. Bukkit.broadcastMessage(ChatColor.WHITE + "- Added permissions");
    33. Bukkit.broadcastMessage(ChatColor.WHITE + "- Fixed a few bugs");
    34. }
    35.  
    36. @EventHandler
    37. public void onDeath(PlayerDeathEvent event) {
    38. event.getEntity()
    39. .getWorld()
    40. .dropItem(event.getEntity().getLocation(),
    41. new ItemStack(Material.GOLDEN_APPLE, 1));
    42. }
    43.  
    44. @SuppressWarnings("deprecation")
    45. public boolean onCommand(CommandSender sender, Command cmd,
    46. String commandLabel, String[] args) {
    47. Player p = (Player) sender;
    48. PlayerInventory inv = p.getInventory();
    49. if (cmd.getName().equalsIgnoreCase("archer")) {
    50. List<String> ls = new ArrayList<String>();
    51. p.sendMessage(ChatColor.GREEN + "You have selected the Archer kit!");
    52. p.setGameMode(GameMode.SURVIVAL);
    53. inv.clear();
    54. inv.setHeldItemSlot(1);
    55. p.setHealth(20);
    56. p.setFoodLevel(20);
    57. inv.setHelmet(setName(new ItemStack(Material.IRON_HELMET),
    58. "Archer Helmet", ls));
    59. inv.setChestplate(setName(new ItemStack(Material.IRON_CHESTPLATE),
    60. "Archer Chestplate", ls));
    61. inv.setLeggings(setName(new ItemStack(Material.IRON_LEGGINGS),
    62. "Archer Leggings", ls));
    63. inv.setBoots(setName(new ItemStack(Material.IRON_BOOTS),
    64. "Archer Boots", ls));
    65. ItemStack archeraxe = new ItemStack(Material.GOLD_AXE, 1);
    66. archeraxe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 2);
    67. archeraxe.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
    68. inv.addItem(archeraxe);
    69. ItemStack archerbow = new ItemStack(Material.BOW, 1);
    70. archerbow.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1);
    71. inv.addItem(archerbow);
    72. ItemStack archerrod = new ItemStack(Material.FISHING_ROD, 1);
    73. inv.addItem(archerrod);
    74. ItemStack arrow = new ItemStack(Material.ARROW, 1);
    75. inv.addItem(arrow);
    76. }
    77. if (cmd.getName().equalsIgnoreCase("warrior")) {
    78. List<String> ls = new ArrayList<String>();
    79. p.sendMessage(ChatColor.GREEN
    80. + "You have selected the Warrior kit!");
    81. p.setGameMode(GameMode.SURVIVAL);
    82. inv.clear();
    83. inv.setHeldItemSlot(1);
    84. p.setHealth(20);
    85. p.setFoodLevel(20);
    86. inv.setHelmet(setName(new ItemStack(Material.IRON_HELMET),
    87. "Warrior Helmet", ls));
    88. inv.setChestplate(setName(new ItemStack(Material.IRON_CHESTPLATE),
    89. "Warrior Chestplate", ls));
    90. inv.setLeggings(setName(new ItemStack(Material.IRON_LEGGINGS),
    91. "Warrior Leggings", ls));
    92. inv.setBoots(setName(new ItemStack(Material.IRON_BOOTS),
    93. "Warrior Boots", ls));
    94. ItemStack warriorsword = new ItemStack(Material.IRON_SWORD, 1);
    95. warriorsword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    96. warriorsword.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
    97. inv.addItem(warriorsword);
    98. ItemStack archerrod = new ItemStack(Material.FISHING_ROD, 1);
    99. inv.addItem(archerrod);
    100. }
    101. if (cmd.getName().equalsIgnoreCase("knight")) {
    102. List<String> ls = new ArrayList<String>();
    103. p.sendMessage(ChatColor.GREEN + "You have selected the Knight kit!");
    104. p.setGameMode(GameMode.SURVIVAL);
    105. inv.clear();
    106. inv.setHeldItemSlot(1);
    107. p.setHealth(20);
    108. p.setFoodLevel(20);
    109. inv.setHelmet(setName(new ItemStack(Material.IRON_HELMET),
    110. "Knight Helmet", ls));
    111. inv.setChestplate(setName(new ItemStack(Material.IRON_CHESTPLATE),
    112. "Knight Chestplate", ls));
    113. inv.setLeggings(setName(new ItemStack(Material.IRON_LEGGINGS),
    114. "Knight Leggings", ls));
    115. inv.setBoots(setName(new ItemStack(Material.IRON_BOOTS),
    116. "Knight Boots", ls));
    117. inv.getHelmet().addUnsafeEnchantment(
    118. Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    119. inv.getChestplate().addUnsafeEnchantment(
    120. Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    121. inv.getLeggings().addUnsafeEnchantment(
    122. Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    123. inv.getBoots().addUnsafeEnchantment(
    124. Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    125. ItemStack knightsword = new ItemStack(Material.DIAMOND_SWORD, 1);
    126. knightsword.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
    127. inv.addItem(knightsword);
    128. ItemStack knightrod = new ItemStack(Material.FISHING_ROD, 1);
    129. inv.addItem(knightrod);
    130. ItemStack knightbow = new ItemStack(Material.BOW, 1);
    131. knightbow.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1);
    132. knightbow.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1);
    133. inv.addItem(knightbow);
    134. ItemStack arrow = new ItemStack(Material.ARROW, 1);
    135. inv.addItem(arrow);
    136. }
    137. if (cmd.getName().equalsIgnoreCase("kits")) {
    138. p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + ""
    139. + ChatColor.STRIKETHROUGH
    140. + "---------------------------------------------");
    141. p.sendMessage(ChatColor.GREEN + "Default Kit(s): "
    142. + ChatColor.WHITE + "Archer, Warrior");
    143. p.sendMessage(ChatColor.GREEN + "Donor Kit(s): " + ChatColor.WHITE
    144. + "Knight");
    145. p.sendMessage(ChatColor.DARK_GREEN + "To change kit, type "
    146. + ChatColor.WHITE + "/[kit]");
    147. p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + ""
    148. + ChatColor.STRIKETHROUGH
    149. + "---------------------------------------------");
    150. }
    151. return false;
    152. }
    153.  
    154. private ItemStack setName(ItemStack is, String name, List<String> lore) {
    155. ItemMeta im = is.getItemMeta();
    156. if (name != null) {
    157. im.setDisplayName(name);
    158. }
    159. if (lore != null) {
    160. im.setLore(lore);
    161. }
    162. is.setItemMeta(im);
    163. return is;
    164. }
    165. }
    166.  
     
  2. Offline

    CarPet

    I can't even find when you are supposed to be adding the effects, you drop the apple but then you do nothing with it
     
  3. Offline

    Camaflicks


    I know :( I don't know how to do it at all. Some guy said it would work, but looking at the code all it does is "drop" a golden apple, which doesn't acutally work. :p
     
  4. Offline

    CarPet

    So the apple isn't even dropped is what you are asking to be solved?

    EDIT: You haven't even registered the events, you can do that with
    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);
    2.  
     
  5. Offline

    Camaflicks


    Well that is the first thing. But the important thing is: how to add effects to it so it automatically gives me golden apple effects, instead of me having to eat it, and also are there plugins premade to do this? an example is on playastro.net or on CanadianFries YouTube channel.
     
  6. Listen to PlayerPickupItemEvent or PlayerItemPickupEvent idk. Then check if the type is GOLDEN_APPLE, if so, add the potion effects using event.getPlayer().addPotionEffect(new PotionEffect(BLAH, BLAH, BLAH));
     
  7. Offline

    Camaflicks


    Thank you! But still, the apple wont drop. DId I even add the EventHandler correctly? Could you make an example?
     
  8. So you did register the event as the person previously said? Yes you did EventHandler correctly. This may not work as the player may instantly pick it up and then their inventory is cleared as they die. Maybe make a scheduler and spawn the item 10 ticks after they die.
     
  9. Offline

    Camaflicks



    Yay! Thank you for tghat :) Now it dropss!


    Okay so, to make this clear: I added the Register Event, and now the apple drops. So, when someone kills me, an apple should be there. Whoever picks the apple up (whether it be the person who killed the other person or just some random player) should get golden apple effects for blank amount of time. Is this possible? Again, look on playastro.net or CanadianFries channel for an example.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
    KingFaris11 likes this.
  10. Offline

    CarPet

    Camaflicks Yes it is very possible, right now the apple will drop but not do anything to the person that picked it up because you haven't done anything with the PlayerPickupItemEvent yet, you will have to check the item that the player picked up and the apply the effect
     
    Camaflicks likes this.
  11. Offline

    Camaflicks


    Awesome! Thank you for all the help man! This was a little more difficult than I expected, but probably since I haven't dealt much with EventHandlers. Do you recommend any tutorials for me to get good at EventHandlers? Anyways, thank you so much! Now I can finish my plugin :)
     
  12. Offline

    CarPet

    There are a few good tutorials out there now these days I believe, I started with basic java and got into Bukkit a few months later.

    The best two channels out there seem to be (in my opinion)
    Pogostick: https://www.youtube.com/user/PogoStick29Dev/playlists Has a ton of stuff you can learn there!
    BCBroz: https://www.youtube.com/playlist?list=PL4A0C6138F3F5AA41

    Glad I could help

    Camaflicks You realize there is an exact plugin like this right? http://dev.bukkit.org/bukkit-plugins/gappledrop/

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

    Camaflicks


    One last question before I annyot you :p How do I check if it is a golden apple? Or where can I found out? Should I check the bukkit documentation?
     
  14. Offline

    hubeb

    Camaflicks
    Code:java
    1. ItemStack apple = new ItemStack(Material.GOLD_APPLE,1,(short)1);
    2. //Drop item apple = god apple/super golden apple/enchanted golden apple
     
  15. Offline

    Camaflicks

  16. Haha what a coincidence, the author of that plugin's my friend, he's an amateur at plugin developing so don't worry. ;) I'll make sure he doesn't abandon it.
     
  17. Offline

    Camaflicks

    Than
    Thanks! :D Funny :p
     
  18. Offline

    CarPet

    Camaflicks yeah, seems you learned quite a bit! Have fun learning and see you some other time!
     
  19. Offline

    CandyCranium

    KingFaris11
    No one likes you, smart one >:) And yes, I'll keep it updated.
     
    KingFaris11 likes this.
  20. Offline

    Camaflicks

    Cya :)

    Did you know that I love you? LOL :D Thnx for the plugin

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

    CandyCranium

    Camaflicks
    Lol no problem. Lots of people love me. :)
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page