Mob-Spawning Prevention Not Working

Discussion in 'Plugin Development' started by The Gaming Grunts, Feb 8, 2014.

Thread Status:
Not open for further replies.
  1. Hey everyone! So, what the code below is supposed to do is prevent hostile mobs from spawning if a value is set to false in a .yml file. However, this doesn't seem to be working at all. What did I do wrong? Thanks in advance for the help :)

    Code:java
    1. @EventHandler
    2. public void onSpawn(CreatureSpawnEvent e){
    3. File worlds = new File(Main.getInstance().getDataFolder(), "worlds.yml");
    4. FileConfiguration c = YamlConfiguration.loadConfiguration(worlds);
    5.  
    6. if (!(c.getBoolean(e.getEntity().getWorld().getName() + ".allow hostile mobs"))){
    7. System.out.println("false");
    8. if (e.getEntity() instanceof Monster){
    9. System.out.println("monster");
    10. if (e.getSpawnReason() == SpawnReason.NATURAL || e.getSpawnReason() == SpawnReason.SPAWNER_EGG || e.getSpawnReason() == SpawnReason.SPAWNER_EGG){
    11. System.out.println("reason");
    12. e.getEntity().remove();
    13. System.out.println("[WorldManager] Prevented the spawning of a " + e.getEntity().getType());
    14. }
    15. }
    16. }else{
    17. System.out.println("true");
    18. }


    worlds.yml

    Code:
    world:
      weather: true
      allow pvp: false
      allow hostile mobs: false
      allow passive mobs: true
      allow hunger: false
      allow health regeneration: true
     
  2. Offline

    pyropyro78

    The Gaming Grunts
    I will look more into this and get back to you but my initial thought without doing any research or handling spawning before is that instead of using e.getEntity().remove() use e.setCancelled(true);
     
  3. Offline

    _Filip

    Did you get an error? If so, can you post the stack trace?
    Try adding some debug messages, with
    Code:
    System.out.println(the value you want to check);
     
  4. pyropyro78
    e.setCancelled() for entity spawning causes memory leaks

    swampshark19
    Did you not see the ones I put? :p None of them even displayed :(
     
  5. Offline

    xTigerRebornx

    The Gaming Grunts I would highly recommend you read through this before progressing through your code. Removing the entity will still cause the problems canceling the event does.
     
  6. Offline

    Maurdekye

    The Gaming Grunts Put the e.getEntity().remove() inside a scheduled event 1 tick later. That will run after the entity has fully spawned, and will get rid of it.
     
    xTigerRebornx likes this.
  7. Offline

    _Filip

    The Gaming Grunts
    Oh wow, sorry, I didn't even look at the code. Haha.
    Check if the FileConfiguration instance is being made properly, try reading another value from it and output it to the console.
    Make a debug message above the first if statement, with this we can see if the event is even being called.
     
  8. Maurdekye
    The rest of the code isn't even running, so how would that even work?
     
  9. Offline

    xTigerRebornx

    The Gaming Grunts How are you spawning them? Look at your code for checking the SpawnReason, you check SPAWNER_EGG twice, I think the 3rd one should be something else?
     
  10. Offline

    Maurdekye

    The Gaming Grunts It's definitely a problem with your file configuration. I suggest you either use the default config file, or use this class I created specifically for hosting multiple config files. It's basically the same as using a normal config file, but you have to assign the files you create to a variable, and use that variable to call config file methods. Like so;
    Code:java
    1. CustomConfig config1 = new CustomConfig(this, "config1.yml");
    2. config1.getConfig().options().copyDefaults(true);
    3. config1.saveConfig();


    Edit: Nevermind, then..
     
  11. Whoops didn't even notice that. One of them was supposed to be SPAWNER. Thanks for the catch :)
     
  12. Offline

    xTrollxDudex

    The Gaming Grunts
    Your server will eventually run out of heap space leaking memory. The only safe way is to remove the mobs from getMobsFor in ChunkProviderServer (or ChunkServerProvider, forgot) before returning it.
     
  13. xTrollxDudex
    Will it? I have the boolean for hostile mob spawning set to false (prevents mobs from spawning), the time set to night, and dispencers with fast clocks running trying to spawn creepers. So far, there has been almost no increase in RAM usage. Here's the code I'm running:

    Code:java
    1. @EventHandler
    2. public void onSpawn(CreatureSpawnEvent e){
    3. File worlds = new File(Main.getInstance().getDataFolder(), "worlds.yml");
    4. FileConfiguration c = YamlConfiguration.loadConfiguration(worlds);
    5.  
    6. if (c.getBoolean(e.getEntity().getWorld().getName() + ".allow hostile mobs") == false){
    7. if (e.getEntity() instanceof Monster){
    8. if (e.getSpawnReason() == SpawnReason.NATURAL || e.getSpawnReason() == SpawnReason.SPAWNER || e.getSpawnReason() == SpawnReason.SPAWNER_EGG){
    9. e.getEntity().remove();
    10. System.out.println("[WorldManager] Prevented the spawning of a " + e.getEntity().getType());
    11. }
    12. }
    13. }else{
    14. System.out.println("true");
    15. }
     
  14. Offline

    Maurdekye

    The Gaming Grunts The increase in RAM usage is when you cancel the event. Right now you're just removing the entity, which, if it's working for you, go ahead and continue using it.
     
  15. Offline

    xTrollxDudex

  16. Offline

    xTrollxDudex

    The Gaming Grunts
    Well, looks like Bukkit may have fixed it. Or your observation period may have been too short.
     
  17. xTrollxDudex
    I'll try running it for longer with more spawners & dispensers going. It is entirely possible that Bukkit fixed it, but I think the garbage collection helps out a bunch
     
Thread Status:
Not open for further replies.

Share This Page