Jump to content

Recommended Posts

Posted

I need to loop through a list in an event handler (specifically for the HarvestDropsEvent), but when I use a for loop it throws a ticking world exception. Is there an accepted way of looping in event handlers?

Posted

Show what you tried.

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
@SubscribeEvent
	public void blockBreak(HarvestDropsEvent event)
	{
		World world = event.getWorld();
		if(!world.isRemote)
		{
			ForageTable table = ForageCraftMod.ForageDict.get(event.getState().getBlock());
			
			java.util.List<ForagePool> pools = table.getPools();
			
			for(int i = 0; i != pools.size(); i++)
			{
				ForagePool pool = table.getPool(i);
				
				if(CHANCE.nextFloat()<pool.getChance())
				{
					event.getDrops().add(new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation(pool.getItem())), pool.getAmount()));
				}
			}
		}
	}

ForageDict is a hashtable using blocks as keys and returning a foragetable object. foragetables are containers for lists of foragepools, which hold a set of primitive values for use in this code, which adds new drops to blocks via json files.

Posted
Quote

Caused by: java.lang.NullPointerException

Its on line 31, whichever line that is.

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
4 minutes ago, Draco18s said:

Its on line 31, whichever line that is.

Don't you mean 33?

Quote
Caused by: java.lang.NullPointerException

    at com.theishiopian.foragecraft.handler.BlockForageHandler.blockBreak(BlockForageHandler.java:33)

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Then table.getPools(); returned null and you didn't check for that.

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

It seems you are  right, just calling table.getPools(); crashes the game. Whats perplexing is its just a simple return function.

 

public List<ForagePool> getPools()
	{
		
		
		return this.table;
	}

this.table is a list of foragetables.

Posted

Ok, good to know. For some reason, my list is becoming null. The weird thing is that I can call from it elsewhere and get the correct result. Anyways, thanks for the clarification, that really helps narrow it down.

 

 

Posted

Also, what did you mean by "instantiated into an instance"? Do you mean initilized as an object? If so, I'm already doing that.

 

ForageTable table = ForageCraftMod.ForageDict.get(event.getState().getBlock());

 

Posted

A tip for combatting NPEs is to make everything that possibly can be final final. Then the compiler will let you know if you forgot to initialise your field.

Step through your code with the debugger and find out why it’s null

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted

Thanks for the tip. Im going to massively refactor this tomorrow. Ill also step through it with the debugger. I think it has to do with the way im creating the forage pools from json. Which is odd because it worked before. If i have any more issues tommorow, ill create a new thread. Night all.

Posted
27 minutes ago, theishiopian said:

Also, what did you mean by "instantiated into an instance"? Do you mean initilized as an object? If so, I'm already doing that.

 


ForageTable table = ForageCraftMod.ForageDict.get(event.getState().getBlock());

 

What if that block doesnt have a ForageTable? Also why are you using a Block and not a IBlockState for your key.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

I know it has a foragetable. I think the foragetables are getting set to null somewhere.

 

I hadnt considered using blockstates. Do they have a string representation like blocks do? (eg minecraft:dirt)

Posted
18 minutes ago, theishiopian said:

I hadnt considered using blockstates. Do they have a string representation like blocks do? (eg minecraft:dirt)

Not an explicit one. It would first specify the Block I'd "minecraft:dirt" and then it would specify the property name and then the value, both can be represented by strings.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
9 hours ago, theishiopian said:

Do they have a string representation like blocks do? (eg minecraft:dirt)

Its almost as if you've not actually played the game.

1) The F3 screen

2) The /give command

3) Blockstate JSON files (and the associated error messages when they aren't correct)

 

All three of these use string representations of blockstates.

 

Or there's just the sheer point that: anything can be turned into a string if you try hard enough.

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.

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.