Solved Get all item types affected by Explosion?

Discussion in 'Plugin Development' started by SuperSniper, Jul 2, 2016.

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

    SuperSniper

    Does anyone know how to iterate through all of the affected block types in an entity explosion and print them out in a string?

    It's probably really simple, and I just can't think of it

    I tried a for loop, but then it prints out 1 type of block the amount of blocks that were affected

    Like this: BLOCKED AFFECTED: LEAVES_2 BLOCKED AFFECTED: LEAVES_2 BLOCKED AFFECTED:
    (keeps going on and on for the amount of blocks that were affected)
     
  2. Offline

    MCMastery

    Code:
    String s  = "";
    for (Block block : evt.getAffectedBlocks()) {
        s += block.getType().name() + " ";
    }
    Or, it looks like you want to include the amount of each block:
    Code:
    Map<Material, Integer> blockTypesAffected = new HashMap<>();
    for (Block block : evt.getAffectedBlocks()) {
        if (blockTypesAffected.contains(block.type())
            blockTypesAffected.put(block.type(), 1);
        else
            blockTypesAffected.put(blockTypesAffected.get(block.type()) + 1);
    }
    That map will then contain the amount of each block type which was affected
     
  3. Offline

    SuperSniper

    Thank you :D. 2nd method was exactly what I was looking for :)
     
Thread Status:
Not open for further replies.

Share This Page