diff --git a/mcpacker/format/datapack/configuredfeature/patch.py b/mcpacker/format/datapack/configuredfeature/patch.py index 9873e52..863f06f 100644 --- a/mcpacker/format/datapack/configuredfeature/patch.py +++ b/mcpacker/format/datapack/configuredfeature/patch.py @@ -18,6 +18,6 @@ class PatchConfiguredFeature: "tries": self.patch.attempts(), "xz_spread": self.patch.radius, "y_spread": self.patch.radius / 2, - "feature": f"{self.pack.name}:plant/{self.patch.name}", + "feature": f"{self.pack.name}:crop/{self.patch.name}", } } diff --git a/mcpacker/format/datapack/configuredfeature/patch_test.py b/mcpacker/format/datapack/configuredfeature/patch_test.py index e8052ab..a0461a9 100644 --- a/mcpacker/format/datapack/configuredfeature/patch_test.py +++ b/mcpacker/format/datapack/configuredfeature/patch_test.py @@ -35,7 +35,7 @@ def test_asJsonBlob(blob:JsonBlob): "tries": 58, "xz_spread": 3, "y_spread": 1.5, - "feature": "test:plant/poppy" + "feature": "test:crop/poppy" } } """).strip() diff --git a/mcpacker/model/spawn.py b/mcpacker/model/spawn.py index f194bfd..f92802d 100644 --- a/mcpacker/model/spawn.py +++ b/mcpacker/model/spawn.py @@ -26,3 +26,6 @@ class Spawn[SpawnableKind: Spawnable, EcotypeKind: Ecotype]: f"spawnable={self.spawnable!r}" ")" ) + + def __str__(self) -> str: + return self.name diff --git a/mcpacker/ui/runner.py b/mcpacker/ui/runner.py index 29cd72b..1e2b02a 100644 --- a/mcpacker/ui/runner.py +++ b/mcpacker/ui/runner.py @@ -10,6 +10,7 @@ from mcpacker.write.datapack.disablefeaturewriter import DisableFeatureWriter from mcpacker.write.datapack.disablespawnwriter import DisableSpawnWriter from mcpacker.write.datapack.metawriter import DataPackMetaWriter from mcpacker.write.datapack.writer import DataPackWriter +from mcpacker.write.datapack.plantwriter import PlantWriter from mcpacker.write.incontrol.spawnerwriter import SpawnerWriter from mcpacker.write.report.writer import ReportWriter from mcpacker.write.resourcepack.metawriter import ResourcePackMetaWriter @@ -74,6 +75,7 @@ class Runner: DataPackWriter(p, o, [ DataPackMetaWriter(p, o, 48), DepositWriter(p, o), + PlantWriter(p, o), DisableFeatureWriter(p, o), DisableSpawnWriter(p, o), ]), diff --git a/mcpacker/write/datapack/plantwriter.py b/mcpacker/write/datapack/plantwriter.py new file mode 100644 index 0000000..3f95377 --- /dev/null +++ b/mcpacker/write/datapack/plantwriter.py @@ -0,0 +1,119 @@ +from mcpacker.format.datapack.biomemodifier.addfeature import AddFeatureBiomeModifier +from mcpacker.format.datapack.configuredfeature.crop import CropConfiguredFeature +from mcpacker.format.datapack.configuredfeature.patch import PatchConfiguredFeature +from mcpacker.format.datapack.configuredfeature.tree import TreeConfiguredFeature +from mcpacker.format.datapack.placedfeature.crop import CropPlacedFeature +from mcpacker.format.datapack.placedfeature.patch import PatchPlacedFeature +from mcpacker.write.writer import Writer +from mcpacker.model.flora.patch import Patch +from mcpacker.model.flora.tree import Tree +from mcpacker.model.modpack import ModPack +from mcpacker.write.json import dumps +from pathlib import Path + +import mcpacker.model.altitude as AL +import mcpacker.format.datapack.generationstep as GE + + +# Class ############################################################################################ + +class PlantWriter(Writer): + + def __init__(self, pack:ModPack, outputDir:Path): + super().__init__(pack, outputDir) + + def doWrite(self): + self._writeBiomeModifiers() + self._writeCropConfiguredFeatures() + self._writeCropPlacedFeatures() + self._writePatchConfiguredFeatures() + self._writePatchPlacedFeatures() + self._writeTreeConfiguredFeatures() + self._writeTreePlacedFeatures() + + # Private Methods ########################################################## + + def _writeBiomeModifiers(self): + modName = self.pack.name + + for biome in self.pack.world.biomes: + features:list[str] = [] + for spawn in self.pack.world.plantSpawns: + if not spawn.habitat.accepts(biome): continue + + kind = None + if isinstance(spawn.plant, Patch): + kind = "patch" + if isinstance(spawn.plant, Tree): + kind = "tree" + + if not kind: continue + + features.append(f"{self.pack.name}:{kind}/{spawn.plant.name}") + + modifier = AddFeatureBiomeModifier(str(biome.gameId), features, GE.STRONGHOLDS) + path = self.locator.biomeModifiers() / f"add_{biome.gameId.name!s}_plants.json" + self.resetOutputFile(path) + path.write_text(dumps(modifier.asJsobBlob())) + + def _writeCropConfiguredFeatures(self): + for spawn in self.pack.world.plantSpawns: + if not isinstance(spawn.plant, Patch): continue + + feature = CropConfiguredFeature(spawn.plant) + path = self.locator.configuredFeatures() / "crop" / f"{spawn.name}.json" + self.resetOutputFile(path) + path.write_text(dumps(feature.asJsonBlob())) + + def _writeCropPlacedFeatures(self): + for spawn in self.pack.world.plantSpawns: + if not isinstance(spawn.plant, Patch): continue + + feature = CropPlacedFeature(self.pack, spawn) + path = self.locator.placedFeatures() / "crop" / f"{spawn.name}.json" + self.resetOutputFile(path) + path.write_text(dumps(feature.asJsonBlob())) + + def _writePatchConfiguredFeatures(self): + for spawn in self.pack.world.plantSpawns: + if not isinstance(spawn.plant, Patch): continue + + feature = PatchConfiguredFeature(self.pack, spawn.plant) + path = self.locator.configuredFeatures() / "patch" / f"{spawn!s}.json" + self.resetOutputFile(path) + path.write_text(dumps(feature.asJsonBlob())) + + def _writePatchPlacedFeatures(self): + for spawn in self.pack.world.plantSpawns: + if not isinstance(spawn.plant, Patch): continue + + for altitude in AL.ALL: + band = AL.intersect(altitude, spawn.habitat.altitude) + if not band: continue + + feature = PatchPlacedFeature(self.pack, spawn, altitude) + path = self.locator.placedFeatures() / "patch" / f"{spawn!s}_{band.name!s}.json" + self.resetOutputFile(path) + path.write_text(dumps(feature.asJsonBlob())) + + def _writeTreeConfiguredFeatures(self): + for spawn in self.pack.world.plantSpawns: + if not isinstance(spawn.plant, Tree): continue + + feature = TreeConfiguredFeature(spawn.plant) + path = self.locator.configuredFeatures() / "tree" / f"{spawn!s}.json" + self.resetOutputFile(path) + path.write_text(dumps(feature.asJsonBlob())) + + def _writeTreePlacedFeatures(self): + for spawn in self.pack.world.plantSpawns: + if not isinstance(spawn.plant, Tree): continue + + for altitude in AL.ALL: + band = AL.intersect(altitude, spawn.habitat.altitude) + if not band: continue + + feature = PatchPlacedFeature(self.pack, spawn, altitude) + path = self.locator.placedFeatures() / "tree" / f"{spawn!s}_{band.name!s}.json" + self.resetOutputFile(path) + path.write_text(dumps(feature.asJsonBlob()))