Jump to content

[1.7.2] Randomizing works, with one minor flaw.


Godson

Recommended Posts

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.

Link to comment
Share on other sites

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.

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.