CreatureSpawnEvent, Using Custom Methods For Spawning

Discussion in 'Plugin Development' started by yewtree8, Aug 18, 2014.

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

    yewtree8

    Hello there, recently I have been trying to create a plugin that spawns custom zombies for a server I am making, however, when The spawning activates, there are stack traces created every time a creature spawn is tried to be activated. Can anyone help me in fixing this problem?


    My class with methods for spawning the zombies
    Code:java
    1. package com.comule.matandgrant.utils;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Zombie;
    8.  
    9. import com.comule.matandgrant.Main;
    10.  
    11. public class ZombieUtils {
    12.  
    13. private static String worldname = Main.getInstance().getConfig().getString("World_To_Spawn_Zombies_In");
    14.  
    15.  
    16.  
    17. public static void spawnNormZombie(Location loc) {
    18. Zombie z = (Zombie) Bukkit.getServer().getWorld(worldname).spawn(loc, Zombie.class);
    19. z.setCustomName(ChatColor.DARK_PURPLE + "Walker "+ ChatColor.WHITE + "-" + ChatColor.RED + "20 Health");
    20. z.setCustomNameVisible(true);
    21. z.setMaxHealth(20);
    22. z.setHealth(20);
    23. }
    24.  
    25. public static void spawnStrongZombie(Location loc) {
    26. Zombie z = (Zombie) Bukkit.getServer().getWorld(worldname).spawn(loc, Zombie.class);
    27. z.setCustomName(ChatColor.DARK_PURPLE + "Hench Walker "+ ChatColor.WHITE + "- " + ChatColor.RED + "40 Health");
    28. z.setCustomNameVisible(true);
    29. z.setMaxHealth(40);
    30. z.setHealth(40);
    31.  
    32.  
    33. }
    34.  
    35.  
    36.  
    37. }
    38.  




    My class for the actual event of spawning the zombie, I have tried editing the algorithm for spawning zombies, it seems that the actual error is coming from my static method from the previous class.
    Code:java
    1. package com.comule.matandgrant.listeners;
    2.  
    3. import java.util.Random;
    4.  
    5. import net.minecraft.server.v1_7_R3.EntityInsentient;
    6. import net.minecraft.server.v1_7_R3.EntityLiving;
    7. import net.minecraft.server.v1_7_R3.EntityZombie;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.Location;
    11. import org.bukkit.Material;
    12. import org.bukkit.entity.Entity;
    13. import org.bukkit.entity.EntityType;
    14. import org.bukkit.entity.LivingEntity;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.entity.Zombie;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.EventPriority;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.entity.CreatureSpawnEvent;
    21. import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    22. import org.bukkit.event.entity.EntityCombustEvent;
    23. import org.bukkit.potion.PotionEffect;
    24. import org.bukkit.potion.PotionEffectType;
    25.  
    26. import com.comule.matandgrant.utils.ZombieUtils;
    27.  
    28. public class ZombieSpawning implements Listener {
    29.  
    30.  
    31.  
    32. @EventHandler
    33. public void onEntityCombust(EntityCombustEvent event) {
    34. if (Bukkit.getWorld(event.getEntity().getWorld().getName()).getTime() >= 0L) {
    35. if (Bukkit.getWorld(event.getEntity().getWorld().getName()).getTime() <= 24000L) {
    36. if (event.getEntityType() == EntityType.ZOMBIE) {
    37. event.setCancelled(true);
    38.  
    39. }
    40. }
    41.  
    42. }
    43. }
    44.  
    45.  
    46. @EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled=true)
    47. public void onEntitySpawn(CreatureSpawnEvent event) {
    48. Entity entity = event.getEntity();
    49. if(entity instanceof Player) {
    50. return;
    51. }
    52. event.setCancelled(true);
    53. Random r = new Random();
    54. int randomNum = r.nextInt(100) + 1 ;
    55.  
    56. if(randomNum <= 5) {
    57. ZombieUtils.spawnStrongZombie(event.getLocation());
    58. return;
    59. } else if(randomNum >5) {
    60. ZombieUtils.spawnNormZombie(event.getLocation());
    61. return;
    62. }
    63.  
    64. }
    65. }


    The stack trace, This is the bit I need help with, I'm not too good with reading stack traces, I know that the error is coming from the spawning zombie but I need recommendations on how to fix this, I have looked at some of the other threads for this type of topic but all seem to be too confusing for me or they are just unanswered, uncomplicated replies appreciated:)

    STACKTRACE : http://imgur.com/OZ6hGms

    Thanks

    - Mat
     
  2. Offline

    xTigerRebornx

  3. Offline

    yewtree8

  4. Offline

    ZachBora

    yewtree8 It's like your ZombieUtils class cannot be found. Can you post your full server log instead of a screenshot? Look in the logs folder.
     
  5. Offline

    xTigerRebornx

    yewtree8 From what I've gathered, the cause of the problem is (not surprised about this) the use of statics. More specifically, you are trying to access the instance of your class that extends JavaPlugin before an instance exists. My recommendation is to drop the use of statics, and turn the utility class into non-static methods located in either the class that extends JavaPlugin or the listener that it is needed in (the first if its required in multiple places)

    Edit: ZachBora the class is found fine, it would've thrown a ClassNotFoundException if the class weren't found, the problem is in the class definition (hence the 'Def' part)
     
  6. Offline

    ZachBora

    xTigerRebornx I think his plugin did an error when it loaded and that class failed to load correctly, because of like you said the
    private static String worldname = Main.getInstance().getConfig().getString("World_To_Spawn_Zombies_In");

    Maybe if he changed it to this :
    Code:java
    1.  
    2. private static [URL='http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky'][U][COLOR=#0066cc]String[/COLOR][/U][/URL] worldname = null;
    3.  
    4. private String getWorldName() {
    5. if(worldname == null) worldname = Main.getInstance().getConfig().getString("World_To_Spawn_Zombies_In");
    6. return worldname;
    7. }
    8.  

    and call getWorldName() instead of using worldname

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

    yewtree8

    ZachBora xTigerRebornx well, my code now looks like this.

    Code:java
    1.  
    2.  
    3.  
    4. @EventHandler
    5. public void onEntityCombust(EntityCombustEvent event) {
    6. if (Bukkit.getWorld(event.getEntity().getWorld().getName()).getTime() >= 0L) {
    7. if (Bukkit.getWorld(event.getEntity().getWorld().getName()).getTime() <= 24000L) {
    8. if (event.getEntityType() == EntityType.ZOMBIE) {
    9. event.setCancelled(true);
    10.  
    11. }
    12. }
    13.  
    14. }
    15. }
    16.  
    17.  
    18. @EventHandler(priority=EventPriority.HIGHEST, ignoreCancelled=true)
    19. public void onEntitySpawn(CreatureSpawnEvent event) {
    20. Entity entity = event.getEntity();
    21. if(entity instanceof Player) {
    22. return;
    23. }
    24. event.setCancelled(true);
    25. Random r = new Random();
    26. int randomNum = r.nextInt(100) + 1 ;
    27.  
    28. if(randomNum <= 5) {
    29. spawnStrongZombie(event.getLocation());
    30. return;
    31. } else if(randomNum >5) {
    32. spawnNormZombie(event.getLocation());
    33. return;
    34. }
    35.  
    36. }
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43. public void spawnNormZombie(Location loc) {
    44. String worldname = Main.getInstance().getConfig().getString("World_To_Spawn_Zombies_In");
    45. Zombie z = (Zombie) Bukkit.getServer().getWorld(getWorldName()).spawn(loc, Zombie.class);
    46. z.setCustomName(ChatColor.DARK_PURPLE + "Walker "+ ChatColor.WHITE + "-" + ChatColor.RED + "20 Health");
    47. z.setCustomNameVisible(true);
    48. z.setMaxHealth(20);
    49. z.setHealth(20);
    50. }
    51.  
    52. public void spawnStrongZombie(Location loc) {
    53. String worldname = Main.getInstance().getConfig().getString("World_To_Spawn_Zombies_In");
    54. Zombie z = (Zombie) Bukkit.getServer().getWorld(getWorldName()).spawn(loc, Zombie.class);
    55. z.setCustomName(ChatColor.DARK_PURPLE + "Strong Walker "+ ChatColor.WHITE + "- " + ChatColor.RED + "40 Health");
    56. z.setCustomNameVisible(true);
    57. z.setMaxHealth(40);
    58. z.setHealth(40);
    59.  
    60.  
    61. }
    62.  
    63. private static String worldname = null;
    64.  
    65. private String getWorldName() {
    66. if(worldname == null) worldname = Main.getInstance().getConfig().getString("World_To_Spawn_Zombies_In");
    67. return worldname;
    68. }
    69.  
    70.  
    71. }
    72.  
    73.  




    However I keep on getting a stacktrace when I log in, (Warning, Long)

    http://pastebin.com/k7rPv81j



    Any more help on the situation?
     
  8. Offline

    ZachBora

    yewtree8 when you login, it's trying to spawn zombies.

    Caused by: java.lang.NullPointerException
    at com.comule.matandgrant.listeners.ZombieSpawning.spawnNormZombie(ZombieSpawning.java:74)

    Means that's on line 74 of your class there is something null. I am guess it's this line :
    Code:java
    1. Zombie z = (Zombie) Bukkit.getServer().getWorld(getWorldName()).spawn(loc, Zombie.class);


    And it is probably caused by
    Code:java
    1. Main.getInstance().getConfig().getString("World_To_Spawn_Zombies_In");

    returning Null. What is this "Main" ? I do not think you are accessing your config correctly.

    What you should do it create a constructor on your class and when you create it pass in your plugin class and store that in a variable. Then you can simple access that variable .getConfig()
     
Thread Status:
Not open for further replies.

Share This Page