Jump to content

Recommended Posts

Posted

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...

Posted

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.

Posted

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.

Posted

 

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...

Posted

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.

Posted

Yes. It can return a normal int, but any form of random will make it crash. :(

 

I can't think of any other way into making this happen. Damn shame, since I was going to try and release this for Easter. :/

 

Thanks for your help though.

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

Posted

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...

Posted

It's just the same error. I think it doesn't like my code returning a random. Which is stupid, considering that the random returns an int. :/

 

I have no idea how to fix this.

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • that happens every time I enter a new dimension.
    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
  • Topics

×
×
  • Create New...

Important Information

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