added setting, refactored code

This commit is contained in:
2026-02-01 18:24:36 +01:00
parent 05240660f2
commit 7d5fb025bd
3 changed files with 51 additions and 44 deletions

View File

@@ -23,12 +23,15 @@ import java.util.stream.Collectors;
public class BlockBattle extends StatelessGame {
private final Team teamBlue = new Team(new Pos(0,101,20).add(0.5), Team.Color.BLUE);
private final Team teamRed = new Team(new Pos(0, 101, -20).add(0.5), Team.Color.RED);
private final int itemCount;
private final Map<Player, Team> teams = new WeakHashMap<>();
public BlockBattle() {
public BlockBattle(int itemCount) {
super(Dimension.THE_END.key, "Block Battle", new FirstWinsScore());
this.itemCount = itemCount;
this.eventNode().addChild(
CombatFeatures.empty()
.add(CombatFeatures.VANILLA_ATTACK)
@@ -97,42 +100,26 @@ public class BlockBattle extends StatelessGame {
@Override
protected void onBlockBreak(@NotNull PlayerBlockBreakEvent playerBlockBreakEvent) {
final Block[] blockCantBreakList = new Block[] {
Block.YELLOW_CONCRETE_POWDER,
Block.GOLD_BLOCK,
Block.BLUE_CONCRETE_POWDER,
Block.BLUE_CONCRETE,
Block.BLUE_STAINED_GLASS,
Block.RED_CONCRETE_POWDER,
Block.RED_CONCRETE,
Block.RED_STAINED_GLASS
};
for(Block block : blockCantBreakList) {
if(playerBlockBreakEvent.getBlock().equals(block))
playerBlockBreakEvent.setCancelled(true);
}
boolean isAllowed = Arrays.stream(Team.Color.values())
.map(Team.Color::getMaterial)
.map(Material::block)
.toList()
.contains(playerBlockBreakEvent.getBlock());
if(!isAllowed) playerBlockBreakEvent.setCancelled(true);
}
@Override
protected void onPlayerMove(@NotNull PlayerMoveEvent playerMoveEvent) {
if(playerMoveEvent.getNewPosition().y() < 75) {
if(playerMoveEvent.getNewPosition().y() < 95) {
var player = playerMoveEvent.getPlayer();
if(this.isBeforeBeginning) {
player.teleport(this.getSpawn());
} else {
player.teleport(this.teams.get(player).getSpawnPosition());
}
player.teleport(
this.isBeforeBeginning
? this.getSpawn()
: this.teams.get(player).getSpawnPosition()
);
player.getInventory().clear();
if(this.teams.get(player).getColor() == Team.Color.RED) {
ItemStack item = ItemStack.of(Material.RED_WOOL);
player.getInventory().addItemStack(item);
}
if(this.teams.get(player).getColor() == Team.Color.BLUE) {
ItemStack item = ItemStack.of(Material.BLUE_WOOL);
player.getInventory().addItemStack(item);
}
this.giveItems(player);
}
}
@@ -140,17 +127,10 @@ public class BlockBattle extends StatelessGame {
protected void onStart() {
this.setTeams();
this.getPlayers().forEach(player -> {
player.teleport(this.teams.get(player).getSpawnPosition()).thenRun(() -> {
this.giveItems(player);
player.setGameMode(GameMode.SURVIVAL);
player.teleport(this.teams.get(player).getSpawnPosition());
if(this.teams.get(player).getColor() == Team.Color.RED) {
ItemStack item = ItemStack.of(Material.RED_WOOL);
player.getInventory().addItemStack(item);
}
if(this.teams.get(player).getColor() == Team.Color.BLUE) {
ItemStack item = ItemStack.of(Material.BLUE_WOOL);
player.getInventory().addItemStack(item);
}
});
});
}
@@ -205,4 +185,13 @@ public class BlockBattle extends StatelessGame {
public Pos getSpawn() {
return new Pos(0, 101, 0).add(0.5);
}
private void giveItems(Player player) {
player.getInventory().clear();
ItemStack item = ItemStack.of(
this.teams.get(player).getColor().getMaterial(),
this.itemCount
);
player.getInventory().addItemStack(item);
}
}

View File

@@ -1,8 +1,10 @@
package eu.mhsl.minenet.minigames.instance.game.stateless.types.blockBattle;
import eu.mhsl.minenet.minigames.instance.game.Game;
import eu.mhsl.minenet.minigames.instance.game.stateless.config.ConfigManager;
import eu.mhsl.minenet.minigames.instance.game.stateless.config.GameFactory;
import eu.mhsl.minenet.minigames.instance.game.stateless.config.Option;
import eu.mhsl.minenet.minigames.instance.game.stateless.config.common.NumericOption;
import eu.mhsl.minenet.minigames.instance.room.Room;
import eu.mhsl.minenet.minigames.message.component.TranslatedComponent;
import net.minestom.server.item.Material;
@@ -20,6 +22,12 @@ public class BlockBattleFactory implements GameFactory {
return TranslatedComponent.byId("game_BlockBattle#description");
}
@Override
public ConfigManager configuration() {
return new ConfigManager()
.addOption(new NumericOption("itemCount", Material.WHITE_WOOL, TranslatedComponent.byId("game_BlockBattle#itemCount"), 1, 2, 3));
}
@Override
public Material symbol() {
return Material.GREEN_CONCRETE;
@@ -27,6 +35,6 @@ public class BlockBattleFactory implements GameFactory {
@Override
public Game manufacture(Room parent, Map<String, Option<?>> configuration) throws Exception {
return new BlockBattle().setParent(parent);
return new BlockBattle(configuration.get("itemCount").getAsInt()).setParent(parent);
}
}

View File

@@ -1,11 +1,21 @@
package eu.mhsl.minenet.minigames.instance.game.stateless.types.blockBattle;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.item.Material;
public class Team {
public enum Color {
RED,
BLUE
RED(Material.RED_WOOL),
BLUE(Material.BLUE_WOOL);
private final Material block;
Color(Material block) {
this.block = block;
}
public Material getMaterial() {
return this.block;
}
}
private final Pos spawnPosition;
private final Color color;