Skip to content

Commit

Permalink
Merge pull request #2 from ItzSkyReed/0.3
Browse files Browse the repository at this point in the history
0.3
  • Loading branch information
ItzSkyReed authored Jan 6, 2025
2 parents 33c6e91 + 3686488 commit a1c927a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ A Python library to parse BlazeAndCavesAdvancementsPack and its Addons
### Requirements

- Python version 3.12 or higher
- BACAP or addon for minecraft 1.21+

#### Limitations:
- Currently, datapacks with overlays are not supported; parser will work only inside `data` folder of the datapack.

### Installing

Expand Down
12 changes: 6 additions & 6 deletions src/BACAP_Parser/AdvType.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ def __repr__(self):


class MultipleTypesMatch(Exception):
def __init__(self):
super().__init__("Multiple types match the given frame, color, and tab.")
def __init__(self, frame: str, color: Color, tab: str):
super().__init__(f"Multiple types match the given frame: \"{frame}\", color: \"{color}\", and tab: \"{tab}\".")


class NoTypesMatch(Exception):
def __init__(self):
super().__init__("No types match the given frame, color, and tab.")
def __init__(self, frame: str, color: Color, tab: str):
super().__init__(f"No types match the given frame: \"{frame}\", color: \"{color}\", and tab: \"{tab}\".")


class AdvTypeManager:
Expand Down Expand Up @@ -138,8 +138,8 @@ def recognize_type(self, *, frame: str | None = None, color: Color | None = None
]

if len(matching_types) > 1:
raise MultipleTypesMatch
raise MultipleTypesMatch(frame, color, tab)
elif len(matching_types) == 1:
return matching_types[0]
else:
raise NoTypesMatch
raise NoTypesMatch(frame, color, tab)
11 changes: 8 additions & 3 deletions src/BACAP_Parser/Advancement.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,14 @@ def __init__(self, path: Path, adv_json: dict, datapack: Datapack, reward_mcpath
self._background = self._json["display"].get("background")
self._icon = Item(self._json["display"]["icon"])

self._exp = self._initialize_reward("exp", Exp)
self._reward = self._initialize_reward("reward", Reward)
self._trophy = self._initialize_reward("trophy", Trophy)
if self._datapack.reward_namespace_path is not None:
self._exp = self._initialize_reward("exp", Exp)
self._reward = self._initialize_reward("reward", Reward)
self._trophy = self._initialize_reward("trophy", Trophy)
else:
self._exp = None
self._reward = None
self._trophy = None

def _initialize_reward(self, name: Literal["exp", "reward", "trophy"], cls: Type[Exp | Reward | Trophy]):
"""
Expand Down
5 changes: 2 additions & 3 deletions src/BACAP_Parser/Color.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ def __set_color(self, color: str):
self._color = MINECRAFT_TEXT_COLORS_MAP[color]
else:
color = color.lstrip('#')

if len(color) != 6:
raise ValueError(f"Invalid hex color length. A hex color must have exactly 6 characters (without '#').")

try:
color = int(color, 16)
color_check = int(color, 16)
except ValueError:
raise ValueError(f"Invalid hex color: '{color}'. It must consist of valid hex digits (0-9, A-F).")

if not (0 <= color <= 0xFFFFFF):
if not (0 <= color_check <= 0xFFFFFF):
raise ValueError(f"Hex color '{color}' is out of valid range (0 to #FFFFFF).")

self._color = f"#{color}"
Expand Down
15 changes: 9 additions & 6 deletions src/BACAP_Parser/Datapack.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from .TabNameMapper import TabNameMapper

class Datapack:
def __init__(self, name: str, path: Path, adv_type_manager: AdvTypeManager, reward_namespace: str, technical_tabs: Iterable[str] | None = None, tab_name_mapper: TabNameMapper = TabNameMapper()):
def __init__(self, name: str, path: Path, adv_type_manager: AdvTypeManager, reward_namespace: str | None = None, technical_tabs: Iterable[str] | None = None, tab_name_mapper: TabNameMapper = TabNameMapper()):
"""
:param name: Name of the datapack that will be used to identify it in Parser instance
:param path: Path to the datapack
:param adv_type_manager: AdvTypeManager instance
:param reward_namespace: namespace where rewards (exp, trophy, reward) are stored
:param reward_namespace: namespace where rewards (exp, trophy, reward) are stored.
If None Exp, Trophy and item rewards will not be parsed.
:param technical_tabs: a list of tabs with technical advancements
:param tab_name_mapper: TabNameMapper instance with custom tabs, if not specified.
It will be created automatically with default BACAP tabs
Expand All @@ -31,13 +32,15 @@ def __init__(self, name: str, path: Path, adv_type_manager: AdvTypeManager, rewa

self._namespaces = [entry for entry in (self._path / "data").iterdir() if entry.is_dir()]

if reward_namespace not in [entry.name for entry in self._namespaces]:
raise FileNotFoundError(f"Reward namespace \"{reward_namespace}\" does not exist, possible namespaces: {[entry.name for entry in self._namespaces]}")
if reward_namespace is not None:
if reward_namespace not in [entry.name for entry in self._namespaces]:
raise FileNotFoundError(f"Reward namespace \"{reward_namespace}\" does not exist, possible namespaces: {[entry.name for entry in self._namespaces]}")
self._reward_namespace_path = next(entry for entry in self._namespaces if entry.name == reward_namespace)
else:
self._reward_namespace_path = None

self._reward_namespace = reward_namespace

self._reward_namespace_path = next(entry for entry in self._namespaces if entry.name == reward_namespace)

self._adv_type_manager = adv_type_manager
self._advancement_manager = AdvancementManager(self._path, self, technical_tabs)
self._tab_name_mapper = tab_name_mapper
Expand Down
4 changes: 4 additions & 0 deletions src/BACAP_Parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def trim_path_to_namespace(path: Path, namespaces: Sequence[Path]) -> Path:
return Path(*path.parts[i:])
return path


def cut_namespace(string_with_namespace: str) -> str:
"""
:param string_with_namespace: string that contains namespace
Expand Down Expand Up @@ -85,6 +86,9 @@ def safe_load_json(path: Path, encoding: str = "utf-8") -> ExtendedDict | None:
def arabic_to_rims(value: int | str) -> str:
"""
Converts an integer to its Roman numeral representation.
:param value: Integer or string number.
Number > 0
:return: Roman numeral representation
"""
value = int(value)

Expand Down

0 comments on commit a1c927a

Please sign in to comment.