Jump to content

[1.7.2] Giving entityPlayer a Random Item


SilentThief

Recommended Posts

Hello, I am having a bit of trouble giving the person right clicking my item, a random item out of the Minecraft Vanilla Items. Here's my code so far:

 

public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer entityPlayer) {

	if (entityPlayer.capabilities.isCreativeMode) {
		return itemStack;
	}else {

		--itemStack.stackSize;

		entityPlayer.inventory.addItemStackToInventory(new ItemStack(itemRand, 1));

	}

	return itemStack;
}

}

 

itemRand is a variable inside of the Minecraft Item class.

Link to comment
Share on other sites

Well, what are you trying to use this for, ultimately? If you change the item to a random one, then it will most likely end up being a vanilla item. And then you wouldn't be able to change it again from Item#onItemRightClick.

 

You could probably create a custom item that simply takes the texture of another item, changing that item randomly each time it is used.

I like to make mods, just like you. Here's one worth checking out

Link to comment
Share on other sites

itemRand is a Random object, meaning you use it to get random numbers - it's not an Item. You can use itemRand to get a random Item from the list of all registered items, or you could make your own list of items you want to give as rewards and select randomly from that. In either case, I suggest you start by brushing up on some basic Java, as it will make your journey much easier.

 

For starters, here is the documentation on the Random class. That will help you figure out how to get a random number ;)

Link to comment
Share on other sites

Osmthing you may be able to do is make a HashMap of all of the Items that can be awarded (this would filter out non-accessible items and give you more customization) Then you could get a random int, from itemRand and get the item stack that corresponds to that item in the HashMap.

Don't be afraid to ask question when modding, there are no stupid question! Unless you don't know java then all your questions are stupid!

Link to comment
Share on other sites

i think this will work good 8)

add this and replace itemrand with stack

Random r = new Random();
ItemStack stack = new ItemStack(Item.itemList[r.nextInt(Item.itemList.length) + 1], r.next(3 /*MAX QUANTITY*/) /* this will generate random quantity */)

hope this work!

Link to comment
Share on other sites

@Mecblader Thank you for your suggestion, I will try hash maps if I don't find a better solution to this.

 

@AlexStone There are a few issues with that code.

1. Where are you getting itemList from?

2. Where are you getting r.next from?

I'm guessing r.nextInt();

Link to comment
Share on other sites

Yea, using a random each time crashes the game.

 

I tried using the code AlexStone provided, with a custom itemList I created. It does crash the game.

 

Even if I were to do:

 

ItemStack stack = new ItemStack(ItemList.itemList[r.nextInt()];

It would still end up crashing the game.

 

I will try a few other things, see if they work.

Link to comment
Share on other sites

I haven't tested this code yet, so yeah:

public static Item getRandomItem() {
	Item i = null;
	Object[] objects = Item.itemRegistry.getKeys().toArray();
	Object select = objects[utilRF.RANDOM.nextInt(objects.length)];
	if(select instanceof Item) {
		i = (Item) select;
	} else {
		return getRandomItem();
	}
	return i;
}

 

UtilRF.RANDOM is a Random object.

Kain

Link to comment
Share on other sites

While it is nice to use elegant coding, or impressive to use tricky coding, sometimes I think it is best to just do something simple and "brute force".  Why not just do use a random int to pick from a case statement and just list out all the items you want to have possible.  Sure it will be fairly long list (although not really that long), but it will work very simply and should perform well too.  The nice thing is that it is gives full control and won't have any weird occasional issues where you pick some unusable item out of the gameregistry.

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

Link to comment
Share on other sites

Define a new List where you store/cache all possible items:

private List<Item> filteredItems = null;

 

In your onItemUse method, do this:

- If the list is uninitialized, initialize it and fill the array with all items:

        if( filteredItems == null ) {
            filteredItems = new ArrayList<Item>();
            Iterators.addAll(filteredItems, Iterators.filter(Iterators.forArray(Item.itemsList), Predicates.notNull()));
        }

The last statement in the block is basically, it filters the itemsList array by throwing out any null value which are in there (basically all unassigned/free items) and adds them to your filteredItems list.

 

- After that, get a random element from your filteredItems list and make a new ItemStack out of that element:

        ItemStack stack = null;
        if( items.size() > 0 ) {
            stack = new ItemStack(items.get(par2EntityPlayer.getRNG().nextInt(items.size())), 1);
        }

 

The only problem I see is that some items have multiple metadata values as subtypes (dyes and potions are the most prominent example) and you'll get only the first item subtype

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.

Link to comment
Share on other sites

It only returns it if the object isn't an instance of an item, which is unlikely.

For some reason it returns it everytime. And, why is there:

UtilRF.RANDOM.nextInt()?

Instead of UtilRF.nextInt()

 

I don't use UtilRF.nextInt() because UtilRF doesn't have a method called nextInt. I keep a static Random object in the class whenever I need to use one.

Kain

Link to comment
Share on other sites

Ok, I've gotten the method to work. It also returns blocks too, since each block has an ItemBlock version.

public static Item getRandomItem() {
	Item i = null;
	int length = Item.itemRegistry.getKeys().toArray().length;
	Object select = Item.itemRegistry.getObjectById(UtilRF.RANDOM.nextInt(length));
	if(select != null && select instanceof Item) {
		i = (Item) select;
	} else {
		return getRandomItem();
	}
	return i;
}

 

100% sure to work, tested.

Kain

Link to comment
Share on other sites

Ok, I've gotten the method to work. It also returns blocks too, since each block has an ItemBlock version.

public static Item getRandomItem() {
	Item i = null;
	int length = Item.itemRegistry.getKeys().toArray().length;
	Object select = Item.itemRegistry.getObjectById(UtilRF.RANDOM.nextInt(length));
	if(select != null && select instanceof Item) {
		i = (Item) select;
	} else {
		return getRandomItem();
	}
	return i;
}

 

100% sure to work, tested.

I got confused on that UtilRF.RANDOM part, I wasn't thinking...lol

Didn't know UtilRF was the class. lol

 

Anyways, testing the code.

 

Doesn't seem as if the code is working, does nothing actually, here is my class:

package com.hex.lootbag;

import java.util.Random;

import com.hex.hexcore.HexCore;

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

public class ItemLootBag extends Item {

public ItemLootBag() {

	setUnlocalizedName("itemLootBag");
	setCreativeTab(CreativeTabs.tabAllSearch);
	setTextureName("lootbag:ItemLootBag");
	setMaxStackSize(1);

}

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {

	System.out.println("Working!");

	if (player.capabilities.isCreativeMode) {
		getRandomItem();
		return item;
	}else{
		--item.stackSize;
		getRandomItem();
		return item;
	}
}

static Random RANDOM = new Random();

public static Item getRandomItem() {
	Item i = null;
	int length = Item.itemRegistry.getKeys().toArray().length;
	Object select = Item.itemRegistry.getObjectById(ItemLootBag.RANDOM.nextInt(length));
	if(select != null && select instanceof Item) {
		i = (Item) select;
	} else {
		return getRandomItem();
	}
	return i;
}

}

Link to comment
Share on other sites

FTFY

package com.hex.lootbag;

import java.util.Random;

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

public class ItemLootBag extends Item {

public ItemLootBag() {

	setUnlocalizedName("itemLootBag");
	setCreativeTab(CreativeTabs.tabAllSearch);
	setTextureName("lootbag:ItemLootBag");
	setMaxStackSize(1);

}

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {

	System.out.println("Working!");

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

static Random RANDOM = new Random();

public static Item getRandomItem() {
	Item i = null;
	int length = Item.itemRegistry.getKeys().toArray().length;
	Object select = Item.itemRegistry.getObjectById(ItemLootBag.RANDOM.nextInt(length));
	if(select != null && select instanceof Item) {
		i = (Item) select;
	} else {
		return getRandomItem();
	}
	return i;
}

}

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.