Explosion Properties - How Does It Work?

Discussion in 'Plugin Development' started by BajanAmerican, Sep 2, 2013.

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

    BajanAmerican

    Hey guys, I have a quick question. This might sound stupid, but is it possible to grab an explosion and have it only affect certain players? So say I create an explosion (not through TnT, but through the world), is it possible for that explosion to only harm players in a certain Arraylist? If so, where would this be registered? Thanks!
     
  2. Offline

    Tarestudio

  3. Offline

    BajanAmerican

    Tarestudio
    What does the code mean by "reset invincibility"?
    And I am assuming that the variable "entity" is the entity that is exposed to the explosion?
    Thanks!
     
  4. Offline

    Shinxs

    Store the players that dont get hurt in a list then create an explosion and listen to EntityDamageEvent and if the cause equals an explosion check if that player is in the list if it is do event.setCancelled(true);
     
  5. Offline

    BajanAmerican

    So since I am creating the explosion through the world, would the damage causer be
    Code:
    event.getCause() == DamageCause.ENTITY_EXPLOSION
    or
    Code:
    event.getCause() == DamageCause.BLOCK_EXPLOSION
    And this is what the code should look like?
    Code:java
    1. if(event.getCause() == DamageCause.ENTITY_EXPLOSION){
    2. if(noHurt.contains(player.getName())){
    3. event.setCancelled(true);
    4. noHurt.remove(player.getName());
    5. }
    6. }
     
  6. Offline

    Tarestudio

    BajanAmerican
    Shinxs method would stop damage to the listed players by any explosion of the right type (I dont know which type it is), not only by the explosions you created. If this is good for you, just register the event, log the damagecause and create your explosion, so you will see which you need.
    To my posted code, I cant say more then the linked post does. Its not from me. But entity should be your player and invincibility is what I called 'godmode', so that after explosion the player can be harmed again.
     
  7. Offline

    Shinxs

    i think it should be BLOCK_EXPLOSION and u can get al the enities in a radius and save them in a list and then check all of it so u know that its ur explosion
     
  8. Offline

    BajanAmerican

    Shinxs
    Tarestudio
    I did what Shinxs said and added the players to an arraylist that cannot be hurt when damage occurs. Although, its not working. This is the code:
    Code:java
    1. if(!(event.getEntity() instanceof Player)){
    2. return;
    3. }
    4. Player player = (Player) event.getEntity();
    5. if(noHurt.contains(player.getName())){
    6. event.setCancelled(true);
    7. noHurt.clear();
    8. }


    I know the players are being added to the noHurt arraylist because I added logger messages which indicated that they were added to the arraylist. What do you guys think?
     
  9. Offline

    Tarestudio

    BajanAmerican
    You should not clear the List after the first hit. Clear the list before you fill a new one maybe.
    Did you add a looger message to this event? Maybe one after the instanceof-check and one in your contains-check.
     
  10. Offline

    BajanAmerican

    I used this code:
    Code:java
    1. if(CauldronWars.hasStarted){
    2. if(!(event.getEntity() instanceof Player)){
    3. return;
    4. }
    5. Player player = (Player) event.getEntity();
    6. if(noHurt.contains(player.getName())){
    7. event.setCancelled(true);
    8. new Thread(new Runnable() {
    9.  
    10. public void run() {
    11. try {
    12. Thread.sleep(2000);
    13. logger.info("THREAD HAS CLEARED NOHURT!!");
    14. noHurt.clear();
    15. } catch (Exception e) {
    16. e.printStackTrace();
    17. }
    18. }
    19. }).start();
    20. }
    21. }

    And it is still not working. The players are without a doubt being added to the List noHurt because I added a logger message saying they are, which IS displayed in console. So, when I wait 2 seconds to make sure they get the invincibility that they should have and then I clear the noHurt list and add the logger message which DOES come 2 seconds after the the Thread sleeps. But during the 2 seconds, the player IS damaged even though they are in the noHurt list. I honestly do not know what is going on, this is confusing the living hell out of me. Please help me with this because this has become almost a nightmare lol. Thanks!

    Shinxs
    Maybe you could shine some light on this too? Thanks!

    [Note] If I do not make the new runnable, it works without a problem, but if I do that, then I can't take the players out of the Arraylist.

    Is there anyone else who could be of assistance to help with this as well?

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

    Shinxs

    do noHurt.remove(player) and dont create a new thread
     
  12. Offline

    BajanAmerican

    No I can't do that because I add an arraylist of players to noHurt and I want to remove everyone from the noHurt arraylist. So, there are players Jonny, Mike, and Joe (they are all on the same team). Jonny gets a snowball. He throws the snowball on the ground where Jonny and Mike are both standing. When the snowball hits the ground, they are all added to the arraylist noHurt and then an explosion occurs. When that explosion occurs, they cannot take the damage because they are in the noHurt arraylist. Then after they are exposed to the explosion and they have taken no damage, they will get removed from the noHurt arralist. I do not want to just take the players that were exposed to the explosion out of noHurt because that will leave Joe, who will still be in the noHurt arraylist and not take the damage of whatever he is damaged by next. Does this explain what I am trying to accomplish?

    chasechocolate
    Do you have any ideas?

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

    Shinxs

    but now u remove all of them after an damage event so it'll only work for one player
     
  14. Offline

    Tarestudio

    BajanAmerican
    As Shinxs said, dont create a new Thread, not this way. Look at schedulers: http://wiki.bukkit.org/Scheduler_Programming
    Also you should not create this schedule on the damageevent, but on the time the explosion is created.
    For debugging, add log-entries, after checking for instanceof Player and after noHurts.contains()
    Have you tried, setting damage to 0?
     
Thread Status:
Not open for further replies.

Share This Page