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

So I have a custom item that's a bit similar to the Lucky Blocks mod or the Pandora's Box mod, however my item has more bad outcomes than it does good outcomes, and the mod isn't solely based on that single item.

 

Anyways, I got a working randomizing method by registering "Chance" as Random. And then I used Chance.nextInt(3) == 0 to check for the first number.

 

According to what I've learned in Java, nextInt(int n) checks from 0 and the number I specified, except it excludes that number and just leaves the number before that.

 

So after filling in all three checks, I've got it to randomly do one of three things, however, there is a random chance that it does absolutely nothing.

 

Here's the Item's code so you can see what I've done, and maybe with that you can point out my mistake?

 

public class ItemUnstableEssence extends Item {

public ItemUnstableEssence() {
	super();
	setMaxStackSize(64);
	setCreativeTab(Mod1.tabNegative);
}

@SideOnly(Side.CLIENT)
    public boolean hasEffect(ItemStack par1ItemStack)
    {
        return true;
    }

Random Chance = new Random();

@SideOnly(Side.CLIENT)
    public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
    {
        par3List.add("The unstable power of Positive and Negative");
        par3List.add("essences fused into this essence.");
        par3List.add("Be warned, this unstable power can have");
        par3List.add("positive or negative effects.");
    }

 public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
   {

       if (!par3EntityPlayer.capabilities.isCreativeMode)
       {
           --par1ItemStack.stackSize;
       }
       if (!par2World.isRemote && Chance.nextInt(3) == 0)
       {
           par2World.createExplosion(null, par3EntityPlayer.lastTickPosX, par3EntityPlayer.lastTickPosY, par3EntityPlayer.lastTickPosZ, 5, true);
       }
       else if (!par2World.isRemote && Chance.nextInt(3) == 1){
    	   par3EntityPlayer.addExperienceLevel(3);
       }
       else if (!par2World.isRemote && Chance.nextInt(3) == 2) {
    	   par3EntityPlayer.addChatMessage(new ChatComponentTranslation("msg.Unstable_NothingMessage.txt"));
       }
       return par1ItemStack;
   }

}

I may ask questions asking for help, but I know what I'm doing. If my post is helpful in any way, give a "Thank You" in return, I'd really appreciate it.

You are not keeping the value returned, you keep calling the method, which does what it says.

(=Returns a random value each time you call it.)

  • Author

Ah, alright. I see the problem now, and I've fixed it.

 

In case anyone wants to know what I did:

 

I did this to keep the value of "Chance"

	Random random = new Random();
int Chance = random.nextInt(3);

 

Then when the action is executed, I made it so it gives "Chance" a new value.

 

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
   {

       if (!par3EntityPlayer.capabilities.isCreativeMode)
       {
           --par1ItemStack.stackSize;
       }
       if (!par2World.isRemote && Chance == 0)
       {
           par2World.createExplosion(null, par3EntityPlayer.lastTickPosX, par3EntityPlayer.lastTickPosY, par3EntityPlayer.lastTickPosZ, 5, true);
           Chance = random.nextInt(3);
       }
       else if (!par2World.isRemote && Chance == 1){
    	   par3EntityPlayer.addExperienceLevel(3);
    	   par3EntityPlayer.addChatMessage(new ChatComponentTranslation("msg.Unstable_Levels.txt"));
    	   Chance = random.nextInt(3);
       }
       else if (!par2World.isRemote && Chance == 2) {
    	   par3EntityPlayer.addChatMessage(new ChatComponentTranslation("msg.Unstable_NothingMessage.txt"));
    	   Chance = random.nextInt(3);
       }
       return par1ItemStack;
   }

 

Thanks for pointing out my mistake, GotoLink. I didn't really notice it too much. :P

I may ask questions asking for help, but I know what I'm doing. If my post is helpful in any way, give a "Thank You" in return, I'd really appreciate it.

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.