1

I am trying to save the location of a player in minecraft to a list this works good but now how do I get my location back by searching trought the list on playerName?

Piece of code where the list class is created and the list

public static class Character {

    private String name;

    private Location location;
    public Character(String name, Location location) {
        this.name = name;
        this.location = location;
    }
}

public static class Location {
    private int x;
    private int y;
    private int z;

    public Location(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

List<Character> rawInput = new ArrayList<Character>();

piece of code where I add an item to the list :

else if (args[0].equalsIgnoreCase("select"))
{
    int tmpX = (int)player.getLocation().getX();
    int tmpY = (int)player.getLocation().getY();
    int tmpZ = (int)player.getLocation().getZ();
    rawInput.add(
        new Character( player.getName(), new Location( tmpX, tmpY, tmpZ )));
    player.sendMessage(
        ChatColor.GOLD + "[PlusCommands] " + ChatColor.GREEN
      + "selected location set to player location!");
}

This works all fine but how do I get the the data back for example :

This is a list with locations : Playername X Y Z :

PlayerThree 32 13 46

PlayerTwo 12 60 212

PlayerOne 43 62 523

So I want to search for the right player in this example case I am PlayerOne So I want to get the data from the playerList where the string says PlayerOne

In this case thats this one : PlayerOne 43 62 523

How do I do this???

I hope I am clear enough sorry for it if not.

4
  • 1
    Add a getter method in your class? Commented Jun 11, 2013 at 22:43
  • @noMAD getter method? Commented Jun 11, 2013 at 22:45
  • public static Location getLocation(String name) { //Do stuff to check if name is available //Get location specific to name return location; } Commented Jun 11, 2013 at 22:46
  • Use a map not arraylist. Commented Jun 11, 2013 at 22:54

4 Answers 4

3

In place of List<Character> rawInput = new ArrayList<Character>(); use a Map<String,Character> rawInput = new LinkedHashMap<>();

To add a player:

rawInput.put( aNewCharacter.getName(), aNewCharacter );

You should check the returned value of put: if non null, the name is already used. Read the Javadoc of java.util.Map

To find a player:

Character c = rawInput.get( "PlayerOne" ); // returns PlayerOne 43 62 523
Sign up to request clarification or add additional context in comments.

3 Comments

Yap that's it. Maps are one of the most frequent data structures used in java. Way more often than arraylist. But be careful when using custom objects as keys and not implementing equals() and hashcode() on those custom objects. Read further about hashmaps to learn why.
What is aNewCharacter? where is the location put?
@StefanoL "Maps are one of the most frequent data structures used in java. Way more often than arraylist." I don't agree with that.
1

You need to add getters to these classes like so:

package com.sandbox;

public class Sandbox {

    public static void main(String[] args) {
        Character player = new Character("Foo", new Location(1, 2, 3));

        int tmpX = player.getLocation().getX();
        int tmpY = player.getLocation().getY();
        int tmpZ = player.getLocation().getZ();
    }

    public static class Character {

        private String name;
        private Location location;

        public Character(String name, Location location) {
            this.name = name;
            this.location = location;
        }

        public String getName() {
            return name;
        }


        public Location getLocation() {
            return location;
        }


    }

    public static class Location {
        private int x;
        private int y;
        private int z;

        public Location(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public int getZ() {
            return z;
        }
    }

}

Take note of my main. It shows that you don't need to cast to (int).

2 Comments

where do I have to call getName or getY etc,
I think I have to go sleep I am getting brain damage :P I try to learn but I don't understand I'm sorry of wasting your time.
0
static Map<String,Location> nameAndLocation = new HashMap<String, Location>();    

public Character(String name, Location location) {
            this.name = name;
            this.location = location;
            addToMap(this.name, this.location);
        }

public static void addToMap(String name, Location location) {
  nameAndLocation.put(name,location);
}

public static Location getLocation(String name) {
  return nameAndLocation.get(name);
}

Comments

0

This is some pretty messy code.

You shouldn't be creating your own Location or Player class, as Bukkit already includes one of each. Instead of making your own, use org.bukkit.util.Location + org.bukkit.entity.Player and you shouldn't have a problem!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.