From 3fe57d5fe99f2ee790712c8337ee3884b4bad556 Mon Sep 17 00:00:00 2001
From: lars <larslukasneuhaus@gmx.de>
Date: Sat, 23 Nov 2024 22:24:08 +0100
Subject: [PATCH] continued fixing pr comments

---
 .../game/stateless/StatelessGame.java         |  2 +-
 .../game/stateless/types/tetris/Tetris.java   | 20 +++---
 .../types/tetris/game/TetrisGame.java         | 69 ++++++-------------
 .../types/tetris/game/Tetromino.java          | 25 +++++--
 4 files changed, 49 insertions(+), 67 deletions(-)

diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/StatelessGame.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/StatelessGame.java
index cef6668..465e306 100644
--- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/StatelessGame.java
+++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/StatelessGame.java
@@ -50,7 +50,7 @@ public class StatelessGame extends Game {
 
                 int timeLeft = timeLimit - timePlayed;
                 switch (timeLeft) {
-                    case 60, 30, 10, 5, 4, 3, 2, 1 ->
+                    case 90, 60, 30, 10, 5, 4, 3, 2, 1 ->
                         new ChatMessage(Icon.SCIENCE).appendStatic("Noch " + timeLeft + " Sekunden!").send(getPlayers());
                 }
 
diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java
index a5d506b..d08e009 100644
--- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java
+++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/Tetris.java
@@ -4,8 +4,6 @@ import eu.mhsl.minenet.minigames.instance.game.stateless.StatelessGame;
 import eu.mhsl.minenet.minigames.instance.game.stateless.types.tetris.game.TetrisGame;
 import eu.mhsl.minenet.minigames.instance.Dimension;
 import eu.mhsl.minenet.minigames.instance.game.stateless.types.tetris.game.Tetromino;
-import eu.mhsl.minenet.minigames.message.Icon;
-import eu.mhsl.minenet.minigames.message.type.ChatMessage;
 import eu.mhsl.minenet.minigames.score.PointsWinScore;
 import net.kyori.adventure.text.Component;
 import net.minestom.server.coordinate.Pos;
@@ -18,7 +16,6 @@ import net.minestom.server.item.ItemStack;
 import net.minestom.server.item.Material;
 import org.jetbrains.annotations.NotNull;
 
-import java.util.List;
 import java.util.Map;
 import java.util.Random;
 import java.util.WeakHashMap;
@@ -54,7 +51,7 @@ class Tetris extends StatelessGame {
             .forEach(Entity::remove);
 
         if(this.hasCombat) {
-            this.tetrisGames.values().forEach(tetrisGame -> tetrisGame.updateOtherTetrisGames((List<TetrisGame>) this.tetrisGames.values()));
+            this.tetrisGames.values().forEach(tetrisGame -> tetrisGame.updateOtherTetrisGames(this.tetrisGames.values()));
         }
 
         this.tetrisGames.forEach((player, tetrisGame) -> tetrisGame.start());
@@ -146,13 +143,12 @@ class Tetris extends StatelessGame {
         getScore().insertResult(player, tetrisGame.getScore());
 
         boolean allGamesLost = this.tetrisGames.values().stream()
-                .filter(tetrisGame1 -> !tetrisGame1.lost)
+                .filter(game -> !game.lost)
                 .toList()
                 .isEmpty();
         if(!setTimeLimit && !allGamesLost) {
             this.setTimeLimit(90);
             setTimeLimit = true;
-            new ChatMessage(Icon.SCIENCE).appendStatic("Noch 90 Sekunden!").send(getPlayers());
         }
     }
 
@@ -161,8 +157,8 @@ class Tetris extends StatelessGame {
         p.getInventory().setItemStack(0, ItemStack.builder(Material.BIRCH_BUTTON).customName(Component.text("Controller")).build());
         p.setSprinting(false);
 
-        if(this.tetrisGames.get(p) == null) {
-            this.tetrisGames.put(p, new TetrisGame(
+        this.tetrisGames.computeIfAbsent(p, player -> {
+            TetrisGame newTetrisGame = new TetrisGame(
                 this,
                 getSpawn().sub(6, 8, 15).add(this.tetrisGames.size()*30, 0, 0),
                 Tetromino.Shape.J,
@@ -170,10 +166,12 @@ class Tetris extends StatelessGame {
                 this.isFast,
                 this.hasCombat,
                 this.randomSeed
-            ));
+            );
             this.tetrisGames.get(p).generate();
-            this.tetrisGames.forEach((player, tetrisGame) -> tetrisGame.updateOtherTetrisGames(this.tetrisGames.values().stream().toList()));
-        }
+            this.tetrisGames.values().forEach(tetrisGame -> tetrisGame.updateOtherTetrisGames(this.tetrisGames.values()));
+            return newTetrisGame;
+        });
+
         TetrisGame tetrisGame = this.tetrisGames.get(p);
 
         p.teleport(tetrisGame.getPlayerSpawnPosition());
diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java
index 5057f0b..20d2902 100644
--- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java
+++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/TetrisGame.java
@@ -45,10 +45,6 @@ public class TetrisGame {
         space
     }
 
-    public TetrisGame(StatelessGame instance, Pos lowerLeftCorner, long randomSeed) {
-        this(instance, lowerLeftCorner, Tetromino.Shape.J, 3, false, false, randomSeed);
-    }
-
     public TetrisGame(StatelessGame instance, Pos lowerLeftCorner, Tetromino.Shape startTetrominoShape, int nextTetrominoesCount, boolean isfast, boolean hasCombat, long randomSeed) {
         this.isFast = isfast;
         this.hasCombat = hasCombat;
@@ -68,19 +64,24 @@ public class TetrisGame {
     }
 
     public void pressedButton(Button button) {
-        if(this.lastPresses.getOrDefault(button, 0L) >= System.currentTimeMillis()-100) return;
+        final int standardButtonDelay = 100;
+        final int buttonDebounce = 70;
+
+        if(this.lastPresses.getOrDefault(button, 0L) >= System.currentTimeMillis()-standardButtonDelay) return;
 
         this.lastPresses.put(button, System.currentTimeMillis());
-        if(button == Button.W) this.lastPresses.put(button, System.currentTimeMillis()+70);
-        if(button == Button.S) this.lastPresses.put(button, System.currentTimeMillis()-70);
+        if(button == Button.W) this.lastPresses.put(button, System.currentTimeMillis()+buttonDebounce);
+        if(button == Button.S) this.lastPresses.put(button, System.currentTimeMillis()-buttonDebounce);
+
+        if(this.lost || this.paused) return;
 
         switch (button) {
-            case A -> this.moveLeft();
+            case A -> this.currentTetromino.moveLeft();
             case S -> this.moveDown();
-            case D -> this.moveRight();
+            case D -> this.currentTetromino.moveRight();
             case W -> this.hardDrop();
-            case mouseLeft -> this.rotate(false);
-            case mouseRight -> this.rotate(true);
+            case mouseLeft -> this.currentTetromino.rotate(false);
+            case mouseRight -> this.currentTetromino.rotate(true);
             case space -> this.switchHold();
         }
     }
@@ -96,18 +97,16 @@ public class TetrisGame {
             if(this.lost) return TaskSchedule.stop();
             int standardTickDelay = 40;
             if(this.isFast) standardTickDelay = 20;
-            if(this.paused) return TaskSchedule.tick(Math.round((float) standardTickDelay /this.level));
+
+            TaskSchedule nextTick = TaskSchedule.tick(Math.round((float) standardTickDelay /this.level));
+            if(this.paused) return nextTick;
             this.tick();
-            return TaskSchedule.tick(Math.round((float) standardTickDelay /this.level));
+            return nextTick;
         });
 
         this.updateInfo();
         this.nextTetrominoes.forEach(tetromino -> {
-            double xChange;
-            switch (tetromino.shape) {
-                case O, I -> xChange = 0;
-                case null, default -> xChange = -0.5;
-            }
+            double xChange = -tetromino.getXChange();
             tetromino.setPosition(this.nextPosition.sub(xChange, 4*this.nextTetrominoes.indexOf(tetromino), 0));
             tetromino.drawAsEntities();
         });
@@ -131,7 +130,7 @@ public class TetrisGame {
         return this.score;
     }
 
-    public void updateOtherTetrisGames(List<TetrisGame> tetrisGames) {
+    public void updateOtherTetrisGames(Collection<TetrisGame> tetrisGames) {
         List<TetrisGame> games = new ArrayList<>(tetrisGames);
         games.remove(this);
         games.removeIf(tetrisGame -> tetrisGame.lost);
@@ -151,23 +150,7 @@ public class TetrisGame {
     }
 
 
-    private boolean rotate(boolean clockwise) {
-        if(this.lost || this.paused) return false;
-        return this.currentTetromino.rotate(clockwise);
-    }
-
-    private boolean moveLeft() {
-        if(this.lost || this.paused) return false;
-        return this.currentTetromino.moveLeft();
-    }
-
-    private boolean moveRight() {
-        if(this.lost || this.paused) return false;
-        return this.currentTetromino.moveRight();
-    }
-
     private boolean moveDown() {
-        if(this.lost || this.paused) return false;
         if(!this.currentTetromino.moveDown()) {
             this.setActiveTetrominoDown();
             return false;
@@ -178,7 +161,6 @@ public class TetrisGame {
     }
 
     private boolean hardDrop() {
-        if(this.lost || this.paused) return false;
         if(!this.currentTetromino.moveDown()) {
             this.setActiveTetrominoDown();
             return false;
@@ -195,7 +177,6 @@ public class TetrisGame {
 
     private boolean switchHold() {
         if(!holdPossible) return false;
-        if(this.lost || this.paused) return false;
 
         Tetromino newCurrentTetromino;
         if(this.holdTetromino == null) {
@@ -208,18 +189,14 @@ public class TetrisGame {
         }
 
         this.currentTetromino.remove();
-        this.holdTetromino = new Tetromino(this.instance, this.currentTetromino.shape);
+        this.holdTetromino = new Tetromino(this.instance, this.currentTetromino.getShape());
         this.currentTetromino = newCurrentTetromino;
 
         this.currentTetromino.setPosition(this.tetrominoSpawnPosition);
         this.currentTetromino.draw();
         if(!this.currentTetromino.moveDown()) loose();
 
-        double xChange;
-        switch (this.holdTetromino.shape) {
-            case O, I -> xChange = 0;
-            case null, default -> xChange = 0.5;
-        }
+        double xChange = this.holdTetromino.getXChange();
         this.holdTetromino.setPosition(this.holdPosition.add(xChange, 0, 0));
         this.holdTetromino.drawAsEntities();
         this.holdPossible = false;
@@ -239,11 +216,7 @@ public class TetrisGame {
         Tetromino newTetromino = this.tetrominoBag.removeFirst();
         this.nextTetrominoes.add(newTetromino);
         this.nextTetrominoes.forEach(tetromino -> {
-            double xChange;
-            switch (tetromino.shape) {
-                case O, I -> xChange = 0;
-                case null, default -> xChange = -0.5;
-            }
+            double xChange = -tetromino.getXChange();
             tetromino.setPosition(this.nextPosition.sub(xChange, 4*this.nextTetrominoes.indexOf(tetromino), 0));
             tetromino.drawAsEntities();
         });
diff --git a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java
index 5a08ff8..f673961 100644
--- a/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java
+++ b/src/main/java/eu/mhsl/minenet/minigames/instance/game/stateless/types/tetris/game/Tetromino.java
@@ -14,7 +14,7 @@ import java.util.List;
 import java.util.UUID;
 
 public class Tetromino {
-    public final Shape shape;
+    private final Shape shape;
     private final StatelessGame instance;
     private Pos position;
     private int[][] shapeArray;
@@ -90,12 +90,10 @@ public class Tetromino {
             getBlockPositions().forEach(pos -> {
                 Entity ghostBlock = new Entity(ghostEntityType);
                 ((FallingBlockMeta) ghostBlock.getEntityMeta()).setBlock(this.getGhostBlock());
-//                ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock());
                 ghostBlock.setNoGravity(true);
                 ghostBlock.setGlowing(true);
                 ghostBlock.setTag(uuidTag, this.uuid.toString());
                 ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(0.5, 0, 0.5));
-//                ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(1, 0, 1));
             });
         }
 
@@ -106,11 +104,9 @@ public class Tetromino {
         getBlockPositions().forEach(pos -> {
             Entity ghostBlock = new Entity(ghostEntityType);
             ((FallingBlockMeta) ghostBlock.getEntityMeta()).setBlock(this.getColoredBlock());
-//                ((BlockDisplayMeta) ghostBlock.getEntityMeta()).setBlockState(this.getGhostBlock());
             ghostBlock.setNoGravity(true);
             ghostBlock.setTag(uuidTag, this.uuid.toString());
             ghostBlock.setInstance(this.instance, pos.add(0.5, 0, 0.5));
-//                ghostBlock.setInstance(this.instance, pos.sub(positionChange).add(1, 0, 1));
         });
     }
 
@@ -138,12 +134,27 @@ public class Tetromino {
         this.instance.getEntities().stream()
             .filter(entity -> {
                 String tagValue = entity.getTag(uuidTag);
-                if(tagValue != null) return entity.getTag(uuidTag).equals(this.uuid.toString());
-                return false;
+                if(tagValue == null) return false;
+                return entity.getTag(uuidTag).equals(this.uuid.toString());
             })
             .forEach(Entity::remove);
     }
 
+    public double getXChange() {
+        switch (this.shape) {
+            case O, I -> {
+                return 0;
+            }
+            case null, default -> {
+                return 0.5;
+            }
+        }
+    }
+
+
+    public Shape getShape() {
+        return this.shape;
+    }
 
     private Block getGhostBlock() {
         Block returnBlock;