"Refactored BuildBattle game logic and chunk generator: optimized code structure, improved plot assignment, and enhanced rating system."
This commit is contained in:
1 parent
7e0c126a0d
commit
53b13b6560
2 files changed
+86
-116
No files matched your search
+74
-95
@@ -6,7 +6,7 @@ import eu.mhsl.minenet.minigames.score.PointsWinScore;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.entity.GameMode;
|
||||
import net.minestom.server.entity.Player;
|
||||
@@ -19,22 +19,23 @@ import net.minestom.server.timer.TaskSchedule;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
|
||||
public class BuildBattle extends StatelessGame {
|
||||
private static final int height = 100;
|
||||
private int spawnPosx = 0;
|
||||
private int spawnPosz = 0;
|
||||
private final int spacing = 39;
|
||||
private final int rowCount = 5;
|
||||
private Map<Player, Pos> spawnPositions = new WeakHashMap<>();
|
||||
private int seconds;
|
||||
private static final int plotHalf = BuildBattleChunkgenerator.inner / 2;
|
||||
private static final int rowCount = 5;
|
||||
private static final int ratingSeconds = 15;
|
||||
private final Map<Player, Integer> scores = new HashMap<>();
|
||||
private final Map<Player, Integer> currentVotes = new HashMap<>();
|
||||
private Player currentOwner = null;
|
||||
private boolean listenerRegistered = false;
|
||||
private static final List<Material> ratingMaterials = List.of(
|
||||
Material.RED_CONCRETE, Material.ORANGE_CONCRETE, Material.YELLOW_CONCRETE,
|
||||
Material.LIME_CONCRETE, Material.GREEN_CONCRETE
|
||||
);
|
||||
private static final List<ItemStack> ratingItems = IntStream.range(0, ratingMaterials.size())
|
||||
.mapToObj(i -> ItemStack.builder(ratingMaterials.get(i))
|
||||
.customName(Component.text((i + 1) + " Punkte"))
|
||||
.build())
|
||||
.toList();
|
||||
private static final List<String> themes = List.of(
|
||||
"Burg", "Raumschiff", "Unterwasserwelt", "Pizzeria", "Spukhaus",
|
||||
"Bauernhof", "Dschungel", "Zukunftsstadt", "Piratenschiff", "Vulkan",
|
||||
@@ -47,96 +48,84 @@ public class BuildBattle extends StatelessGame {
|
||||
"Ein Dönerladen im Weltall", "Ein Spa für Enderman", "Der Käse-Palast",
|
||||
"Mein Zimmer nach 3 Wochen ohne Aufräumen", "Die Hüpfburg des Todes"
|
||||
);
|
||||
private String theme = "";
|
||||
|
||||
private int spawnPosx = 0;
|
||||
private int spawnPosz = 0;
|
||||
private final Map<Player, Pos> spawnPositions = new WeakHashMap<>();
|
||||
private final int seconds;
|
||||
private final Map<Player, Integer> currentVotes = new HashMap<>();
|
||||
private Player currentOwner = null;
|
||||
|
||||
public BuildBattle(int seconds) {
|
||||
super(Dimension.THE_END.key, "BluidBattle", new PointsWinScore());
|
||||
super(Dimension.THE_END.key, "BuildBattle", new PointsWinScore());
|
||||
this.seconds = seconds;
|
||||
this.setGenerator(new BuildBattleChunkgenerator(height));
|
||||
this.eventNode().addListener(PlayerUseItemEvent.class, this::onRatingItemUse);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
this.theme = themes.get(this.rnd.nextInt(themes.size()));
|
||||
String theme = themes.get(this.rnd.nextInt(themes.size()));
|
||||
|
||||
Title title = Title.title(
|
||||
this.showTitle(Title.title(
|
||||
Component.text("Thema", NamedTextColor.GOLD),
|
||||
Component.text(this.theme, NamedTextColor.YELLOW)
|
||||
);
|
||||
this.getPlayers().forEach(player -> {
|
||||
player.showTitle(title);
|
||||
player.sendMessage(Component.text("Euer Thema: ", NamedTextColor.GRAY)
|
||||
.append(Component.text(this.theme, NamedTextColor.YELLOW)));
|
||||
});
|
||||
|
||||
Component.text(theme, NamedTextColor.YELLOW)
|
||||
));
|
||||
this.sendMessage(Component.text("Euer Thema: ", NamedTextColor.GRAY)
|
||||
.append(Component.text(theme, NamedTextColor.YELLOW)));
|
||||
this.getPlayers().forEach(player -> player.setGameMode(GameMode.CREATIVE));
|
||||
|
||||
MinecraftServer.getSchedulerManager()
|
||||
.buildTask(() -> {
|
||||
this.getPlayers().forEach(player -> player.sendMessage("Die Bauzeit ist beendet!"));
|
||||
this.getPlayers().forEach(p -> {
|
||||
p.setGameMode(GameMode.ADVENTURE);
|
||||
p.setAllowFlying(true);
|
||||
p.setFlying(true);
|
||||
this.scores.put(p, 0);
|
||||
});
|
||||
MinecraftServer.getSchedulerManager()
|
||||
.buildTask(() -> {
|
||||
this.rating();
|
||||
}).delay(TaskSchedule.seconds(3)).schedule();
|
||||
}).delay(TaskSchedule.seconds(this.seconds)).schedule();
|
||||
this.scheduler().scheduleTask(this::endBuildPhase, TaskSchedule.seconds(this.seconds), TaskSchedule.stop());
|
||||
}
|
||||
|
||||
private void endBuildPhase() {
|
||||
this.sendMessage(Component.text("Die Bauzeit ist beendet!"));
|
||||
this.getPlayers().forEach(p -> {
|
||||
p.setGameMode(GameMode.ADVENTURE);
|
||||
p.setAllowFlying(true);
|
||||
p.setFlying(true);
|
||||
});
|
||||
this.scheduler().scheduleTask(this::rating, TaskSchedule.seconds(3), TaskSchedule.stop());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onPlayerJoin(Player player) {
|
||||
Pos spawnPos = new Pos(this.spawnPosx * this.spacing, height, this.spawnPosz * this.spacing);
|
||||
this.spawnPositions.put(player, spawnPos.add(16, 0, 16));
|
||||
Pos plotCenter = new Pos(
|
||||
this.spawnPosx * BuildBattleChunkgenerator.period + plotHalf,
|
||||
height,
|
||||
this.spawnPosz * BuildBattleChunkgenerator.period + plotHalf
|
||||
);
|
||||
this.spawnPositions.put(player, plotCenter);
|
||||
|
||||
if(this.spawnPosx >= this.rowCount) {
|
||||
if(this.spawnPosx >= rowCount) {
|
||||
this.spawnPosx = 0;
|
||||
this.spawnPosz++;
|
||||
}
|
||||
this.spawnPosx++;
|
||||
player.setGameMode(GameMode.ADVENTURE);
|
||||
MinecraftServer.getSchedulerManager().scheduleNextTick(() ->
|
||||
player.teleport(spawnPos.add(16, 1.5, 16)));
|
||||
this.scheduler().scheduleNextTick(() -> player.teleport(plotCenter.add(0, 1.5, 0)));
|
||||
return super.onPlayerJoin(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBlockBreak(@NotNull PlayerBlockBreakEvent event) {
|
||||
var player = event.getPlayer();
|
||||
Pos blockPos = event.getBlockPosition().asPos();
|
||||
event.setCancelled(!this.isInArea(blockPos, player));
|
||||
event.setCancelled(!this.isInArea(event.getBlockPosition(), event.getPlayer()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBlockPlace(@NotNull PlayerBlockPlaceEvent event) {
|
||||
var player = event.getPlayer();
|
||||
Pos blockPos = event.getBlockPosition().asPos();
|
||||
event.setCancelled(!this.isInArea(blockPos, player));
|
||||
event.setCancelled(!this.isInArea(event.getBlockPosition(), event.getPlayer()));
|
||||
}
|
||||
|
||||
private void rating() {
|
||||
List<Player> owners = new ArrayList<>(this.getPlayers());
|
||||
this.registerRatingListener();
|
||||
Queue<Player> owners = new ArrayDeque<>(this.getPlayers());
|
||||
|
||||
AtomicInteger index = new AtomicInteger(0);
|
||||
MinecraftServer.getSchedulerManager().submitTask(() -> {
|
||||
this.scheduler().submitTask(() -> {
|
||||
if (this.currentOwner != null) this.finishRound();
|
||||
|
||||
Player next = null;
|
||||
while (index.get() < owners.size()) {
|
||||
Player candidate = owners.get(index.getAndIncrement());
|
||||
if (candidate.isOnline() && this.spawnPositions.containsKey(candidate)) {
|
||||
next = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (next == null) {
|
||||
return TaskSchedule.stop();
|
||||
}
|
||||
Player next = owners.poll();
|
||||
while (next != null && !(next.isOnline() && this.spawnPositions.containsKey(next))) next = owners.poll();
|
||||
if (next == null) return TaskSchedule.stop();
|
||||
|
||||
this.startRound(next);
|
||||
return TaskSchedule.seconds(ratingSeconds);
|
||||
@@ -148,42 +137,30 @@ public class BuildBattle extends StatelessGame {
|
||||
this.currentVotes.clear();
|
||||
|
||||
Pos plot = this.spawnPositions.get(owner).add(0, 1.5, 0);
|
||||
this.sendMessage(Component.text("Bewerte das Grundstück von " + owner.getUsername() + "!"));
|
||||
this.getPlayers().forEach(p -> {
|
||||
p.teleport(plot);
|
||||
p.sendMessage(Component.text("Bewerte das Grundstück von " + owner.getUsername() + "!"));
|
||||
p.getInventory().clear();
|
||||
if (p != owner) this.giveRatingItems(p);
|
||||
else p.getInventory().clear();
|
||||
});
|
||||
}
|
||||
|
||||
private void giveRatingItems(Player player) {
|
||||
Material[] materials = {
|
||||
Material.RED_CONCRETE, Material.ORANGE_CONCRETE, Material.YELLOW_CONCRETE,
|
||||
Material.LIME_CONCRETE, Material.GREEN_CONCRETE
|
||||
};
|
||||
player.getInventory().clear();
|
||||
for (int i = 0; i < materials.length; i++) {
|
||||
player.getInventory().setItemStack(i, ItemStack.builder(materials[i])
|
||||
.customName(Component.text((i + 1) + " Punkte"))
|
||||
.build());
|
||||
for (int i = 0; i < ratingItems.size(); i++) {
|
||||
player.getInventory().setItemStack(i, ratingItems.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
private void registerRatingListener() {
|
||||
if (this.listenerRegistered) return;
|
||||
this.listenerRegistered = true;
|
||||
private void onRatingItemUse(PlayerUseItemEvent event) {
|
||||
Player voter = event.getPlayer();
|
||||
if (this.currentOwner == null || voter == this.currentOwner) return;
|
||||
|
||||
this.eventNode().addListener(PlayerUseItemEvent.class, event -> {
|
||||
Player voter = event.getPlayer();
|
||||
if (this.currentOwner == null || voter == this.currentOwner) return;
|
||||
int slot = voter.getHeldSlot();
|
||||
if (slot >= ratingItems.size()) return;
|
||||
|
||||
int slot = voter.getHeldSlot();
|
||||
if (slot > 4) return;
|
||||
|
||||
event.setCancelled(true);
|
||||
this.currentVotes.put(voter, slot + 1);
|
||||
voter.sendMessage(Component.text("Du hast " + (slot + 1) + " Punkte vergeben."));
|
||||
});
|
||||
event.setCancelled(true);
|
||||
this.currentVotes.put(voter, slot + 1);
|
||||
voter.sendMessage(Component.text("Du hast " + (slot + 1) + " Punkte vergeben."));
|
||||
}
|
||||
|
||||
private void finishRound() {
|
||||
@@ -195,11 +172,13 @@ public class BuildBattle extends StatelessGame {
|
||||
this.getScore().insertResult(owner, sum);
|
||||
}
|
||||
|
||||
private boolean isInArea(Pos blockToCheck, Player player) {
|
||||
Pos pos1 = this.spawnPositions.get(player).add(15, 1, 15);
|
||||
Pos pos2 = this.spawnPositions.get(player).add(-16, 300, -16);
|
||||
return blockToCheck.x() >= Math.min(pos1.x(), pos2.x()) && blockToCheck.x() <= Math.max(pos1.x(), pos2.x())
|
||||
&& blockToCheck.y() >= Math.min(pos1.y(), pos2.y()) && blockToCheck.y() <= Math.max(pos1.y(), pos2.y())
|
||||
&& blockToCheck.z() >= Math.min(pos1.z(), pos2.z()) && blockToCheck.z() <= Math.max(pos1.z(), pos2.z());
|
||||
private boolean isInArea(Point block, Player player) {
|
||||
Pos center = this.spawnPositions.get(player);
|
||||
double dx = block.x() - center.x();
|
||||
double dy = block.y() - center.y();
|
||||
double dz = block.z() - center.z();
|
||||
return dx >= -plotHalf && dx < plotHalf
|
||||
&& dy >= 1 && dy <= 300
|
||||
&& dz >= -plotHalf && dz < plotHalf;
|
||||
}
|
||||
}
|
||||
+12
-21
@@ -5,43 +5,34 @@ import eu.mhsl.minenet.minigames.world.generator.terrain.BaseGenerator;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
|
||||
public class BuildBattleChunkgenerator extends BaseGenerator {
|
||||
private static final int inner = 32;
|
||||
static final int inner = 32;
|
||||
private static final int ring = 3;
|
||||
private static final int wall = 1;
|
||||
private static final int wallHeight = 200;
|
||||
|
||||
static final int period = wall + ring + inner + ring;
|
||||
|
||||
private static final int period = wall + ring + inner + ring;
|
||||
|
||||
private static final int offset = wall + ring;
|
||||
private static final int innerStart = wall + ring;
|
||||
private static final int innerEnd = innerStart + inner;
|
||||
|
||||
private static final Block middleBlocks = Block.QUARTZ_BLOCK;
|
||||
private static final Block outerBlocks = Block.STONE_BRICKS;
|
||||
private static final Block wallBlocks = Block.POLISHED_DEEPSLATE;
|
||||
|
||||
public BuildBattleChunkgenerator(int height) {
|
||||
this.addMixIn(unit -> {
|
||||
GeneratorUtils.foreachXZ(unit, point -> {
|
||||
int lx = Math.floorMod(point.blockX() + offset, period);
|
||||
int lz = Math.floorMod(point.blockZ() + offset, period);
|
||||
|
||||
boolean isWall = lx < wall || lz < wall;
|
||||
if (isWall) {
|
||||
for (int y = 0; y <= wallHeight; y++) {
|
||||
unit.modifier().setBlock(point.withY(height + y), wallBlocks);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.addMixIn(unit -> GeneratorUtils.foreachXZ(unit, point -> {
|
||||
int lx = Math.floorMod(point.blockX() + innerStart, period);
|
||||
int lz = Math.floorMod(point.blockZ() + innerStart, period);
|
||||
|
||||
if (lx < wall || lz < wall) {
|
||||
unit.modifier().fill(point.withY(height), point.add(1, 0, 1).withY(height + wallHeight + 1), wallBlocks);
|
||||
} else {
|
||||
unit.modifier().setBlock(point.withY(height), floorBlock(lx, lz));
|
||||
});
|
||||
});
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private static Block floorBlock(int lx, int lz) {
|
||||
int innerStart = wall + ring;
|
||||
int innerEnd = innerStart + inner;
|
||||
|
||||
boolean inInner = lx >= innerStart && lx < innerEnd
|
||||
&& lz >= innerStart && lz < innerEnd;
|
||||
|
||||
|
||||
Reference in new issue
Block a user