Disable natural mob spawning for specific mobs.

Discussion in 'Plugin Development' started by heknonn, Jul 24, 2019.

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

    heknonn

    I am trying to disable natural spawning for certain mobs and I couldn't find a solution on google and this didn't work for some reason
    Code:
    @EventHandler
        public void mobSpawn(CreatureSpawnEvent e) {
            if (!e.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.NATURAL)) return;
            if (e.getEntity().getType().equals(EntityType.PIG) e.setCancelled(true);
    Also cancelling all natural mob spawning didn't work...
     
  2. Offline

    Machine Maker

    Just to check, you are registering the event in the onEnable() method correct?
     
  3. Offline

    heknonn

    Yes.
    I also did some testing, I printed out if the event is being cancelled and I was basically spammed with "true" so I guess I am not using the correct event or something...
     
  4. Offline

    Machine Maker

    Well spamming with "true" is what you would get right? Cause the game is constantly trying to spawn mobs and then it is being cancelled. So that event gets triggered all the time.

    Another thing I noticed in those 4 lines of code, was the last one is missing a closing parenthesis. That may just be here, and not in your actual code.

    But anyway, if you have it printing something in the console if the event is being cancelled, isn't that what you wanted? Do those 4 lines of code prevent all Pigs from spawning?
    Remember that it won't get rid of pigs already spawned in the world, just prevent new ones from spawning.
     
  5. Offline

    heknonn

    Its's only here... I tried cancelling all spawn events that are natural, I deleted the world folder did /killall all with essentials and they keep spawning..
     
  6. Offline

    Kars

    This is what me and some friends use on our server to prevent cod and salmon spawns. Works like a charm.
    PHP:
    @EventHandler
    public void onCoddOrSalmonSpawn(EntitySpawnEvent event) {
        
    Entity entity event.getEntity();
        if (
    entity.getType() == EntityType.COD || entity.getType() == EntityType.SALMON)
            
    event.setCancelled(true);
    }
     
  7. Offline

    heknonn

    Well then what the hell
    PHP:
        @EventHandler
        
    private void mobSpawn(CreatureSpawnEvent e) {
            if (!(
    e.getSpawnReason() == CreatureSpawnEvent.SpawnReason.NATURAL)) return;
            
    e.getEntity().teleport(e.getLocation().add(0,10000000,0));
            
    e.getEntity().setGravity(false);
            
    deadEntities.add(e.getEntity().getUniqueId());
            
    e.getEntity().damage(400);
        }

        @
    EventHandler
        
    private void mobDeath(EntityDeathEvent e) {
            if (!
    deadEntities.contains(e.getEntity().getUniqueId())) return;
            
    e.getDrops().clear();
            
    deadEntities.remove(e.getEntity().getUniqueId());
        }
    This works for me xD
     
  8. Offline

    KarimAKL

    @heknonn Wouldn't you just be able to use Entity#remove()?
     
  9. Offline

    Kars

    @heknonn
    That's a TERRIBLE workaround.

    Try comparing entity type with == instead of equals
     
    KarimAKL likes this.
  10. Offline

    Machine Maker

    @Kars so I thought that was the original issue, but .equals on an enum is literally just a different way of writing ==. I had to look it up to make sure.
     
    heknonn likes this.
  11. Offline

    heknonn

    Also doesn't work, tried it also...
     
  12. Offline

    Kars

    @heknonn Use EntitySpawnEvent not CreatureSpawnEvent
     
  13. Offline

    heknonn

    But how can I check the spawn reason with EntitySpawnEvent?
     
  14. Offline

    Machine Maker

    But now I want to know why CreatureSpawnEvent isnt working. Theoretically it would be better to use Creature because it is triggered less often, right?
     
  15. Offline

    Kars

    Because Mojang is exerting prejudice against pigs. In their eyes, pigs are not creatures. We must not stand for such displays of small mindedness and bigotry. Pigs are creatures and we must force Mojang to recognise them as such. Unite!
     
    Zombie_Striker likes this.
  16. Offline

    Zombie_Striker

    #GamersRiseUp

    @heknonn
    You should try using EntitySpawnEvent, just in case there is an issue with the previous event (though I wouldn't understand why that would happen). If that still does not happen, try broadcasting a message containing the entity type and the spawn cause to try and figure out why pigs are not being detected.
     
  17. Offline

    heknonn

    All creatures aren't detected?
     
  18. Offline

    Machine Maker

    Well like Zombie said, we aren't quite sure why CreatureSpawnEvent isn't working, so just stick with the other one atm.
     
Thread Status:
Not open for further replies.

Share This Page