added sidebar

This commit is contained in:
2025-10-04 16:11:10 +02:00
parent 2a6f2f2a44
commit 2fac287e1e
2 changed files with 28 additions and 3 deletions

View File

@@ -4,6 +4,8 @@ import eu.mhsl.minenet.minigames.instance.Dimension;
import eu.mhsl.minenet.minigames.instance.game.stateless.StatelessGame; import eu.mhsl.minenet.minigames.instance.game.stateless.StatelessGame;
import eu.mhsl.minenet.minigames.score.PointsWinScore; import eu.mhsl.minenet.minigames.score.PointsWinScore;
import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Pos; import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec; import net.minestom.server.coordinate.Vec;
@@ -16,6 +18,7 @@ import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.network.packet.server.play.ParticlePacket; import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle; import net.minestom.server.particle.Particle;
import net.minestom.server.scoreboard.Sidebar;
import net.minestom.server.sound.SoundEvent; import net.minestom.server.sound.SoundEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -23,12 +26,11 @@ import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
// TODO: Info für Score
class TurtleGame extends StatelessGame { class TurtleGame extends StatelessGame {
private final int radius; private final int radius;
private final Map<Player, EntityCreature> turtlePlayerMap = new WeakHashMap<>(); private final Map<Player, EntityCreature> turtlePlayerMap = new WeakHashMap<>();
private final Map<Player, Integer> scoreMap = new WeakHashMap<>(); private final Map<Player, Integer> scoreMap = new WeakHashMap<>();
private final Map<Player, Sidebar> sidebarMap = new HashMap<>();
private final ArrayList<Entity> snacks = new ArrayList<>(); private final ArrayList<Entity> snacks = new ArrayList<>();
private final ArrayList<Entity> bombs = new ArrayList<>(); private final ArrayList<Entity> bombs = new ArrayList<>();
private final Block snackBlock = Block.SUNFLOWER.withProperty("half", "upper"); private final Block snackBlock = Block.SUNFLOWER.withProperty("half", "upper");
@@ -91,6 +93,7 @@ class TurtleGame extends StatelessGame {
this.explode(p, bomb); this.explode(p, bomb);
this.generateNewBomb(2); this.generateNewBomb(2);
this.speed += this.maxSpeedIncrease / this.getPlayers().size(); this.speed += this.maxSpeedIncrease / this.getPlayers().size();
this.sidebarMap.values().forEach(sidebar -> sidebar.updateLineScore("1", (int) this.speed));
}); });
} }
} }
@@ -103,6 +106,7 @@ class TurtleGame extends StatelessGame {
this.snacks.remove(snack); this.snacks.remove(snack);
snack.remove(); snack.remove();
this.scoreMap.put(p, this.scoreMap.get(p) + 1); this.scoreMap.put(p, this.scoreMap.get(p) + 1);
this.sidebarMap.get(p).updateLineScore("0", this.scoreMap.get(p));
} }
protected void explode(Player p, Entity bomb) { protected void explode(Player p, Entity bomb) {
@@ -118,6 +122,7 @@ class TurtleGame extends StatelessGame {
this.getScore().insertResult(p, this.scoreMap.get(p)); this.getScore().insertResult(p, this.scoreMap.get(p));
this.bombs.remove(bomb); this.bombs.remove(bomb);
bomb.remove(); bomb.remove();
this.sidebarMap.values().forEach(sidebar -> sidebar.updateLineScore("2", (int) this.turtlePlayerMap.keySet().stream().filter(player -> !player.isFlying()).count()));
} }
@Override @Override
@@ -127,6 +132,25 @@ class TurtleGame extends StatelessGame {
this.turtlePlayerMap.put(p, turtle); this.turtlePlayerMap.put(p, turtle);
} }
this.scoreMap.putIfAbsent(p, 0); this.scoreMap.putIfAbsent(p, 0);
this.sidebarMap.putIfAbsent(p, new Sidebar(Component.text("Info:").color(NamedTextColor.GOLD)));
Sidebar sidebar = this.sidebarMap.get(p);
sidebar.createLine(new Sidebar.ScoreboardLine(
"0",
Component.text("Score: ").color(NamedTextColor.DARK_GREEN),
this.scoreMap.get(p)
));
sidebar.createLine(new Sidebar.ScoreboardLine(
"1",
Component.text("Speed: ").color(NamedTextColor.DARK_BLUE),
(int) this.speed
));
sidebar.createLine(new Sidebar.ScoreboardLine(
"2",
Component.text("Players: ").color(NamedTextColor.AQUA),
this.turtlePlayerMap.size()
));
sidebar.addViewer(p);
this.sidebarMap.values().forEach(bar -> bar.updateLineScore("2", this.turtlePlayerMap.size()));
EntityCreature turtle = this.turtlePlayerMap.get(p); EntityCreature turtle = this.turtlePlayerMap.get(p);
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> { MinecraftServer.getSchedulerManager().scheduleNextTick(() -> {
@@ -143,6 +167,7 @@ class TurtleGame extends StatelessGame {
protected void onPlayerLeave(Player p) { protected void onPlayerLeave(Player p) {
EntityCreature turtle = this.turtlePlayerMap.get(p); EntityCreature turtle = this.turtlePlayerMap.get(p);
turtle.remove(); turtle.remove();
this.sidebarMap.get(p).removeViewer(p);
} }
@Override @Override

View File

@@ -27,7 +27,7 @@ public class TurtleGameFactory implements GameFactory {
public ConfigManager configuration() { public ConfigManager configuration() {
return new ConfigManager() return new ConfigManager()
.addOption(new NumericOption("radius", Material.HEART_OF_THE_SEA, TranslatedComponent.byId("optionCommon#radius"), 10, 20, 30, 40)) .addOption(new NumericOption("radius", Material.HEART_OF_THE_SEA, TranslatedComponent.byId("optionCommon#radius"), 10, 20, 30, 40))
.addOption(new NumericOption("startSpeed", Material.LEATHER_BOOTS, TranslatedComponent.byId("game_TurtleGame#startSpeed"), 2, 4, 6)); .addOption(new NumericOption("startSpeed", Material.LEATHER_BOOTS, TranslatedComponent.byId("game_TurtleGame#startSpeed"), 2, 4, 6, 8));
} }
@Override @Override