gilesknap / mciwb

Minecraft Interactive world builder
Apache License 2.0
299 stars 6 forks source link

When I build doors, only half of then is created #104

Closed jhutar closed 1 year ago

jhutar commented 1 year ago

Hello. First of all, thank you for this great tool! It is a lot of fun playing with it myself and with my kids :-)

I have a problem building doors. Imagine this code:

pos = world.player.pos
world.set_block(pos, Item.STONE)
world.set_block(pos + Direction.UP, Item.STONE)
world.set_block(pos + Direction.WEST * 2, Item.STONE)
world.set_block(pos + Direction.WEST * 2 + Direction.UP, Item.STONE)
world.set_block(pos + Direction.WEST, Item.ACACIA_DOOR)

But the result looks like this:

image

We have tried multiple ways, but were unsuccessful. Is it possible to build complete doors?

gilesknap commented 1 year ago

Hi @jhutar thanks for the interest in this project.

You have come across something I have not tried yet. I assume this is because doors take up 2 blocks and the setblock command applies to a single block only.

Have you tried adding

world.set_block(pos + Direction.WEST + Direction.UP,  Item.ACACIA_DOOR)
gilesknap commented 1 year ago

OK it looks like that won't work because you need to set the DataValue of the two door blocks to represent the different halves of a door. See https://www.digminecraft.com/game_commands/setblock_door.php#:~:text=Place%20Top%20of%20Door&text=Type%20the%20command%20in%20the,South%20from%20our%20current%20location.

After work today I'll take a look at doing this from mciwb I may need to make a change to the library to allow passing of DataValues (but it might already be there).

jhutar commented 1 year ago

Thank you!

gilesknap commented 1 year ago

Hi @jhutar, I have an update. But have run out of time this evening to get the tests working against latest dependencies. So if you want to try a preview of this right away then you can do a git pull origin dev to get the development branch. Later this week I'll fix the tests and merge into main branch.

What I have done is add nbt= parameter to the set_block command (nbt is Named Binary Tag that some blocks have to add info). Its basic because it relies on the caller knowing what strings to pass for NBTs but will do for now.

I have also added a Utils class that implements a place_door function by utilising set_block with nbt values. So now you can do, for example:

 world.utils.place_door(pos, Item.ACACIA_DOOR, facing=Direction.NORTH, right_hinge=True)

Have a go and see if it works for you.

Here is the code for place_door as an example of how to use the nbts:

    def place_door(
        self,
        pos: Vec3,
        door_type: Item,
        facing: Direction = Direction.NORTH,
        open: bool = False,
        right_hinge: bool = False,
    ):
        """
        Place a door in the world

        :param pos: the position of the door in the world
        :param door_type: the type of door to place
        :param direction: the direction the door should face
        :param open: whether the door should be open or closed
        :param left_hinge: whether the door should have a left or right hinge
        """
        if not str(door_type).upper().endswith("_DOOR"):
            raise ValueError("door_type must be a door")

        hinge = "right" if right_hinge else "left"
        open = "true" if open else "false"

        common_nbt = [
            f"hinge={hinge}",
            f"open={open}",
            f"facing={Direction.name(facing)}",
        ]

        nbt = ["half=lower"] + common_nbt
        # doors wont replace doors with different nbt so need to clear first
        self.world.set_block(pos, Item.AIR)
        self.world.set_block(pos, door_type, nbt=nbt)
        nbt = ["half=upper"] + common_nbt
        self.world.set_block(pos + Direction.UP, Item.AIR)
        self.world.set_block(pos + Direction.UP, door_type, nbt=nbt)
gilesknap commented 1 year ago

@jhutar did you get a chance to try this? Did it work for you? Thanks/

jhutar commented 1 year ago

Sorry for delay. Works as expected now, thank you!

pos = world.player.pos
world.set_block(pos, Item.STONE)
world.set_block(pos + Direction.UP, Item.STONE)
world.set_block(pos + Direction.WEST * 2, Item.STONE)
world.set_block(pos + Direction.WEST * 2 + Direction.UP, Item.STONE)
world.utils.place_door(pos + Direction.WEST, Item.ACACIA_DOOR, facing=Direction.NORTH, right_hinge=True)

BTW is it possible that before it was possible to open a mciwb shell --player jhutar shell before user/client connected to server? I have a feeling it worked before when I installed mciwb from pip. Now when I switched to git install in both main and dev branches it does not work (world.player.pos causes ERROR: AttributeError: 'NoneType' object has no attribute 'pos'). It is OK, I was just surprised I have not hit it before.

gilesknap commented 1 year ago

@jhutar thanks for getting back to me.

It should be possible to connect with no player logged into the server. You should get an error but everything continues to run and then you can later do world.add_player("myplayername").

So if that is not what happens then maybe I have introduced a bug.

gilesknap commented 1 year ago

@jhutar This fix is now merged into main #106