|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
| 4 | +from typing import final |
| 5 | + |
| 6 | +from pydantic import Field, field_validator, model_validator |
| 7 | + |
| 8 | +from minebase.types.base import MinecraftDataModel |
| 9 | + |
| 10 | + |
| 11 | +@final |
| 12 | +class CollisionBoxData(MinecraftDataModel): |
| 13 | + """Minecraft-Data for a collision shape. |
| 14 | +
|
| 15 | + Note that the meaning of the 6 numbers in this collision box is unclear, with the minecraft-data JSON schema |
| 16 | + only stating that it means: "The min/max x/y/z corner coordinates of this box.", For this reason, this model |
| 17 | + might very well be incorrect in interpreting it as (min_x, min_y, min_z, max_x, max_y, max_z). |
| 18 | +
|
| 19 | + A request for clarification was submitted, this model should be updated once it is answered: |
| 20 | + https://github.com/PrismarineJS/minecraft-data/issues/1054 |
| 21 | + """ |
| 22 | + |
| 23 | + min_x: float = Field(ge=-0.25, le=1.5) |
| 24 | + min_y: float = Field(ge=-0.25, le=1.5) |
| 25 | + min_z: float = Field(ge=-0.25, le=1.5) |
| 26 | + max_x: float = Field(ge=-0.25, le=1.5) |
| 27 | + max_y: float = Field(ge=-0.25, le=1.5) |
| 28 | + max_z: float = Field(ge=-0.25, le=1.5) |
| 29 | + |
| 30 | + @model_validator(mode="before") |
| 31 | + @classmethod |
| 32 | + def from_sequence(cls, value: object) -> dict[str, object]: |
| 33 | + """Ensure the value is a 6 element tuple, then convert it into a dict for pydantic.""" |
| 34 | + if not isinstance(value, Sequence): |
| 35 | + raise TypeError(f"CollisionBoxData must be initialized from a sequence, got {type(value).__qualname__}") |
| 36 | + |
| 37 | + if len(value) != 6: |
| 38 | + raise ValueError("CollisionBoxData must have exactly 6 elements") |
| 39 | + |
| 40 | + # Use camelCase here since MinecraftDataModel expects it |
| 41 | + return { |
| 42 | + "minX": value[0], |
| 43 | + "minY": value[1], |
| 44 | + "minZ": value[2], |
| 45 | + "maxX": value[3], |
| 46 | + "maxY": value[4], |
| 47 | + "maxZ": value[5], |
| 48 | + } |
| 49 | + |
| 50 | + @model_validator(mode="after") |
| 51 | + def validate_min_less_than_max(self) -> CollisionBoxData: |
| 52 | + """Validate that the min coordinate is always smaller (or equal) than the corresponding max coordinate.""" |
| 53 | + try: |
| 54 | + if self.min_x > self.max_x: |
| 55 | + raise ValueError(f"min_x ({self.min_x}) must be less than max_x ({self.max_x})") # noqa: TRY301 |
| 56 | + if self.min_y > self.max_y: |
| 57 | + raise ValueError(f"min_y ({self.min_y}) must be less than max_y ({self.max_y})") # noqa: TRY301 |
| 58 | + if self.min_z > self.max_z: |
| 59 | + raise ValueError(f"min_z ({self.min_z}) must be less than max_z ({self.max_z})") # noqa: TRY301 |
| 60 | + except ValueError: |
| 61 | + # This is stupid, I'm aware, the above is essentially dead code |
| 62 | + # The reason for this is that the above is currently failing, likely |
| 63 | + # due to misunderstanding in what the 6 numbers in this data mean. |
| 64 | + # Once that will become known, this function should be re-enabled. |
| 65 | + # See: |
| 66 | + # https://github.com/PrismarineJS/minecraft-data/issues/1054 |
| 67 | + return self |
| 68 | + |
| 69 | + return self |
| 70 | + |
| 71 | + |
| 72 | +@final |
| 73 | +class BlockCollisionShapeData(MinecraftDataModel): |
| 74 | + """Minecraft-Data for a block collision model. |
| 75 | +
|
| 76 | + blocks: |
| 77 | + Mapping of block name -> collision shape ID(s). |
| 78 | +
|
| 79 | + The value can either be a single number: collision ID shared by all block states of this block, |
| 80 | + or a list of numbers: Shape IDs of each block state of this block. |
| 81 | +
|
| 82 | + shapes: Collision shapes by ID, each shape being composed of a list of collision boxes. |
| 83 | + """ |
| 84 | + |
| 85 | + blocks: dict[str, int | list[int]] |
| 86 | + shapes: dict[int, list[CollisionBoxData]] |
| 87 | + |
| 88 | + @field_validator("blocks") |
| 89 | + @classmethod |
| 90 | + def validate_block_ids(cls, v: dict[str, int | list[int]]) -> dict[str, int | list[int]]: |
| 91 | + """Ensure all specified shape IDs for the blocks are non-negative.""" |
| 92 | + for key, val in v.items(): |
| 93 | + if isinstance(val, int): |
| 94 | + if val < 0: |
| 95 | + raise ValueError(f"Got an unexpected collision ID value for block {key!r} (must be at least 0)") |
| 96 | + continue |
| 97 | + |
| 98 | + for idx, collision_id in enumerate(val): |
| 99 | + if collision_id < 0: |
| 100 | + raise ValueError( |
| 101 | + f"Got an unexpected collision ID value for block {key!r}. ID #{idx} must be at least 0", |
| 102 | + ) |
| 103 | + |
| 104 | + return v |
| 105 | + |
| 106 | + @field_validator("shapes") |
| 107 | + @classmethod |
| 108 | + def validate_shape_ids(cls, v: dict[int, list[CollisionBoxData]]) -> dict[int, list[CollisionBoxData]]: |
| 109 | + """Ensure all shape IDs are non-negative.""" |
| 110 | + for key in v: |
| 111 | + if key < 0: |
| 112 | + raise ValueError(f"Collision IDs can't be negative (but found: {key})") |
| 113 | + return v |
| 114 | + |
| 115 | + @model_validator(mode="after") |
| 116 | + def validate_shape_references(self) -> BlockCollisionShapeData: |
| 117 | + """Validate that all collision IDs specified for blocks have corresponding shape(s).""" |
| 118 | + for block_name, shape_ids in self.blocks.items(): |
| 119 | + if isinstance(shape_ids, int): |
| 120 | + shape_ids = [shape_ids] # noqa: PLW2901 |
| 121 | + |
| 122 | + for shape_id in shape_ids: |
| 123 | + if shape_id not in self.shapes: |
| 124 | + raise ValueError( |
| 125 | + f"Block {block_name!r} has a collision shape ID {shape_id}, without corresponding shape data", |
| 126 | + ) |
| 127 | + |
| 128 | + return self |
| 129 | + |
| 130 | + def shape_for(self, block_name: str) -> list[CollisionBoxData] | list[list[CollisionBoxData]]: |
| 131 | + """Get the collision shape for given block name. |
| 132 | +
|
| 133 | + This is a convenience helper-function to skip having to look up the collision shape ID(s) |
| 134 | + for the block and then having to look up the corresponding collision shape(s) based on that. |
| 135 | +
|
| 136 | + Return: |
| 137 | + Either a: |
| 138 | +
|
| 139 | + - List of collision shape IDs (for all block states) |
| 140 | + - List of lists of collision shape IDs (representing different collision shapes for different block states) |
| 141 | + """ |
| 142 | + shape_ids = self.blocks[block_name] |
| 143 | + if isinstance(shape_ids, int): |
| 144 | + return self.shapes[shape_ids] |
| 145 | + |
| 146 | + return [self.shapes[shape_id] for shape_id in shape_ids] |
0 commit comments