Solved TNT ignited when placed

Discussion in 'Plugin Development' started by FabeGabeMC, Dec 9, 2013.

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

    FabeGabeMC

    Hello Bukkit Community.
    I have been trying to code this correctly in the plugin I'm developing:
    http://forums.bukkit.org/threads/wip-custom-survival-games-plugin.203954/

    What I am trying to code is that when you place a block of TNT in the ground, it changes to the ticking TNT entity.

    I know the first nine lines of code go like this:

    Code:java
    1. @EventHandler
    2. public void onBlockPlace(final BlockPlaceEvent Event) {
    3. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    4. public void run(){
    5.  
    6.  
    7. }
    8. }, 1);
    9. }



    Notice the 'onBlockPlace'. Is it correct? If not, can someone send me the correct code so that when it is placed, it explodes?

    Thanks,

    FabeGabe
     
  2. Offline

    Gater12

    FabeGabeMC It's as simple as it sounds. When the block placed is TNT, change the block to air, spawn in the PrimedTNT.class at the location where the block was placed and you can set the fuse ticks to whatever you like!
     
    FabeGabeMC likes this.
  3. Offline

    FabeGabeMC

    Gater12 I have not learned how to spawn in an entity, but I know what to type to make it be set to Air.
    Could you please send me the code for the entity spawn in the block's location if possible?

    Thanks,
    Gabe
     
  4. Offline

    Gater12

    FabeGabeMC
    Code:java
    1.  
    2. e.getBlock().getWorld().spawn(e.getBlock().getLocation().add(0, 1, 0), TNTPrimed.class).setFuseTicks(int);
    3.  
     
  5. Offline

    FabeGabeMC

    Gater12 Thanks for the code!
    I'll try it in a second and I will reply back with the result.

    Gater12 I get this error that I can't import Entity from the BlockPlaceEvent.
    This is what I did:
    Code:java
    1. @EventHandler
    2. public void onBlockPlace(final BlockPlaceEvent Event) {
    3. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    4. public void run() {
    5. Entity e = Event.getEntity();
    6. e.getBlock().getWorld().spawn(e.getBlock().getLocation().add(0, 1, 0), TNTPrimed.class).setFuseTicks(10);
    7. }
    8. }, 1);
    9. }


    How do I use it properly? The Event.getEntity(); is inderlined in red.
    also, the .getBlock() 's.

    Gater12
    http://gyazo.com/af34e49acd8165d8a03cd5ceea8412f6
    That's what I mean

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

    Drkmaster83

    Firstly, I get what you're doing with the naming of variables and all. But you MUST NOT capitalize the first letter of a variable. Ever. It's for conventions and it may cause some confusion with your IDE. Your IDE may think you're trying to import "Event" rather than name the event.

    Secondly, when you use a variable in a scheduler, the original object has to have a final modifier with it.

    Edit: Also, your issue is that you don't need that entity that you were making. Secondly, you need to use common sense and see that when that guy posted "e.getBlock()", you were supposed to make it "event.getBlock()" to match your event variable. Not dissing you, but you have to start playing around with the code.

    Code:
    @EventHandler
    public void onBlockPlace(final BlockPlaceEvent event)
    {
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
            {
                @Override
        public void run()
                {
                    event.getBlock().setType(Material.AIR);
    event.getBlock().getWorld().spawn(event.getBlock().getLocation().add(0, 1, 0), TNTPrimed.class).setFuseTicks(10);
        }
            }, 1);
    }
    
     
  7. Offline

    Garris0n

    drtshock likes this.
  8. Offline

    Drkmaster83

    Garris0n
    All right, all right! I get it. It may just be conventional and not cause confusion with the IDE, but I recollect having issues with my IDE because of my naming style when I first started out.
     
  9. Offline

    FabeGabeMC

    Drkmaster83 It can be any size but I have this problem now:
    How will I change this so only TNT is the one blowing up? When I place grass, it changes to the entity too!
    Well, any block.
    How will I define that ONLY TnT can be placed and turned into the PrimedTNT entity?
     
  10. Offline

    Developing

    FabeGabeMC
    Check whether the type of the block placed is tnt before spawning a primed tnt.
    Code:java
    1. @EventHandler
    2. public void onBlockPlace(final BlockPlaceEvent event)
    3. {
    4.  
    5. if(event.getBlock().getType() == Material.TNT){
    6.  
    7. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    8. {
    9. @Override
    10. public void run()
    11. {
    12. event.getBlock().setType(Material.AIR);
    13. event.getBlock().getWorld().spawn(event.getBlock().getLocation().add(0, 1, 0), TNTPrimed.class).setFuseTicks(10);
    14. }
    15. }, 1);
    16.  
    17. }
     
  11. Offline

    FabeGabeMC

Thread Status:
Not open for further replies.

Share This Page