From b81e1b0e806f0e594cef5011ae1869d0dbe62bf0 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 19 Oct 2022 14:26:38 +0200 Subject: implement models and compat BlockWorld class --- BerryClient/addons/blockworld.py | 41 ++++++++++++++++++++++++++++ BerryClient/addons/core.py | 5 ++++ BerryClient/addons/msg.py | 9 ------- BerryClient/addons/reload.py | 5 ---- BerryClient/core.py | 5 ++-- BerryClient/models/House.py | 52 ++++++++++++++++++++++++++++++++++++ BerryClient/models/Roof.py | 22 +++++++++++++++ BerryClient/models/Wall.py | 28 +++++++++++++++++++ BerryClient/models/WallWithDoor.py | 19 +++++++++++++ BerryClient/models/WallWithWindow.py | 17 ++++++++++++ 10 files changed, 187 insertions(+), 16 deletions(-) create mode 100644 BerryClient/addons/blockworld.py create mode 100644 BerryClient/addons/core.py delete mode 100644 BerryClient/addons/msg.py delete mode 100644 BerryClient/addons/reload.py create mode 100644 BerryClient/models/House.py create mode 100644 BerryClient/models/Roof.py create mode 100644 BerryClient/models/Wall.py create mode 100644 BerryClient/models/WallWithDoor.py create mode 100644 BerryClient/models/WallWithWindow.py diff --git a/BerryClient/addons/blockworld.py b/BerryClient/addons/blockworld.py new file mode 100644 index 0000000..958e61a --- /dev/null +++ b/BerryClient/addons/blockworld.py @@ -0,0 +1,41 @@ +import mcpi.block + +from BerryClient.commands import command +from BerryClient.models.House import House + +MATERIAL_MAPPING = { + "air": mcpi.block.AIR, + "stone": mcpi.block.STONE, + "brick": mcpi.block.STONE_BRICK, +} + +class BlockWorld: + + def __init__(self, core): + self.core = core + + def set_block(self, x, y, z, block): + pos = (x, y, z) + block_obj = MATERIAL_MAPPING[block] + + self.core.setBlock(*pos, block_obj.id) + + def set_blocks(self, x1, y1, z1, x2, y2, z2, block): + pos1 = (x1, y1, z1) + pos2 = (x2, y2, z2) + block_obj = MATERIAL_MAPPING[block] + + self.core.setBlocks(*pos1, *pos2, block_obj.id) + + def player_position(self) -> tuple: + return self.core.player.getPos() + + def __getattr__(self, attr): + return getattr(self.core, attr) + +@command() +def b(core): + bw = BlockWorld(core) + + h = House(bw.player_position(), bw) + h.build() diff --git a/BerryClient/addons/core.py b/BerryClient/addons/core.py new file mode 100644 index 0000000..958bb2b --- /dev/null +++ b/BerryClient/addons/core.py @@ -0,0 +1,5 @@ +from BerryClient.commands import command + +@command(name="reload") +def _reload(core): + core.reload() diff --git a/BerryClient/addons/msg.py b/BerryClient/addons/msg.py deleted file mode 100644 index 61edabd..0000000 --- a/BerryClient/addons/msg.py +++ /dev/null @@ -1,9 +0,0 @@ -import logging - -from BerryClient.commands import command - -log = logging.getLogger(__name__) - -@command() -def msg(core, *content): - log.info(" ".join(content)) diff --git a/BerryClient/addons/reload.py b/BerryClient/addons/reload.py deleted file mode 100644 index 958bb2b..0000000 --- a/BerryClient/addons/reload.py +++ /dev/null @@ -1,5 +0,0 @@ -from BerryClient.commands import command - -@command(name="reload") -def _reload(core): - core.reload() diff --git a/BerryClient/core.py b/BerryClient/core.py index 5f59e1b..1b4e5b5 100644 --- a/BerryClient/core.py +++ b/BerryClient/core.py @@ -11,8 +11,9 @@ log = logging.getLogger() class BerryCore: ADDONS = [ - "BerryClient.addons.msg", - "BerryClient.addons.reload" + "BerryClient.addons.core", + + "BerryClient.addons.blockworld", ] ADDON_MODULES = {} diff --git a/BerryClient/models/House.py b/BerryClient/models/House.py new file mode 100644 index 0000000..72ae549 --- /dev/null +++ b/BerryClient/models/House.py @@ -0,0 +1,52 @@ +from BerryClient.models.Roof import Roof +from BerryClient.models.Wall import Wall +from BerryClient.models.WallWithDoor import WallWithDoor +from BerryClient.models.WallWithWindow import WallWithWindow + +class House: + wallFront: Wall + wallLeft: Wall + wallRight: Wall + wallBack: Wall + pos: tuple + roof: Roof + __bw: "BlockWorld" + + def __init__(self, pos, bw): + self.pos = pos + self.__bw = bw + + x, y, z = self.pos + + x -= (Wall.width/2) + z -= (Wall.width/2) + + corner_bottom_left = (x, y, z) + corner_bottom_right = (x + Wall.width-1, y, z) + corner_top_left = (x, y, z + Wall.width-1) + + self.wallFront = WallWithDoor(corner_bottom_left, bw) + + self.wallLeft = WallWithWindow(corner_bottom_left, bw) + self.wallLeft.rotated = True + + self.wallRight = WallWithWindow(corner_bottom_right, bw) + self.wallRight.rotated = True + + self.wallBack = Wall(corner_top_left, bw) + + roof_corner = (x, y + Wall.height, z) + self.roof = Roof(roof_corner, bw) + + def build(self): + self.wallFront.build() + self.wallLeft.build() + self.wallRight.build() + self.wallBack.build() + self.roof.build() + + def change_wall_material(self, new_material_id: str): + wallFront.material_id = new_material_id + wallLeft.material_id = new_material_id + wallRight.material_id = new_material_id + wallBack.material_id = new_material_id diff --git a/BerryClient/models/Roof.py b/BerryClient/models/Roof.py new file mode 100644 index 0000000..5bef395 --- /dev/null +++ b/BerryClient/models/Roof.py @@ -0,0 +1,22 @@ + +class Roof: + width: int = 6 + depth: int = 6 + roof_material_id: str = "brick" + pos: tuple + __bw: "BlockWorld" + + def __init__(self, pos, bw): + self.pos = pos + self.__bw = bw + + def build(self): + + for d in range(self.depth): + x, y, z = self.pos + z += d + + from_pos = (x, y, z) + to_pos = (x + self.width-1, y, z) + + self.__bw.set_blocks(*from_pos, *to_pos, self.roof_material_id) diff --git a/BerryClient/models/Wall.py b/BerryClient/models/Wall.py new file mode 100644 index 0000000..cbd761a --- /dev/null +++ b/BerryClient/models/Wall.py @@ -0,0 +1,28 @@ + +class Wall: + width: int = 6 + height: int = 5 + pos: tuple + rotated: bool = False + material_id: str = "stone" + _bw: "BlockWorld" + + def __init__(self, pos, bw): + self.pos = pos + self._bw = bw + + def build(self): + for h in range(self.height): + x, y, z = self.pos + y += h + + from_pos = (x,y,z) + + if self.rotated: + z += self.width - 1 + else: + x += self.width - 1 + + to_pos = (x,y,z) + + self._bw.set_blocks(*from_pos, *to_pos, self.material_id) diff --git a/BerryClient/models/WallWithDoor.py b/BerryClient/models/WallWithDoor.py new file mode 100644 index 0000000..0d9da8a --- /dev/null +++ b/BerryClient/models/WallWithDoor.py @@ -0,0 +1,19 @@ +from BerryClient.models.Wall import Wall + +class WallWithDoor(Wall): + door_material_id: str = "air" + + def build(self): + super().build() + + x, y, z = self.pos + + if self.rotated: + z += (self.width / 2) + else: + x += (self.width / 2) + + from_pos = (x, y, z) + to_pos = (x, y+1, z) + + self._bw.set_blocks(*from_pos, *to_pos, self.door_material_id) diff --git a/BerryClient/models/WallWithWindow.py b/BerryClient/models/WallWithWindow.py new file mode 100644 index 0000000..36ea92f --- /dev/null +++ b/BerryClient/models/WallWithWindow.py @@ -0,0 +1,17 @@ +from BerryClient.models.Wall import Wall + +class WallWithWindow(Wall): + window_material_id: str = "air" + + def build(self): + super().build() + + x, y, z = self.pos + if self.rotated: + z += (self.width / 2) + else: + x += (self.width / 2) + + window_pos = (x, y+1, z) + + self._bw.set_block(*window_pos, self.window_material_id) \ No newline at end of file -- cgit v1.2.3