Jump to content

World.destroyBlock not visible by other players


Shifty887

Recommended Posts

Hi,

 

Making my first mod so forgive me if this is trivial.

 

I have a pickaxe which mines multiple blocks. I use the onBlockDestroyed method to remove all adjacent blocks in a given direction, effectively the pickaxe now mines 9 blocks at a time. This currently works.

 

The problem is that its now impossible to mine a single block which can cause problems - like mining upwards. So i added a right click to destroy a single block. I did this using the onItemUse method.

 

However this does not work for other players, when a player right clicks they see the block disappear but the other players do not. Leaving this 'ghost' block behind - other players can still mine it themselves. I thought this must be a client/server issue so after a quick search I found world.isRemote. Now I only call world.destroyBlock when !world.isRemote so that it runs on the server. But still the same issue, the player who clicks can see the block disappear but noone else.

 

I've tried looking through the other src files and other peoples mods with no luck. Any help will be greatly appreciated.

 

On a side note what is the best way to test this. I tried launching the server through the IntelliJ run config and then multiple clients. But the only way to get multiple clients is to set offline-mode to false and then suddenly none of the clients can remove blocks, they instantly reappear (seems wrong). Which means I can only test it with 1 client and can't see if I've fixed the issue or not.

Link to comment
Share on other sites

The right click method runs client side. If you log out and log back in, you'll find that the block was not destroyed.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Here is the code in question as requested:

 

    @Override
    public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        if(!world.isRemote) {
            if (player.isSneaking()) {
                if (facing != EnumFacing.DOWN) {
                    placeTorch(world, pos.offset(facing), facing);
                }
            } else {
                world.destroyBlock(pos, true);
            }
        }
        return EnumActionResult.PASS;
    }

    private void placeTorch(World world, BlockPos pos, EnumFacing facing) {
        IBlockState torch = Blocks.TORCH.getDefaultState();
        world.setBlockState(pos, torch.withProperty(BlockTorch.FACING, facing));
    }

 

Basically If I right click then it removes the block, if I shift right click it places a torch. This works from the clients perspective.

Link to comment
Share on other sites

Ok, if it has if(!world.isRemote), then it should only run on the server and do nothing on the client at all, even if the method this is contained in was client-side-only (in which case, nothing would happen on the server either, because it would be unaware and the if-statement prevents the client from doing anything at all).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

If there is, I'm not aware of it.  The right-click method has always given me a little bit of trouble keeping things synced (dates back to the first time I started using it), so the only way to be absolutely sure is to use packets.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

So I changed to use packets - following your tutorial diesieben07 and it all works. So thanks for that!

 

Secondly I was seeing strange results because I was testing it lazily - ie only firing up the client and/or opening it to LAN. The reason I was doing this is because when I start the test server and connect to it through the test client I can't seem to break any blocks. I've noticed this is only when I have it in offline mode and set the flag on the client to allow invalid credentials. If I have it in online mode and use my account's login details then it works but I can no longer test with multiple clients as I can only be logged in on one client at a time.

 

Also is there any doc showing which methods are called on what side ? If I had known that this method was only called on the client that would have saved some time.

 

I'll continue to look into this / writing some tests to avoid doing it manually. But the original problem I posted about has now been fixed so thanks to all who helped :)

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.