Inventory Help

Discussion in 'Plugin Development' started by bobthefish, Jul 21, 2014.

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

    bobthefish

    Hi

    Im trying to make a plugin that converts gold that you pick up into money. I need the gold to be taken from you as soon as you pick it up, this code wont work, any suggestions?
    Code:java
    1. @EventHandler
    2. public void onPickUp(PlayerPickupItemEvent e){
    3. Player p = e.getPlayer();
    4. if(e.getItem().getItemStack().getType().equals(Material.GOLD_INGOT)){
    5. p.sendMessage("you picked one up");
    6. ItemStack g = e.getItem().getItemStack();
    7. int amt = g.getAmount();
    8. p.getInventory().remove(Material.GOLD_INGOT);
     
  2. Offline

    Drew1080

    bobthefish
    "This code won't work" is a very broad statement. Was there any errors in the console? Did you register the event?
     
  3. Yeah, What is not working? remove the gold? The event? add the money?
    Show us some errors :D
     
  4. Offline

    bobthefish

    Drew1080 1) the bug in your signature totally got me, I thought it was real and spent a little while trying to squish it xD
    2) I am sorry for being so broad Im tired and wasnt thinking straight. Everything in that block of code works, except that it wont remove the gold. there are not errors in the console. I have just found out that I seems to work for every piece of gold you pick up, after the first one, so the fist stack is always there but it clears all consecutive stacks, if that makes any sense. thanks for your help

    anybody help me?

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

    dsouzamatt

    bobthefish Try using
    Code:java
    1. p.updateInventory();

    after removing the gold
     
    Landen22 likes this.
  6. Im not sure but maby you can try
    After you got the amount then just?
    e.getItem().remove();
    Maby that will work?
     
  7. Offline

    dsouzamatt

    Necrodoom
     
  8. Offline

    stonar96

    I think you can't remove the itemstack from the inventory in the PlayerPickupItemEvent, because the itemstack is not in the inventory at this moment. Try to remove the item on the floor and cancel the event
     
  9. stonar96
    He can do e.getItem().remove(); ? Then it will take the item that is picked up.
    bobthefish stonar96 is right!
    You cant remove that gold becaus when you trigger the event it isnt in the inventory it is on his wat to the inventory.
     
  10. Offline

    stonar96

  11. Offline

    DevilMental

    bobthefish I already had this kind of problem, so this is the code I used for my emerald money system (sorry it's in french)
    Code:java
    1. @EventHandler
    2. public void onPickup(final PlayerPickupItemEvent event) {
    3. if (event.getItem().getItemStack().getType() == Material.EMERALD) {
    4. final Player p = event.getPlayer();
    5. final ItemStack[] items = p.getInventory().getContents();
    6. final List<ItemStack> items_ = new ArrayList<ItemStack>();
    7. int iMoney = 0;
    8. for (final ItemStack item : items) {
    9. if (item != null) {
    10. if (item.getType().equals(Material.EMERALD)) {
    11. items_.add(new ItemStack(Material.AIR, 1));
    12. iMoney += item.getAmount();
    13. } else {
    14. items_.add(item);
    15. }
    16. } else {
    17. items_.add(null);
    18. }
    19. }
    20. p.getInventory().setContents(
    21. items_.toArray(new ItemStack[items_.size()]));
    22. Functions.setMoney(p, Functions.getMoney(p) + (iMoney * 10));
    23. Functions.updateMoney(event.getPlayer());
    24. p.sendMessage(ChatColor.GOLD
    25. + "<MONEY> Vous avez trouvé de l'argent !");
    26. p.updateInventory();
    27. new BukkitRunnable() {
    28. @Override
    29. public void run() {
    30. final Player p = event.getPlayer();
    31. final ItemStack[] items = p.getInventory().getContents();
    32. final List<ItemStack> items_ = new ArrayList<ItemStack>();
    33. int iMoney = 0;
    34. for (final ItemStack item : items) {
    35. if (item != null) {
    36. if (item.getType().equals(Material.EMERALD)) {
    37. items_.add(new ItemStack(Material.AIR, 1));
    38. iMoney += item.getAmount();
    39. } else {
    40. items_.add(item);
    41. }
    42. } else {
    43. items_.add(null);
    44. }
    45. }
    46. p.getInventory().setContents(
    47. items_.toArray(new ItemStack[items_.size()]));
    48. Functions
    49. .setMoney(p, Functions.getMoney(p) + (iMoney * 10));
    50. Functions.updateMoney(event.getPlayer());
    51. p.updateInventory();
    52. }
    53. }.runTaskLater(Plugin, 1);
    54. }
    55. }
    56.  

    It's kind of barbarian but tag me if you want more informations ;-)

    DevilMental
     
  12. Offline

    bobthefish

    stonar96 TheLegendOffPabu dsouzamatt I tried the p.updateInventory(); no effect. The not being able to remove it part makes sense if I think about it I guess

    DevilMental I understand pert of your code, however could you maybe highlight/comment around the part where you remove the item from the inventory? and all the parts that go into it, im not worried about the money stuff, my money part works for me right now
     
  13. Offline

    DevilMental

    bobthefish Actually, you need to make a BukkitRunable that runs 1 tick after the event, to loop into every item and remove one of them, because when the event is run, the gold ingot is not in your inventory yet.

    Tag me if it seems a bit complicated, I will spoonfeed you code ;-)
    ~ DevilMental
     
  14. Offline

    bobthefish

    DevilMental while being spoon fed code would be easy, I would like to try and stay away from that, but basically what happens is that you schedule it for 1 tick later, which is when it is part of your inventory? is that what happens?
     
  15. Offline

    DevilMental

    bobthefish 1 tick later the item is actually in the inventory, unlike on the event where the item is not.
     
  16. Offline

    stonar96

    bobthefish i didn't write that you should do p.updateInventory(); It's completely clear that this has no effect. I said you should just cancel the event and remove the item, not the itemstack from the inventory, because the itemstack is not in the inventory durring the PlayerPickupItemEvent
     
  17. Offline

    stormneo7

    Code:java
    1. p.getInventory().remove(Material.GOLD_INGOT);

    Shouldn't that be p.getInventory().removeItem(ItemStack); ?
    I don't even think that works...
     
  18. Offline

    bobthefish

    stormneo7 I know it doesnt work, but Im not sure what Im supposed to use, everyone is saying that you cant remove the itemstack because it isnt in the inventory yet
     
  19. Offline

    stormneo7

    Might I ask you, what's your economy plugin? How do you give a player money? /eco give [PlayerName] [Amount] correct?

    Here try this.

    Code:java
    1. @EventHandler
    2. public void onPick(PlayerPickupItemEvent evt){
    3. if(evt.getItem().getItemStack().getType() == Material.GOLD_INGOT){
    4. final Player p = evt.getPlayer(); // Define the Player Triggering the Event
    5. ItemStack item = evt.getItem().getItemStack().clone(); // Gets the ItemStack of the Event and Clones It
    6. evt.setCancelled(true); // Cancels the Event
    7. evt.getItem().remove(); // Removes the Item
    8. int AmountOfIngots = item.getAmount(); // Amount of Ingots
    9. p.sendMessage(ChatColor.GREEN + "You have picked up " + AmountOfIngots + " gold pieces!"); // Sends the player a message
    10. p.getServer().dispatchCommand(p.getServer().getConsoleSender(), "eco give " + p.getName() + " " + AmountOfIngots);
    11. // Dispatches a command from console. In this case, the command was ["eco give " + p.getName() + " " + AmountOfIngots].
    12. // What we've done here, the console would do [eco give stormneo7 30] if my name was stormneo7 and there were 30 ingots that I picked up.
    13. }
    14. }


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

    bobthefish

    stormneo7 Ok, I will try that, but for the sake of knowledge, why do you clone the item stack?

    stormneo7 It works! THANK YOU! but I would still like to know why...

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

    stormneo7

    idk It's more of a muscle memory than a theory. I just prefer to clone things when I get the chance so the default variables don't change.
     
  22. Offline

    bobthefish

    I am using a custom economy, I store the money in a players.yml and give it straight to them
     
Thread Status:
Not open for further replies.

Share This Page