Getting Held Item

I’m trying to make a plugin that implements a beta 1.7.3 eating system. I have a Spigot version that works but I wanted to make it for Sponge. I’m having problems with checking the item type. Maybe I am just doing something wrong but I’m doing it how the tutorial says and I’ve tried googling it as well. Here is the code.

[code]public class PlayerInteractListener

public void onPlayerInteraction(InteractBlockEvent.Secondary event, @Root Player player)
	if (isBeef(player.getItemInHand().orElse(null)))
		HealthData newHealth = player.getHealthData().set(Keys.HEALTH, player.get(Keys.HEALTH).get() + 1);

public boolean isBeef(ItemStack stack)
	ItemType type = stack.getItem();
	return type.equals(ItemTypes.COOKED_BEEF);


I left out the imports. Yes the listener is called in the main class and also working. However errors are being thrown at the type check.

What are the errors?

[22:57:27] [Server thread/ERROR] [Sponge/]: Could not pass InteractBlockEvent$Secondary$OffHand$Impl to [email protected] java.lang.NoSuchMethodError:; at io.github.geotan00.fasteat.listeners.PlayerInteractListener.onPlayerInteraction( ~[PlayerInteractListener.class:?] at org.spongepowered.common.event.listener.SecondaryListener_PlayerInteractListener_onPlayerInteraction38.handle(Unknown Source) ~[?:?] at org.spongepowered.common.event.RegisteredListener.handle( ~[RegisteredListener.class:1.10.2-2002-5.0.0-BETA-1517] at [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.common.SpongeImpl.postEvent( [SpongeImpl.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.common.event.SpongeCommonEventFactory.callInteractBlockEventSecondary( [SpongeCommonEventFactory.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.common.event.SpongeCommonEventFactory.callInteractBlockEventSecondary( [SpongeCommonEventFactory.class:1.10.2-2002-5.0.0-BETA-1517] at [lv.class:?] at [me.class:?] at [jj.class:?] at [jj.class:?] at [PacketUtil.class:1.10.2-2002-5.0.0-BETA-1517] at$1.redirect$onProcessPacket$0(SourceFile:39) [fl$1.class:?] at$ [fl$1.class:?] at java.util.concurrent.Executors$ Source) [?:1.8.0_92] at Source) [?:1.8.0_92] at net.minecraft.util.Util.func_181617_a(SourceFile:45) [h.class:?] at net.minecraft.server.MinecraftServer.func_71190_q( [MinecraftServer.class:?] at net.minecraft.server.dedicated.DedicatedServer.func_71190_q( [ld.class:?] at net.minecraft.server.MinecraftServer.func_71217_p( [MinecraftServer.class:?] at [MinecraftServer.class:?] at Source) [?:1.8.0_92]

My first thought is your not using Java 8

The only JDK and JREs I have are Java 8. If I’m not using Java 8 then there is a big problem somewhere else.

What version of Sponge are you using? The 1.9 builds have removed getItemInHand() recently because you can hold two items at once

Actually it’s been replaced with getItemInHand(HandType). I forgot about that

Looks like your running 5.0.0-BETA-1517 however your plugin is depending on an older API version.

Interesting. I am definitely using the latest 1.10 SpongeForge and I am also using the latest bleeding 5.0.0 of the API.

Your plugin wouldn’t compile if you were using API 5.0.0 since the method simply doesn’t exist. Make sure your IDE dependencies are correctly pointing to an updated 5.0.0 snapshot build.

I am 100% positive that I am using the latest 5.0.0 API, and the method is recognized in Eclipse.

Did you use 4.x in the past? If so you may need to do a dependency refresh because as @gabizou stated it would most definitely not compile

I never use 4.x I only started 2 days ago.

What build system are you using? If it’s gradle then post your build.gradle file.

I figured it out. In Maven, I had copied the Maven code from the tutorial (I’ve never worked with Maven before) which had Sponge dependency as 4.1.0. I didn’t think that really mattered but apparently it does.

Now, what do you put for the two hands? It doesn’t autofill HandType.

See the HandTypes class

It is common in SpongeAPI for XXType to have an associated XXTypes with all vanilla-known types.

How do i go about fixing this while running minecraft 1.10.2
forge 1.10.2 -
I am running into this issue with ItemAuction Plugin

read the stacktrace its says whats wrong

Method getItemInHand in the Player class which returns Optional is missing. Usually this happens when one or more java libraries are not up to date with application/sdk/…

Uh, no. It’s because getItemInHand takes a HandType in 5.0.0. This is a 4.1 plugin that they’re trying to run on 5.0. If it didn’t know what a class was, it’d say NoClassDefFoundError.

i would like to suggest you to read once again what i wrote :wink: