Assigning a horse to a player?

Discussion in 'Plugin Development' started by Faith, Jul 4, 2013.

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

    Faith

    I am creating a plugin which allows a a player to 'claim' a horse which makes it so only they can mount/leash the horse. This is what I have so far:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label,
    2. String[] args) {
    3. Player p = (Player) sender;
    4.  
    5. if (cmd.getName().equalsIgnoreCase("hclaim")) {
    6. if (p.isInsideVehicle()) {
    7. if (p.getVehicle().getType() == EntityType.HORSE) {
    8. Horse horse = (Horse) p.getVehicle();
    9.  
    10. }
    11. } else {
    12. p.sendMessage(ChatColor.RED + "You must be on a horse to claim it!");
    13. }
    14.  
    15. }
    16. return false;
    17.  
    18. }


    I need some help with 'assigning the horse to the player'. Any help is appreciated.
     
  2. Offline

    DarkBladee12

    Faith You could make such a hashmap "HashMap<String, List<UUID>>", simply add the UUID of the claimed horse to the list of the player.
     
  3. Offline

    Faith

    DarkBladee12 Sorry, but I haven't previously worked with HashMaps so could you demonstrate how it would look? Appreciate it.
     
  4. Offline

    chasechocolate

    Code:java
    1. HashMap<String, List<UUID>> playerHorses = new HashMap<String, List<UUID>>();
    2.  
    3. //Assigning a horse
    4. if(!(playerHorses.containsKey(player.getName()))){
    5. playerHorses.put(player.getName(), new ArrayList<UUID>());
    6. }
    7.  
    8. playerHorses.get(player.getName()).add(horse.getUniqueId());
     
  5. Offline

    DarkBladee12

    Faith It should look like this:

    Code:java
    1. // Top of the class
    2. private HashMap<String, List<UUID>> assigned = new HashMap<String, List<UUID>>();
    3. // ....
    4. private void assignHorse(Player p, Horse h){
    5. String name = p.getName();
    6. List<UUID> list = new ArrayList<UUID>();
    7. // Check if the player has already an entry in the HashMap
    8. if(assigned.hasKey(name){
    9. list = assigned.get(name);
    10. }
    11. assigned.add(h.getUniqueId());
    12. // Update the list in the HashMap
    13. assigned.put(name, list);
    14. }
    15.  
    16. private void removeAssignment(Player p, Horse h){
    17. String name = p.getName();
    18. // Check if the player has already an entry in the HashMap
    19. if(!assigned.hasKey(name){
    20. return;
    21. }
    22. List<UUID> list = assigned.get(name);
    23. list.remove(h.getUniqueId());
    24. // Update the list in the HashMap
    25. assigned.put(name, list);
    26. }
     
Thread Status:
Not open for further replies.

Share This Page