Exploding

Discussion in 'Plugin Development' started by alex123099, Oct 27, 2013.

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

    alex123099

    Hey all!
    I have a function that creates an explosion:
    Code:java
    1. public static void explosion(Player caster){
    2. if(caster.getGameMode().equals(GameMode.CREATIVE)){
    3. caster.sendMessage(ChatColor.RED + "Can not be used in creative mode!");
    4. return;
    5. }
    6.  
    7. String firePath = "explosion.fire", breakPath = "explosion.blockbreak";
    8. double hpFeePercent = 30.0;
    9. int mp = caster.getFoodLevel();
    10.  
    11. if(mp>=EXPLOSION_MP){
    12. Location loc = caster.getLocation();
    13. World w = loc.getWorld();
    14. w.createExplosion(loc.getX(),loc.getY(),loc.getZ(), 6f, skillsCfg.getConfig().getBoolean(firePath), skillsCfg.getConfig().getBoolean(breakPath));
    15. caster.playSound(loc, Sound.CREEPER_HISS, 50, 0);
    16.  
    17. double hpFee = (hpFeePercent/100.0)*caster.getHealth();
    18. caster.setHealth(caster.getHealth()-hpFee);
    19.  
    20. caster.setFoodLevel(mp-EXPLOSION_MP);
    21. } else
    22. caster.sendMessage(noMana);
    23. }


    The explosion and the creeper sound work perfectly, however, what I am trying to achieve is that the player who is creating the explosion(caster) does not get damaged by the explosion, only from the hpFee later in the function. How can I cancel this explosion damage on that player, preferably without any listeners?
     
  2. Offline

    JPG2000

    alex123099 Its called a method, not a function. :p

    I would not think its possible to cancel it without listeners. But with lisiteners, heres how I would do it:

    Create a global ArrayList, storing players names (strings). When eber the explosion method is called add the caster to the string.

    Then, below, use the entitydamagevent. If the entity is a player and the players name is in the array list, cancel and remove.
     
    alex123099 likes this.
  3. alex123099 Listen for the EntityDamageByEntityEvent, check if the damaged entity is the caster and that the damager is your explosion (trough UUID), and then just don't apply damage.

    JPG2000 actually, some programming language make a diffrence between functions and methods, in java however, there is no diffrence. But according to oracle, in java, functions do not exist, but the term function gets used much when people are talking about methods that it would be impossible to "ban" the term function when talking about methods in Java. :p

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

    alex123099

    CaptainBern Never knew there was a difference between a function and a method, I just thought them to be synonyms. On-topic, it's a shame you cannot cancel the explosion without an event listener. Thanks to you two for the help :)
     
  5. Offline

    alex123099

    CaptainBern
    I got kind of stuck now, have never used UUIDs and don't know how to continue:
    Code:java
    1. public static ArrayList<String> explosionCasters = new ArrayList<String>();
    2.  
    3. @EventHandler
    4. public void onEntityDamage(EntityDamageByEntityEvent e){
    5. if(e.getDamager().getUniqueId()))
    6. }


    How do I check whether it was an explosion that caused the damage?
     
  6. alex123099 The UUID is unique for each entity, when you create the explosion, just store it's UUID + caster somewhere, then on the damage event, check if the player that got damaged is a caster if so, then check if the uuid of the damager is the stored uuid, if so then just cancel the damage applied to the player.
     
  7. Offline

    OracleTarget

    /difficulty peaceful
    :D
     
  8. Offline

    alex123099

    CaptainBern
    So this is what I got:
    Code:java
    1. public static HashMap<UUID,String> explosionData = new HashMap<UUID,String>();
    2.  
    3. @EventHandler
    4. public void onEntityDamage(EntityDamageByEntityEvent e){
    5. UUID eventId = e.getDamager().getUniqueId();
    6. if(e.getEntity() instanceof Player)
    7. for(int i=0; i<explosionData.size(); i++)
    8. if(explosionData.containsKey(eventId))
    9. if(((Player)e.getEntity()).getName().equalsIgnoreCase(explosionData.get(eventId)))
    10. e.setCancelled(true);
    11. }


    Now I started wondering what will happen if 2 people execute the explosion method at the same time (that's impossible of course but a difference of a few millis)? Will one be hit by the explosion of the other or will neither of them get hit by any explosions?

    Edit: How can I get the UUID of the explosion if I am using World.createExplosion method?
     
  9. alex123099 Well I will give you a list of what to do:
    1. in the explosion(Player player) method; change world.createExplosion(.. to Entity explosion = world.createExplosion(..., then to get the uuid of the explosion do explosion.getEntityId(), store this uuid in a hashmap with <String, UUID> whereas string is the player name.
    2. Listen for the EntityDamageByEntity event, check if the hashmap contains the player name, if so continue, then check if you do: UUID originUUID = hashmap.get(player.getName()); and then check if the "originUUID" is equal to it, is so then cancel it.
     
  10. Offline

    alex123099

    CaptainBern
    I got everything set up other than getting the UUID of the explosion.
    The createExplosion method I am using returns a boolean so I am unable to store it as an Entity.
    I am using:
    World.createExplosion(x,y,z,damage,fire,break blocks), which returns a boolean.
     
  11. alex123099 Oh, in that case try using worls.spawnEntity(EntityType.EXPLOSION); ...
     
  12. Offline

    alex123099

    CaptainBern There does not exist an explosion, nor a TNT entity anymore :confused:, plus I must have a method that gives me the possibility to decide whether the explosion creates fire and breaks blocks or not.
     
  13. alex123099 Hmmm... Let me do some research...
     
  14. Offline

    alex123099

    CaptainBern Maybe I could use these functions somehow (from LivingEntity) :
    setNoDamageTicks (int ticks) - Sets the living entity's current no damage ticks.
    setLastDamage (double damage) - Sets the damage dealt within the current no damage ticks time period.

    I don't fully understand the concept of damage ticks and no damage ticks, but maybe these functions are helpful?
     
  15. alex123099 Hmm I doubt that would be helpfull...
     
Thread Status:
Not open for further replies.

Share This Page