Solved A "Mob Burn in Sunlight" Event?

Discussion in 'Plugin Development' started by LandonTheGeek, Oct 30, 2013.

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

    LandonTheGeek

    I am trying to complete a private plugin for a server, but I haven't found an event on JavaDocs for this type of action:

    When mobs are hit in the sunlight, I don't want them to burn like they normally do. (Canceling the task)

    If you don't mind setting me up on the EventHandler too, I would appreciate it.
    Thanks
     
    fussionzz97 likes this.
  2. Offline

    The_Doctor_123

    EntityCombustEvent may be used to check when an entity lights on fire. The problem is, I believe it fires for any type of reason for being lit. You could add some thing to check if it's day and there's no solid blocks above the entity.
     
  3. Offline

    LandonTheGeek

    The_Doctor_123
    Wow, that would be advanced. I thought it would be easy. Lol. I guess it is something to work for.

    If anyone could help by setting a method and constructor up for me and walking me through it, I would love that.
    (Still learning! :) )
    Thanks

    The_Doctor_123

    Oh. Yes. I have another thought. Making it so that the mobs couldn't burn at all (the certain ones of course), but making it so that arrows and the weapons could still get them with fire.

    I don't know.

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

    xTrollxDudex

    HelpfulBeast
    Try seeing if EntityCombustEvent is caused by EntityCombustByEntityEvent? I think you can use instanceof but probably not.
     
  5. Offline

    LandonTheGeek

    xTrollxDudex

    So you think I could set the entity as the sun? Lol.
     
  6. Offline

    The_Doctor_123

    HelpfulBeast
    Completely untested, but if it doesn't work, hopefully this will give you the right idea:
    Code:java
    1. @EventHandler
    2. public void onEntityCombust(EntityCombustEvent event)
    3. {
    4. Entity entity = event.getEntity();
    5. EntityType[] burnableMobs = new EntityType[] {EntityType.SKELETON, EntityType.ZOMBIE /* Blah blah blah */};
    6. Material[] exemptBlocks = new Material[] {Material.GLASS /* Blah blah blah */};
    7. if (Arrays.asList(burnableMobs).contains(entity.getType()))
    8. {
    9. Location loc = entity.getLocation();
    10. boolean isOutside = true;
    11. for (int i = loc.getBlockY() ; i < 255 ; i++)
    12. {
    13. loc.setY(i);
    14. Block block = loc.getWorld().getBlockAt(loc);
    15. if (!Arrays.asList(exemptBlocks).contains(block.getType()) && !block.isEmpty())
    16. {
    17. isOutside = false;
    18. break;
    19. }
    20. }
    21. if (isOutside)
    22. {
    23. event.setCancelled(true);
    24. }
    25. }
    26. }
     
  7. Offline

    xTrollxDudex

    HelpfulBeast
    No. The burning arrow is one, you want to exclude that
     
  8. Offline

    LandonTheGeek

    The_Doctor_123 xTrollxDudex
    Thank you both for your help! Works good, made a few changes, and it looks okay! :) SOLVED ( for now. xD )
     

  9. If you really want to feel amazing, it is better if you code yourself and do some research, if you don't want to feel amazing, I'm not sure why you are here.
    As The_Doctor_123 said EntityCombustEvent is the event to listen for. You would get the Entity involved in the Event and simply create a method like the one below and use it with an if statement, be sure to pass it the entity and if it returns true do your code.

    Code:java
    1. /**
    2. * Checks whether {@code entity} is exposed to sunlight.
    3. *
    4. * @param entity entity to check
    5. * @return true if {@code entity} is exposed to sunlight, otherwise false
    6. */
    7. public boolean exposedToSunlight(Entity entity) {
    8.  
    9. Vector vector = entity.getLocation().toVector();
    10. BlockIterator it =
    11. new BlockIterator(entity.getWorld(), vector, new Vector(0, 1, 0), 0, 0);
    12. while (it.hasNext()) {
    13. if (it.next().getType().isSolid())
    14. return false;
    15. }
    16. return true;
    17. }

    Edit: Apparently when editing it messes up format, sorry.
     
    HelpfulBeast likes this.
  10. Offline

    The_Doctor_123

    HelpfulBeast
    The code actually works? XD

    SupaHam
    Gah! I knew there was an isSolid method somewhere! I thought it was in Block. :p
     
    HelpfulBeast likes this.
  11. Offline

    LandonTheGeek

  12. The_Doctor_123 :p

    I usually wouldn't give out a method that easily, but if you don't understand what the method does, feel free to let me know.

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

    The_Doctor_123

    SupaHam
    A slight issue with the method: It doesn't check if the mob is actually supposed to burn in sunlight, which can easily be fixed, however.
     
  14. Offline

    LandonTheGeek

    The_Doctor_123
    I was about to reply with that. They seem to be burning, but then stop right at there death point and live. I don't know if that made any sense, but I am trying. :)
     
  15. I should mention I have not tested that code sorry to say, just made it up from previous experience.
     
  16. Offline

    The_Doctor_123

  17. Offline

    LandonTheGeek

    The_Doctor_123
    Seems so, yeah.

    Let me try to explain better: They burn, but don't actually die from it.

    The_Doctor_123

    They get hurt too (they just don't die)

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

    The_Doctor_123

    HelpfulBeast
    Erm, well, that doesn't make much sense. I'm not modifying the fire's properties(which is just the amount of time alit..).
     
  19. That is for you to do your code. You should setDuration(0)
     
    HelpfulBeast likes this.
  20. Offline

    LandonTheGeek

    The_Doctor_123
    Hm... Would you think we could set a (VERY LONG) interval up to patch that issue? :p

    SupaHam

    Cool, I will try to place it in here. Attempting. xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  21. Hint: You would place it in the body of that if statement (If you were following my guide). If you were following doctor's you'd put it in the if(isOutside) body.
     
  22. Offline

    The_Doctor_123

    HelpfulBeast SupaHam
    This actually makes no sense. If I cancel the event, then there's no fire, correct? Hopefully this isn't a client-sided issue.
     
  23. I'm guessing it doesn't hurt as he said, it just plays the animation (hurt and fire)
    Edit: Can you confirm their health is lowering?
     
  24. Offline

    LandonTheGeek

    SupaHam

    Mmk. I added it into the body, and I get an error thrown. It asks me to create a private method at the bottom, but I know far well that won't do it (I hope). Here is a pic: http://puu.sh/54vmJ.png

    SupaHam
    Well, they are becoming red, yes. The red pulsing.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  25. That method belongs to the EntityCombustEvent so insert the event variable before it
     
  26. Offline

    LandonTheGeek

  27. Offline

    The_Doctor_123

    HelpfulBeast SupaHam
    If the fire doesn't hurt, my guess would be that this is all client-sided. But before concluding that, lets run many different types of tests.

    HelpfulBeast
    The object you want to call the method on would be your EntityCombustEvent. So..
    event.setDuration(0)

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

    LandonTheGeek

    The_Doctor_123
    Once again, I am trying to learn. :) Like this?

    Code:
    if (isOutside)
                {               
                    event.setCancelled(true);
                    event.setDuration(0);
                }
     
  29. When I say a method belongs to class x that means you have to type the class/object name, followed by the seperator '.', then the method name. setDuration() is a non-static method meaning it can't be called by typing the class name, you need to use an object which, in this case, would be event
     
    HelpfulBeast likes this.
  30. Offline

    LandonTheGeek

    SupaHam
    Alright, sorry about the issues I am causing. :p

    I will complete this tomorrow, and get responses from you guys.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page