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 XYZ :
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.
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
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)
.
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);
}
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.