How do you spawn custom mobs?

Discussion in 'Plugin Development' started by Deleted user, Feb 28, 2015.

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

    Deleted user

    I am using the following code.
    Code:
    CraftWorld craft = (CraftWorld)getServer().getWorlds().get(0);
    WorldServer world = craft.getHandle();
    Boss boss = new Boss(world);
    Location spawn = craft.getSpawnLocation();
    boss.setLocation(spawn.getX(), spawn.getY(), spawn.getZ(), spawn.getYaw(), spawn.getPitch());
    world.addEntity(boss);
    Code:
    public class Boss extends EntityZombie {
        public Boss(World world) {
            super(world);
        }
    }
    If I run it by staying away, when I come back I see nothing.
    If I run it by being close, my client crashes.
     
  2. Offline

    anson023

    try this ?
    Code:
    public boolean onCommand(CommandSender sender , Command cmd , String lable , String[] args) {
        Player p = (Player) sender;
        if(lable.equalsIgnoreCase("customskeleton") && getConfig().getBoolean("SkeletonCommand")) {
            Location location = p.getLocation();
            Skeleton sk = (Skeleton) p.getWorld().spawnCreature(location, EntityType.SKELETON);
            double dropchancehelmet = getConfig().getDouble("SkeletonEdit.dropchancehelmet");
            double dropchancechestplate = getConfig().getDouble("SkeletonEdit.dropchancechestplate");
            double dropchancelegs = getConfig().getDouble("SkeletonEdit.dropchancelegs");
            double dropchanceboots = getConfig().getDouble("SkeletonEdit.dropchanceboots");
            double dropchanceiteminhand = getConfig().getDouble("SkeletonEdit.dropchanceiteminhand");
            double health = getConfig().getDouble("SkeletonEdit.health");
            String name = getConfig().getString("SkeletonEdit.name");
            boolean namevisible = getConfig().getBoolean("SkeletonEdit.namevisible");
            int fireticks = getConfig().getInt("SkeletonEdit.fireticks");
            boolean removewhenfaraway = getConfig().getBoolean("SkeletonEdit.removewhenfaraway");
            ItemStack boots = new ItemStack(Material.getMaterial(getConfig().getInt("SkeletonEdit.boots")));
            ItemStack chestplate = new ItemStack(Material.getMaterial(getConfig().getInt("SkeletonEdit.chestplate")));
            ItemStack helmet = new ItemStack(Material.getMaterial(getConfig().getInt("SkeletonEdit.helmet")));
            ItemStack legs = new ItemStack(Material.getMaterial(getConfig().getInt("SkeletonEdit.legs")));
            ItemStack iteminhand = new ItemStack(Material.getMaterial(getConfig().getInt("SkeletonEdit.iteminhand")));
            boolean canpickupitems = getConfig().getBoolean("SkeletonEdit.canpickupitems");
    /*************************************************************/
            sk.setRemoveWhenFarAway(removewhenfaraway);
            sk.getEquipment().setBoots(boots);
            sk.getEquipment().setHelmet(helmet);
            sk.getEquipment().setChestplate(chestplate);
            sk.getEquipment().setLeggings(legs);
            sk.getEquipment().setItemInHand(iteminhand);
            sk.setMaxHealth((int)health);
            sk.setHealth((int)health);
            sk.setCanPickupItems(canpickupitems);
           
            if(getConfig().getBoolean("SkeletonEdit.usewither")) {
                int witherchance = getConfig().getInt("SkeletonEdit.usewither");
                int ran = ((int)(Math.random() * 10000 + 1));
                if(ran <= witherchance*100) {
                    sk.setSkeletonType(Skeleton.SkeletonType.WITHER);
                    }
                }
                if(getConfig().getBoolean("SkeletonEdit.usecustomnameatall")) {
                    sk.setCustomName(name);
                    sk.setCustomNameVisible(namevisible);       
                }
                sk.getEquipment().setBootsDropChance((float)dropchanceboots);
                sk.getEquipment().setHelmetDropChance((float)dropchancehelmet);
                sk.getEquipment().setChestplateDropChance((float)dropchancechestplate);
                sk.getEquipment().setLeggingsDropChance((float)dropchancelegs);
                sk.getEquipment().setItemInHandDropChance((float)dropchanceiteminhand);
                sk.setFireTicks(fireticks);
            }
        return false;
    }
    @EventHandler
    [B]public void[/B] onEntityByEntity(EntityDamageByEntityEvent e) {
     if(getConfig().getBoolean("UseCustomDamage") && 
    e.getEntity() [B]instanceof [/B]Player){
    [B]if[/B](e.getDamager instanceof LivingEntity){
     LivingEntity damager = (LivingEntity) e.getDamager();
    [B]if[/B](damager.getCustomName() == getConfig().getString("SkeletonEdit.name")) {
     e.setDamage((int)getConfig().getDouble("SkeletonEdit.damage"));
     }[B]else[/B]{}
     }
     }
    }
    @EventHandler
    [B]public void[/B] onEntityDeath(EntityDeathEvent e) {
    [B]if[/B](getConfig().getBoolean("UseCustomDeaths") && 
     e.getEntity instanceof LivingEntity){
     LivingEntity le = e.getEntity();
    [B]if[/B](getConfig().getBoolean("CustomDeath.SkeletonDeathUse") && 
     le.getCustomName == getConfig().getString("SkeletonEdit.name")){
    [B]int [/B]exp = getConfig().getInt("CustomDeath.SkeletonDeath.Exp");
     e.setDroppedExp(exp);
     ItemStack drop1 = ItemStack(Material.getMaterial(getConfig().getInt("CustomDeath.SkeletonDeath.drop1")));
     e.getDrops.clear();
     ArrayList drops = [B]new [/B]ArrayList();
     drops.add(drop1);
     e.getDrops.addAll(drops);
     }
     }
    }
    
     
  3. Offline

    Deleted user

    @anson023 I am trying to spawn a custom zombie, not the vanilla one.

    Do I need to register any custom entity, even if I'm not trying to replace their natural spawning? If yes, how?
    @teej107

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

    teej107

    @Joiner I rarely use NMS so I don't think I can help.
     
  5. Offline

    Deleted user

  6. Offline

    Deleted user

  7. Offline

    Deleted user

  8. Okay... Maybe use this one:

    Code:
    Player p = (Player) sender;
                Location loc = p.getLocation();
                Creeper creeper = (Creeper) p.getWorld().spawnCreature(loc, CreatureType.CREEPER);
                creeper.setCustomName(ChatColor.GOLD + "Willi");
                creeper.setCustomNameVisible(true);
                creeper.setPowered(true);

    Change everything to a Zombie or whatever and try it :D
     
  9. Offline

    Deleted user

  10. @Joiner
    It isnt the "Vanilla one"... It has a name...
    What do you want to change?
     
  11. Offline

    Deleted user

  12. Offline

    Deleted user

    Last bump.
     
  13. Offline

    Tecno_Wizard

    @Joiner, typically custom mobs are mods only. Yes, plugins are technically mods, but they aren't usually editing the game itself, just adding behaviors. Hence most of us have no idea how to help you.
     
  14. Offline

    ItsMattHogan

    You can't make a custom mob without client modding, although, you can manipulate an existing mob, however, it's quite restrictive in what you can do.

    I used this API a while back: https://github.com/kumpelblase2/Remote-Entities/ but it's probably very out of date.

    Post the crash report from your client?
     
  15. Offline

    Deleted user

  16. Offline

    Funergy

    @Joiner Did you register the entity?
    Since you are editing a living entity you will need to register it. Things like a Player and an Arrow don't need to be registered
     
  17. Offline

    Deleted user

    @Funergy No! I've started this thread just to understand how to do that!
     
  18. @Joiner You need to register your entity before spawning it. Note that registering it will override the default one so that's why you should store the original entity. So when you spawn your entity: store original entity -> register custom entity -> spawn custom entity -> register original entity again.

    In EntityAPI we made a little tool for this. We use an EntityREgistrationEntry to store the old/original entity, and then use our own EntityRegistry class to handle the registering.

    Note that this code is most likely outdated, just to give you an idea.

    Edit: Credit for this way of doing it goes to @kumpelblase2
     
    DSH105 likes this.
Thread Status:
Not open for further replies.

Share This Page