|
| 1 | +"""Plugin that generates an installation advancement. |
| 2 | +
|
| 3 | +Installation advancements replace installation messages in an easily viewable |
| 4 | +and non-obstructive way by putting them on a single advancement page. |
| 5 | +
|
| 6 | +Reference: https://mc-datapacks.github.io/en/conventions/datapack_advancement.html |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | +__all__ = [ |
| 11 | + "InstallationAdvancementOptions", |
| 12 | + "installation_advancement", |
| 13 | +] |
| 14 | + |
| 15 | + |
| 16 | +from typing import Optional |
| 17 | + |
| 18 | +from beet import Advancement, Context, PluginOptions, configurable |
| 19 | +from beet.core.utils import JsonDict, TextComponent, normalize_string |
| 20 | + |
| 21 | + |
| 22 | +class InstallationAdvancementOptions(PluginOptions): |
| 23 | + icon: JsonDict = {"item": "minecraft:apple"} |
| 24 | + author_namespace: Optional[str] = None |
| 25 | + author_description: str = "" |
| 26 | + author_skull_owner: Optional[str] = None |
| 27 | + project_namespace: Optional[str] = None |
| 28 | + project_advancement_path: Optional[str] = None |
| 29 | + |
| 30 | + |
| 31 | +def beet_default(ctx: Context): |
| 32 | + ctx.require(installation_advancement) |
| 33 | + |
| 34 | + |
| 35 | +@configurable(validator=InstallationAdvancementOptions) |
| 36 | +def installation_advancement(ctx: Context, opts: InstallationAdvancementOptions): |
| 37 | + author_namespace = opts.author_namespace or normalize_string(ctx.project_author) |
| 38 | + project_namespace = opts.project_namespace or ctx.project_id |
| 39 | + project_advancement_path = ( |
| 40 | + opts.project_advancement_path |
| 41 | + or f"{author_namespace}:{project_namespace}/installed" |
| 42 | + ) |
| 43 | + skull_owner = opts.author_skull_owner or ctx.project_author |
| 44 | + |
| 45 | + if not author_namespace: |
| 46 | + raise ValueError( |
| 47 | + "Missing author namespace. Either author or author_namespace need to be configured" |
| 48 | + ) |
| 49 | + |
| 50 | + if not skull_owner: |
| 51 | + raise ValueError( |
| 52 | + "Missing skull owner. Either author or author_skull_owner need to be configured" |
| 53 | + ) |
| 54 | + |
| 55 | + ctx.data["global:root"] = create_root_advancement() |
| 56 | + ctx.data[f"global:{author_namespace}"] = create_author_advancement( |
| 57 | + ctx.project_author, opts.author_description, skull_owner |
| 58 | + ) |
| 59 | + ctx.data[project_advancement_path] = create_project_advancement( |
| 60 | + f"{ctx.project_name} {ctx.project_version}", ctx.project_description, author_namespace, opts.icon |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def create_root_advancement(): |
| 65 | + return Advancement( |
| 66 | + { |
| 67 | + "display": { |
| 68 | + "title": "Installed Datapacks", |
| 69 | + "description": "", |
| 70 | + "icon": {"item": "minecraft:knowledge_book"}, |
| 71 | + "background": "minecraft:textures/block/gray_concrete.png", |
| 72 | + "show_toast": False, |
| 73 | + "announce_to_chat": False, |
| 74 | + }, |
| 75 | + "criteria": {"trigger": {"trigger": "minecraft:tick"}}, |
| 76 | + } |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def create_author_advancement( |
| 81 | + author: TextComponent, |
| 82 | + author_description: TextComponent, |
| 83 | + skull_owner: str, |
| 84 | +): |
| 85 | + return Advancement( |
| 86 | + { |
| 87 | + "display": { |
| 88 | + "title": author, |
| 89 | + "description": author_description, |
| 90 | + "icon": { |
| 91 | + "item": "minecraft:player_head", |
| 92 | + "nbt": f"{{'SkullOwner': '{skull_owner}'}}", |
| 93 | + }, |
| 94 | + "show_toast": False, |
| 95 | + "announce_to_chat": False, |
| 96 | + }, |
| 97 | + "parent": "global:root", |
| 98 | + "criteria": {"trigger": {"trigger": "minecraft:tick"}}, |
| 99 | + } |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def create_project_advancement( |
| 104 | + project_name: TextComponent, |
| 105 | + project_description: TextComponent, |
| 106 | + author_namespace: str, |
| 107 | + icon: JsonDict, |
| 108 | +): |
| 109 | + return Advancement( |
| 110 | + { |
| 111 | + "display": { |
| 112 | + "title": project_name, |
| 113 | + "description": project_description, |
| 114 | + "icon": icon, |
| 115 | + "announce_to_chat": False, |
| 116 | + "show_toast": False, |
| 117 | + }, |
| 118 | + "parent": f"global:{author_namespace}", |
| 119 | + "criteria": {"trigger": {"trigger": "minecraft:tick"}}, |
| 120 | + } |
| 121 | + ) |
0 commit comments