New to plugin dev, need code check.

Discussion in 'Plugin Development' started by jtc883, Feb 18, 2012.

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

    jtc883

    So I jsut started out working on a custom plugin for my server. My first task i wanna do is a simple command /dragon to spawn an ender dragon for my players to fight.

    here's what i have. from what i'm reading 'im doing it right but a dragon never spawns. Any help would be appreciated.

    CODE:

    public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
    Player player = (Player)cs;

    if (cmd.getName().equalsIgnoreCase("dragon"))
    player.getWorld().spawnCreature(player.getLocation(), CreatureType.ENDER_DRAGON);
    return false;
     
  2. Offline

    Sir Savary

    Not bad for a first time, but let me show you a few things.

    First off, when you post code, do it using syntax=java, like so (Put in square brackets also):

    Code:java
    1.  
    2. public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
    3. Player player = (Player)cs;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("dragon"))
    6. player.getWorld().spawnCreature(player.getLocation(), CreatureType.ENDER_DRAGON);
    7. return false;
    8.  


    Now, in your command there, you have it returning false when the command completes, that is bad. Make it return true! You also didn't use brackets on the if statement, or put a closing bracket on the command. I'd also like yo point out that you need a full plugin code file like thing for this to work. Below is the edited code.


    Code:java
    1.  
    2. public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
    3. Player player = (Player)cs;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("dragon"))
    6. player.getWorld().spawnCreature(player.getLocation(), CreatureType.ENDER_DRAGON);
    7. return false;
    8.  
     
  3. Offline

    jtc883

    As I was typing I figured it out, really dumb mistake. I am calling a few different commands in their own command class, guess what dummy over here forgot to do. "getCommand("dragon").setExecutor(myExecutor);" Thanks for your help tho, I really appreciate the friendliness of the community for us noobs.
     
  4. Offline

    LaLa

    You can't assume that the command sender is a player, because if the console sent your command, it would get back a nice amount of errors. You usually need to check if the sender is a player, like this:
    Code:java
    1.  
    2. if (!(cs instanceof Player)) {
    3. // they are not a player, most likely want to send them a message like
    4. cs.sendMessage("You need to be a player to do this");
    5. return true;
    6. }
    7.  

    On a side note, you may want to spawn the dragon several blocks above the player, and not at their exact location.
     
  5. Offline

    jtc883

    So if I want to spawn it farther away say from where I'm standing I would use getTargerBlock() instead of player.getLocation()?

    And now my next step is to do a countdown from 10 for my players to see when the fighting will begin.
     
  6. Offline

    LaLa

    To spawn 20 blocks above them, you would do this instead.
    Code:java
    1.  
    2. Location loc = player.getLocation();
    3. player.getWorld().spawnCreature(new Location(player.getWorld(), loc.getX(), loc.getY()+20, loc.getZ()), CreatureType.ENDER_DRAGON);
    4. return true;
    5.  


    The target block is the block the player is looking at.
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    Height can cause some problems, you really want to take the min between loc.getY() + 20 and World.getMaxHeight()
     
  8. Since the first posts have messed up with the return true/false here's an explanation when to return tr and when false:

    true: Correct usage, e.g right argument length and type.
    false: wrong usage, wrong amount of parameters or wrong type (This will also send the usage declared in the plugin.yml to the user)
     
    ZNickq likes this.
Thread Status:
Not open for further replies.

Share This Page