Mob Spawn Detection Event Problem

Discussion in 'Plugin Development' started by no1mann, May 29, 2014.

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

    no1mann

    I am trying to make an event that detects a mob spawn, and spawns that mob another like 3 times. For example, every time 1 mob spawns, 3 actually spawn. But, it doesn't work, anyone see why? Thanks.


    Code:
    public class MobListener implements Listener{
     
    public FileConfiguration config;
     
    public MobListener(FileConfiguration cFile){
    config = cFile;
    }
     
    @EventHandler
    public void onSpawn(CreatureSpawnEvent e){
    Location location = e.getLocation();
    LivingEntity Mob = (LivingEntity) e.getEntity();
     
    if(Mob.getType().name().toString() == "Zombie"){
    for(int i = 1; i < config.getInt("Zombie"); i++){
    Mob.getWorld().spawn(location, Zombie.class);
    }
    }
    if(Mob.getType().name().toString() == "Skeleton"){
    for(int i = 1; i < config.getInt("Skeleton"); i++){
    Mob.getWorld().spawn(location, Skeleton.class);
    }
    }
            }
     
  2. Offline

    Bammerbom

    @no1mann ​
    mob.getType.toString() == "Zombie"​
    Here are 2 things wrong.​
    1: toString is still capitalized​
    2: use .equalsIgnoreCase instead of ==​
    so your code will be​
    if(Mob.getType().name().toString().equalsIgnoreCase("Zombie")){​
     
  3. Offline

    no1mann

    Thanks for the help, but it didn't work, any other suggestions?
    Code:java
    1. import org.bukkit.Location;
    2. import org.bukkit.configuration.file.FileConfiguration;
    3. import org.bukkit.entity.Creeper;
    4. import org.bukkit.entity.Enderman;
    5. import org.bukkit.entity.Ghast;
    6. import org.bukkit.entity.LivingEntity;
    7. import org.bukkit.entity.Skeleton;
    8. import org.bukkit.entity.Spider;
    9. import org.bukkit.entity.Zombie;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.CreatureSpawnEvent;
    13.  
    14. public class MobListener implements Listener{
    15.  
    16. public FileConfiguration config;
    17.  
    18. public MobListener(FileConfiguration cFile){
    19. config = cFile;
    20. }
    21.  
    22. @EventHandler
    23. public void onSpawn(CreatureSpawnEvent e){
    24. Location location = e.getLocation();
    25. LivingEntity Mob = (LivingEntity) e.getEntity();
    26.  
    27. if(Mob.getType().name().toString().equalsIgnoreCase("Zombie")){
    28. for(int i = 1; i < config.getInt("Zombie"); i++){
    29. Mob.getWorld().spawn(location, Zombie.class);
    30. }
    31. }
    32. if(Mob.getType().name().toString().equalsIgnoreCase("Skeleton")){
    33. for(int i = 1; i < config.getInt("Skeleton"); i++){
    34. Mob.getWorld().spawn(location, Skeleton.class);
    35. }
    36. }
    37. if(Mob.getType().name().toString().equalsIgnoreCase("Spider")){
    38. for(int i = 1; i < config.getInt("Spider"); i++){
    39. Mob.getWorld().spawn(location, Spider.class);
    40. }
    41. }
    42. if(Mob.getType().name().toString().equalsIgnoreCase("Creeper")){
    43. for(int i = 1; i < config.getInt("Creeper"); i++){
    44. Mob.getWorld().spawn(location, Creeper.class);
    45. }
    46. }
    47. if(Mob.getType().name().toString().equalsIgnoreCase("Enderman")){
    48. for(int i = 1; i < config.getInt("Enderman"); i++){
    49. Mob.getWorld().spawn(location, Enderman.class);
    50. }
    51. }
    52. if(Mob.getType().name().toString().equalsIgnoreCase("Ghast")){
    53. for(int i = 1; i < config.getInt("Ghast"); i++){
    54. Mob.getWorld().spawn(location, Ghast.class);
    55. }
    56. }
    57. }
    58. }
     
  4. Offline

    Zupsub

    Debug your plugin or learn to do so!
    For example is Mob.getWorld().spawn... called?
     
  5. Offline

    metalhedd

    You do realize this will cause an infinite number of mobs to spawn forever until the server crashes right? every time you spawn one you trigger the event again, which will cause 3 more to spawn. ad infinitum.
     
  6. Offline

    no1mann

    Yes, I know, I am going to add e.getSpawnReason() == SpawnReason.NATURAL to the if statement. Thanks though.

    Here is updated code that still doesn't work. But here is the main class aswell.
    MAIN CLASS:
    Code:java
    1. import java.io.File;
    2. import org.bukkit.command.Command;
    3. import org.bukkit.command.CommandSender;
    4. import org.bukkit.configuration.file.FileConfiguration;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class MoreMobs extends JavaPlugin{
    8.  
    9. public FileConfiguration config;
    10. public File cFile;
    11. public MobListener ml;
    12.  
    13. public String version = "1.0";
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("MoreMobs V" + version + " has been Sucessfully Enabled!");
    18.  
    19. config = getConfig();
    20. config.options().copyDefaults(true);
    21. saveConfig();
    22. cFile = new File(getDataFolder(), "config.yml");
    23.  
    24. ml = new MobListener(config, getLogger());
    25. }
    26.  
    27. @Override
    28. public void onDisable(){
    29. getLogger().info("MoreMobs" + version + " has been Sucessfully Disabled!");
    30. }
    31.  
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    33. return false;
    34. }
    35. }


    LISTENER CLASS:
    Code:java
    1. import java.util.logging.Logger;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.configuration.file.FileConfiguration;
    5. import org.bukkit.entity.EntityType;
    6. import org.bukkit.entity.LivingEntity;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.CreatureSpawnEvent;
    10. import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    11.  
    12. public class MobListener implements Listener{
    13.  
    14. public FileConfiguration config;
    15. public Logger logger2;
    16.  
    17. public MobListener(FileConfiguration cFile, Logger logger){
    18. config = cFile;
    19. logger2 = logger;
    20. }
    21.  
    22. @EventHandler
    23. public void onSpawn(CreatureSpawnEvent e){
    24. LivingEntity Mob = (LivingEntity) e.getEntity();
    25. Location location = e.getLocation();
    26. logger2.severe("Mob Spawned");
    27.  
    28. if(Mob.getType().equals(EntityType.ZOMBIE) && e.getSpawnReason() == SpawnReason.NATURAL){
    29. for(int i = 1; i < config.getInt("Zombie"); i++){
    30. location.getWorld().spawnEntity(location, EntityType.ZOMBIE);
    31. }
    32. }
    33. if(Mob.getType().equals(EntityType.SKELETON) && e.getSpawnReason() == SpawnReason.NATURAL){
    34. for(int i = 1; i < config.getInt("Skeleton"); i++){
    35. location.getWorld().spawnEntity(location, EntityType.SKELETON);
    36. }
    37. }
    38. if(Mob.getType().equals(EntityType.SPIDER) && e.getSpawnReason() == SpawnReason.NATURAL){
    39. for(int i = 1; i < config.getInt("Spider"); i++){
    40. location.getWorld().spawnEntity(location, EntityType.SPIDER);
    41. }
    42. }
    43. if(Mob.getType().equals(EntityType.CREEPER) && e.getSpawnReason() == SpawnReason.NATURAL){
    44. for(int i = 1; i < config.getInt("Creeper"); i++){
    45. location.getWorld().spawnEntity(location, EntityType.CREEPER);
    46. }
    47. }
    48. if(Mob.getType().equals(EntityType.ENDERMAN) && e.getSpawnReason() == SpawnReason.NATURAL){
    49. for(int i = 1; i < config.getInt("Enderman"); i++){
    50. location.getWorld().spawnEntity(location, EntityType.ENDERMAN);
    51. }
    52. }
    53. if(Mob.getType().equals(EntityType.GHAST) && e.getSpawnReason() == SpawnReason.NATURAL){
    54. for(int i = 1; i < config.getInt("Ghast"); i++){
    55. location.getWorld().spawnEntity(location, EntityType.GHAST);
    56. }
    57. }
    58. }
    59. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  7. Offline

    Gater12

    no1mann
    You never registered your events.
     
  8. Offline

    no1mann

    Gater12 I feel stupid now, I've just started learning bukkit, Thank you so much for the help.
     
Thread Status:
Not open for further replies.

Share This Page