Solved Spawning non-destructive PrimedTNT in Bukkit.

Discussion in 'Plugin Development' started by WampyCakes, Nov 17, 2014.

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

    WampyCakes

    So I am creating a hardcore mob's plugin. And for the creeper when it spawns I want it to have a chance to spawn primed-TNT. I want this TNT to make you go flying a little but not get damaged. And I want it to not mess up the terrain. So how can I have a primed-TNT spawn that doesn't damage the terrain at all?

    Thanks in advance.
    ~Wampy
     
  2. Offline

    asofold

    I don't know if you can set so in the spawned entity (primed tnt) directly, but you could use metadata with the primed tnt entity, e.g. "noblockdamage", then you intercept the explosion event and if there is an exploding entity with it and it if it has matadata "noblockdamage", then you can remove the damage to the blocks without cancelling the event.

    The EntityExplodeEvent allows to get the entity (just check for the metadata, if the entity is not null), and to clear the block list, unless i am mistaken.
    http://jd.bukkit.org/dev/apidocs/org/bukkit/event/entity/EntityExplodeEvent.html

    I don't think tnt sets things on fire, otherwise you could also intercept the ExplosionPrimeEvent to check the same way and remove fire:
    http://jd.bukkit.org/dev/apidocs/org/bukkit/event/entity/ExplosionPrimeEvent.html

    That's just a quick thought - you shuld test if the metadata gets preserved if the chunks unload (and server shutdown), i am not familiar with that aspect. If it's preserved this would be a pretty simple solution. If it is not preserved you might consider to remove primed tnt on unload/shutdown, if it has that metadata (or store the uuids in a file/database, but then the data can become a lot to maintain and also could become inconsistent if the entities are removed for whatever reason).
     
  3. Offline

    WampyCakes

    Could you give me an example of how to do the ItemMeta? I've done ItemMeta before in like InventoryGUIs but I am not excellent with it. So if you can show me how to set the specific meta for that spawned tnt and check it and do what you said about ""noblockdamage", then you intercept the explosion event and if there is an exploding entity with it and it if it has matadata " that would be awesome! Thanks.
     
  4. Offline

    asofold

    Entities including the primed tnt entity implement the interface Metadatable (not ItemMeta!):
    http://jd.bukkit.org/dev/apidocs/org/bukkit/metadata/Metadatable.html

    So you have spawned the primed tnt (say it's named primed) and just add that metadata:
    primed.setMetaData("noblockdamage", new FixedMetaDataValue(yourPlugin, true)); // We don't really use the value.

    So when handling the EntityExplodeEventt:
    if (event.getEntity() != null && event.getEntity().hasMetaDataValue("noblockdamage") {event.blockList().clear()};

    That's the idea with using metadata. I still don't know if it gets preserved over server restarts, i've heard they would, but that could have been a plugin or other thing (need to look up...).
     
  5. Offline

    WampyCakes

    What do I chance "yourPlugin" to? I am little bit confused. :/
     
  6. Offline

    mrCookieSlime Retired Staff

    WampyCakes
    You replace it with an instance of your Plugins main class.
     
  7. Offline

    WampyCakes

    mrCookieSlime asofold Thank you very much! This was the one part of my plugin I was getting cornfused at lol.
     
  8. Offline

    asofold

    WampyCakes Did you check if the tnt will keep the metadata over a (clean) restart?

    The risk would be that the tnt explodes once the chunk is loaded, then causes block damage. That could be a slight problem if you have instant teleportation, so the chunk has a chance to unload before the tnt explodes. If a player walks into view-distance of the chunk with the saved TNT, it would likely remote-blow-up with destroying blocks there. So i'd suggest testing once or twice with /stop once spawned near you and restart (a log message stating location + if has metadata or not could help)
     
  9. Offline

    WampyCakes

    asofold You're right! I tested it, and it does make an explosion when you have the TNT spawn then /stop log back on and BAM! I will do a little research but if you know how to fix this please tell me.
     
  10. Offline

    Skionz

    Remove the TNT onDisable or just get rid of TNT damage all together.
     
  11. Offline

    WampyCakes

    I fixed it. This thread is no longer need to post on.
     
  12. Offline

    ChipDev

  13. Offline

    timtower Moderator Moderator

    Please use the report button for stuff like this.
     
Thread Status:
Not open for further replies.

Share This Page