悪いやり方

メモリリーク (OutOfMemoryError) やラグ、データの不整合を引き起こす可能性があるこれらの悪いやり方は避けられるべきです。

参照の保存

以下のようなインスタンスは

絶対に プラグイン内部に保存もしくはキャッシュされるべきではありません。

これの主な理由は次のとおりです:

  • 参照により適切なガーベジコレクションを妨げることになります

  • そのインスタンスは使用時には無効になっているかもしれません

これは対応するスナップショットを利用したり、インスタンスの UUID を保存して必要なときに取得することで簡単に回避できます。

メインスレッドでの IO

設定・データファイルの読み込みや更新確認、ウェブサイトへの接続といったいくつかの IO 操作は時間がかかり、サーバーの性能に大きく影響します。このような操作にはそれ専用のスレッドもしくは組み込みスケジューラの非同期機能を利用すべきです。しかし、サーバー起動時やプラグインの初期化時にメインスレッドで必要なファイルや設定を読み込むのは全くもって問題ありません。

Sponge.asyncScheduler().submit(Task.builder().execute(this::checkForUpdates).build());

詳細については スケジューラ を参照してください。

これが正しく行われていない場合、クライアントがタイムアウトする、もしくはウォッチドッグがサーバーを強制終了する可能性さえあります。

Accessing game objects outside the main thread

Accessing game objects outside of the main thread can lead to crashes, inconsistencies and various other problems and should be avoided. If you have a lengthy operation on a different thread use the scheduler to make changes to such game objects on the main thread. If you want to use a game object in a different thread use a snapshot of the instance or a detached data container.

警告

If this is done wrong, you can get a ConcurrentModificationException with or without a server crash at best and a corrupted player/world/server at worst.