Search code examples
coordinatesblockminecraftminecraft-forge

Minecraft Forge 1.7.10: How to place a block on certain coordinates?


I am creating a Mod and I want to place a block on a specified coordinates, how can I do that? I am using Minecraft Forge 1.7.10. I checked the Block.class and World.class but I didn't find something that does that.. I would really appreciate if someone can help. Best,


Solution

  • There are several World methods that set blocks. The one thing you need to make sure of is that you need to call them on the server side of the mod, not on the client side. If called from the server side (using the proper method), then it'll automatically send the block change to all nearby players (and store the block change). You can call these methods from either server or client side, but generally you'll only want to call them from server side (you can check with the isRemote field of the World - if it's true then it's on the client; you'll only want to actually do stuff when it's false). Sometimes it does make sense to call from both the client and the server (EG an item that always changes a block, just so that the player doesn't need to deal with lag), but you always also want to change it with the server.

    Now, there are several setBlock-like methods. The ones you'll be most interested in is setBlock's 4-parameter method. This method takes an x, y, and z coordinate and then the Block to set. If you want to add metadata, you'll need to use the 6-parameter method, which has x, y, z, the Block, the metadata, and then a flags parameter. This flags parameter does several things, but you'll generally want to set it to 3 so that it causes a block update, sends the change to the client, and doesn't skip rendering. The 4-parameter method simply calls the 6-parameter method with a metadata value of 0 and a flags value of 3.

    So:

    if (!world.isRemote) {
        // Sets the block at 9, 64, 20 to dirt
        world.setBlock(9, 64, 20, Blocks.dirt);
    
        // Sets the block at 9, 64, 21 to wool:15, IE black wool
        world.setBlock(9, 64, 21, Blocks.wool, 15, 3);
    }