在SQL中保存播放器数据

时间:2015-09-27 15:46:44

标签: java sql json

好的,这是将我的播放器数据保存到SQL并加载它的代码,这是我尝试使用的新方法,除了加载库存外,它还能正常工作。

public static int loadPlayer(Player player) throws Exception {
    int returnCode = 1;
    ByteArrayInputStream bais;
    ObjectInputStream ins;

    PreparedStatement ps = MySQL.getConnection().prepareStatement("SELECT * FROM players WHERE playerName = ? LIMIT 1");
    ps.setString(1, player.getUsername());
    ResultSet results = ps.executeQuery();

    if (results.next()) {
        if (!results.getString("password").equalsIgnoreCase(/*Misc.stringToMD5(*/player.getPassword()/*)*/)) {
            returnCode = 2;
        } else {
            bais = new ByteArrayInputStream(results.getBytes("playerData"));
            ins = new ObjectInputStream(bais);

            PlayerData data = (PlayerData) ins.readObject();
            data.convertPlayerData(player);

            ins.close();
            bais.close();
            returnCode = 0;
        }
    }
    ps.close();
    return returnCode;
}

public static void savePlayer(Player player) throws Exception {
    PreparedStatement ps = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);

    oos.writeObject(new PlayerData(player).getObject());
    oos.flush();
    oos.close();
    bos.close();
    if (!userExists(player)) {
        ps = MySQL.getConnection().prepareStatement("INSERT INTO players (playerName, password, playerData) VALUES(?, ?, ?)");
        ps.setString(1, player.getUsername());
        ps.setString(2, /*Misc.stringToMD5(*/player.getPassword()/*)*/);
        ps.setBytes(3, bos.toByteArray());
    } else {
        ps = MySQL.getConnection().prepareStatement("UPDATE players SET playerData = ? WHERE playerName = ?");
        ps.setObject(1, bos.toByteArray());
        ps.setString(2, player.getUsername());
    }
    ps.executeUpdate();
    ps.close();
}

public static class PlayerData implements Serializable {

    private static final long serialVersionUID = 7156373174557675162L;
    private int x;
    private int y;
    private int z;
    private byte rights;

    private Item[] inventory;

    /**
     * Feels the local data with the players details
     * 
     * @param player
     *            the player
     */
    public PlayerData(Player player) {
        x = player.getLocation().getX();
        y = player.getLocation().getY();
        z = player.getLocation().getZ();
        rights = ((byte) player.getRights());

        inventory = player.getInventory().getItems();
    }

    /**
     * Convert the data to our player
     * 
     * @param player
     *            convert out local data to the player
     */
    public void convertPlayerData(Player player) {
        player.getLocation().setAs(new Location(x, y, z));
        player.setRights(rights);

        if (inventory != null) {
            for (int i = 0; i < inventory.length; i++) {
                player.getInventory().getItems()[i] = ItemCheck.check(player, inventory[i]);
            }
        }

    }

    /**
     * Return the player details as object
     * 
     * @return the object
     */
    public Object getObject() {
        return this;
    }

}

这是旧系统,json工作正常并加载库存。

public static final class PlayerContainer {
    private final Item[] inventory;

    public PlayerContainer(Player player) {
        inventory = player.getInventory().getItems();
    }

    public void parseDetails(Player player) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter("./data/characters/containers/" + player.getUsername() + ".json", false));
        try {
            writer.write(PlayerSave2.GSON.toJson(this));
            writer.flush();
        } finally {
            writer.close();
        }
    }

    public static boolean loadDetails(Player player) throws Exception {
        File file = new File("./data/characters/containers/" + player.getUsername() + ".json");

        if (!file.exists()) {
            return false;
        }

        BufferedReader reader = new BufferedReader(new FileReader(file));
        try {
            PlayerContainer details = PlayerSave2.GSON.fromJson(reader, PlayerContainer.class);

            if (details.inventory != null) {
                for (int i = 0; i < details.inventory.length; i++) {
                    player.getInventory().getItems()[i] = ItemCheck.check(player, details.inventory[i]);
                    System.out.println("Inv: "+details.inventory[i]);
                }
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }

        return true;
    }
}

这是我在SQL中获得的BLOB输出:

'[Lorg.saranthium.game.entity.item.Item;´×o<s°9x

JSON输出

inventory":[{"id":4151,"amount":1},null,null ...

那为什么这不能在SQL上正常工作?

0 个答案:

没有答案