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.

[Mod development] onItemUseFirst: returning true prevents calling serverside

Featured Replies

Posted

Hi all,

 

I've been having a strange bug: I have an item that overrides the onItemUseFirst method in the base Item class, and because it changes blocks in the minecraft world it has to be called both clientside and serverside. Through testing, I've determined that whenever it returns false, it appears to be invoked both clientside and serverside and works fine (barring the fact it's returning false when it really shouldn't, slightly unusual things happen with levers and buttons) but when it returns true, it is never called serverside at all. 

 

Notes on testing:

 

 

At first, I had the item determine if it modified any blocks, and return true if it did.  This created weird temporary blocks, that disappeared when right-clicked, when water flowed on top of them, and on world save/load.  Adding an if (!world.isRemote) test made the item change blocks correctly, but because of how I tested if it modified blocks caused it to always return false.  Changing the return statement to simply return true, combined with the if (!world.isRemote) on the block-changing code, played the 'item-used-animation' but didn't change any blocks, while changing the return statement to simply return false caused the item to work properly, but there was no 'item-used-animation' and blocks had onBlockActivated code called that I don't really want called.

 

 

 

What exactly causes this to happen? 

I didn't tried this, but what if you would return true on the client and return false on the server? Like:

 

if(world.isRemote)
    return true;
else
    return false;

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Its down to how MC reports clicking items/blocks. Its really shitty, but basically, you should just return false on the client side, and only do any real processing on the server.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

  • Author

Its down to how MC reports clicking items/blocks. Its really shitty, but basically, you should just return false on the client side, and only do any real processing on the server.

 

Yeah, I was afraid of that.  So returning true clientside will always prevent calling serverside. . .  Thanks. 

 

One follow-up question, minecraft plays a little animation with whatever item's in the player's hand when left-clicking on a block or right-clicking with certain items in hand -- notably when placing vines and lily-pads in the world (and, prior to 1.3.2, when onItemUse returned true).  Is there any way to play that animation when using my item, instead of the 'default' item-is-rightclicked-on-a-block one?

There is a method in EntityPlayer called swingItem() that triggers that animation. Just call that when the block is right clicked and viola!

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.