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

I made a chewing gum for my mod. It's not getting used up when you eat it. I wanted to introduce a chance that it's randomly swallowed. I tried using java.util.Random#nextInt and it seemed to work fine until I noticed the stacksize and burp sounds glitching. I'm assuming the random number on the client is not the same as on the server.

Here's my code:


public class ChewedGum extends ItemFood {
	
	private static final int CHANCE_SWALLOW_ONE_OF = 3;

	...
	
	@Override
	@Nullable
	public ItemStack onItemUseFinish(ItemStack stack, World worldIn,
			EntityLivingBase entityLiving) {

        if (entityLiving instanceof EntityPlayer){
        	EntityPlayer player = (EntityPlayer)entityLiving;
        	
        	if(new Random().nextInt(CHANCE_SWALLOW_ONE_OF) == 0) {
        		--stack.stackSize;
        		worldIn.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_PLAYER_BURP, SoundCategory.PLAYERS, 0.5F, worldIn.rand.nextFloat() * 0.1F + 0.9F);
        	}
        }
		return stack;
	}

	...
}

 

Is there a way to get the same random number on both ends?

 

Edited by grexter

Not sure exactly storing the seed in the NBT would work well.  Main reason is that setting the seed, and then asking for a random number will give the same random number over and over.  It may be better to have the server entirely in control, telling the client when to play the burp sound.

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.