Check to see if command argument is equal to a mob type

Discussion in 'Plugin Development' started by tylersyme, May 9, 2013.

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

    tylersyme

    Code:
    if (commandLabel.equalsIgnoreCase("spawn") && args.length == 2)
    {
    for (int i = 0; i < Integer.parseInt(args[1]); i++)
    {
    if (args[1].length() < 100)
    {
    theWorld.spawnEntity(loc, EntityType.CREEPER);
    } else {
    player.sendMessage(ChatColor.RED + "You cannot spawn more than 100 mobs at one time");
    return false;
    }
    }
    Yeeea, it's not done yet. This command will have 2 arguments: [mob type] [number of mobs]
    e.g. /spawn creeper 18 (that will spawn 18 creepers)
    I don't know how to check to make sure that the first argument is equal to a LivingEntity. How do I make sure?
     
  2. Offline

    r0306

    tylersyme
    Reformat your code, it's messed up.

    You can match the command with the names of the entitytypes as defined in the Bukkit API using the getName() function for EntityType. Otherwise, you would have to code your own check for their names.
     
  3. Offline

    Jamie Marks

    Code:
    if(!( Integer.pareint(args[1]) > 100)){
    for (int i = 0; i < Integer.parseInt(args[1]); i++)
    {
    //spawn creeper
    }
    } else {
    //cant spawn over 100 stuff here
    }
    also you may want to have this in a try catch so if the player does say /spawn creeper sdsg it wont cause an error to console.

    should work may need editing to fit your stuff. hope it helped!

    edit: to make sure its a living entity a try catch would work aswell.
     
  4. Offline

    tylersyme

    I'm a noob to programming in general, a try catch sounds pretty important. Let me look it up :)
     
  5. Offline

    Jamie Marks

    tylersyme yeah what it will do is for example someone puts spawn crpx (spells it wrong) the catch will tell them that the mob they typed is wrong, if your in real need of help i might be able to post an example.
     
  6. Offline

    tylersyme

    That would be great if you could. That knowledge could be used for a lot more than just this
     
  7. Offline

    Jamie Marks

    sat in college with no Eclipse so exuse errors here,

    Code:
    try{
    if(!( Integer.pareint(args[1]) > 100)){
    for (int i = 0; i < Integer.parseInt(args[1]); i++)
    {
    player.getWorld().spawn(Creeper.class)
    }
    } else {
    //cant spawn over 100 stuff here
    }
    }catch(exception e) {
    player.sendMessage("No Number given try again")
    }
     
  8. I would do it like this:

    Make a new Enum with all the mob types.

    Also make an interface that contains something like: spawnMob(Player player);

    Make a class for each mob that is in your enum, if your enum contains Creeper then make a class Creeper and let it implement your interface.

    Then check if the command equals a specific enum type, if true > continue.

    Then do something like this:
    urInterface lol;
    String mobclass = "urpackage." + the arg/mob;
    lol = (urInterface) Class.forname(mobclass).newInstance();
    and then do lol.spawnMob(Player player);

    in each mobclass u do:
    public static void spawnMob(Player player){
    player.getTargetBlock().getLocation().spawnEntity(Creeper.class); < if this is your creeper class

    You can also add an extra integer argument to the spawnMob() method so you can define how many mobs to spawn.

    I hope I was clear ;D, there are other ways but I would do it like this ;p (if you need any info then just pm me)
     
  9. Offline

    chasechocolate

    Code:java
    1. EntityType type = null;
    2.  
    3. try{
    4. type = EntityType.valueOf(args[0]);
    5. } catch(Exception e){
    6. //Couldn't find the mob type
    7. }
     
  10. Oh didn't knew that was acctually possible...
     
Thread Status:
Not open for further replies.

Share This Page