Skip to content

Commit 391785b

Browse files
committed
Add documentation
1 parent eff61df commit 391785b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

common/src/main/java/com/mrbysco/armorposer/animation/AnimationHandler.java

+18
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,45 @@
2020
import java.util.Set;
2121

2222
public class AnimationHandler {
23+
// Cache of item frames positions that are currently being animated
2324
private static final Set<BlockPos> cachedFrames = new HashSet<>();
2425

26+
/**
27+
* Handles the animation for the armor stand when an item frame is powered while holding a compatible armor poser book
28+
*
29+
* @param frame The item frame that is being checked
30+
*/
2531
public static void onFrameUpdate(ItemFrame frame) {
2632
if (frame.level() instanceof ServerLevel serverLevel && frame.getDirection() == Direction.UP) {
2733
BlockPos pos = frame.blockPosition();
2834
ItemStack frameStack = frame.getItem();
35+
// Check if the item frame is holding a valid armor poser book
2936
if (isValidArmorPoserBook(frameStack)) {
37+
// Check if the item frame is powered
3038
if (serverLevel.getSignal(pos, Direction.UP) >= 1) {
3139
if (!cachedFrames.contains(pos)) {
3240
cachedFrames.add(pos);
3341
WrittenBookContent bookContent = frameStack.getOrDefault(DataComponents.WRITTEN_BOOK_CONTENT, WrittenBookContent.EMPTY);
42+
// The name to match the armor stand with (empty string if no match required)
3443
String match = "";
3544
if (!bookContent.pages().isEmpty()) {
3645
var firstPage = bookContent.pages().getFirst();
3746
if (!firstPage.raw().getString().isEmpty())
3847
match = firstPage.raw().getString();
3948
}
49+
// Targeting conditions for the armor stand
4050
TargetingConditions conditions = TargetingConditions.forNonCombat();
4151
if (!match.isEmpty()) {
4252
String finalMatch = match;
4353
conditions.selector((livingEntity, level) ->
4454
livingEntity.getName().getString().equals(finalMatch));
4555
}
56+
// Search for the nearest armor stand within the search radius
4657
var nearestStand = serverLevel.getNearestEntity(ArmorStand.class, conditions, null, pos.getX(), pos.getY(), pos.getZ(),
4758
AABB.ofSize(frame.position(), Reference.ANIMATION_SEARCH_RADIUS, Reference.ANIMATION_SEARCH_RADIUS, Reference.ANIMATION_SEARCH_RADIUS));
4859
if (nearestStand != null) {
4960
CompoundTag customTag = frameStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag();
61+
// Check if the book contains a saved pose
5062
if (customTag.contains("SavedPose")) {
5163
CompoundTag poseTag = customTag.getCompound("SavedPose");
5264
BookCopyData bookCopyData = new BookCopyData(nearestStand.getUUID(), poseTag);
@@ -61,6 +73,12 @@ public static void onFrameUpdate(ItemFrame frame) {
6173
}
6274
}
6375

76+
/**
77+
* Checks if the item frame is holding a valid armor poser book
78+
*
79+
* @param stack The item stack that is being checked
80+
* @return True if the item stack is a valid armor poser book
81+
*/
6482
private static boolean isValidArmorPoserBook(ItemStack stack) {
6583
return stack.is(Items.WRITTEN_BOOK)
6684
&& stack.getCustomName() != null

common/src/main/java/com/mrbysco/armorposer/data/BookCopyData.java

+13
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,32 @@ public record BookCopyData(UUID entityUUID, CompoundTag tag) {
2626
BookCopyData::tag,
2727
BookCopyData::new);
2828

29+
/**
30+
* Handles applying the pose data to the player's offhand book if it's an armor poser book
31+
*
32+
* @param player The player to apply the data to
33+
*/
2934
public void handleData(Player player) {
3035
if (!tag.isEmpty()) {
3136
ItemStack offStack = player.getOffhandItem();
37+
// Check if the player is holding an armor poser book
3238
if (offStack.is(Items.WRITTEN_BOOK) && offStack.getCustomName() != null && offStack.getCustomName().getString().equals("Armor Poser")) {
3339
CustomData data = offStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
3440
CompoundTag tagCopy = data.copyTag();
41+
// Set the datapack to ArmorStatuesV2 to make it compatible with the Armor Statues Datapack
3542
tagCopy.putString("datapack", "ArmorStatuesV2");
43+
// Set the pose data to the book
3644
tagCopy.put("SavedPose", tag);
3745
offStack.set(DataComponents.CUSTOM_DATA, CustomData.of(tagCopy));
3846
}
3947
}
4048
}
4149

50+
/**
51+
* Handles the pose data for the armor stand
52+
*
53+
* @param armorStand The armor stand to apply the pose to
54+
*/
4255
public void handleFrame(ArmorStand armorStand) {
4356
CompoundTag entityTag = armorStand.saveWithoutId(new CompoundTag());
4457
CompoundTag entityTagCopy = entityTag.copy();

0 commit comments

Comments
 (0)