Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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.

  • Author

Ok I suspected as much.

 

What method should I be using then ? Or do I need to send some request to the server to do the deletion from that method ?

  • Author

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.

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.

  • Author

Is there a better method than onItemUse then ? A method that does execute on the server that happens on right click or that I can check to see if the player is right clicking ?

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.

  • Author

Apologies I meant Item not ItemTool.

 

I'll look into packets to send the update to the server on right click. Seems a bit over complicated though.

  • Author

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 :)

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.