Jump to content

Recommended Posts

Posted

Hello MCF community! I have a little issue with 'black-listing items', from appearing when I right click this item. This item gives the player a random item, block, etc, out of the pool of items that minecraft provides. I have tried to make it so certain items don't appear in the pool. I have tried creating array lists of all of the items in Minecraft, also tried simply creating itemstacks and then doing a switch statement saying if they appear, the action will loop, no luck there. So, I'd like a little bit of help if possible.

 

package com.rien.nn.items;

import java.util.List;
import java.util.Random;

import com.rien.nn.Main;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ItemLootBag extends Item 
{	
public ItemLootBag() 
{
	this.setUnlocalizedName("itemLootBag");
	this.setTextureName("niknaks:itemLootBag");
	this.setCreativeTab(Main.NNTab);
	this.setMaxStackSize(16);
}

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) 
{	
	//Banned Items
	ItemStack netherPortalBlock = new ItemStack(Blocks.portal);
	ItemStack endPortalBlock = new ItemStack(Blocks.end_portal);
	ItemStack endPortalFrame = new ItemStack(Blocks.end_portal_frame);
	ItemStack fireBlock = new ItemStack(Blocks.fire);
	ItemStack waterBlock = new ItemStack(Blocks.water);

	int min = 1;
	int max = 3;
	Random r = new Random();
	int randNum = r.nextInt(max-min) + min;

	if (player.capabilities.isCreativeMode) 
	{
		player.dropPlayerItemWithRandomChoice(new ItemStack(getRandomItem(), randNum), false);
		return item;
	} 
	else 
	{
		--item.stackSize;
		player.dropPlayerItemWithRandomChoice(new ItemStack(getRandomItem(), randNum), false);
		return item;
	}
}

static Random rand = new Random();

public static Item getRandomItem() 
{
	Item i = null;

	int length = Item.itemRegistry.getKeys().toArray().length;
	Object select = Item.itemRegistry.getObjectById(ItemLootBag.rand.nextInt(length));

	if(select != null && select instanceof Item) 
	{
		i = (Item) select;
	}
	else 
	{
		return getRandomItem();
	}
	return i;
}
}

Posted

No offense but I am not sure how the code you posted would possibly work to prevent items.

 

Where do you check if the item is a banned item?  You would at least need to create a list or array of the banned items and test the random item against it.  Your code just gets a random item and never checks if it is banned.

 

I don't think you really need to create instances of each banned item either, that is what the instanceof operator is for (comparing to see if something is an instance).

 

Also, when you check if select instanceof Item you don't also check if it is null because instanceof won't match if it is null already. 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

I have two ideas on how to do this. My interests would lead me to use recursion. The other option is to use a do-

 

... Wait. You will need two for loops and recursion.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Posted

No offense but I am not sure how the code you posted would possibly work to prevent items.

 

Where do you check if the item is a banned item?  You would at least need to create a list or array of the banned items and test the random item against it.  Your code just gets a random item and never checks if it is banned.

 

I don't think you really need to create instances of each banned item either, that is what the instanceof operator is for (comparing to see if something is an instance).

 

Also, when you check if select instanceof Item you don't also check if it is null because instanceof won't match if it is null already.

No no no, that isn't the code. Those item stacks listed are just the items that are going to be banned. :P

There is no code for the blacklist yet.

Posted

I have two ideas on how to do this. My interests would lead me to use recursion. The other option is to use a do-

 

... Wait. You will need two for loops and recursion.

Recursion is a good idea, I will try and figure out a way to incorporate that without creating a loop that will throw stack traces. xD

 

But, that's for making the method run once an argument is true or false, or equal to something, I don't know what to do to check if the items I want banned, will be not drawn from the pool of items. Because I can't use -

if (getRandomItem == (new ItemStack(Blocks.portal)
{
getRandomItem();
}

 

or

 

if (ItemStack == (new ItemStack(Blocks.portal)
{
getRandomItem();
}

Posted

Okay, if the list of banned items is short, like it seems to be, I don't think you need an array or list of banned items but can just check them directly.

 

The point of the "recursion" is simply the idea that if you get a random item that is banned you'll want to choose again (and it may again find a banned item).

 

The Java while statement is meant for these sort of situations -- it keeps looping until it finds a condition that is true (in this case until you get a non-banned item.

 

So I suggest something like this (probably needs some debug as I didn't run this, but it should be right idea):

1. create a local boolean variable called something like foundItem and initialize to false.

2. create a local ItemStack variable called something like itemToDrop and initialize to null.

3. have a while loop that continues as long as !foundItem.

4. inside the loop, set itemToDrop to a generated random item and check if it is any of the banned items.  If they are not any banned item (probably should also check for null), then set founditem to true.

5. loop should exit with a valid itemToDrop so then you can just drop it.

 

Anyway, hope you get the idea.  Main point is that the while loop is intended for such repeated checks.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Okay, if the list of banned items is short, like it seems to be, I don't think you need an array or list of banned items but can just check them directly.

 

The point of the "recursion" is simply the idea that if you get a random item that is banned you'll want to choose again (and it may again find a banned item).

 

The Java while statement is meant for these sort of situations -- it keeps looping until it finds a condition that is true (in this case until you get a non-banned item.

 

So I suggest something like this (probably needs some debug as I didn't run this, but it should be right idea):

1. create a local boolean variable called something like foundItem and initialize to false.

2. create a local ItemStack variable called something like itemToDrop and initialize to null.

3. have a while loop that continues as long as !foundItem.

4. inside the loop, set itemToDrop to a generated random item and check if it is any of the banned items.  If they are not any banned item (probably should also check for null), then set founditem to true.

5. loop should exit with a valid itemToDrop so then you can just drop it.

 

Anyway, hope you get the idea.  Main point is that the while loop is intended for such repeated checks.

Didn't think to do it that way...guess I wasn't thinking you could create an ItemStack variable. It did indeed work, thank you very much. :)

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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