Solved Trouble with CreatureSpawner

Discussion in 'Plugin Development' started by ArcheRion720, Jul 11, 2017.

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

    ArcheRion720

    How i can make spawner spawn mobs with equipment, name, etc.?

    Code:
    @EventHandler
        public void RightClickWithPerms(PlayerInteractEvent e)
        {
                Block b = e.getClickedBlock();
    
                if(b.getType().equals(Material.MOB_SPAWNER)){
                    CreatureSpawner cs = (CreatureSpawner) b.getState();
                   //not sure what's going here
                }
         }
     
  2. Offline

    Machine Maker

    There isn't a really simple way of doing this. The best solution I think there is, is to change the NBTTags of the spawner using NMS. You are able do spawn spawners with the /give command that have mobs with armor so I know you can do this with NBT.
     
  3. Offline

    bennie3211

  4. Offline

    Machine Maker

    @bennie3211 Yes, I know you can change the equipment of a mob after they spawn, but you can also change what the spawner spawns. As in, the mob inside the spawner will appear to have armor and a sword. That avoids listening to the event and checking if it came from a spawner.

    @ArcheRion720 But, there is an easier way, but less recommended to have the server run the "/setblock" command with the NBT data in the command as well as the location of the spawner. I just see a lot of people saying that it's better if you use NMS.
     
  5. Offline

    ArcheRion720

    Problem is i cannot find tag for mob spawning, because you can't pick spawner on creative with ctrl (pick with nbt tags)
    and i couldn't find it on wiki
     
  6. Offline

    Machine Maker

  7. Offline

    ArcheRion720

    okay, but i don't know how to apply nbt tag to spawner via code

    EDIT: exactly how to get creature's spawner tags

    EDIT2: i have problem with one tag too,
    Vanilla command (open)
    {SpawnData:{id:"minecraft:zombie",HandItems:[{id:"minecraft:iron_shovel",Count:1b},{}]}}


    is HandItems list of tags?
     
    Last edited: Jul 12, 2017
  8. Offline

    Machine Maker

    @ArcheRion720 I figured out how to do it with code! Check it out!
    Code:
    TileEntityMobSpawner spawner = (TileEntityMobSpawner) ((CraftWorld) p.getWorld()).getHandle().getTileEntity(new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getZ()));
            NBTTagCompound tag = spawner.d();
            NBTTagCompound idTag = tag.getCompound("SpawnData");
            idTag.setString("id", "minecraft:zombie");
            NBTTagList equipList = new NBTTagList();
            NBTTagCompound equip = new NBTTagCompound();
            equip.setString("id", "minecraft:iron_sword");
            equip.setShort("Count", (short) 1);
            equipList.add(equip);
            equipList.add(new NBTTagCompound());
            idTag.set("HandItems", equipList);
            tag.setShort("SpawnCount", (short)10);
            tag.setShort("MaxEntitiesNearby", (short)100);
            tag.setShort("MinSpawnDelay", (short) 1);
            tag.setShort("MaxSpawnDelay", (short) 2);
            spawner.a(tag);
    So, first you need to get the TileEntityMobSpawner object, which you do like I did, but replace the X,Y,and Z coords with your own. Then, you need to get the NBT tag compound from the spawner which you do by calling the d() method on it. Then you set the tags and tag lists and there you go! This will spawn zombies with iron swords in their hand. If you want to, you can put armor on them by creating a new Tag list and setting it to "ArmorItems"
     
Thread Status:
Not open for further replies.

Share This Page