Skip to content

Commit

Permalink
Add command to toggle legal move highlighting (per player)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Feb 13, 2023
1 parent b3a560d commit b25b75f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "xyz.jpenilla"
version = "0.0.1-SNAPSHOT"
version = "0.1.0-SNAPSHOT"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
Expand Down Expand Up @@ -93,6 +93,7 @@ bukkit {
"chesscraft.command.accept",
"chesscraft.command.next_promotion",
"chesscraft.command.forfeit",
"chesscraft.command.show_legal_moves"
)
defaultTrue.forEach {
register(it) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/xyz/jpenilla/chesscraft/ChessGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.stream.Collectors;
import net.kyori.adventure.audience.Audience;
import org.bukkit.Color;
import org.bukkit.NamespacedKey;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -41,6 +42,7 @@
import xyz.niflheim.stockfish.exceptions.StockfishInitException;

public final class ChessGame {
public static final NamespacedKey HIDE_LEGAL_MOVES_KEY = new NamespacedKey("chesscraft", "hide_legal_moves");
private static final String STARTING_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

private final ChessBoard board;
Expand Down Expand Up @@ -137,7 +139,7 @@ public void displayParticles() {
final Player player = chessPlayer.player();
this.blockParticles(player, selectedPos, Color.AQUA);

if (this.validDestinations != null) {
if (this.validDestinations != null && !player.getPersistentDataContainer().has(HIDE_LEGAL_MOVES_KEY)) {
this.validDestinations.stream()
.map(this.board::toWorld)
.forEach(pos -> this.blockParticles(player, pos, Color.GREEN));
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/xyz/jpenilla/chesscraft/command/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.checkerframework.checker.nullness.qual.Nullable;
import xyz.jpenilla.chesscraft.BoardManager;
import xyz.jpenilla.chesscraft.ChessBoard;
import xyz.jpenilla.chesscraft.ChessCraft;
import xyz.jpenilla.chesscraft.ChessGame;
import xyz.jpenilla.chesscraft.ChessPlayer;
import xyz.jpenilla.chesscraft.command.argument.ChessBoardArgument;
import xyz.jpenilla.chesscraft.command.argument.PromotionArgument;
Expand Down Expand Up @@ -138,6 +140,11 @@ public void register() {
.permission("chesscraft.command.next_promotion")
.handler(this::nextPromotion));

this.mgr.command(chess.literal("show_legal_moves")
.senderType(Player.class)
.permission("chesscraft.command.show_legal_moves")
.handler(this::showLegalMoves));

this.mgr.command(chess.literal("forfeit")
.senderType(Player.class)
.permission("chesscraft.command.forfeit")
Expand Down Expand Up @@ -276,6 +283,17 @@ private void nextPromotion(final CommandContext<CommandSender> ctx) {
sender.sendMessage(this.messages().nextPromotionSet(type));
}

private void showLegalMoves(final CommandContext<CommandSender> ctx) {
final Player player = (Player) ctx.getSender();
final boolean hidden = player.getPersistentDataContainer().has(ChessGame.HIDE_LEGAL_MOVES_KEY);
if (hidden) {
player.getPersistentDataContainer().remove(ChessGame.HIDE_LEGAL_MOVES_KEY);
} else {
player.getPersistentDataContainer().set(ChessGame.HIDE_LEGAL_MOVES_KEY, PersistentDataType.BYTE, (byte) 1);
}
player.sendMessage(this.messages().showingLegalMoves(hidden));
}

private void deleteBoard(final CommandContext<CommandSender> ctx) {
final String board = ctx.<ChessBoard>get("board").name();
this.boardManager.deleteBoard(board);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/xyz/jpenilla/chesscraft/config/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ public Component invalidMove() {
return parse(this.invalidMove);
}

private String showingLegalMoves = "Highlighting of legal moves<gray>:</gray> <on_off>";

public Component showingLegalMoves(final boolean value) {
return parse(this.showingLegalMoves, this.onOff(value));
}

private String on = "<green>On";

public Component on() {
return parse(this.on);
}

private String off = "<red>Off";

public Component off() {
return parse(this.off);
}

private TagResolver onOff(final boolean value) {
if (value) {
return Placeholder.component("on_off", this.on());
}
return Placeholder.component("on_off", this.off());
}

private static TagResolver blackWhitePlayerTags(final ChessPlayer black, final ChessPlayer white) {
return playerTags(black, "black", white, "white", PieceColor.BLACK);
}
Expand Down

0 comments on commit b25b75f

Please sign in to comment.