Jump to content

Recommended Posts

Posted

I've made a block which has many versions of itself using metadata.

 

I used a for loop in my main mod class to add metadata-specific names to each block version, but I'm getting a null pointer error, for no apparent reason (There's obviously a reason :P)

 

Adding names (the first for loop works but isn't for the relevant block, the second for loop is the one that doesn't work):

 

for (int k = 0; k < Variables.getLastInt(ItemAnt.antMeta) + Variables.getLastInt(ItemAnt.typeMeta); k++){

		ItemStack ant = new ItemStack(itemAnt, 1, k);

		String name = ItemAnt.names[k];

		LanguageRegistry.addName(ant, name);

	}

	for (int i = 0; i < Variables.getLastInt(BlockAntHill.hillMeta); i++){

		ItemStack hill = new ItemStack(blockAntHill, 1, i);

		String name2 = BlockAntHill.hillNames[i];

		/* Line 168 --->*/ LanguageRegistry.addName(hill, BlockAntHill.hillNames[i]);

	}

 

The getLastInt method:

public static int getLastInt(int[] array){

	int a;

	a = array[array.length - 1];

	return a;

}

 

The hillMeta variable in BlockAntHill:

 

public static final int[] hillMeta = {0, 1, 2, 3, 4, 5, 6};

 

The hillNames variable in BlockAntHill:

private static final String NAME = "Ant Hill";

public static final String[] hillNames = {"Forest "+NAME, "Hillside "+NAME, "Desert "+NAME, "Swamp "+NAME, 
	"Plains "+NAME, "Jungle "+NAME, "Snowy "+NAME};

 

The blockAntHill object:

public static Block blockAntHill;
blockAntHill = new BlockAntHill(blockAntHillID);

 

The error:

---- Minecraft Crash Report ----
// Daisy, daisy...

Time: 6/10/13 10:09 PM
Description: Failed to start game

java.lang.NullPointerException
at cpw.mods.fml.common.registry.LanguageRegistry.addNameForObject(LanguageRegistry.java:110)
at cpw.mods.fml.common.registry.LanguageRegistry.addName(LanguageRegistry.java:120)
at vivadaylight3.myrmecology.common.Myrmecology.mainInit(Myrmecology.java:168)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:494)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)
at com.google.common.eventbus.EventBus.post(EventBus.java:267)
at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:165)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)
at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:45)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)
at com.google.common.eventbus.EventBus.post(EventBus.java:267)
at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:98)
at cpw.mods.fml.common.Loader.initializeMods(Loader.java:696)
at cpw.mods.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:213)
at net.minecraft.client.Minecraft.startGame(Minecraft.java:448)
at net.minecraft.client.MinecraftAppletImpl.startGame(MinecraftAppletImpl.java:44)
at net.minecraft.client.Minecraft.run(Minecraft.java:733)
at java.lang.Thread.run(Thread.java:680)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- System Details --
Details:
Minecraft Version: 1.5.2
Operating System: Mac OS X (x86_64) version 10.8.3
Java Version: 1.6.0_45, Apple Inc.
Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Apple Inc.
Memory: 36162168 bytes (34 MB) / 85000192 bytes (81 MB) up to 2138767360 bytes (2039 MB)
JVM Flags: 1 total; -Xmx2g
AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used
Suspicious classes: FML and Forge are installed
IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
FML: MCP v7.51 FML v5.2.17.716 Minecraft Forge 7.8.0.716 4 mods loaded, 4 mods active
mcp{7.44} [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized
FML{5.2.17.716} [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized->Initialized
Forge{7.8.0.716} [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized->Initialized
Myrmecology{0.0.1} [Myrmecology] (bin) Unloaded->Constructed->Pre-initialized->Errored
LWJGL: 2.4.2
OpenGL: Intel HD Graphics 3000 OpenGL Engine GL version 2.1 INTEL-8.10.44, Intel Inc.
Is Modded: Definitely; Client brand changed to 'fml,forge'
Type: Client (map_client.txt)
Texture Pack: Default
Profiler Position: N/A (disabled)
Vec3 Pool Size: ~~ERROR~~ NullPointerException: null

 

I appreciate any help, thanks in advance!

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Posted

Why... do you... never mind, I don't even want to ask.

 

You probably want to use i/k < myList.length if you want to register the name for all of the items. As it is you're leaving out the last one.

Your problem could be caused by a failure to register your block. Are you registering your block?

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

What is it you don't want to ask me about? I would like to optimise my code if that's the thing.

 

I solved it by registering the block prior to adding then name, that was the very obvious problem :P (noob moment)

 

Thanks for the help!

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Posted

The reason why I have the getLastInt() method is because I often forget how to do several things in Java, so I make methods so that I don't forget :)

 

If you mean native 'speaking' language, I was born in England and it's English.

 

If you mean native programming language, I come from a PHP, Css and HTML background.

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

Posted

The reason why I have the getLastInt() method is because I often forget how to do several things in Java, so I make methods so that I don't forget :)

 

If you mean native 'speaking' language, I was born in England and it's English.

 

If you mean native programming language, I come from a PHP, Css and HTML background.

I did mean programming... I'm a Python person myself, and I hate Java with all my heart. But I mod MC anyway, because I like modding and there's little I can do about the language.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

I like Java, but prefer PHP for its simplicity.

 

For some reason I can't register names for each metadata variant for my antHill block.

 

Each metadata variant appears in the creative menu but has the name "Snow Ant Hill" which is the last value of the hillNames array. I haven't been able to figure out why  :-\  ???

 

I event tried adding the names manually with several LanguageRegistry.addName(block variable, metadata name) but I got the same result.

 

Adding names in main class:

blockAntHill = new BlockAntHill(blockAntHillID);

for (int k = 0; k < Variables.getLastInt(BlockAntHill.hillMeta); k++){

		ItemStack ant = new ItemStack(blockAntHill, 1, k);

		LanguageRegistry.addName(ant, BlockAntHill.hillNames[k]);

	}

 

The BlockAntHill class:

package vivadaylight3.myrmecology.common.block;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import vivadaylight3.myrmecology.common.Myrmecology;
import vivadaylight3.myrmecology.common.item.ItemAnt;
import vivadaylight3.myrmecology.common.item.ItemExtractor;
import vivadaylight3.myrmecology.common.lib.Variables;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import net.minecraft.world.World;

public class BlockAntHill extends Block
{

public static final int[] hillMeta = {0, 1, 2, 3, 4, 5, 6};

private static final String NAME = "Ant Hill";

public static final String[] hillNames = {"Forest "+NAME, "Hillside "+NAME, "Desert "+NAME, "Swamp "+NAME, 
	"Plains "+NAME, "Jungle "+NAME, "Snowy "+NAME};

private static Icon[] icons;

private final String[] iconNames = {Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME, 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Forest", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Hills", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Desert",
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Swamp", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Plains", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Jungle", 
		Myrmecology.TEXTURE_PREFIX+Myrmecology.ITEM_ANT_NAME+"_Snow"};

public BlockAntHill(int par1)
{
	super(par1, Material.ground);
	setUnlocalizedName("antHill");
	setCreativeTab(Myrmecology.tabMyrmecology);
	setStepSound(Block.soundGrassFootstep);
	setHardness(1.0F);
	setResistance(1.0F);
}

@Override
public void registerIcons(IconRegister iconRegister){

	icons = Variables.iconsToArray(iconRegister, iconNames);

}

@Override
public Icon getIcon(int side, int metadata){

	return icons[metadata];

}

public String getUnlocalizedName(ItemStack itemStack)
    {

    	return this.getUnlocalizedName() + hillNames[itemStack.getItemDamage()];
    
    }

@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {

	for (int k = 0; k < hillMeta.length + 1; k++){

		par3List.add(new ItemStack(this, 1, k));

	}
    }

@Override
public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9){

	return true;

}

public void harvestBlock(World world, EntityPlayer entity, int x, int y, int z){

	if(!world.isRemote){

		ItemStack tool = entity.getCurrentEquippedItem();
		ItemStack extractor = new ItemStack(Myrmecology.itemExtractor);

		if(tool != null && tool == extractor){

			int meta = world.getBlockMetadata(x, y, z);

			for(int k = 0; k < hillMeta.length * 5 + 1; k++){

				if(meta == k){

					ItemStack drone = new ItemStack(Myrmecology.itemAnt, k + ItemAnt.typeMeta[1], 1);
					ItemStack queen = new ItemStack(Myrmecology.itemAnt, k + ItemAnt.typeMeta[0], 1);
					entity.dropPlayerItem(drone);
					entity.dropPlayerItem(queen);

				}

			}

		}
	}

}

}

 

Does anyone have any idea why? :/

"Thinking that coding is the nerdy IT guy at work rebooting your computer is like thinking that music is what happens when the piano tuner comes round." - Ed Rex

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

    • I know that this may be a basic question, but I am very new to modding. I am trying to have it so that I can create modified Vanilla loot tables that use a custom enchantment as a condition (i.e. enchantment present = item). However, I am having trouble trying to implement this; the LootItemRandomChanceWithEnchantedBonusCondition constructor needs a Holder<Enchantment> and I am unable to use the getOrThrow() method on the custom enchantment declared in my mod's enchantments class. Here is what I have so far in the GLM:   protected void start(HolderLookup.Provider registries) { HolderLookup.RegistryLookup<Enchantment> registrylookup = registries.lookupOrThrow(Registries.ENCHANTMENT); LootItemRandomChanceWithEnchantedBonusCondition lootItemRandomChanceWithEnchantedBonusCondition = new LootItemRandomChanceWithEnchantedBonusCondition(0.0f, LevelBasedValue.perLevel(0.07f), registrylookup.getOrThrow(*enchantment here*)); this.add("nebu_from_deepslate", new AddItemModifier(new LootItemCondition[]{ LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.DEEPSLATE).build(), LootItemRandomChanceCondition.randomChance(0.25f).build(), lootItemRandomChanceWithEnchantedBonusCondition }, OrichalcumItems.NEBU.get())); }   Inserting Enchantments.[vanilla enchantment here] actually works but trying to declare an enchantment from my custom enchantments class as [mod enchantment class].[custom enchantment] does not work even though they are both a ResourceKey and are registered in Registries.ENCHANTMENT. Basically, how would I go about making it so that a custom enchantment declared as a ResourceKey<Enchantment> of value ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath([modid], [name])), declared in a seperate enchantments class, can be used in the LootItemRandomChanceWithEnchantedBonusCondition constructor as a Holder? I can't use getOrThrow() because there is no level or block entity/entity in the start() method and it is running as datagen. It's driving me nuts.
    • Hi here is an update. I was able to fix the code so my mod does not crash Minecraft. Please understand that I am new to modding but I honestly am having a hard time understanding how anyone can get this to work without having extensive programming and debugging experience as well as searching across the Internet, multiple gen AI bots (claude, grok, openai), and examining source code hidden in the gradle directory and in various github repositories. I guess I am wrong because clearly there are thousands of mods so maybe I am just a newbie. Ok, rant over, here is a step by step summary so others can save the 3 days it took me to figure this out.   1. First, I am using forge 54.1.0 and Minecraft 1.21.4 2. I am creating a mod to add a shotgun to Minecraft 3. After creating the mod and compiling it, I installed the .jar file to the proper directory in Minecraft and used 1.21.4-forge-54.1.0 4. The mod immediately crashed with the error: Caused by: java.lang.NullPointerException: Item id not set 5. Using the stack trace, I determined that the Exception was being thrown from the net.minecraft.world.item.Item.Properties class 6. It seems that there are no javadocs for this class, so I used IntelliJ which was able to provide a decompiled version of the class, which I then examined to see the source of the error. Side question: Are there javadocs? 7. This method, specifically, was the culprit: protected String effectiveDescriptionId() {      return this.descriptionId.get(Objects.requireNonNull(this.id, "Item id not set"));  } 8. Now my quest was to determine how to set this.id. Looking at the same source file, I determined there was another method:  public Item.Properties setId(ResourceKey<Item> pId) {             this.id = pId;             return this;   } 9. So now, I need to figure out how to call setId(). This required working backwards a bit. Starting from the constructor, I stubbed out the variable p which is of type Item.Properties public static final RegistryObject<Item> SHOTGUN = ITEMS.register("shotgun", () -> new ShotgunItem(p)); Rather than putting this all on one line, I split it up for readability like this: private static final Item.Properties p = new Item.Properties().useItemDescriptionPrefix().setId(rk); Here is was the missing function, setId(), which takes a type of ResourceKey<Item>. My next problem is that due to the apparent lack of documentation (I tried searching the docs on this site) I could not determine the full import path to ResourceKey. I did some random searching on the Internet and stumbled across a Github repository which gave two clues: import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; Then I created the rk variable like this: private static ResourceKey<Item> rk = ResourceKey.create(Registries.ITEM, ResourceLocation.parse("modid:shotgunmod")); And now putting it all together in order: private static ResourceKey<Item> rk = ResourceKey.create(Registries.ITEM, ResourceLocation.parse("modid:shotgunmod")); private static final Item.Properties p = new Item.Properties().useItemDescriptionPrefix().setId(rk); public static final RegistryObject<Item> SHOTGUN = ITEMS.register("shotgun", () -> new ShotgunItem(p)); This compiled and the mod no longer crashes. I still have more to do on it, but hopefully this will save someone hours. I welcome any feedback and if I missed some obvious modding resource or tutorial that has this information. If not, I might suggest we add it somewhere for people trying to write a mod that doesn't crash. Thank you !!!  
    • I will keep adding to this thread with more information in case anyone can help, or at the very least I can keep my troubleshooting organized. I decided to downgrade to 54.1.0 in the hopes that this would fix the issue but it didn't. At least now I am on a "recommended" version. The crash report did confirm my earlier post that the Exception is coming from effectiveDescriptionId(). I'll continue to see if I can find a way to set the ID manually.   Caused by: java.lang.NullPointerException: Item id not set         at java.base/java.util.Objects.requireNonNull(Objects.java:259) ~[?:?]         at TRANSFORMER/[email protected]/net.minecraft.world.item.Item$Properties.effectiveDescriptionId(Item.java:465) ~[forge-1.21.4-54.1.0-client.jar!/:?]         at TRANSFORMER/[email protected]/net.minecraft.world.item.Item.<init>(Item.java:111) ~[forge-1.21.4-54.1.0-client.jar!/:?]         at TRANSFORMER/[email protected]/com.example.shotgunmod.ShotgunItem.<init>(ShotgunItem.java:19) ~[shotgunmod-1.0.0.jar!/:1.0.0]         at TRANSFORMER/[email protected]/com.example.shotgunmod.ModItems.lambda$static$0(ModItems.java:15) ~[shotgunmod-1.0.0.jar!/:1.0.0]         at TRANSFORMER/[email protected]/net.minecraftforge.registries.DeferredRegister$EventDispatcher.lambda$handleEvent      
    • It just randomly stop working after a rebooted my dedicated server PLEASE HELP!   com.google.gson   Failed to start the minecraft server com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive; at path $  
  • Topics

×
×
  • Create New...

Important Information

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