1

我正在尝试将我的世界中玩家的位置保存到一个列表中,这很好,但现在我如何通过在 playerName 上的列表中搜索来找回我的位置?

创建列表类的代码段和列表

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>();

我在列表中添加项目的一段代码:

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!");
}

这一切正常,但我如何取回数据,例如:

这是一个包含位置的列表:玩家名 XYZ:

球员三号 32 13 46

播放器二 12 60 212

播放器一号 43 62 523

所以我想在这个例子中搜索合适的播放器 我是 PlayerOne 所以我想从 playerList 中获取数据,其中字符串为 PlayerOne

在这种情况下,就是这个:PlayerOne 43 62 523

我该怎么做呢???

如果没有,我希望我很清楚。

4

4 回答 4

3

代替List<Character> rawInput = new ArrayList<Character>();使用Map<String,Character> rawInput = new LinkedHashMap<>();

添加播放器:

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

您应该检查 put 的返回值:如果非 null,则该名称已被使用。阅读java.util.Map的 Javadoc

寻找玩家:

Character c = rawInput.get( "PlayerOne" ); // returns PlayerOne 43 62 523
于 2013-06-11T22:47:01.703 回答
1

您需要像这样向这些类添加 getter:

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;
        }
    }

}

记下我的main. 它表明您不需要强制转换为(int).

于 2013-06-11T22:48:33.967 回答
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!

于 2013-06-17T03:58:04.267 回答
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);
}
于 2013-06-11T22:53:27.803 回答