Add configured features for patch and crop
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
from mcpacker.format.json import JsonBlob
|
||||
from mcpacker.model.flora.companion import Companion
|
||||
from mcpacker.model.flora.patch import Patch
|
||||
from typing import cast
|
||||
|
||||
import mcpacker.format.json as json
|
||||
import mcpacker.model.flora.immersion as IM
|
||||
|
||||
|
||||
# Class ############################################################################################
|
||||
|
||||
class CropConfiguredFeature:
|
||||
|
||||
def __init__(self, patch:Patch):
|
||||
self.patch = patch
|
||||
|
||||
def asJsonBlob(self) -> JsonBlob:
|
||||
return {
|
||||
"type": "minecraft:simple_block",
|
||||
"config": {
|
||||
"to_place": {
|
||||
"type": "minecraft:weighted_state_provider",
|
||||
"entries": self._buildCompanionEntries(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Private Methods ##########################################################
|
||||
|
||||
def _buildBaseProperties(self) -> JsonBlob:
|
||||
result = { k: str(v) for k, v in (self.patch.blocks[0].properties or {}).items() }
|
||||
if self.patch.immersion == IM.SUBMERGED:
|
||||
result["waterlogged"] = "true"
|
||||
|
||||
return cast(JsonBlob, result)
|
||||
|
||||
def _buildCompanionEntries(self) -> JsonBlob:
|
||||
usedWeight = sum(c.weight for c in self.patch.companions)
|
||||
weight = 100 - usedWeight
|
||||
|
||||
results:list[JsonBlob] = []
|
||||
results.append(self._buildPlantState(weight))
|
||||
|
||||
for companion in self.patch.companions:
|
||||
results.append({
|
||||
"data": { "Name": str(companion.gameId) },
|
||||
"weight": companion.weight,
|
||||
})
|
||||
|
||||
return results
|
||||
|
||||
def _buildPlantState(self, weight) -> JsonBlob:
|
||||
result = {
|
||||
"data": {
|
||||
"Name": str(self.patch.blocks[0].gameId),
|
||||
},
|
||||
"weight": weight,
|
||||
}
|
||||
|
||||
properties = self._buildBaseProperties()
|
||||
if properties:
|
||||
result["data"]["Properties"] = properties
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,56 @@
|
||||
from mcpacker.format.datapack.configuredfeature.crop import CropConfiguredFeature
|
||||
from mcpacker.format.json import JsonBlob
|
||||
from mcpacker.model.flora.companion import Companion
|
||||
from mcpacker.model.flora.patch import Patch
|
||||
from pytest import fixture
|
||||
|
||||
import mcpacker.model.flora.density as DE
|
||||
import mcpacker.model.flora.immersion as IM
|
||||
import mcpacker.write.json as json
|
||||
import textwrap
|
||||
|
||||
|
||||
# Fixtures #########################################################################################
|
||||
|
||||
@fixture(name="blob")
|
||||
def createBlob():
|
||||
yield CropConfiguredFeature(Patch(
|
||||
blocks = "minecraft:poppy",
|
||||
companions = [Companion("dandelion", 25)],
|
||||
density = DE.THIN,
|
||||
immersion = IM.SUBMERGED,
|
||||
name = "poppy",
|
||||
radius = 3,
|
||||
)).asJsonBlob()
|
||||
|
||||
|
||||
# Tests ############################################################################################
|
||||
|
||||
def test_asJsonBlob(blob:JsonBlob):
|
||||
assert json.dumps(blob) == textwrap.dedent("""
|
||||
{
|
||||
"type": "minecraft:simple_block",
|
||||
"config": {
|
||||
"to_place": {
|
||||
"type": "minecraft:weighted_state_provider",
|
||||
"entries": [
|
||||
{
|
||||
"data": {
|
||||
"Name": "minecraft:poppy",
|
||||
"Properties": {
|
||||
"waterlogged": "true"
|
||||
}
|
||||
},
|
||||
"weight": 75
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"Name": "minecraft:dandelion"
|
||||
},
|
||||
"weight": 25
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
""").strip()
|
||||
@@ -0,0 +1,23 @@
|
||||
from mcpacker.format.json import JsonBlob
|
||||
from mcpacker.model.flora.patch import Patch
|
||||
from mcpacker.model.modpack import ModPack
|
||||
|
||||
|
||||
# Class ############################################################################################
|
||||
|
||||
class PatchConfiguredFeature:
|
||||
|
||||
def __init__(self, pack:ModPack, patch:Patch):
|
||||
self.pack = pack
|
||||
self.patch = patch
|
||||
|
||||
def asJsonBlob(self) -> JsonBlob:
|
||||
return {
|
||||
"type": "minecraft:random_patch",
|
||||
"config": {
|
||||
"tries": self.patch.attempts(),
|
||||
"xz_spread": self.patch.radius,
|
||||
"y_spread": self.patch.radius / 2,
|
||||
"feature": f"{self.pack.name}:plant/{self.patch.name}",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
from mcpacker.format.datapack.configuredfeature.patch import PatchConfiguredFeature
|
||||
from mcpacker.format.json import JsonBlob
|
||||
from mcpacker.model.flora.companion import Companion
|
||||
from mcpacker.model.flora.patch import Patch
|
||||
from mcpacker.model.modpack import ModPack
|
||||
from pytest import fixture
|
||||
|
||||
import mcpacker.model.flora.density as DE
|
||||
import mcpacker.model.flora.immersion as IM
|
||||
import mcpacker.write.json as json
|
||||
import textwrap
|
||||
|
||||
|
||||
# Fixtures #########################################################################################
|
||||
|
||||
@fixture(name="blob")
|
||||
def createBlob():
|
||||
yield PatchConfiguredFeature(ModPack("test"), Patch(
|
||||
blocks = "minecraft:poppy",
|
||||
companions = [Companion("dandelion", 25)],
|
||||
density = DE.THIN,
|
||||
immersion = IM.SUBMERGED,
|
||||
name = "poppy",
|
||||
radius = 3,
|
||||
)).asJsonBlob()
|
||||
|
||||
|
||||
# Tests ############################################################################################
|
||||
|
||||
def test_asJsonBlob(blob:JsonBlob):
|
||||
assert json.dumps(blob) == textwrap.dedent("""
|
||||
{
|
||||
"type": "minecraft:random_patch",
|
||||
"config": {
|
||||
"tries": 58,
|
||||
"xz_spread": 3,
|
||||
"y_spread": 1.5,
|
||||
"feature": "test:plant/poppy"
|
||||
}
|
||||
}
|
||||
""").strip()
|
||||
@@ -1,10 +1,10 @@
|
||||
from mcpacker.model.flora.plant import Plant
|
||||
from mcpacker.model.resourceid import ResourceId
|
||||
|
||||
|
||||
# Class ############################################################################################
|
||||
|
||||
class Companion:
|
||||
|
||||
def __init__(self, plant:Plant, weight:int):
|
||||
self.plant = plant
|
||||
def __init__(self, gameId:ResourceId|str, weight:int):
|
||||
self.gameId = ResourceId.parse(gameId)
|
||||
self.weight = weight
|
||||
|
||||
Reference in New Issue
Block a user