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
{

@Listener
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);
		player.offer(newHealth);
	}
}

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

}
[/code]

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 org.spongepowered.mod.plugin.SpongeModPluginContainer@4caeb62f java.lang.NoSuchMethodError: org.spongepowered.api.entity.living.player.Player.getItemInHand()Ljava/util/Optional; at io.github.geotan00.fasteat.listeners.PlayerInteractListener.onPlayerInteraction(PlayerInteractListener.java:19) ~[PlayerInteractListener.class:?] at org.spongepowered.common.event.listener.SecondaryListener_PlayerInteractListener_onPlayerInteraction38.handle(Unknown Source) ~[?:?] at org.spongepowered.common.event.RegisteredListener.handle(RegisteredListener.java:95) ~[RegisteredListener.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:225) [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:208) [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:253) [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEventManager.java:238) [SpongeModEventManager.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.common.SpongeImpl.postEvent(SpongeImpl.java:138) [SpongeImpl.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.common.event.SpongeCommonEventFactory.callInteractBlockEventSecondary(SpongeCommonEventFactory.java:219) [SpongeCommonEventFactory.class:1.10.2-2002-5.0.0-BETA-1517] at org.spongepowered.common.event.SpongeCommonEventFactory.callInteractBlockEventSecondary(SpongeCommonEventFactory.java:209) [SpongeCommonEventFactory.class:1.10.2-2002-5.0.0-BETA-1517] at net.minecraft.server.management.PlayerInteractionManager.func_187251_a(PlayerInteractionManager.java:118) [lv.class:?] at net.minecraft.network.NetHandlerPlayServer.func_184337_a(NetHandlerPlayServer.java:679) [me.class:?] at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.func_148833_a(SourceFile:55) [jj.class:?] at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.func_148833_a(SourceFile:11) [jj.class:?] at org.spongepowered.common.network.PacketUtil.onProcessPacket(PacketUtil.java:119) [PacketUtil.class:1.10.2-2002-5.0.0-BETA-1517] at net.minecraft.network.PacketThreadUtil$1.redirect$onProcessPacket$0(SourceFile:39) [fl$1.class:?] at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13) [fl$1.class:?] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_92] at java.util.concurrent.FutureTask.run(Unknown 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.java:668) [MinecraftServer.class:?] at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:387) [ld.class:?] at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:613) [MinecraftServer.class:?] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:471) [MinecraftServer.class:?] at java.lang.Thread.run(Unknown 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 - 12.18.2.2105
I am running into this issue with ItemAuction Plugin

read the stacktrace its says whats wrong
java.lang.NoSuchMethodError: org.spongepowered.api.entity.living.player.Player.getItemInHand()Ljava/util/Optional;

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: