Final location of a falling anvil?

Discussion in 'Plugin Development' started by Goty_Metal, Dec 11, 2012.

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

    Goty_Metal

    Hi guys, i'm doing a traps plugin but i get stuck in the falling alvils, i can create an anvil over player to hurt him, but how can i delete the anvil when falls? i don't know how to fill a variable with the anvil final location or something :(

    Code:
    Block playerlocation = playerlocation.getLocation().add(0, +5, 0).getBlock();
     
    then
     
                playerlocation.setType(Material.ANVIL);
     
    then???
     
    location???.setType(Material.Air);

    Thanks!
     
  2. Offline

    TKramez

    Code:
    new BukkitRunnable() {
                                public void run() {
                                    if (fblock.getVelocity().equals(new Vector(0, 0, 0))) {
                                        plugin.getServer().getPluginManager().callEvent(new PlagueSpreadEvent(fblock.getLocation().getBlock(), event.getSource()));
                                        this.cancel();
                                    }
                                }
                            }.runTaskTimer(plugin, 1 * 20, 1 * 20);
    This is the code I use to find the final location of a falling block.
     
  3. Offline

    V10lator

  4. Offline

    skipperguy12

    Another way:
    Every time you spawn an anvil, save what chunk it is in an arraylist.

    Then loop through all the blocks in the chunk, and if its an avil, set it to air!

    :D

    Heres how you loop through a chunk:

    for (int x = 0; x < 16; x++) {
    for (int z = 0; z < 16; z++) {
    for (int y = 0; y < 128; y++) {
    //Code :D
    }
    }
    }
     
  5. Offline

    V10lator

    skipperguy12 Well, that solution would create memory leaks + be inefficient. Why periodically check 65536 blocks (per chunk!) if you could just relax and wait for the event to be called?
     
  6. Offline

    Goty_Metal

    Yes... the chunk sollution has a bit bad performance, but is an option, i don't understand how to do the UUID thing XD
     
  7. Offline

    V10lator

    Goty_Metal
    UUID uuid = yourSpawnedFallingBlock.getUniqueId();
    Then just save it, to a HashMap, for example:
    map.add(uuid);
    later on, check:
    Code:java
    1. if(map.contains(entity.getUniqueId())
    2. {
    3. // Match!
    4. ...
    5. map.remove(entity.getUniqueId());
    6. }


    WAIT... Actually I see you don't spawn the anvil as FallingBlock, you just place it as normal block. Spawn it as entity instead! Something like
    FallingBlock fb = world.spawnFallingBlock(loc, Material.ANVIL, (byte)0);
     
  8. Offline

    fireblast709

    Its an abstract class that implements Runnable, implemented in the Bukkit API. It contains methods to call it like you normally do, and also a method that allows you to cancel the task (so you can easily cancel a repeated task from the inside)
     
  9. Offline

    V10lator

    fireblast709 Wow, thanks. I really must have missed this. Let's just hope it's not that long there. :eek:
     
  10. Offline

    fireblast709

    V10lator know it myself for a short time as well ;3. I am actually wondering when it was implemented, as it is pretty useful
     
  11. Offline

    Goty_Metal

    Ok i'm doing with the spawn thing you said,, what's the byte value??? thanks!

    Can't add it in a hashmap, it says "the method add UUID is underfined for the type map" :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  12. Offline

    V10lator

    Goty_Metal The byte value is the blocks data. As most blocks don't have (use) data 0 should be fine.

    //EDIT: For a map it's put, but a map has key -> value pairs, which isn't needed here. Use a HashSet instead.
     
  13. Offline

    Goty_Metal

    Sorry it's true maps/hashmaps it's put, and add for hashsets, thanks :)

    Ok i get it to work, i spawned and deleted it from hashset and transformed into air (deleted the block) after seconds... the huge problem is the spawned block doesn't make damage (as a normal alvil does).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  14. Offline

    V10lator

    Goty_Metal Damn, I hoped this wouldn't happen... Let me check NMS code... Okay, there is a boolean but it's not accessible through bukkits API. So first off complain here: https://github.com/Bukkit/CraftBukkit/commit/8f12382e8efc8c39a919af9180dd884caf3720ff Then kick out the bukkit.jar from your build path and add the craftbukkit.jar. Then cast to CB block, then get the NMS block, then use a setter there to manipulate. Should look something like this:
    Code:java
    1. FallingBlock anvil = world.spawnFallingBlock(loc, Material.ANVIL, (byte)0);
    2. ((CraftFallingSand)anvil).getHandle().e(true);
    3. set.add(e.getUniqueId());
    4. ...

    To explain the one magic line: First we cast to CraftFallingSand (yes, the name is old and deprecated...) then we get the NMS EntityFallingBlock with getHandle() and finally we call it's e function to manipulate the boolean.
     
  15. Offline

    Goty_Metal

    I think.. it could be simple to get the player damaged like:

    jug.damage(5);

    XD
     
  16. Offline

    V10lator

    Goty_Metal then we need a FlyingBlockMoveEvent to pass the moment a anvil reaches a player (it can hit you above the ground, too).
     
  17. Offline

    Goty_Metal

    I just calculated the time the anvil takes to reach the player, so the "effect" is like the damage comes from the anvil, working fine, i hope bukkit add egain this function you said before :)
     
  18. Offline

    V10lator

    Goty_Metal The function is there, you can use it if you want to but be warned that you leave the bukkit API!
     
  19. Offline

    Goty_Metal

    Well i mean, to use it in the standart bukkit api XD thanks :)
     
Thread Status:
Not open for further replies.

Share This Page