Spawning a mob at player location on command (Newbie developer)

Discussion in 'Plugin Development' started by wolvenmoon, Oct 6, 2011.

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

    wolvenmoon

    Hi guys and gals,

    So, for my first plugin (and first time coding in Java) I want to create a command that spawns a creeper near a target player. The command will be executed using "/behindyou playername".

    I've been following http://wiki.bukkit.org/Plugin_Tutorial#Starting_a_Plugin_Project loosely. I've not programmed extensively for 3 years now, and when I did I did it in C++. Figures that Minecraft would remind me coding is sort of fun. :D

    I'm having an issue figuring out how to appropriately call the function method that spawns a creature. My IDE is griping at me - my plugin isn't in a working state (and I don't know how far I have left to go, even). When I look at what I have in Eclipse it's rather puny, but we all start somewhere.

    So here's what I have so far, my error is

    Code:
    Description    Resource    Path    Location    Type
    The method spawnCreature(Location, CreatureType) is undefined for the type BehindYou    BehindYou.java    /behindyou/src/behindyou    line 44    Java Problem
    (I know I have too many imports for what I'm doing. You all can smack me with a wooden shovel after I get this working.)

    Code:
    package behindyou;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.entity.CreatureType;
    import org.bukkit.entity.Creeper;
    import org.bukkit.World;
    
    public class BehindYou extends JavaPlugin {
    
    Logger debuglog = Logger.getLogger("Minecraft");
    
    public void onEnable(){
        debuglog.info("BehindYou Enabled");
        
    }
    
    public void onDisable(){
        
    }
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, Player args){ //note that 'args' is string[] in tutorial
        if(cmd.getName().equalsIgnoreCase("behindyou")) //if the player types /behindyou targetname do...
        {
            
            Player target = args;
            Location playerLoc = target.getLocation();
            
            //double x = playerLoc.getX();
            //double y = playerLoc.getY();
            //double z = playerLoc.getZ();
            
            playerLoc.add(-8.0,0.0,-8.0);
            
            spawnCreature(playerLoc, org.bukkit.entity.CreatureType.CREEPER);
            //action to do
            
            return true;
        } //if it worked, return true, else if it didn't work, the command failed, and return false
        return false;
    }
    
    }
    
    What am I doing wrong / not importing?

    Thanks!
     
  2. Offline

    Lolmewn

    You are doing the commands wrong.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String str, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
    is what it normally looks like.
     
    wolvenmoon likes this.
  3. Offline

    SparrowMaxx

    Java doesn't know what to do with spawnCreature. If you don't add some reference to another file/object/etc, it assumes that the method spawnCreature is somewhere in your file; it's not.

    However, spawnCreature does exist in Bukkit, right?

    Use this:
    target.getWorld().spawnCreature(blahblahblah);

    You take the player "target," then get the world he is living in right now. You were looking to use the spawnCreature method from the World class, which now you have access to from your player, "target."
     
    wolvenmoon likes this.
  4. Offline

    wolvenmoon

    Thank you both for the help!

    I got to another hurdle and have firmly smacked straight into it. Apparently my command makes bukkit sad, and while it would really like to let me access the command, it cannot, and thus will leak tears for me.

    Literally: "Bukkit sad. Bukkit want you to access command, but Bukkit cannot let you. Bukkit will leak tears :'("

    I'm an op, have a permissions plugin running, but apparently my command makes baby bukkit cry.

    Code is thus:

    Code:
    package behindyou;
    //all code in the package 'behindyou' is released WTFPL.
    
    import java.util.logging.Logger;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.entity.CreatureType;
    import org.bukkit.entity.Creeper;
    import org.bukkit.World;
    
    public class BehindYou extends JavaPlugin {
    
    Logger debuglog = Logger.getLogger("Minecraft");
    
    public void onEnable(){
        debuglog.info("BehindYou Enabled");
    
    }
    
    public void onDisable(){
    
    }
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){ //note that 'args' is string[] in tutorial
        if(cmd.getName().equalsIgnoreCase("behindyou")) //if the player types /behindyou targetname do...
        {
    
            Player target = this.getServer().getPlayer(args[0]);
            Location playerLoc = target.getLocation();
    
            //double x = playerLoc.getX();
            //double y = playerLoc.getY();
            //double z = playerLoc.getZ();
    
            playerLoc.add(-8.0,0.0,-8.0);
    
            target.getWorld().spawnCreature(playerLoc, org.bukkit.entity.CreatureType.CREEPER);
            //action to do
    
            return true;
        } //if it worked, return true, else if it didn't work, the command failed, and return false
        return false;
    }
    
    }
    
    plugin.yml is such:

    Code:
    name: BehindYou
    main: behindyou.BehindYou
    version: 0.1
    commands:
        behindyou:
            description: Spawns a creeper around a target player
            permission: behindyou.spawn
            usage: /behindyou [player]
    
    Attempted on a vanilla bukkit server as well as a bukkit server with a number of other plugins, both with the same sad bukkit error. What did I miss? (I feel so horrible - I made a bukkit cry! I must be a bad person! ;P )
     
  5. It means that you don't have the permission. Double check again that you have it. If it still doesn't work, just remove the permission from your plugin.yml for testing purposes.
     
    wolvenmoon likes this.
Thread Status:
Not open for further replies.

Share This Page