Skip to content

Commit

Permalink
Add and make default Display entity PieceHandler (plugin now requires…
Browse files Browse the repository at this point in the history
… 1.19.4)
  • Loading branch information
jpenilla committed Mar 15, 2023
1 parent 949812b commit 1334a3b
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 8 deletions.
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ repositories {
}

dependencies {
compileOnly("io.papermc.paper", "paper-api", "1.19.3-R0.1-SNAPSHOT") {
compileOnly("io.papermc.paper", "paper-api", "1.19.4-R0.1-SNAPSHOT") {
exclude("org.yaml", "snakeyaml")
}
implementation("xyz.niflheim:stockfish-java:4.0.0-SNAPSHOT")
implementation(platform("cloud.commandframework:cloud-bom:1.8.1"))
implementation(platform("cloud.commandframework:cloud-bom:1.8.2"))
implementation("cloud.commandframework:cloud-paper")
compileOnly("com.mojang", "brigadier", "1.0.18")
implementation("cloud.commandframework:cloud-minecraft-extras") {
Expand Down Expand Up @@ -64,7 +64,7 @@ tasks {
dependsOn(shadowJar)
}
runServer {
minecraftVersion("1.19.3")
minecraftVersion("1.19.4")
}
processResources {
val props = mapOf(
Expand Down Expand Up @@ -93,7 +93,7 @@ tasks {
}

val releaseNotes = providers.environmentVariable("RELEASE_NOTES")
val versions = listOf("1.19.3")
val versions = listOf("1.19.4")
val shadowJar = tasks.shadowJar.flatMap { it.archiveFile }

hangarPublish.publications.register("plugin") {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=xyz.jpenilla
version=0.2.1-SNAPSHOT
version=0.3.0-SNAPSHOT
Binary file modified resources/ChessCraft_Resource_Pack.zip
Binary file not shown.
6 changes: 5 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
rootProject.name = "chesscraft"
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0"
}

includeBuild("work/Stockfish-Java")

rootProject.name = "chesscraft"
6 changes: 5 additions & 1 deletion src/main/java/xyz/jpenilla/chesscraft/BoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ public void interact(final PlayerInteractEvent event) {

@EventHandler
public void interact(final PlayerInteractAtEntityEvent event) {
if (event.getRightClicked().getType() != EntityType.ARMOR_STAND) {
if (event.getRightClicked().getType() != EntityType.ARMOR_STAND
&& event.getRightClicked().getType() != EntityType.INTERACTION) {
return;
}
if (!event.getRightClicked().getPersistentDataContainer().has(BoardManager.PIECE_KEY)) {
return;
}
final Location loc = event.getRightClicked().getLocation();
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/xyz/jpenilla/chesscraft/PieceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.destroystokyo.paper.profile.PlayerProfile;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand All @@ -30,9 +32,15 @@
import org.bukkit.block.BlockFace;
import org.bukkit.block.Skull;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Display;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Interaction;
import org.bukkit.entity.ItemDisplay;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.profile.PlayerTextures;
import org.bukkit.util.Transformation;
import org.joml.AxisAngle4f;
import org.joml.Vector3f;
import xyz.jpenilla.chesscraft.config.PieceOptions;
import xyz.jpenilla.chesscraft.data.CardinalDirection;
import xyz.jpenilla.chesscraft.data.Vec3;
Expand All @@ -44,6 +52,86 @@ public interface PieceHandler {

void removeFromWorld(ChessBoard board, World world);

final class DisplayEntity implements PieceHandler {
private final PieceOptions.DisplayEntity options;

public DisplayEntity(final PieceOptions.DisplayEntity options) {
this.options = options;
}

@Override
public void applyToWorld(final ChessBoard board, final BoardStateHolder game, final World world) {
board.forEachPosition(boardPosition -> {
final Vec3 pos = board.toWorld(boardPosition);
removePieceAt(world, pos);
final Piece piece = game.piece(boardPosition);

if (piece == null) {
return;
}
world.spawn(pos.toLocation(world), ItemDisplay.class, itemDisplay -> {
itemDisplay.setTransformation(new Transformation(
// center on block
new Vector3f(0.5f, 0, 0.5f),
// flip upwards
new AxisAngle4f((float) Math.toRadians(90.0D), 1, 0, 0),
// scale
new Vector3f(0.5f),
// piece rotation
new AxisAngle4f((float) Math.toRadians(rotation(board.facing(), piece)), 0, 0, 1)
));
itemDisplay.setItemDisplayTransform(ItemDisplay.ItemDisplayTransform.FIXED);
itemDisplay.setItemStack(this.options.item(piece));
itemDisplay.setInvulnerable(true);
itemDisplay.getPersistentDataContainer().set(BoardManager.PIECE_KEY, PersistentDataType.STRING, board.name());
});
world.spawn(new Location(world, pos.x() + 0.5, pos.y(), pos.z() + 0.5), Interaction.class, interaction -> {
interaction.setInvulnerable(true);
interaction.getPersistentDataContainer().set(BoardManager.PIECE_KEY, PersistentDataType.STRING, board.name());
interaction.setResponsive(true);
interaction.setInteractionHeight((float) this.options.height(piece.type()));
interaction.setInteractionWidth(0.5f);
});
});
}

private static float rotation(final CardinalDirection facing, final Piece piece) {
final double deg = facing.radians() * 180 / Math.PI;
if (piece.color() == PieceColor.WHITE) {
return (float) deg;
}
return (float) deg + 180f;
}

@Override
public void removeFromWorld(final ChessBoard board, final World world) {
board.forEachPosition(pos -> removePieceAt(world, board.toWorld(pos)));
}

private static void removePieceAt(final World world, final Vec3 pos) {
final List<Entity> entities = new ArrayList<>();
entities.addAll(world.getNearbyEntities(
pos.toLocation(world),
0.25,
0.5,
0.25,
e -> e instanceof Display
));
entities.addAll(world.getNearbyEntities(
new Location(world, pos.x() + 0.5, pos.y(), pos.z() + 0.5),
0.25,
0.5,
0.25,
e -> e instanceof Interaction
));
for (final Entity entity : entities) {
if (entity.getPersistentDataContainer().has(BoardManager.PIECE_KEY)) {
entity.remove();
}
}
}
}

final class ItemFrame implements PieceHandler {
private final PieceOptions.ItemFrame options;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/xyz/jpenilla/chesscraft/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

@ConfigSerializable
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
public final class MainConfig {
private String stockfishEngine = "15.1:AUTO";
private PieceOptions pieces = new PieceOptions.ItemFrame();
private PieceOptions pieces = new PieceOptions.DisplayEntity();
private Messages messages = new Messages();

public String stockfishEngine() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/xyz/jpenilla/chesscraft/config/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import xyz.jpenilla.chesscraft.data.piece.PieceType;

@ConfigSerializable
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
public final class Messages {
private String checkmate = "<winner_color>♚</winner_color><winner_displayname> <green>beat <loser_color>♚</loser_color><loser_displayname> by checkmate!";

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/xyz/jpenilla/chesscraft/config/PieceOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import xyz.jpenilla.chesscraft.data.piece.PieceColor;
import xyz.jpenilla.chesscraft.data.piece.PieceType;

@SuppressWarnings("FieldMayBeFinal")
public interface PieceOptions {
Serializer SERIALIZER = new Serializer();

Expand All @@ -40,6 +41,7 @@ public interface PieceOptions {
Mode mode();

enum Mode {
DISPLAY_ENTITY(DisplayEntity.class),
ITEM_FRAME(ItemFrame.class),
PLAYER_HEAD(PlayerHead.class);

Expand All @@ -50,6 +52,62 @@ enum Mode {
}
}

@ConfigSerializable
final class DisplayEntity implements PieceOptions {
private Material material = Material.PAPER;
private Map<PieceType, Double> heights = Map.of(
PieceType.PAWN, 2.0D - 11.5D / 16.0D,
PieceType.BISHOP, 2.0D - 3.5D / 16.0D,
PieceType.KNIGHT, 2.0D - 9.0D / 16.0D,
PieceType.ROOK, 2.0D - 10.0D / 16.0D,
PieceType.QUEEN, 2.0D - 4.0D / 16.0D,
PieceType.KING, 2.0D
);
private Map<PieceType, Integer> whiteCustomModelData = Map.of(
PieceType.PAWN, 7,
PieceType.BISHOP, 8,
PieceType.KNIGHT, 9,
PieceType.ROOK, 10,
PieceType.QUEEN, 11,
PieceType.KING, 12
);
private Map<PieceType, Integer> blackCustomModelData = Map.of(
PieceType.PAWN, 1,
PieceType.BISHOP, 2,
PieceType.KNIGHT, 3,
PieceType.ROOK, 4,
PieceType.QUEEN, 5,
PieceType.KING, 6
);

private int customModelData(final Piece piece) {
if (piece.color() == PieceColor.WHITE) {
return this.whiteCustomModelData.get(piece.type());
}
return this.blackCustomModelData.get(piece.type());
}

public ItemStack item(final Piece piece) {
final ItemStack stack = new ItemStack(this.material);
stack.editMeta(meta -> meta.setCustomModelData(this.customModelData(piece)));
return stack;
}

public double height(final PieceType type) {
return this.heights.getOrDefault(type, 2.0D);
}

@Override
public PieceHandler createHandler() {
return new PieceHandler.DisplayEntity(this);
}

@Override
public Mode mode() {
return Mode.DISPLAY_ENTITY;
}
}

@ConfigSerializable
final class ItemFrame implements PieceOptions {
private Material material = Material.PAPER;
Expand Down

0 comments on commit 1334a3b

Please sign in to comment.