@@ -173,7 +173,6 @@ private void teleportAndSetGameMode(Player peeker, Player target) {
173173 peeker .setSpectatorTarget (null );
174174 }
175175
176- // 先切换游戏模式
177176 peeker .setGameMode (GameMode .SPECTATOR );
178177
179178 // 切换完成后再传送
@@ -195,57 +194,64 @@ private void teleportAndSetGameMode(Player peeker, Player target) {
195194
196195 public void restorePlayerState (Player peeker , PeekData data ) {
197196 plugin .getServer ().getRegionScheduler ().run (plugin , data .getOriginalLocation (), task -> {
197+
198198 // 如果玩家在附身状态,先退出附身
199199 if (peeker .getGameMode () == GameMode .SPECTATOR && peeker .getSpectatorTarget () != null ) {
200200 peeker .setSpectatorTarget (null );
201201 }
202202
203- // 先改变游戏模式
204- peeker .setGameMode (data .getOriginalGameMode ());
205-
206- // 恢复状态
207- peeker .setHealth (Math .min (data .getHealth (), 20 ));
208- peeker .setFoodLevel (data .getFoodLevel ());
209- peeker .setSaturation (data .getSaturation ());
210-
211- // 清除现有效果并应用保存的效果
212- peeker .getActivePotionEffects ().forEach (effect
213- -> peeker .removePotionEffect (effect .getType ()));
214- data .getPotionEffects ().forEach (effect
215- -> peeker .addPotionEffect (effect ));
216-
217203 // 在传送前先清除动量
218204 peeker .setVelocity (new Vector (0 , 0 , 0 ));
219205
220- // 延迟1tick后传送
221- plugin .getServer ().getRegionScheduler ().runDelayed (plugin , data .getOriginalLocation (), delayedTask -> {
222- peeker .teleportAsync (data .getOriginalLocation ()).thenAccept (success -> {
223- if (!success ) {
224- plugin .getLogger ().warning (String .format (
225- "无法将玩家 %s 传送回原位置,正在尝试传送到重生点" ,
226- peeker .getName ()
227- ));
228-
229- Location spawnLoc = peeker .getBedSpawnLocation () != null
230- ? peeker .getBedSpawnLocation ()
231- : peeker .getWorld ().getSpawnLocation ();
232-
233- if (spawnLoc != null ) {
234- plugin .getServer ().getRegionScheduler ().run (plugin , spawnLoc , spawnTask -> {
235- peeker .teleportAsync (spawnLoc ).thenAccept (spawnSuccess -> {
236- if (!spawnSuccess ) {
237- plugin .getLogger ().severe (String .format (
238- "无法将玩家 %s 传送到任何安全位置" ,
239- peeker .getName ()
240- ));
241- }
242- });
206+ peeker .teleportAsync (data .getOriginalLocation ()).thenAccept (success -> {
207+ if (success ) {
208+ // 传送成功后再改变游戏模式
209+ plugin .getServer ().getRegionScheduler ().run (plugin , data .getOriginalLocation (), modeTask -> {
210+ // 再次确保动量为0
211+ peeker .setVelocity (new Vector (0 , 0 , 0 ));
212+
213+ peeker .setGameMode (data .getOriginalGameMode ());
214+ // 恢复新增状态
215+ peeker .setHealth (Math .min (data .getHealth (), 20 ));
216+ peeker .setFoodLevel (data .getFoodLevel ());
217+ peeker .setSaturation (data .getSaturation ());
218+
219+ // 清除现有效果并应用保存的效果
220+ peeker .getActivePotionEffects ().forEach (effect
221+ -> peeker .removePotionEffect (effect .getType ()));
222+ data .getPotionEffects ().forEach (effect
223+ -> peeker .addPotionEffect (effect ));
224+ });
225+ } else {
226+ plugin .getLogger ().warning (String .format (
227+ "无法将玩家 %s 传送回原位置,正在尝试传送到重生点" ,
228+ peeker .getName ()
229+ ));
230+
231+ Location spawnLoc = peeker .getBedSpawnLocation () != null
232+ ? peeker .getBedSpawnLocation ()
233+ : peeker .getWorld ().getSpawnLocation ();
234+
235+ if (spawnLoc != null ) {
236+ plugin .getServer ().getRegionScheduler ().run (plugin , spawnLoc , spawnTask -> {
237+ peeker .teleportAsync (spawnLoc ).thenAccept (spawnSuccess -> {
238+ if (spawnSuccess ) {
239+ // 传送到重生点成功后再改变游戏模式
240+ plugin .getServer ().getRegionScheduler ().run (plugin , spawnLoc , modeTask -> {
241+ peeker .setGameMode (data .getOriginalGameMode ());
242+ });
243+ } else {
244+ plugin .getLogger ().severe (String .format (
245+ "无法将玩家 %s 传送到任何安全位置" ,
246+ peeker .getName ()
247+ ));
248+ }
243249 });
244- }
245- plugin .getMessages ().send (peeker , "teleport-failed" );
250+ });
246251 }
247- });
248- }, 5L );
252+ plugin .getMessages ().send (peeker , "teleport-failed" );
253+ }
254+ });
249255 });
250256 }
251257
0 commit comments