Jump to content

Item returning random item when used? - Solved


StrangeOne101

Recommended Posts

I'm making an item that returns or just adds a random item from a list of ids to the players inventory. Everytime I use the item, the game crashes.

 

So far, I've tried...

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
	 ItemStack item = new ItemStack(this.randItem, 1, 0);
	 return item;
    }

 

and...

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
	 ItemStack item = new ItemStack(this.getRandomItemfromList(), 1, 0);
	 par1ItemStack.stackSize--;
	 par2EntityPlayer.inventory.addItemStackToInventory(item);
	 return true;
    }

 

Both end in a crash. Could someone please help me?

 

Thanks.

The enders are everywhere. You can't escape...

Link to comment
Share on other sites

Try this:

 

world.spawnEntityInWorld(new EntityItem(world, x, y, z, new ItemStack(Item.bow)));

 

Just replace Item.bow with the item you want to spawn.

 

Make sure to check that the world isn't remote, or you'll end up with a phantom item as well.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I tried it and it worked just fine.

 

What's the error?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

 

java.lang.NullPointerException
at so101.eastersurprise.eggs.ItemEasterEgg.getRandomItemByRarity(ItemEasterEgg.java:53)
at so101.eastersurprise.eggs.ItemEasterEgg.onItemUse(ItemEasterEgg.java:62)
at net.minecraft.item.ItemStack.tryPlaceItemIntoWorld(ItemStack.java:149)
at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerRightClick(PlayerControllerMP.java:396)
at net.minecraft.client.Minecraft.clickMouse(Minecraft.java:1312)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1796)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:831)
at net.minecraft.client.Minecraft.run(Minecraft.java:756)
at java.lang.Thread.run(Unknown Source)

 

 

At the moment, getRandomItemByRarity() only returns "random.nextInt(256)+1". I will actually add the ids I need later but I want to get this working first.

The enders are everywhere. You can't escape...

Link to comment
Share on other sites

Try just returning a flat value, rather than a random one.

 

In any case, the error is in that getItemByRarity function, not at the spawning it into the world part.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I've redone the way it chooses the random item now, through the config file's list of ids. By default, I've put only vanilla IDs in.

 

But you are right. I do need to post my code if I ever want this fixed.

 

[spoiler=ItemEasterEgg]

package so101.eastersurprise.eggs;
import java.util.List;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import net.minecraft.world.World;

import cpw.mods.fml.relauncher.*;
import so101.eastersurprise.Main;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
public class ItemEasterEgg extends Item 
{
protected Icon[] icon = new Icon[5];
private Random random;
private int randItem;

protected int[] items;

public ItemEasterEgg(int par1) 
{
	super(par1); 
	setCreativeTab(CreativeTabs.tabMaterials); 
	setMaxStackSize(1);
	setHasSubtypes(true);
}

/**Adds the texture from the directory stated**/
@Override
public void updateIcons(IconRegister ir)
{
	icon[0] = ir.registerIcon("EasterSurpriseMod:blue");
	icon[1] = ir.registerIcon("EasterSurpriseMod:yellow");
	icon[2] = ir.registerIcon("EasterSurpriseMod:green 2");
	icon[3] = ir.registerIcon("EasterSurpriseMod:pink");
	icon[4] = ir.registerIcon("EasterSurpriseMod:purple");
	//icon[5] = ir.registerIcon("EasterSurpriseMod:purple");
}

@SideOnly(Side.CLIENT)
public Icon getIconFromDamage(int i)
{
	//return icon[i*2+random.nextInt(1)];
	return icon[i];
}

public int getRandomItemByRarity()
{
	this.items[0] = 43;
	this.items[1] = 24;
	this.items[2] = 20;

	return items[random.nextInt(3)];
}

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
	if (par1ItemStack.getItemDamage()==4)
	{
		par2EntityPlayer.addStat(Main.EasterEggRareAch, 1);
	}
	 par2EntityPlayer.addExperience(15);
	 par2EntityPlayer.addScore(3+(this.getMetadata(this.itemID)*2));
	 RandomItemSelect ris = new RandomItemSelect();
	 ItemStack item = new ItemStack(ris.getItemFromRarity(par1ItemStack.getItemDamage()), 1, 0);
	 par1ItemStack.stackSize--;
	 par3World.spawnEntityInWorld(new EntityItem(par3World, par4, par5, par6,  item));
	 return true;
    }

@SideOnly(Side.CLIENT)
public void getSubItems(int itemID, CreativeTabs tab, List itemList)
{
	for(int i=0;i<5;i++)
	{
		itemList.add(new ItemStack(itemID,1,i)); 
	}
}
}

 

 

[spoiler=RandomItemSelect]

package so101.eastersurprise.eggs;

import java.util.Random;

import so101.eastersurprise.Main;
import so101.eastersurprise.client.ConfigEasterSurprise;

public class RandomItemSelect 
{
private int i;
private Random rand;

private ConfigEasterSurprise cc = Main.cc;

private int[] commonIDs;
private int commonIDLength;
private int[] uncommonIDs;
private int uncommonIDLength;
private int[] rareIDs;
private int rareIDLength;

public RandomItemSelect()
{
	commonIDLength = cc.toStringArray.length;
	uncommonIDLength = cc.toStringArray2.length;
	rareIDLength = cc.toStringArray3.length;
	commonIDs = cc.commonIDs;
	uncommonIDs = cc.commonIDs;
	rareIDs = cc.commonIDs;
}

protected int getCommonID()
{
	return commonIDs[rand.nextInt(commonIDLength)];
}

protected int getUncommonID()
{
	return uncommonIDs[rand.nextInt(uncommonIDLength)];
}

protected int getRareID()
{
	return rareIDs[rand.nextInt(rareIDLength)];
}

public int getItemFromRarity(int rarity)
{
	switch(rarity)
	{
		case 0:
			return getCommonID();
		case 1:
			return getCommonID();
		case 2:
			return getUncommonID();
		case 3:
			return getUncommonID();
		case 4:
			return getRareID();
	}
	return getCommonID();

}
}

 

 

[spoiler=ConfigEasterEggs]

package so101.eastersurprise.client;

import java.awt.List;
import java.lang.reflect.Array;

import net.minecraft.block.Block;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.Property;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

public class ConfigEasterSurprise 
{
public static int easterEggID;
public static String[] toStringArray;
public static String[] toStringArray2;
public static String[] toStringArray3;
public static int[] commonIDs;
public static int[] uncommonIDs;
public static int[] rareIDs;

public static void loadConfig(FMLPreInitializationEvent e)
{
	Configuration config = new Configuration(e.getSuggestedConfigurationFile()); //Gets the file

	config.load(); //Loads it

	/** Items **/

	Property easteregg; //This is a property, see below
	easteregg = config.getItem("Easter Egg ID", 28600); //This gets the property
	easteregg.comment = "Deafult ID for the Easter Eggs."; //This adds a comment
	easterEggID = easteregg.getInt(); //This gets the value


	String easterEggCommonIDs;
	easterEggCommonIDs = config.get(Configuration.CATEGORY_GENERAL, "Common IDs", "1,2,3,4,5,6,12,13,17,18,20,23,24,25,27,28,29,31,33,37,38,39,40,43,44,45,47,48,50,53,54,55,61,65,66,67,69,70,72,76,77,80,81,82,84,85,86,87,88,89,91,96,97,98,99,100,101,102,103,105,107,105,109,111,112,113,114,123,125,126,125,134,135,136,139,143,145,146,147,148,151,155,157,158,259,260,261,262,263,268,269,270,271,272,273,274,275,280,281,282,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,318,319,320,321,323,324,325,326,328,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,384,385,386,388,389,390,391,392,393,394,395,396,397,398,399,400,401,404,405,406,407,408,2256,2257,2258,2259,2260,2261,2262,2263,2264,2265,2266,2267,264,265,266,267").getString();
	config.addCustomCategoryComment(Configuration.CATEGORY_GENERAL, "All the item IDs that the easter eggs can give you. DO NOT leave spaces in the ID list or it won't work.");
	toStringArray = easterEggCommonIDs.split(",");
	commonIDs = new int[toStringArray.length];
	for(int i = 0; i < toStringArray.length; i++) 
	{
		try
		{
			commonIDs[i] = Integer.parseInt(toStringArray[i]); 
		}
		catch(NumberFormatException numberFormatException)  
		{
			numberFormatException.printStackTrace(); 
		}
	}
	  
	String easterEggUnCommonIDs;
	easterEggUnCommonIDs = config.get(Configuration.CATEGORY_GENERAL, "Uncommon IDs", "2,14,15,16,19,30,41,42,46,47,48,49,79,84,89,97,110,111,121,122,129,130,133,145,152,153,256,257,258,267,283,284,285,286,293,302,303,304,305,306,307,308,309,314,315,316,317,322,327,345,347,354,368,378,369,379,381,384,407").getString();
	//config.addCustomCategoryComment(Configuration.CATEGORY_GENERAL, "All the item IDs that uncommon easter eggs can give you.");
	toStringArray2 = easterEggCommonIDs.split(",");
	uncommonIDs = new int[toStringArray.length];
	for(int i = 0; i < toStringArray.length; i++) 
	{
		try
		{
			uncommonIDs[i] = Integer.parseInt(toStringArray[i]); 
		}
		catch(NumberFormatException numberFormatException)  
		{
		    numberFormatException.printStackTrace(); 
		}
	}
		  
	String easterEggRareIDs;
	easterEggRareIDs = config.get(Configuration.CATEGORY_GENERAL, "Rare IDs", "52,57,116,122,130,133,138,145,152,276,277,278,279,310,311,312,313,322,329,399").getString();
	//config.addCustomCategoryComment(Configuration.CATEGORY_GENERAL, "All the item IDs that common easter eggs can give you.");
	toStringArray3 = easterEggCommonIDs.split(",");
	rareIDs = new int[toStringArray.length];
	for(int i = 0; i < toStringArray.length; i++) 
	{
		try
		{
			rareIDs[i] = Integer.parseInt(toStringArray[i]); 
		}
		catch(NumberFormatException numberFormatException)  
		{
	    numberFormatException.printStackTrace(); 
		}
	}


	//General
	config.save(); 

}

}

 

 

Ask if you don't understand some parts.

The enders are everywhere. You can't escape...

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.