Solved read out of ItemStack array?

Discussion in 'Spigot Plugin Development' started by aufgosh, Feb 21, 2020.

  1. Hey,

    how would it be possible to read out of the ItemStack array you get out of the BlockBreakEvent



    So that you could read out the index of Sand and the Amount ?:O

    My code:
    p.sendMessage("§aYou recieved §c" + e.getBlock().getDrops() + " §aout of §c" + e.getBlock().getType().name() );
  2. event.getBlock().getDrops().iterator().next() will return an ItemStack which you can then get the material and amount from.

    If anyone has a better way to the first element from a collection, please correct me.
    • Informative Informative x 1
  3. But i dont think this sovles my problem since i dont know how to sperate it out of the "[ItemStack{}]" also, why does this method not work on all type of stones? it just says that they drop nothing? even on ores etc
  4. Can you give us some information on what you actually want to happen?
  5. I don't really understand what you mean, but if you want to get the material of the item stack, just use ItemStack#getType()
  6. Yes, sorry

    Basically what i want is a Message that would Display "You recieved: Testitem1 x 1, Testitem3 x 79 out of Stone
  7. Sorry but i think i confused you guys with the title from what i actually want? I want to work with the Information of the itemdrop which you get from the BreakBlockEvent which is in an ItemStack array

    you get the array with e.getBlock().getDrops() but i am not sure on how to procceed
  8. There‘s a thing called loops.
    Code (Java):
    for (ItemStack stack: ...getDrops()) {
        // for each stack do something
    You could also do this using streams, however, try to understand loops first.
    • Agree Agree x 2
  9. i understand loops but i dont understand in what kind of context the stuff is saved. I understand that it is saved in an Array but i am clueless on how to access this array
  10. Well, first of all, the method does not return an array, but a Collection. Just loop over it the way I did.
    What I think your problem is that you want to print out a custom string. What you, however, print out is the definition of toString(), defined in Collection. You‘ll need to create a custom String that might look something like this:

    Code (Java):
    final StringBuilder builder = new StringBuilder();
    for(ItemStack stack: ...drops()) {
        builder.append(stack.getName()).append(„x“).append(stack.().getAmount()).append(„ „):
    • Like Like x 1
  11. It's not an array it's a collection, you should learn some basics..
  12. You really should learn java first. Regardless, I'd recommend using a StringJoiner.
    Code (Java):
    StringJoiner text = new StringJoiner(", ")
    for (ItemStack item : collection) {
        text.append(item.getMaterial().name() + "x" + item.getAmount());
    • Like Like x 1
  13. Thank you both :)
    Now only one Problem left: Why do i get a null response from any type of Stone, ore etc?
  14. Hmm, I suppose this is due to the fact that you are not accounting for the item that this block was actually broken with (since you are accessing block#getDrops()).
  15. i did e.getPlayer().getItemInHand() which worked but sadly getItemInHand is depracted.. but i guess i will just use it :)
  16. use Inventory#getItemInMainHand(), Javadoc here.
    • Like Like x 1