Solved Testing the Spawn Location of a mob.

Discussion in 'Plugin Development' started by Birdgeek3, May 7, 2014.

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

    Birdgeek3

    So the title is the best way I could think of describing the issue im having. Im developing a plugin that will apply attributes to a mob depending on what "zone" they spawn in. The zones are predefined by a range set in the config along with the attributes. So say Zone1 is at 500 square blocks, Zone2 is at 1500, and Zone3 is at 2500. I need to findout the exact zone the mob spawns in that way I know what to apply to them.

    This is the relevant code I have but all it's returning is the highest tier zone no matter what.

    onCreatureSpawnEvent

    Code:java
    1. @EventHandler
    2. public void creatureSpawnEvent(CreatureSpawnEvent ev) {
    3. int zone = 1;
    4. if (isAffectedMob(ev.getEntityType())) {
    5. getZoneMobSpawnIn(ev.getEntity());
    6. }
    7. }


    isAffectMob

    Code:java
    1. public boolean isAffectedMob (EntityType e) {
    2. if (affectedMobs.contains(e.toString())) {
    3. return true;
    4. }
    5. else {
    6. return false;
    7. }
    8. }


    getZoneMobSpawnIn

    Code:java
    1. public int getZoneMobSpawnIn(Entity e) {
    2. int zoneNum = getZoneAmount();
    3. int blockX = e.getLocation().getBlockX();
    4.  
    5. for (int i = zoneNum; i !=-1; i--) {
    6. if (i !=1 && blockX < getRange(i) && blockX > getRange(-i)) {
    7. getLogger().info("Mob is in zone: " +i);
    8. return i;
    9.  
    10. }
    11. }


    getZoneAmount

    Code:java
    1. public int getZoneAmount() {
    2. return this.getConfig().getConfigurationSection("zones").getKeys(false).size();
    3. }
    4.  


    And this is what the config looks like

    Code:
    affectedMobs:
    - CREEPER
    zones:
      zone0:
        range: 0
      zone1:
        range: 500
        attributes:
        - SPEED
        - POISON
      zone2:
        range: 1500
        attributes:
        - INVISIBILTY
        - SPEED
      zone3:
        range: 2500
    
    Feel free to ask question if I wasn't clear enough.

    Solved the issue on my own, thanks to anyone who though about it for a bit. The solution was simplifying my For statement that tested if the entity was within a zone. Had some logic lopsided.

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

Share This Page