Jump to content

1.12 BreakEvent and HarvestDropsEvent conflict NPE


X_Khan_X

Recommended Posts

I have an autosmelt class

public class AutosmeltEvent {

	@SubscribeEvent(priority = EventPriority.LOW)
	public static void onAutosmeltUse(HarvestDropsEvent event) {
		EntityPlayer player = (EntityPlayer) event.getHarvester();
		ItemStack hand = player.inventory.getCurrentItem();
		Map<Enchantment, Integer> autosmelt = EnchantmentHelper.getEnchantments(hand);
		if (autosmelt.isEmpty() || !autosmelt.containsKey(InitEnchants.AUTOSMELT))
			return;
		int autosmeltlevel = autosmelt.get(InitEnchants.AUTOSMELT);
		Block block = event.getState().getBlock();
		@SuppressWarnings("unused")
		IBlockState state = event.getState();
		World world = event.getWorld();
		BlockPos pos = event.getPos();
		ItemStack smelt = FurnaceRecipes.instance().getSmeltingResult(new ItemStack(block));
		smelt.setCount(1);
		ItemStack out = smelt.copy();
		if (autosmeltlevel == 1) {
			if (smelt.getItem() == Item.getItemFromBlock(Blocks.AIR) && !player.capabilities.isCreativeMode) {
				world.destroyBlock(pos, true);
			}
			if (block != null) {
				world.setBlockToAir(pos);
				event.getDrops().clear();
				event.getDrops().add(out);
				player.addExperience(1);
			}
		}
	}
}

that works with the enchant and an excavate class

public class ExcavateEvent {

	@SubscribeEvent(priority = EventPriority.NORMAL)
	public static void onExcavateUse(BlockEvent.BreakEvent event) {
		EntityPlayer player = (EntityPlayer) event.getPlayer();
		ItemStack hand = player.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND);
		Map<Enchantment, Integer> excavate = EnchantmentHelper.getEnchantments(hand);
		if (excavate.isEmpty() || !excavate.containsKey(InitEnchants.EXCAVATE))
			return;
		int excavatelevel = excavate.get(InitEnchants.EXCAVATE);
		World world = event.getWorld();
		BlockPos pos = event.getPos();
		Block block = event.getState().getBlock();
		if (excavatelevel > 0) {
			for (int i = 0; i < (excavatelevel + 1); i++) {
				Iterable<BlockPos> it = BlockPos.getAllInBox(pos.add(-i, -i, -i), pos.add(i, i, i));
				for (BlockPos offset2 : it) {
					Block block2 = world.getBlockState(offset2).getBlock();
					if (block2 == block && block2 != Blocks.AIR) {
						try {
							world.destroyBlock(offset2, true);
							hand.damageItem(1, player);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
}

which gives me problems when I use the excavate enchant (without autosmelt on the pickaxe)

 

java.lang.NullPointerException: null
	at com.mod.modcore.events.enchantment.AutosmeltEvent.onAutosmeltUse(AutosmeltEvent.java:30) ~[AutosmeltEvent.class:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_17_AutosmeltEvent_onAutosmeltUse_HarvestDropsEvent.invoke(.dynamic) ~[?:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) [EventBus.class:?]
	at net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(ForgeEventFactory.java:321) [ForgeEventFactory.class:?]
	at net.minecraft.block.Block.dropBlockAsItemWithChance(Block.java:721) [Block.class:?]
	at net.minecraft.block.BlockOre.dropBlockAsItemWithChance(BlockOre.java:92) [BlockOre.class:?]
	at net.minecraft.block.Block.dropBlockAsItem(Block.java:710) [Block.class:?]
	at net.minecraft.world.World.destroyBlock(World.java:477) [World.class:?]
	at com.mod.modcore.events.enchantment.ExcavateEvent.onExcavateUse(ExcavateEvent.java:42) [ExcavateEvent.class:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_11_ExcavateEvent_onExcavateUse_BreakEvent.invoke(.dynamic) [?:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) [ASMEventHandler.class:?]
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) [EventBus.class:?]
	at net.minecraftforge.common.ForgeHooks.onBlockBreakEvent(ForgeHooks.java:838) [ForgeHooks.class:?]
	at net.minecraft.server.management.PlayerInteractionManager.tryHarvestBlock(PlayerInteractionManager.java:309) [PlayerInteractionManager.class:?]
	at net.minecraft.server.management.PlayerInteractionManager.blockRemoving(PlayerInteractionManager.java:261) [PlayerInteractionManager.class:?]
	at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:732) [NetHandlerPlayServer.class:?]
	at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:56) [CPacketPlayerDigging.class:?]
	at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:12) [CPacketPlayerDigging.class:?]
	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) [PacketThreadUtil$1.class:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_221]
	at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_221]
	at net.minecraft.util.Util.runTask(Util.java:53) [Util.class:?]
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:798) [MinecraftServer.class:?]
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743) [MinecraftServer.class:?]
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592) [MinecraftServer.class:?]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]

I know, the solution may be very very simple but I dont know why it throw me an error when breaking blocks

 

AutosmeltEvent line 30:

ItemStack hand = player.inventory.getCurrentItem();

 

ExcavateEvent line 42:

world.destroyBlock(offset2, true);

 

Link to comment
Share on other sites

32 minutes ago, X_Khan_X said:

@SuppressWarnings("unused") IBlockState state = event.getState();

Why?

 

32 minutes ago, X_Khan_X said:

NullPointerException: null at com.mod.modcore.events.enchantment.AutosmeltEvent.onAutosmeltUse(AutosmeltEvent.java:30)

Something is null on line 30.

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.

Link to comment
Share on other sites

14 minutes ago, Animefan8888 said:

Why?

 

Something is null on line 30.

The suppress warning is useless, need to remove the state.

 

The line 30 is the hand item

 

Edit: System.out.print(event.getHarvester()); prints me and null at the same time

Edited by X_Khan_X
Link to comment
Share on other sites

10 minutes ago, X_Khan_X said:

Edit: System.out.print(event.getHarvester()); prints me and null at the same time

Either:
a) one is client side and one is server side

b) two separate, unrelated events (tall grass getting broken fires a harvest drops event).

 

There might be a reason for this line in my code...

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/ores/OreEventHandler.java#L74

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

3 hours ago, X_Khan_X said:

The suppress warning is useless, need to remove the state.

Just remove the unused variable...

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

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.



×
×
  • Create New...

Important Information

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