[UNSOLVED]Making a FallingBlock - explode.

Discussion in 'Plugin Development' started by MatanAlfasi, Oct 4, 2013.

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

    MatanAlfasi

    Well hello, I'm pretty new to Bukkit plugins development,
    and I'm pretty much stuck at changing block properties, or something like that..

    Anyway - I mini - game that one of his classes does :
    Choosing block , by right clicking it inside of the mini - game.
    There are about 10 blocks that you can right click, choose and will to your inventory, you can choose up to 5 blocks, that will come into it.
    of - course - as the block is more rare to find in the arena - when you will throw the block, it's explosion will be bigger, and will effect more on other players.



    now, I have done the throwing block thing, so when you right click, on air, there will be vector, that show him how to throw it, which came out pretty well.

    -Now i am done with throw block thing, and I need it to be exploded after lets say.. 5 sec in air, or the first block that it hits, except of AIR of course ..

    So I need to change the block, so it will be like a TNT that has been turned on fire or something.

    How do I do that ? Here is the code of throwing the block with its vector :

    Code:java
    1. public void onBlockThrow(PlayerInteractEvent event) {
    2. Player player = event.getPlayer();
    3. Action action = event.getAction();
    4. if((action == Action.RIGHT_CLICK_AIR) && plugin.Generals.containsKey(player.getName()) && player.getItemInHand().getType().isBlock())
    5. {
    6. Vector v = player.getLocation().getDirection().normalize().multiply(((Integer)plugin.Generals.get(player.getName())).intValue());
    7. Location l = player.getEyeLocation();
    8. ItemStack is = player.getItemInHand();
    9.  
    10.  
    11. FallingBlock FallingBlock = l.getWorld().spawnFallingBlock(l, is.getType(), is.getData().getData());
    12. FallingBlock.setVelocity(v);
    13.  
    14.  
    15. if(player.getGameMode() == GameMode.SURVIVAL) {
    16. player.getInventory().removeItem(new ItemStack[] {
    17. new ItemStack(is.getType(), 1)
    18.  
    19. });
    20. if(is.getType() != Material.AIR)
    21. player.sendMessage("You just threw the block: " + is.getType());
    22. }
    23.  
    24.  
    25. }
    26. }


    now don't miss-understood, Right now, when you put yourself into the HashMap, it works perfect, all I need to do is make the block into a TNT, but it will still look like the block you have chosen

    Listen, I did the explosion, and it came up pretty well, but it Explodes when I shoot the Falling Block and not when it lands,
    So I tried to do:
    if(FallingBlock.isOnGround)

    But it doesn't work, I need to get the location of the block exactly when it lands, even better will be if I can set time, but I guess it is pretty hard..
    any way - Thanks for the help !

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

    chasechocolate

    I would set some metadata, and on EntityChangeBlockEvent, check if it is a FallingBlock, and it has the certain metadata, then in a 1-tick delayed task, check if it is on the ground, and then create the explosion.
     
  3. Offline

    TomTheDeveloper

    I would put those fallingblocks into a hashmap. Then I would run a repeating task every second to check if the fallingblock already has flew an x amount of seconds. When they did, I would create an explosion at the location of the fallingblock and let it disappear.
     
  4. Offline

    MatanAlfasi

    How do I create a repeating task ?, I can do something like

    Code:java
    1. while(!FallingBlock.isOnGround()){}


    But then The whole server is doomed..

    How can I run a repeating task ? thanks for the help !

    UPCHI -
    All I need is or :
    Find a way to do a repeating task and check every second if the block has changed or its in ground.
    or:
    Do the explosion with EntityBlockChangeEvent.

    which didn't work for me..

    Help ..?

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

    Garris0n

    Code:java
    1. @EventHandler
    2. public void onBlockChange(EntityChangeBlockEvent event){
    3.  
    4. if(!event.getBlock().isEmpty())
    5. return;
    6. //list that you add your entity's id to when the block is thrown
    7. if(!yourListOfEntityIds.contains(event.getEntity().getEntityId))
    8. return;
    9.  
    10. event.setCancelled(true);
    11. yourListOfEntityIds.remove(event.getEntity().getEntityId());
    12. event.getEntity().remove();
    13. world.createExplosion(...);
    14.  
    15. }
     
  6. Offline

    MatanAlfasi

    Any other ideas ? its just doesn't go well with my code ..
    Or can you explain, maybe I did it wrong ? because I don't think I know what is Entity's ids..
     
  7. Offline

    Garris0n

    When the block is created, add it's entity id(a unique identifier for every entity since you should not save entities) to an ArrayList. Then, when the entity hits, if your List contains the entity id, you know it's one of your special explosive fallingblocks, and you can do whatever you want.

    You may want to watch some java tutorials as well.

    READ THE COMMENT CODE HERE. IF YOU DO NOT UNDERSTAND SOMETHING ASK WHAT IT DOES. IF YOU JUST COPY PASTE, YOU WILL LEARN NOTHING.

    Code:java
    1. ArrayList<Integer> myBlocks = new ArrayList<Integer>(); //List of entity ids
    2.  
    3. public static void createFallingBlock(Location location, Vector direction, Material type){ //Method to spawn it.
    4.  
    5. FallingBlock block = location.getWorld().spawn(location, FallingBlock.class); //Spawn the block.
    6. myBlocks.add(block.getEntityId()); //Add the block's entity id to your list for identification later.
    7. block.setVelocity(direction); //Set the block's velocity.
    8.  
    9. }
    10.  
    11. @EventHandler //Event below, make sure you register your events.
    12. public void onBlockChange(EntityChangeBlockEvent event){ //Listen to the event called when it lands.
    13.  
    14. if(!event.getEntity() instanceof FallingBlock) //If the entity isn't a fallingblock, return.
    15. return;
    16.  
    17. if(!event.getBlock().isEmpty()) //If this is a fallingblock falling instead of landing, return.
    18. return;
    19.  
    20. if(!myBlocks.contains(event.getEntity().getEntityId)) //Make sure this is one of your falling blocks.
    21. return;
    22.  
    23. event.setCancelled(true); //Cancel the event so the block doesn't land.
    24. yourListOfEntityIds.remove(event.getEntity().getEntityId()); //Remove it from your list as it will be removed.
    25. event.getEntity().remove(); //Remove it.
    26. float power = 10F; //Power of your explosion.
    27. world.createExplosion(event.getLocation(), power); //Create your explosion at the spot.
    28.  
    29. }
    30.  
     
  8. Offline

    xize

Thread Status:
Not open for further replies.

Share This Page