kirstym Posted December 3, 2015 Share Posted December 3, 2015 I'm producing a few very simple mods to help users who can only play minecraft via an eye-controlled keyboard. Right now I'm trying to add an action that will be equivalent to jumping and placing a block underneath you, but with a single key event. The issue I'm having is how to programmatically add a block to the world, or in particular how to make the newly-placed block persistent. I've found two ways to programmatically add a (vanilla) block to the world: world.setBlockState(...) ItemStack item = player.getHeldItem(); if (item != null) { Block itemBlock = Block.getBlockFromItem(item.getItem()); BlockPos playerPos = player.getPosition(); BlockPos blockPos = new BlockPos(playerPos.getX(), playerPos.getY()-1, // y = up playerPos.getZ()); IBlockState state0 = itemBlock.getDefaultState(); world.setBlockState(blockPos, state0); } onItemUse(...) ItemStack item = player.getHeldItem(); if (item != null) { BlockPos blockPos = new BlockPos(playerPos.getX(), playerPos.getY()-1, // y = up playerPos.getZ()); // Use item, increment stack size if successful (in creative mode). This prevents it being 'used up;' if (item.onItemUse(player, world, blockPos, EnumFacing.UP, 0.5f, 0.5f, 0.5f) && player.capabilities.isCreativeMode) { item.stackSize += 1; } } In both cases, the block initially seems to be placed fine. However, as soon as you save/quit the game and restart, they have vanished. Also, after placing a block automatically, trying to place a block manually (with right click) against your new block will result in replacing it rather than adding it to the side. Is there something I'm supposed to be doing to make the new block persistent? I've tried: - Varying the EnumFacing passed to onItemUse(...). - Calling world.markBlockForUpdate(blockPos) after creating the block - Comparing the state of a manually placed and a programmatically placed block as given by world.getBlockState(blockPos). Nothing is obviously different. Note that I'm using a local world, and I don't necessarily need this to work on a remote server right now (though it would be good to be aware of any limitations in that regard). Many thanks, Quote Link to comment Share on other sites More sharing options...
Choonster Posted December 3, 2015 Share Posted December 3, 2015 If you're trying to do something like placing a block in response to a keypress (or something Minecraft sees as a keypress), you need to send a packet to the server and run the action on the server side. Even in single-player, there's a server thread running in the background that's in charge of the game. If you only perform an action in the client thread, any changes you make will be overwritten by the server's values. diesieben07 has a tutorial on the Simple Network Implementation (used to send packets between the client and server) here. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future. Link to comment Share on other sites More sharing options...
kirstym Posted December 3, 2015 Author Share Posted December 3, 2015 Thanks Choonster, that's really helpful. I'll take a look at diesieben07's tutorial. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.