Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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?

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.

  • Author
@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.

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.

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.

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.

  • Author

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.

1 minute ago, theishiopian said:

SO, for clarification, are there no problems with using for loops in event handlers?

No there isn't. 

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.

  • Author

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.

 

 

  • Author

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());

 

  • Author

Ok, this is very bizzare. Whenever I try to reference my hashtable, I get a null pointer exception. But whenever I do a null check on it, it returns false.

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)

  • Author

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.

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.

  • Author

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)

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.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.