From c526d0fe97052412dd57e58ea2ce99dd32072a4c Mon Sep 17 00:00:00 2001 From: PREMIEREHELL <88470674+PREMIEREHELL@users.noreply.github.com> Date: Wed, 12 Jun 2024 01:42:45 -0700 Subject: [PATCH] Fix Java lighting for 1.14+ (#304) * Java lighting * Renamed methods * Removed unused imports * Remove docstring * Fix setting isLightOn * Re-enable calculate light --------- Co-authored-by: gentlegiantJGC --- amulet/level/formats/anvil_world/format.py | 15 +++++----- .../interfaces/chunk/anvil/anvil_1934.py | 30 +++++++++++++++++++ .../interfaces/chunk/anvil/anvil_2844.py | 2 +- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/amulet/level/formats/anvil_world/format.py b/amulet/level/formats/anvil_world/format.py index b760640d2..c3bdb0b52 100644 --- a/amulet/level/formats/anvil_world/format.py +++ b/amulet/level/formats/anvil_world/format.py @@ -495,14 +495,14 @@ def pre_save_operation( except StopIteration as e: height_changed = e.value - # light = self._calculate_light(level, changed_chunks) - # try: - # while True: - # yield next(light) / 2 - # except StopIteration as e: - # light_changed = e.value + light = self._calculate_light(level, changed_chunks) + try: + while True: + yield next(light) / 2 + except StopIteration as e: + light_changed = e.value - return height_changed # or light_changed + return height_changed or light_changed @staticmethod def _calculate_height( @@ -553,6 +553,7 @@ def _calculate_light( changed_ = False changed_ |= chunk.misc.pop("block_light", None) is not None changed_ |= chunk.misc.pop("sky_light", None) is not None + changed_ |= chunk.misc.pop("isLightOn", None) if changed_: changed = True chunk.changed = True diff --git a/amulet/level/interfaces/chunk/anvil/anvil_1934.py b/amulet/level/interfaces/chunk/anvil/anvil_1934.py index bb13f74d7..cd7d2d4de 100644 --- a/amulet/level/interfaces/chunk/anvil/anvil_1934.py +++ b/amulet/level/interfaces/chunk/anvil/anvil_1934.py @@ -1,5 +1,14 @@ from __future__ import annotations +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from amulet.api.chunk import Chunk + +from amulet_nbt import CompoundTag, ByteTag + +from .base_anvil_interface import ChunkDataType, ChunkPathType + from .anvil_1912 import Anvil1912Interface as ParentInterface @@ -8,13 +17,34 @@ class Anvil1934Interface(ParentInterface): Made lighting optional """ + isLightOn: ChunkPathType = ( + "region", + [("Level", CompoundTag), ("isLightOn", ByteTag)], + ByteTag, + ) + def __init__(self): super().__init__() self._set_feature("light_optional", "true") + self._register_encoder(self._encode_is_light_on) + self._register_decoder(self._decode_is_light_on) @staticmethod def minor_is_valid(key: int): return 1934 <= key < 2203 + def _decode_is_light_on( + self, chunk: Chunk, data: ChunkDataType, floor_cy: int, height_cy: int + ): + chunk.misc["isLightOn"] = self.get_layer_obj( + data, self.isLightOn, pop_last=True + ) + + def _encode_is_light_on( + self, chunk: Chunk, data: ChunkDataType, floor_cy: int, height_cy: int + ): + is_light_on = bool(chunk.misc.pop("isLightOn", None)) + self.set_layer_obj(data, self.isLightOn, ByteTag(is_light_on)) + export = Anvil1934Interface diff --git a/amulet/level/interfaces/chunk/anvil/anvil_2844.py b/amulet/level/interfaces/chunk/anvil/anvil_2844.py index 6c21c0018..24991e7a9 100644 --- a/amulet/level/interfaces/chunk/anvil/anvil_2844.py +++ b/amulet/level/interfaces/chunk/anvil/anvil_2844.py @@ -57,7 +57,7 @@ class Anvil2844Interface(ParentInterface): OldLevel: ChunkPathType = ("region", [("Level", CompoundTag)], CompoundTag) Level: ChunkPathType = ("region", [], CompoundTag) Sections: ChunkPathType = ("region", [("sections", ListTag)], ListTag) - + isLightOn: ChunkPathType = ("region", [("isLightOn", ByteTag)], ByteTag) Entities: ChunkPathType = ("region", [("entities", ListTag)], ListTag) BlockEntities: ChunkPathType = ("region", [("block_entities", ListTag)], ListTag) BlockTicks: ChunkPathType = ("region", [("block_ticks", ListTag)], ListTag)