{
    "format_version": "1.21.0",
    "minecraft:entity": {
        "description": {
            "identifier": "cyn_hao:catalog_book",
            "is_summonable": true,
            "is_spawnable": true,
            "is_experimental": false,
            "properties": {
                "cyn_hao:page_id": {
                    "type": "int",
                    "range": [0, 12],
                    "default": 0,
                    "client_sync": true
                }
            }
        },




        "component_groups": {
            "cyn_hao:group_spawn_egg": {
                "minecraft:spawn_entity": {
                    "entities": {
                        "single_use": true,
                        "min_wait_time": 0,
                        "max_wait_time": 0,
                        "spawn_item": "cyn_hao:catalog_book_spawn_egg",
                        "spawn_sound": "dig.bone_block"
                    }
                },

                "minecraft:scheduler": {
                    "min_delay_secs": 0,
                    "max_delay_secs": 0,
                    "scheduled_events": [
                        {
                            "event": "cyn_hao:event_despawn"
                        }
                    ]
                }
            },

            "cyn_hao:group_despawn": {
                "minecraft:instant_despawn": {
                    "remove_child_entities": false
                }
            }
        },



        "components": {
            "minecraft:loot": {
                "table": "loot_tables/cynosia/horses/entities/cyn_catalog.json"
            },

            "minecraft:type_family": {
                "family": [
                    "cyn_hao:catalog_book",
                    "inanimate"
                ]
            },

            // books CANNOT be killed
            // they can ONLY despawn via a player
            // sneak-punching or sneak-interacting
            "minecraft:health": {
                "value": 10,
                "min": 0,
                "max": 10
            },

            // only players can break a dragon skull
            // it's just furniture
            // damage is reduced to 1
            "minecraft:damage_sensor": {
                "triggers": [
                    {
                        "cause": "entity_attack",
                        "on_damage": {
                            "filters": {
                                "all_of" : [
                                    {
                                        "test": "is_family",
                                        "subject": "other",
                                        "value": "player"
                                    }
                                ]
                            },
                            "event": "cyn_hao:event_player_hit"
                        },
                        "deals_damage": true,
                        "damage_multiplier": 0
                    },
                    {
                        "cause": "all",
                        "deals_damage": false
                    }
                ]
 
            },

            "minecraft:fire_immune": {
            },

            "minecraft:knockback_resistance": {
                "value": 1
            },

            "minecraft:collision_box": {
                "width": 1,
                "height": 1.75
            },

            "minecraft:physics": {
                "has_collision": true,
                "has_gravity": true
            },

            "minecraft:pushable": {
                "is_pushable": true,
                "is_pushable_by_piston": true
            },
            "minecraft:interact": {
                "interactions": [
                    {
                        "on_interact": {
                            "filters": {
                                "all_of": [
                                    { "test": "is_family", "subject": "other", "value": "player" },
                                    { "test": "is_sneak_held", "subject": "other", "value": false }
                                ]
                            },
                            "target": "self",
                            "event": "cyn_hao:event_interact_normal"
                        },
                        "swing": true
                    },
                    {
                        "on_interact": {
                            "filters": {
                                "all_of": [
                                    { "test": "is_family", "subject": "other", "value": "player" },
                                    { "test": "is_sneak_held", "subject": "other", "value": true }
                                ]
                            },
                            "target": "self",
                            "event": "cyn_hao:event_interact_sneak"
                        },
                        "swing": true
                    }
                ]
            }
        },



        "events": {
            // play book placement sound when placed
            // defined here since entities don't have anything
            // like "on_spawn" in their sound events
            "minecraft:entity_spawned": {
                "queue_command": {
                    "command":"playsound item.book.put @a ~ ~ ~ 1.0"
                }
            },

            

            // when a player hits a catalog
            // IF SOMETHING WENT WRONG YOU CAN DO
            // /event entity @e cyn_hao:despawn
            "cyn_hao:event_player_hit": {
                "sequence": [
                    {
                        "filters": [
                            { 
                                "test": "is_sneak_held", 
                                "subject": "player", 
                                "operator": "equals", 
                                "value": true
                            }
                        ],
                        "trigger": "cyn_hao:event_hit_sneak"
                    },
                    {
                      "filters": [
                            { 
                                "test": "is_sneak_held", 
                                "subject": "player", 
                                "operator": "equals", 
                                "value": false
                            }
                        ],
                        "trigger": "cyn_hao:event_hit_normal"
                    },
                    {
                        "queue_command": {
                            // always heal the book, just to be safe
                            "command": "effect @s instant_health 1 10 true"
                        }
                    }
                ]
            },

            // normal hits flip the page backwards, if possible
            // otherwise does nothing
            // also plays a page_flip sound
            "cyn_hao:event_hit_normal": {
                // if the book is NOT already on page 0
                "filters": [
                    {
                        "test": "int_property",
                        "subject": "self",
                        "domain": "cyn_hao:page_id",
                        "operator": ">",
                        "value": 0
                    }
                ],
                // flip the page backwards
                "set_property": {
                    "cyn_hao:page_id": "query.property('cyn_hao:page_id') - 1"
                },
                "queue_command": {
                    "command": [
                        // play the page flip sound
                        "playsound item.book.page_turn @a ~ ~ ~ 0.5"
                    ]
                }
            },

            // sneak hits drop the catalog (spawn egg)
            // and then despawn it
            "cyn_hao:event_hit_sneak": {
                "add": {
                    "component_groups": [
                        "cyn_hao:group_spawn_egg"
                    ]
                },
                "queue_command": {
                    "command":"playsound item.book.put @a ~ ~ ~ 1.0"
                }
            },

            // sneak interactions drop the catalog (spawn egg)
            // and then despawn it
            "cyn_hao:event_interact_sneak": {
                "add": {
                    "component_groups": [
                        "cyn_hao:group_spawn_egg"
                    ]
                },
                "queue_command": {
                    "command":"playsound item.book.put @a ~ ~ ~ 1.0"
                }
            },

            // normal interactions flip the page forwards, if possible
            // otherwise does nothing
            // also plays a page_flip sound
            "cyn_hao:event_interact_normal": {
                // if the book is NOT already on page 12
                "filters": [
                    {
                        "test": "int_property",
                        "subject": "self",
                        "domain": "cyn_hao:page_id",
                        "operator": "<",
                        "value": 12
                    }
                ],
                // flip the page forwards
                "set_property": {
                    "cyn_hao:page_id": "query.property('cyn_hao:page_id') + 1"
                },
                // play the page flip sound
                "queue_command": {
                    "command": [
                        // play the page flip sound
                        "playsound item.book.page_turn @a ~ ~ ~ 0.5"
                    ]
                }
            },

            // despawn the skull, so that it doesn't fall over and die
            // done this way since it seemed easier than trying to cancel
            // the built-in death animation rotation
            "cyn_hao:event_despawn": {
                "add": {
                    "component_groups": [
                        "cyn_hao:group_despawn"
                    ]
                }
            }
        }
    }
}