1

作成中のゲーム用にこのクラスがあり、ArrayList のメモを保存しようとしています。ログアウトすると、サイズが適切に出力されます。たとえば、5 つのメモがある場合、ログアウトすると notes.getSize() は 5 になりますが、ログインすると何もリセットされません。メモが保存されないのはなぜですか?

public class Notes implements Serializable {

    private static final long serialVersionUID = -4947870743226160329L;
    private ArrayList<Note> notes = new ArrayList<Note>(30);

    public class Note implements Serializable {

        private static final long serialVersionUID = -4589885080580317958L;

        private int color = 0;
        private String text = "";

        public Note(int color, String text) {
            this.setColor(color);
            this.setText(text);
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getText() {
            return text;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public int getColor() {
            return color;
        }
    }

    private transient Player player;

    public Notes(Player p) {
        this.player = p;
    }

    public void addNote(String text) {
        System.out.println("Note Text: "+text);
        if (text.length() > 50) {
            player.getPackets().sendGameMessage("You can only enter notes up to 50 characters!");
            return;
        }
        if (notes.size() < 30) {
            notes.add(new Note(0, text));
        } else {
            player.getPackets().sendGameMessage("You cannot add more then 30 notes!");
            return;
        }
        int NoteId = notes.size() - 1;
        player.getPackets().sendConfig(1439, NoteId);
        player.getTemporaryAttributtes().put("selectedNote", NoteId);
        refreshNotes(false);
    }

    public void addNote(String text, int color) {
        notes.add(new Note(color, text));
    }

    public void loadNotes() {
        player.getPackets().sendIComponentSettings(34, 9, 0, 30, 2621470);
        player.getPackets().sendHideIComponent(34, 3, false);
        player.getPackets().sendHideIComponent(34, 44, false);
        player.getPackets().sendIComponentText(34, 13, "Loading notes<br>Please wait...");
        player.getPackets().sendConfig(1439, -1);
        refreshNotes(true);
    }

    public void refreshNotes(boolean sendStartConfigs) {
        for (int i = 0; i < 30; i++) {
            player.getPackets().sendGlobalString(149 + i, i < notes.size() ? notes.get(i).getText() : "");
        }
        if (sendStartConfigs) {
            for (int i = 1430; i < 1450; i++)
                player.getPackets().sendConfig(i, i);
        }
        player.getPackets().sendConfig(1440, getFirstTotalColorValue());
        player.getPackets().sendConfig(1441, getSecondTotalColorValue());
    }


    public int intColorValue(int color, int noteId) {
        return (int) (Math.pow(4, noteId) * color);
    }

    public int getFirstTotalColorValue() {
        int Color = 0;
        for (int i = 0; i < 15; i++) {
            if (notes.size() > i)
                Color += intColorValue(notes.get(i).getColor(), i);
        }
        return Color;
    }

    public int getSecondTotalColorValue() {
        int color = 0;
        for (int i = 0; i < 15; i++) {
            if (notes.size() > (i + 16))
                color += intColorValue(notes.get(i + 16).getColor(), i);
        }
        return color;
    }

    public void deleteSelectedNote() {
        if ((int)player.getTemporaryAttributtes().get("selectedNote") > -1) {
            int slot = (int) player.getTemporaryAttributtes().get("selectedNote");
            notes.remove(slot);
            player.getTemporaryAttributtes().put("selectedNote", -1);
            player.getPackets().sendConfig(1439, -1);
            refreshNotes(false);
        }
    }

    public void clear() {
        notes.clear();
        refreshNotes(false);
    }

    public void editNote(String string, int index) {
        notes.get(index).setText(string);
        refreshNotes(false);
    }

    public void setColor(int color, int index) {
        notes.get(index).setColor(color);
        refreshNotes(false);
    }

    public void deleteNote(int slot) {
        notes.remove(slot);
        refreshNotes(false);
    }

    public void setNotes(ArrayList<Note> setNotes) {
        notes = setNotes;
        refreshNotes(false);
    }

}

そして、これが私が保存/読み込みを管理するクラスです

public class SerializableFilesManager {

private static final String PATH = "data/characters/";
private static final String BACKUP_PATH = "data/charactersBackup/";

public synchronized static final boolean containsPlayer(String username) {
    return new File(PATH + username + ".p").exists();
}

public synchronized static Player loadPlayer(String username) {
    try {
        return (Player) loadSerializedFile(new File(PATH + username + ".p"));
    } catch (Throwable e) {
        Logger.handle(e);
    }
    try {
        Logger.log("SerializableFilesManager", "Recovering account: "
                + username);
        return (Player) loadSerializedFile(new File(BACKUP_PATH + username
                + ".p"));
    } catch (Throwable e) {
        Logger.handle(e);
    }
    return null;
}

public static boolean createBackup(String username) {
    try {
        Utils.copyFile(new File(PATH + username + ".p"), new File(
                BACKUP_PATH + username + ".p"));
        return true;
    } catch (Throwable e) {
        Logger.handle(e);
        return false;
    }
}

public synchronized static void savePlayer(Player player) {
    try {
        storeSerializableClass(player, new File(PATH + player.getUsername()
                + ".p"));
    } catch (ConcurrentModificationException e) {
        //happens because saving and logging out same time
    } catch (Throwable e) {
        Logger.handle(e);
    }
}

public static final Object loadSerializedFile(File f) throws IOException,
        ClassNotFoundException {
    if (!f.exists())
        return null;
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
    Object object = in.readObject();
    in.close();
    return object;
}

public static final void storeSerializableClass(Serializable o, File f)
        throws IOException {

    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
    out.writeObject(o);
    out.close();
}

private SerializableFilesManager() {

}

}

4

3 に答える 3

2
  1. Player を一時的なものとしてマークしないでください。これは保存するためです。一時的なものは保存されないようにし、逆シリアル化するとプレーヤーをデフォルト値の nullにします。

  2. プレーヤー クラスをシリアライズ可能にしましたか?

  3. シリアル化されるオブジェクトグラフ全体またはなし...一時的な目的は、シリアル化中に特定のメンバーをオフにして、シリアル化のプロセスがスムーズに進むようにすることです。

  4. たとえば、ゲームで、プレーヤーの進行状況とそのセッションのプレイ時間を保持したいが、開始時間と終了時間は保持したくないとします。したがって、開始時間と終了時間は一時的なものにすることができます。

于 2012-07-13T08:57:56.030 に答える
1

ArrayList を次のように保存する必要があります。

 FileOutputStream fos = null;
    ObjectOutputStream out = null;
    try {
        fos = new FileOutputStream("filename",false);
        out = new ObjectOutputStream(fos);
        out.writeObject(notes);
        out.close();
        System.out.println("Object Persisted");
    } catch (IOException ex) {
        ex.printStackTrace();
    }

プロジェクトを開くときは、Arraylist を次のようにインポートします。

    FileInputStream fos;
    try {
            fos = new FileInputStream("filename");
             ObjectInputStream oos = new ObjectInputStream(fos);
             notes=(ArrayList) oos.readObject();
                fos.close();
            {
          catch {
        }
于 2012-07-13T08:59:14.153 に答える
1

それはあなたPlayertransient...他の例であなたのロジックを実証しようとしたからです...Playerが に設定されたときprivate、シリアライゼーションは成功し、すべてのデータがロードされました。それ以外の場合、一時的な Player 参照がnullであった場合、 のような他のシリアル化されたフィールドのみをロードしますinttransient Playerにas フィールドしかない場合classNullPointerExceptionが発生しますArrayListが、 size > 0.

于 2012-07-13T08:59:49.767 に答える