Spawn egg and make it follow nearest player in a list?

Discussion in 'Plugin Development' started by Paper, May 31, 2013.

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

    Paper

    Hi, I was wondering how to make it so when a player spawns a mob from an egg, the mob will follow the closest player in a specific list. If you have any code snippets (I don't like being spoonfed code, thanks) of doing this, posting it here would be greatly appreciated. Thanks in advance.
     
  2. Iterate though the list of players, fin out what player is closest to the mob --> mod.setTarget(player)
     
  3. Offline

    BRampersad

    DO you mean follow as in friendly or attack? You could try setting the mob as a tamed pet and it will follow the person. If you mean attack, then you could just set the target as stated above.
     
  4. Offline

    Paper

    BRampersad Yeah I meant them to be hostile. And Sgt_Tailor I'm guessing that would be in CreatureSpawnEvent? If so how would I be able to tell if a player spawned the egg?
     
  5. Offline

    Jake0oo0

    Paper I don't believe there is a PlayerSpawnEntityEvent type of thing, but you could make check player interact events?
     
  6. Last edited by a moderator: Jun 1, 2016
  7. Offline

    Paper

    Yeah I saw that when I was browsing through the docs, but theres no way to find out who spawned it...
     
  8. Then why not spawn it yourself? Listen to the PlayerInteractEvent, check the action for RIGHT_CLICK_BLOCK, retrieve the block from the event, get the spawn egg type and then spawn it on the block and cancel the event? I don't see any point why this is impossible to you.

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event)
    3. {
    4. // For performance reasons, make the basic checks first
    5. if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
    6. {
    7. Player player = event.getPlayer();
    8.  
    9. if (player.getItemInHand().getType() == Material.MONSTER_EGG)
    10. {
    11. // Cancel the event, this prevents the original spawn
    12. event.setCancelled(true);
    13.  
    14. // Retrieve the clicked block
    15. Block clickedBlock = event.getClickedBlock();
    16.  
    17. // Reduce item count of the egg if player is not in creative mode
    18. if (player.getGameMode() != GameMode.CREATIVE)
    19. player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
    20.  
    21. // Get the spawn egg
    22. SpawnEgg spawnEgg = (SpawnEgg) player.getItemInHand().getData();
    23.  
    24. // Spawn the entity
    25. Entity entity = player.getWorld().spawnEntity(clickedBlock.getLocation().add(0, 1, 0), spawnEgg.getSpawnedType());
    26.  
    27. // Now you have your entity you can work with...
    28. }
    29. }
    30. }
     
Thread Status:
Not open for further replies.

Share This Page