|
3 | 3 |
|
4 | 4 | from jinja2 import pass_context |
5 | 5 | from jinja2.runtime import Context |
6 | | -from pydantic import Field, ValidationInfo, field_validator, model_validator |
| 6 | +from pydantic import ( |
| 7 | + Field, |
| 8 | + PrivateAttr, |
| 9 | + ValidationInfo, |
| 10 | + field_validator, |
| 11 | + model_validator, |
| 12 | +) |
7 | 13 |
|
8 | 14 | from hexdoc.core import Entity, ItemStack, ResourceLocation |
9 | 15 | from hexdoc.minecraft import I18n, LocalizedStr |
@@ -47,12 +53,41 @@ class EmptyPage(Page, type="patchouli:empty", template_type="patchouli:page"): |
47 | 53 |
|
48 | 54 |
|
49 | 55 | class EntityPage(PageWithText, type="patchouli:entity"): |
| 56 | + _entity_name: LocalizedStr = PrivateAttr() |
| 57 | + _texture: PNGTexture = PrivateAttr() |
| 58 | + |
50 | 59 | entity: Entity |
51 | 60 | scale: float = 1 |
52 | 61 | offset: float = 0 |
53 | 62 | rotate: bool = True |
54 | 63 | default_rotation: float = -45 |
55 | | - name: LocalizedStr | None = None |
| 64 | + name_field: LocalizedStr | None = Field(default=None, serialization_alias="name") |
| 65 | + |
| 66 | + @property |
| 67 | + def entity_name(self): |
| 68 | + return self._entity_name |
| 69 | + |
| 70 | + @property |
| 71 | + def name(self): |
| 72 | + if self.name_field is None or not self.name_field.value: |
| 73 | + return self._entity_name |
| 74 | + return self.name_field |
| 75 | + |
| 76 | + @property |
| 77 | + def texture(self): |
| 78 | + return self._texture |
| 79 | + |
| 80 | + @model_validator(mode="after") |
| 81 | + def _get_texture(self, info: ValidationInfo) -> Self: |
| 82 | + # can't be on Entity's validator because it's frozen and |
| 83 | + # causes circular references with the PNGTexture |
| 84 | + assert info.context is not None |
| 85 | + i18n = I18n.of(info) |
| 86 | + self._entity_name = i18n.localize_entity(self.entity.id) |
| 87 | + self._texture = PNGTexture.load_id( |
| 88 | + id="textures/entities" / self.entity.id + ".png", context=info.context |
| 89 | + ) |
| 90 | + return self |
56 | 91 |
|
57 | 92 |
|
58 | 93 | class ImagePage(PageWithTitle, type="patchouli:image"): |
|
0 commit comments