Stop Certain Mob Types Spawning

Discussion in 'Plugin Development' started by ZephireNZ, May 5, 2012.

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

    ZephireNZ

    So, I would like to prevent the spawning of certain mobs (namely the hostile mobs). I've already worked out that this code will work for a single mob:
    Code:
        @EventHandler
        public void onCreeperSpawn(CreatureSpawnEvent event) {
            if (event.getEntity() instanceof Creeper) {
                event.setCancelled(true);
            }
        }
    
    But that's just for a creeper. If I were to, say, program it to prevent spawning of creeper, skeletons and zombies, how would I go about doing it efficiently? (ie without copying the code as many times as I need for the mobs I have.
     
  2. Offline

    r0306

    I'm assuming that you are going to add a config file with a list of mob names inside it. If so, you could use this:
    Code:
    @EventHandler
     
        public void onCreeperSpawn(CreatureSpawnEvent event) {
     
            String mob = event.getEntity().getType().toString();
     
                      if (plugin.getConfig().getStringList("Mobs").contains(mob) {
         
                            event.setCancelled(true);
                      }
     
            }
    For a full list of types, go here: http://jd.bukkit.org/doxygen/d5/d27...entity.html#aa8b681536e15cce88eec09dcdf495b3f
     
    Prgr likes this.
  3. Offline

    ZephireNZ

    Grrr... I'm getting Null errors for the plugin.getConfig().getStringList("mobs").contains(mob) :(
    Code:
    16:17:11 [SEVERE] Could not pass event CreatureSpawnEvent to GameMaster
    org.bukkit.event.EventException at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
            at org.bukkit.craftbukkit.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:226)
            at net.minecraft.server.World.addEntity(World.java:893)
            at net.minecraft.server.ItemMonsterEgg.a(ItemMonsterEgg.java:41)
            at net.minecraft.server.ItemMonsterEgg.interactWith(ItemMonsterEgg.java:25)
            at net.minecraft.server.ItemStack.placeItem(ItemStack.java:83)
            at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:303)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:636)
            at net.minecraft.server.Packet15Place.handle(SourceFile:39)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
            at me.zephirenz.plugins.gamemaster.CreatureSpawnListener.onMobSpawn(CreatureSpawnListener.java:24)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301)
            ... 16 more
    
    I'm thinking maybe it's the way I implemented it.
    Java:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onMobSpawn(CreatureSpawnEvent event) {
            String mob = event.getEntity().getType().toString();
            logger.info("Detected " + mob + " Spawning");
            logger.info(plugin.getConfig().getStringList("mobs").toString());
            if (plugin.getConfig().getStringList("mobs").contains(mob)) {
                logger.info("Mob " + mob + " is on 'mobs'");
                event.setCancelled(true);
            }
        }
    
    Config:
    Code:
    # GameMaster config file
    # List of values you can use for mobs at (http://jd.bukkit.org/doxygen/d5/d27/namespaceorg_1_1bukkit_1_1entity.html#aa8b681536e15cce88eec09dcdf495b3f)
    mobs:
      - CREEPER
      - ZOMBIE
      - SKELETON
      - SPIDER
      - ENDERMAN
    
     
  4. Offline

    r0306

    Hmm. I just tried the code and it worked fine. Here, copy and paste this into your config file, removing all previous data:

    Code:
    mobs:
    - CREEPER
    Then try spawning a creeper and see if it works.
     
  5. Offline

    ZephireNZ

    Yep. Same error.
    I'm just starting out, so I wasn't exactly sure how to set up the config file. I just went with what I found on the wiki which was a file in the plugin jar file. Was that not the way to do it? Or do I have to initialize it somehow?
     
  6. Offline

    r0306

    Ok. You were supposed to create it automatically on plugin startup but that's all right. All you have to make sure of is that inside the plugins folder, create a folder with the name of your plugin. Inside, put the config.yml file. That may be the cause of the null pointer.
     
  7. Offline

    ZephireNZ

    Nope :( Still does the same thing... You say it works fine for you?
     
  8. Offline

    Fluttershy

    I think you mean instead of doing this:
    Code:
        @EventHandler
        public void onSpawn(CreatureSpawnEvent event) {
            if (event.getEntity() instanceof Creeper) {
                event.setCancelled(true);
            }
     
            if (event.getEntity() instanceof SKELETON) {
                event.setCancelled(true);
            }
     
            if (event.getEntity() instanceof ZOMBIE) {
                event.setCancelled(true);
            }
     
            if (event.getEntity() instanceof COW) {
                event.setCancelled(true);
            }
     
            if (event.getEntity() instanceof CHICKEN) {
                event.setCancelled(true);
            }
     
            //ECT
        }
    You want to do this?
    Code:
        @EventHandler
        public void onSpawn(CreatureSpawnEvent event) {
            /* Put all mobs that you don't want to spawn into an array */
            EntityType[] blacklist = {
                    EntityType.CREEPER,
                    EntityType.SKELETON,
                    EntityType.ZOMBIE,
                    EntityType.CHICKEN,
                    EntityType.COW,
                    EntityType.ENDERMAN,
                    EntityType.IRON_GOLEM
            };
           
            /* Then loop through the array */
            for(int i = 0; i < blacklist.length; i++)                 
                if(event.getEntityType() == blacklist[i]){
                    event.setCancelled(true);
                }
            }
    }
     
  9. Offline

    r0306

    Yes, make sure that your folder is inside the plugins folder and it has the exact capitalization and name as your plugin. Don't add spaces to your config. Copy and paste this and try it again.
    Code:
    mobs:
    - CREEPER
    - ZOMBIE
    - SKELETON
    - SPIDER
    - ENDERMAN
    Try this first and if it doesn't work, try adding quotation marks around the names. (It worked for me without quotation marks).

    Code:
    mobs:
    - "CREEPER"
    - "ZOMBIE"
    - "SKELETON"
    - "SPIDER"
    - "ENDERMAN"
    Also, can you give me line 24 of CreatureSpawnListener?
     
  10. Offline

    ZephireNZ

    Still the same thing. Line 23 is
    Code:
            logger.info(plugin.getConfig().getStringList("mobs").toString());
    
    If you want, I've actually got it hosted on GitHub: http://github.com/ZephireNZ/GameMaster

    EDIT: Oops! Line 24 is:
    Code:
            if (plugin.getConfig().getStringList("mobs").contains(mob)) {
    
     
  11. Offline

    r0306

    Here, try replacing your main class with this. Import any needed imports.
    Code:
    package me.zephirenz.plugins.gamemaster;
     
    import java.util.logging.Logger;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class GameMaster extends JavaPlugin implements Listener {
     
    public static GameMaster plugin;
    public final Logger logger = Logger.getLogger("Minecraft");
    private CommandListener commandListener;
     
     
        public void onDisable() {
        PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " is now disabled.");
        }
     
        public void onEnable() {
    loadConfig();
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(new CreatureSpawnListener(), this);
    // Command Listener Set Up for /mobs
    commandListener = new CommandListener(this);
    getCommand("mobs").setExecutor(commandListener);
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled.");
        }
     
        public void loadConfig() {
      ArrayList list = new ArrayList();
            list.add("CREEPER");
            list.add("ZOMBIE");
            list.add("SKELETON");
            list.add("SPIDER");
            list.add("ENDERMAN");
            FileConfiguration cfg = this.getConfig();
            FileConfigurationOptions cfgOptions = cfg.options();
            if (!this.getConfig().contains("mobs") {
            this.getConfig().addDefault("mobs", list);
            }
            cfgOptions.copyDefaults(true);
            saveConfig();
        }
    }
     
    
     
  12. Offline

    ZephireNZ

    Nope, still no good. Still getting the NullPointerException at line 23 of CreatureSpawnListener.
    From what I think I can figure out, it's as if it isn't detecting the mobs from the config file. But that's just my guess (from the debug line that's causing the exceptions.

    Oh wait, just realised that there was an addition to onEnable! Adding it now!

    EDIT: ... But it makes no difference... :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  13. Offline

    r0306

    Hmm, do you have teamviewer?
     
  14. Offline

    ZephireNZ

    Yep. Hold on, PM?
     
  15. Offline

    r0306

    K. That would be good.
     
Thread Status:
Not open for further replies.

Share This Page