Bläddra i källkod

stop player tasks if game is restarted midgame

Daniel Nägele 1 år sedan
förälder
incheckning
f8776aa63b

+ 17 - 12
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java

@@ -307,7 +307,7 @@ public class Game {
         Bukkit.getPluginManager().callEvent(new GameStopEvent(this));
     }
 
-    public void reset() {
+    public void triggerRestart() {
         if (Config.isSetup()) return;
 
         if (restart) {
@@ -413,10 +413,8 @@ public class Game {
         Team team = mwPlayer.getTeam();
         boolean playerWasTeamMember = false;
 
-        if (state == GameState.INGAME) {
-            BukkitTask task = playerTasks.get(mwPlayer.getUuid());
-            if (task != null) task.cancel();
-        }
+        BukkitTask task = playerTasks.get(mwPlayer.getUuid());
+        if (task != null) task.cancel();
 
         PlayerDataProvider.getInstance().loadInventory(player);
 
@@ -488,6 +486,12 @@ public class Game {
         HandlerList.unregisterAll(listener);
         taskManager.stopTimer();
 
+        // Just in case this wasn't executed in stopGame() already
+        // This can happen if a game restart gets issued while it's still active
+        for (BukkitTask bt : playerTasks.values()) {
+            bt.cancel();
+        }
+
         if (gameWorld != null) {
             gameWorld.unload();
             gameWorld.delete();
@@ -661,7 +665,7 @@ public class Game {
         itemStack.setAmount(amount - 1);
 
         Fireball fb = player.launchProjectile(Fireball.class);
-        fb.setVelocity(player.getLocation().getDirection().multiply(2.5D));
+        fb.setDirection(player.getLocation().getDirection().multiply(2.5D));
         player.playSound(fb.getLocation(), Sound.BLOCK_ANVIL_LAND, 100.0F, 2.0F);
         player.playSound(fb.getLocation(), Sound.ITEM_FLINTANDSTEEL_USE, 100.0F, 1.0F);
         fb.setYield(3F);
@@ -823,20 +827,21 @@ public class Game {
     }
 
     /**
-     * This method checks whether a team switch would be fair based on 
-     * the new team size. If no empty team results or if the team size 
-     * difference does not exceed a certain value, the switch is 
+     * This method checks whether a team switch would be fair based on
+     * the new team size. If no empty team results or if the team size
+     * difference does not exceed a certain value, the switch is
      * considered acceptable.
-     * 
+     *
      * @param targetTeam the new team
+     *
      * @return (boolean) 'true' if it's a fair team switch
      */
     public boolean isValidTeamSwitch(Team targetTeam) {
-        
+
         // original team sizes
         int targetTeamSize = targetTeam.getMembers().size();
         int currentTeamSize = targetTeam.getEnemyTeam().getMembers().size();
-        
+
         // Preventing an empty team when previously both teams had at least one player:
         if ((currentTeamSize == 1) && (targetTeamSize >= 1)) return false;
 

+ 5 - 8
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java

@@ -24,14 +24,13 @@ import de.butzlabben.missilewars.configuration.Config;
 import de.butzlabben.missilewars.configuration.Lobby;
 import de.butzlabben.missilewars.util.geometry.GameArea;
 import de.butzlabben.missilewars.util.serialization.Serializer;
-import lombok.Getter;
-import org.bukkit.Bukkit;
-import org.bukkit.Location;
-
 import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import lombok.Getter;
+import org.bukkit.Bukkit;
+import org.bukkit.Location;
 
 @Getter
 public class GameManager {
@@ -47,10 +46,8 @@ public class GameManager {
     }
 
     public void restartAll() {
-        var iterator = games.values().iterator();
-        //noinspection WhileLoopReplaceableByForEach
-        while (iterator.hasNext()) {
-            restartGame(iterator.next().getLobby(), false);
+        for (Game game : games.values()) {
+            restartGame(game.getLobby(), false);
         }
     }
 

+ 1 - 1
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/timer/EndTimer.java

@@ -40,7 +40,7 @@ public class EndTimer extends Timer {
                 broadcast(Messages.getMessage(true, Messages.MessageEnum.ENDGAME_TIMER_GAME_STARTS_NEW_IN).replace("%seconds%", Integer.toString(seconds)));
                 break;
             case 0:
-                getGame().reset();
+                getGame().triggerRestart();
                 break;
             default:
                 break;