Talking With Villagers

Discussion in 'Plugin Development' started by Triangle_Aiden, Jun 10, 2014.

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

    Triangle_Aiden

    Hello,

    I am working on a plugin and as a part of it I need villagers to be able to ask questions to a player when clicked on. If the player walks away the conversation between the player and villager will be canceled.

    One simple example would be if a player clicked on a villager called "Farmer Sam", Sam would ask the player if he/she would like a job as a farmer. If a player enters yes in chat, his/her rank is set as Farmer. If no is entered, The farmer would say goodbye.

    It is important that anything the player types in chat while talking to a villager is not showed globally.

    Here is what I have so far -

    VillagerListener.class:
    Code:java
    1. package com.daawsomest.oakwoodcity.villager;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.NPC;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerInteractEntityEvent;
    9. import org.bukkit.event.player.PlayerMoveEvent;
    10.  
    11. public class VillagerListener implements Listener
    12. {
    13. @EventHandler
    14. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event)
    15. {
    16. Player player = event.getPlayer();
    17. if (event.getRightClicked() instanceof NPC)
    18. {
    19. NPC villager = (NPC) event.getRightClicked();
    20. if (villager.getCustomName() != null && villager.getCustomName().equalsIgnoreCase("Farmer Sam"))
    21. {
    22. VillagerManager.getManager().setTalking(player, true);
    23. player.sendMessage(ChatColor.AQUA + "Howdy, I'm Farmer Sam!");
    24. player.sendMessage(ChatColor.AQUA + "Would you like to become a farmer?");
    25. event.setCancelled(true);
    26. }
    27. }
    28. }
    29.  
    30. @EventHandler
    31. public void onMove(PlayerMoveEvent event)
    32. {
    33. Player player = event.getPlayer();
    34. if(VillagerManager.getManager().isTalking(player))
    35. {
    36. VillagerManager.getManager().setTalking(player, false);
    37. }
    38. }
    39. }
    40.  


    and VillagerManager.class:
    Code:java
    1. package com.daawsomest.oakwoodcity.villager;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.entity.Player;
    6.  
    7. public class VillagerManager
    8. {
    9. private static VillagerManager vm = new VillagerManager();
    10.  
    11. private ArrayList<Player> talkingPlayers = new ArrayList<Player>();
    12.  
    13. public static VillagerManager getManager()
    14. {
    15. return vm;
    16. }
    17.  
    18. public void setTalking(Player player, boolean talking)
    19. {
    20. if(talking)
    21. {
    22. talkingPlayers.add(player);
    23. } else if(!talking && talkingPlayers.contains(player))
    24. {
    25. talkingPlayers.remove(player);
    26. }
    27. }
    28.  
    29. public boolean isTalking(Player player)
    30. {
    31. return talkingPlayers.contains(player);
    32. }
    33. }
    34.  


    The problem is that I am not really sure on what to do from here. I am guessing I need a way to get what villager the player is talking to so should I use a HashMap instead of the talkingPlayers array list.

    Each villager will need ask different questions and depending on the answer something will need to happen.

    Any advice?

    Thanks!
     
  2. Offline

    GotRice

    use AsyncPlayerChatEvent to listen for a reply and setCancelled() to the event if the player replies.

    Also consider using a HashMap if you are planning on having more than one villager.
     
Thread Status:
Not open for further replies.

Share This Page