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