Make in-hand item disappear after points received?

Discussion in 'Plugin Development' started by isaytoast24, Sep 1, 2013.

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

    isaytoast24

    I have a plugin that will give you a skull when you get the kill, you can then redeem that skull for points. the problem is the skull stays in your inventory after you redeem the skull, can you make the skull disappear from their hand after they have received their points? (and possibly make enderman particles spawn and the enderman teleporting sound like the skull has teleported away?)


    The part "p.setItemInHand(null);" is what I have added (it's what I though would work and has not necessarily worked) Search for it (ctrl +f) to see it in the code. the area of code that is affected is lines 84-around 120.


    Here is my code:

    Code:java
    1. package to.joe.decapitation.command;
    2.  
    3. import java.sql.Timestamp;
    4. import java.util.Date;
    5. import java.util.List;
    6. import java.util.logging.Level;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandExecutor;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.SkullMeta;
    16.  
    17. import to.joe.decapitation.Bounty;
    18. import to.joe.decapitation.Decapitation;
    19. import to.joe.decapitation.datastorage.DataStorageException;
    20.  
    21. public class BountyCommand implements CommandExecutor {
    22.  
    23. Decapitation plugin;
    24.  
    25. public BountyCommand(Decapitation decapitation) {
    26. plugin = decapitation;
    27. }
    28.  
    29. private void sendHelp(CommandSender sender) {
    30. if (sender.hasPermission("decapitation.bounty.search"))
    31. sender.sendMessage(ChatColor.RED + "/bounty search [username] - search for a bounty on a player");
    32. if (sender.hasPermission("decapitation.bounty.list"))
    33. sender.sendMessage(ChatColor.RED + "/bounty list <page> - list current bounties");
    34. if (sender.hasPermission("decapitation.bounty.place"))
    35. sender.sendMessage(ChatColor.RED + "/bounty place [username] [price] - place a bounty on a player");
    36. if (sender.hasPermission("decapitation.bounty.claim"))
    37. sender.sendMessage(ChatColor.RED + "/bounty claim - claim the bounty of the head you are holding");
    38. if (sender.hasPermission("decapitation.bounty.remove"))
    39. sender.sendMessage(ChatColor.RED + "/bounty remove [username] - remove the bounty of a player");
    40. if (sender.hasPermission("decapitation.bounty.listown"))
    41. sender.sendMessage(ChatColor.RED + "/bounty listown - list unclaimed bounties you have created");
    42. if (sender.hasPermission("decapitation.bounty.place")) {
    43. sender.sendMessage(ChatColor.RED + "/bounty redeem - claim any heads that are owed to you");
    44. sender.sendMessage(ChatColor.RED + "Current tax rate is " + plugin.getTax() + "%");
    45. }
    46. }
    47.  
    48. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    49. if (!(sender instanceof Player)) {
    50. sender.sendMessage(ChatColor.RED + "Players only");
    51. return true;
    52. }
    53. if (!plugin.bounties) {
    54. sender.sendMessage(ChatColor.RED + "Bounties are not enabled");
    55. return true;
    56. }
    57. if (args.length == 0) {
    58. sendHelp(sender);
    59. return true;
    60. }
    61. Player p = (Player) sender;
    62. if (args.length == 1) {
    63. if (args[0].equalsIgnoreCase("list") && sender.hasPermission("decapitation.bounty.list")) {
    64. try {
    65. List<Bounty> bounties = plugin.getDsi().getBounties(0, 9);
    66. if (bounties.size() > 0) {
    67. sender.sendMessage(ChatColor.GREEN + "=========" + ChatColor.GOLD + " Bounties [Page 1 of " + (plugin.getDsi().getNumBounties() + 8) / 9 + "] " + ChatColor.GREEN + "=========");
    68. for (Bounty b : bounties) {
    69. if (sender.hasPermission("decapitation.bounty.viewissuer")) {
    70. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted() + " placed by " + b.getIssuer());
    71. } else {
    72. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted());
    73. }
    74. }
    75. } else {
    76. sender.sendMessage(ChatColor.RED + "There are no bounties");
    77. }
    78. } catch (DataStorageException e) {
    79. plugin.getLogger().log(Level.SEVERE, "Error getting list of bounties", e);
    80. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    81. }
    82. return true;
    83. }
    84. if (args[0].equalsIgnoreCase("claim") && sender.hasPermission("decapitation.bounty.claim")) {
    85. if (p.getItemInHand().getType() != Material.SKULL_ITEM) {
    86. sender.sendMessage(ChatColor.RED + "That's not a head");
    87. return true;
    88. }
    89. ItemStack i = p.getItemInHand();
    90. SkullMeta meta = (SkullMeta) i.getItemMeta();
    91. if (!meta.hasOwner()) {
    92. sender.sendMessage(ChatColor.RED + "That head is not named");
    93. return true;
    94. }
    95. if (!plugin.canClaimOwn && meta.getOwner().equals(p.getName())) {
    96. sender.sendMessage(ChatColor.RED + "You may not turn in your own head");
    97. return true;
    98. }
    99. try {
    100. String hunted = meta.getOwner();
    101. Bounty b = plugin.getDsi().getBounty(hunted);
    102. if (b != null) {
    103. b.setHunter(p.getName());
    104. b.setHunted(hunted);
    105. b.setTurnedIn(new Timestamp(new Date().getTime()));
    106. plugin.getDsi().updateBounty(b);
    107. Decapitation.economy.depositPlayer(p.getName(), b.getReward());
    108. sender.sendMessage(ChatColor.GREEN + "Sucessfully turned in bounty on " + b.getHunted() + " for " + Decapitation.economy.format(b.getReward()));
    109. p.setItemInHand(null);
    110. Player issuer = plugin.getServer().getPlayer(b.getIssuer());
    111. if (issuer != null) {
    112. ItemStack c = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    113. SkullMeta me = (SkullMeta) c.getItemMeta();
    114. me.setOwner(hunted);
    115. c.setItemMeta(me);
    116. if (!issuer.getInventory().addItem(c).isEmpty()) {
    117. issuer.sendMessage(ChatColor.RED + "Not enough room in your inventory to give you a skull");
    118. return true;
    119. }
    120. b.setRedeemed(new Timestamp(new Date().getTime()));
    121. plugin.getDsi().updateBounty(b);
    122. }
    123. } else {
    124. sender.sendMessage(ChatColor.RED + "There does not appear to be a bounty on that head");
    125. return true;
    126. }
    127. } catch (DataStorageException e) {
    128. plugin.getLogger().log(Level.SEVERE, "Error claiming bounty", e);
    129. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    130. return true;
    131. }
    132. return true;
    133. }
    134. if (args[0].equalsIgnoreCase("listown") && sender.hasPermission("decapitation.bounty.listown")) {
    135. try {
    136. List<Bounty> bounties = plugin.getDsi().getOwnBounties(p.getName());
    137. if (bounties.size() > 0) {
    138. sender.sendMessage(ChatColor.GREEN + "========= " + ChatColor.GOLD + "Your bounties " + ChatColor.GREEN + "=========");
    139. for (Bounty b : bounties) {
    140. if (sender.hasPermission("decapitation.bounty.viewissuer")) {
    141. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted() + " placed by " + b.getIssuer());
    142. } else {
    143. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted());
    144. }
    145. }
    146. } else {
    147. sender.sendMessage(ChatColor.RED + "You have no bounties");
    148. }
    149. } catch (DataStorageException e) {
    150. plugin.getLogger().log(Level.SEVERE, "Error getting list of bounties", e);
    151. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    152. }
    153. return true;
    154. }
    155. if (args[0].equalsIgnoreCase("redeem") && sender.hasPermission("decapitation.bounty.place")) {
    156. try {
    157. List<Bounty> bounties = plugin.getDsi().getUnclaimedBounties(p.getName());
    158. if (bounties.size() == 0) {
    159. sender.sendMessage(ChatColor.RED + "Nothing to redeem");
    160. }
    161. for (Bounty b : bounties) {
    162. ItemStack i = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    163. SkullMeta meta = (SkullMeta) i.getItemMeta();
    164. meta.setOwner(b.getHunted());
    165. i.setItemMeta(meta);
    166. if (!p.getInventory().addItem(i).isEmpty()) {
    167. p.sendMessage(ChatColor.RED + "Not enough room in your inventory to give you a skull");
    168. return true;
    169. }
    170. b.setRedeemed(new Timestamp(new Date().getTime()));
    171. plugin.getDsi().updateBounty(b);
    172. }
    173. } catch (DataStorageException e) {
    174. plugin.getLogger().log(Level.SEVERE, "Error getting list of unredeemed bounties", e);
    175. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    176. }
    177. return true;
    178. }
    179. sendHelp(sender);
    180. return true;
    181. }
    182. if (args.length == 2) {
    183. if (args[0].equalsIgnoreCase("search") && sender.hasPermission("decapitation.bounty.search")) {
    184. try {
    185. List<Bounty> bounties = plugin.getDsi().getBounties(args[1]);
    186. int count = 0;
    187. if (bounties.size() > 0) {
    188. sender.sendMessage(ChatColor.GREEN + "=========" + ChatColor.GOLD + " Bounties matching " + args[1] + " " + ChatColor.GREEN + "=========");
    189. for (Bounty b : bounties) {
    190. if (sender.hasPermission("decapitation.bounty.viewissuer")) {
    191. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted() + " placed by " + b.getIssuer());
    192. } else {
    193. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted());
    194. }
    195. count++;
    196. if (count == 8) {
    197. sender.sendMessage(ChatColor.GOLD + "Plus " + (bounties.size() - count) + " more");
    198. return true;
    199. }
    200. }
    201. } else {
    202. sender.sendMessage(ChatColor.RED + "No bounties match your query");
    203. }
    204. } catch (DataStorageException e) {
    205. plugin.getLogger().log(Level.SEVERE, "Error searching for bounties", e);
    206. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    207. return true;
    208. }
    209. return true;
    210. }
    211. if (args[0].equalsIgnoreCase("list") && sender.hasPermission("decapitation.bounty.list")) {
    212. try {
    213. int page = Integer.parseInt(args[1]);
    214. List<Bounty> bounties = plugin.getDsi().getBounties((page - 1) * 9, page * 9);
    215. if (bounties.size() > 0) {
    216. sender.sendMessage(ChatColor.GREEN + "=========" + ChatColor.GOLD + "Bounties [Page " + page + " of " + (plugin.getDsi().getNumBounties() + 8) / 9 + "]" + ChatColor.GREEN + "=========");
    217. for (Bounty b : bounties) {
    218. if (sender.hasPermission("decapitation.bounty.viewissuer")) {
    219. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted() + " placed by " + b.getIssuer());
    220. } else {
    221. sender.sendMessage(ChatColor.GOLD + "" + b.getReward() + " - " + b.getHunted());
    222. }
    223. }
    224. } else {
    225. sender.sendMessage(ChatColor.RED + "There are no bounties");
    226. }
    227. } catch (NumberFormatException e) {
    228. sender.sendMessage(ChatColor.RED + "That's not a number");
    229. } catch (DataStorageException e) {
    230. plugin.getLogger().log(Level.SEVERE, "Error getting list of bounties", e);
    231. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    232. }
    233. return true;
    234. }
    235. if (args[0].equalsIgnoreCase("remove") && sender.hasPermission("decapitation.bounty.remove")) {
    236. if (!args[1].matches("[A-Za-z0-9_]{2,16}")) {
    237. sender.sendMessage(ChatColor.RED + "That doesn't appear to be a valid username");
    238. return true;
    239. }
    240. try {
    241. Bounty bounty = plugin.getDsi().getBounty(args[1], p.getName());
    242. if (bounty != null) {
    243. plugin.getDsi().deleteBounty(bounty);
    244. Decapitation.economy.depositPlayer(p.getName(), bounty.getReward() - bounty.getReward() * plugin.getTax());
    245. sender.sendMessage(ChatColor.GREEN + "Deleted bounty against " + bounty.getHunted() + " for " + Decapitation.economy.format(bounty.getReward()));
    246. sender.sendMessage(ChatColor.GREEN + "You have been refunded " + Decapitation.economy.format(bounty.getReward() - bounty.getReward() * plugin.getTax()));
    247. } else {
    248. sender.sendMessage(ChatColor.RED + "No matches");
    249. }
    250. } catch (DataStorageException e) {
    251. plugin.getLogger().log(Level.SEVERE, "Error deleting bounty", e);
    252. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    253. }
    254. return true;
    255. }
    256. sendHelp(sender);
    257. return true;
    258. }
    259. if (args.length == 3 && args[0].equalsIgnoreCase("place") && sender.hasPermission("decapitation.bounty.place")) {
    260. if (!args[1].matches("[A-Za-z0-9_]{2,16}")) {
    261. sender.sendMessage(ChatColor.RED + "That doesn't appear to be a valid username");
    262. return true;
    263. }
    264. try {
    265. Bounty bounty = plugin.getDsi().getBounty(args[1], p.getName());
    266. int reward = Integer.parseInt(args[2]);
    267. if (reward <= 0) {
    268. sender.sendMessage(ChatColor.RED + "You must set a positive bounty");
    269. return true;
    270. }
    271. if (reward < plugin.minimumBounty) {
    272. sender.sendMessage(ChatColor.RED + "You must place a bounty worth at least " + Decapitation.economy.format(plugin.minimumBounty));
    273. return true;
    274. }
    275. if (Decapitation.economy.has(p.getName(), reward + reward * plugin.getTax())) {
    276. if (bounty == null) {
    277. bounty = new Bounty(p.getName(), args[1], reward);
    278. plugin.getDsi().addBounty(bounty);
    279. Decapitation.economy.withdrawPlayer(p.getName(), reward + reward * plugin.getTax());
    280. sender.sendMessage(ChatColor.GREEN + "Added bounty against " + args[1]);
    281. sender.sendMessage(ChatColor.GREEN + "You have been charged " + Decapitation.economy.format(reward + reward * plugin.getTax()));
    282. } else {
    283. bounty.setReward(bounty.getReward() + reward);
    284. plugin.getDsi().updateBounty(bounty);
    285. Decapitation.economy.withdrawPlayer(p.getName(), reward + reward * plugin.getTax());
    286. sender.sendMessage(ChatColor.GREEN + "Added money to existing bounty against " + args[1]);
    287. sender.sendMessage(ChatColor.GREEN + "You have been charged " + Decapitation.economy.format(reward + reward * plugin.getTax()));
    288. }
    289. } else {
    290. sender.sendMessage(ChatColor.RED + "You don't have enough money (" + Decapitation.economy.format(reward + reward * plugin.getTax()) + ")");
    291. }
    292. } catch (NumberFormatException e) {
    293. sender.sendMessage(ChatColor.RED + "That's not a number");
    294. } catch (DataStorageException e) {
    295. plugin.getLogger().log(Level.SEVERE, "Error adding bounty", e);
    296. sender.sendMessage(ChatColor.RED + "Something went wrong :(");
    297. }
    298. return true;
    299. }
    300. sendHelp(sender);
    301. return true;
    302. }
    303.  
    304. }
    305.  
     
  2. Offline

    sharp237

    Can you not just do something like:
    player.getItemInHand().setAmount(0);
    or set the item to AIR ?
     
  3. Offline

    isaytoast24

    I tried, it doesn't do it if I use the claim command and the first one threw an error at me about "player cannot be resolved" but that might be me being a nOOb
     
Thread Status:
Not open for further replies.

Share This Page