Problem with Zombie Spawning

Discussion in 'Spigot Plugin Development' started by Mariozgr8, Apr 28, 2017.

  1. Hi, so I am developing a plugin that will spawn a zombie at the location entered by the player when a player type /spawz (x) (y) (z). The plugin show in the /pl and everything seem to be working but when I execute the command on the sever I get an internal server error
    Here is my code:
    Code (Text):
    package me.mariozgr8.zombieapocalypse;

    import org.bukkit.plugin.java.JavaPlugin;

    public class MainClass extends JavaPlugin
    {
        @Override
        public void onEnable()
        {
            getLogger().info("Zombie Apocalypse has been enabled");
            this.getCommand("spawnz").setExecutor(new SpawnZCommand(this));
        }
       
        @Override
        public void onDisable()
        {
            getLogger().info("Zombie Apocalypse has been disabled");
        }
    }
     
    Code (Text):
    package me.mariozgr8.zombieapocalypse;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;

    public class SpawnZCommand implements CommandExecutor
    {
       

        public SpawnZCommand(MainClass plugins) {
            // TODO Auto-generated constructor stub
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(!(sender instanceof Player))
            {
                sender.sendMessage("This command can only be performed by a player");
                return true;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("spawnz"))
            {
                if(args.length !=3)
                {
                    p.sendMessage(ChatColor.DARK_RED+"Wrong usage! /spawnz");
                    return true;
                }
                if(args.length == 3)
                {
                    double x = Double.parseDouble(args[0]);
                    double y = Double.parseDouble(args[1]);
                    double z = Double.parseDouble(args[2]);
                    World w = p.getWorld();
                    String worldString = w.toString();
                   
                    Entity zombie1 = Bukkit.getServer().getWorld(worldString).spawnEntity(new Location(w, x, y, z), EntityType.ZOMBIE);
                    zombie1.setCustomName("TEST ZOMBIE");
                }
            }
           
            return false;
        }

    }
    Can anyone tell me whats the error in my code ?
    Thanks for helping :)
     
  2. Not the fix for your issue, but found this which is completely not required xd

    Code (Text):

    World w = p.getWorld();
                    String worldString = w.toString();
                 
                    Entity zombie1 = Bukkit.getServer().getWorld(worldString);
     
    Can be simply changed to this:
    Code (Text):

    Entity zombie1 = w.spawn...();
     
    Post your internal error so we can find the broken code line
     
    • Like Like x 1
  3. Ok so basically your error is on line 46 ( me.mariozgr8.zombieapocalypse.SpawnZCommand.onCommand(SpawnZCommand.java:46) )
    So that line is this:

    Code (Text):

                   Entity zombie1 = Bukkit.getServer().getWorld(worldString).spawnEntity(new Location(w, x, y, z), EntityType.ZOMBIE);
     
    This line can't be the reason so we read the exception again, its a nullpointer so anything by your command is null (i asume that its your worldString) because you just use this:

    Code (Text):

     World w = p.getWorld();
     String worldString = w.toString();
     
    As i mentioned in my post, this is completely useless because you already have the world defined ( w ) so just spawn your entity with this line:

    Code (Text):

                   Entity zombie1 = w.spawnEntity(new Location(w, x, y, z), EntityType.ZOMBIE);
     
     
    • Like Like x 1
  4. Thanks you so much for your help it worked :)