Am I doing anything wrong using CreatureSpawnEvent?

Discussion in 'Plugin Development' started by soshimee, Jan 7, 2021.

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

    soshimee

    I have a code that looks like this:
    Code:
    @EventHandler
    public void onCreatureSpawnEvent(CreatureSpawnEvent event)
    {
        // Some code that doesn't run at all when a mob spawns
    }
    
    However, the code doesn't even run when a mob spawns. Am I doing anything wrong?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @soshimee Please post the line where you register the listener.
     
  3. Offline

    soshimee

    I didn't, sorry, I'm new to Java. Now I registered the listener and then... my system crashed.
    After testing with /gamerule doMobSpawning, it's making a NPE every time I spawn a mob.
     
  4. Offline

    timtower Administrator Administrator Moderator

    Your entire PC?
     
  5. Offline

    soshimee

    Yes, unfortunately, probably because the server somehow ate up all of my RAM.
     
  6. Offline

    timtower Administrator Administrator Moderator

    What are you doing in the event?
     
  7. Offline

    soshimee

    Just giving some boosts to mobs that spawn in by a random chance.
    It doesn't happen anymore though.
    Here's my code:
    Code:
    public class CreatureSpawnHandler implements Listener {
    private Main plugin;Random r = new Random();@EventHandlerpublic void onCreatureSpawnEvent (CreatureSpawnEvent event) {
    if (Main.getInstance().isNight) {
    LivingEntity entity = event.getEntity();EntityType type = event.getEntityType(); if (type.equals(EntityType.ZOMBIE)) {
    entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(40);entity.setHealth(40); if (r.nextFloat() <= .35) {
    entity.getEquipment().setItemInMainHand(new ItemStack(Material.IRON_SWORD));}
    if (r.nextFloat() <= .1) {
    entity.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000000, 0));}
    } else if (type.equals(EntityType.SKELETON)) {
    entity.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000000, 0));} else if (type.equals(EntityType.SPIDER)) {
    entity.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 10000000, 1));}
    }
    }
    }
    
    Wonder why bukkit breaks indentations and some newlines.
    + No errors on the editor side.
    ++ I think the crash was just a coincidence, it doesn't happen anymore whatever I do.

    Fixed it! Main.getInstance() was returning null and I just needed to correct that.
     
    Last edited: Jan 7, 2021
Thread Status:
Not open for further replies.

Share This Page