Skip to content

Commit

Permalink
Preserve litematic subversion
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jun 6, 2024
1 parent 31b4536 commit fa416d0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion litemapy/info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
LITEMATIC_VERSION = 6 # Litematic version we are trying to imitate from Litematica (1.18.2-0.11.3)
LITEMATIC_VERSION = 6
LITEMATIC_SUBVERSION = 1
SPONGE_VERSION = 2 # Sponge format version used by WorldEdit (https://github.com/SpongePowered/Schematic-Specification)
MC_DATA_VERSION = 2975 # Minecraft 1.18.2 (https://minecraft.wiki/w/Data_version)
DEFAULT_NAME = "Unnamed" # Default name given to schematics and regions if unspecified
Expand Down
10 changes: 8 additions & 2 deletions litemapy/schematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Schematic:
description: str
region: dict[str, 'Region']
lm_version: int
lm_subversion: int
mc_version: int
created: int
modified: int
Expand All @@ -33,7 +34,8 @@ class Schematic:
def __init__(self,
name: str = DEFAULT_NAME, author: str = "", description: str = "",
regions: Optional[dict[str, 'Region']] = None,
lm_version: int = LITEMATIC_VERSION, mc_version: int = MC_DATA_VERSION
lm_version: int = LITEMATIC_VERSION, lm_subversion: int = LITEMATIC_SUBVERSION,
mc_version: int = MC_DATA_VERSION
) -> None:
"""
Schematic can be created by optionally providing metadata and regions, or leaving them blank or default.
Expand All @@ -59,6 +61,7 @@ def __init__(self,
self.__regions.update(regions)
self.mc_version = mc_version
self.lm_version = lm_version
self.lm_subversion = lm_subversion
self.__preview = IntArray([])

def save(self, file_path: str, update_meta: bool = True, save_soft: bool = True, gzipped: bool = True,
Expand Down Expand Up @@ -94,6 +97,7 @@ def to_nbt(self, save_soft: bool = True) -> Compound:
raise ValueError("Empty schematic does not have any regions")
root = Compound()
root["Version"] = Int(self.lm_version)
root["SubVersion"] = Int(self.lm_subversion)
root["MinecraftDataVersion"] = Int(self.mc_version)
meta = Compound()
enclose = Compound()
Expand Down Expand Up @@ -133,6 +137,7 @@ def from_nbt(nbt: Compound) -> 'Schematic':
"""
meta: Compound = nbt["Metadata"]
lm_version: Int = nbt["Version"]
lm_subversion: Int = nbt.get("SubVersion", 0)
mc_version: Int = nbt["MinecraftDataVersion"]
width = int(meta["EnclosingSize"]["x"])
height = int(meta["EnclosingSize"]["y"])
Expand All @@ -144,7 +149,8 @@ def from_nbt(nbt: Compound) -> 'Schematic':
for key, value in nbt["Regions"].items():
reg = Region.from_nbt(value)
regions[str(key)] = reg
schematic = Schematic(name=name, author=author, description=desc, regions=regions, lm_version=lm_version,
schematic = Schematic(name=name, author=author, description=desc, regions=regions,
lm_version=lm_version, lm_subversion=lm_subversion,
mc_version=mc_version)
if schematic.width != width:
raise CorruptedSchematicError(
Expand Down
Binary file added tests/litematics/valid/Subversion.litematic
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/test_schematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,14 @@ def test_replace():
assert region.palette[0] == AIR
region.replace(log, stone)
assert region[2, 0, 0] == stone


def test_subversion():
schematic = Schematic.load(path.join(VALID_LITEMATIC_DIRECTORY, "Subversion.litematic"))
assert schematic.lm_subversion == 1
with TemporaryDirectory() as temp:
schematic.lm_subversion = 1337
name = path.join(temp, "Subversion.litematic")
schematic.save(name)
schematic = Schematic.load(name)
assert schematic.lm_subversion == 1337

0 comments on commit fa416d0

Please sign in to comment.