Error getting player inventory?

For some reason, when I try to get a player’s inventory when running a command I get the error:

org.spongepowered.api.entity.player.Player.getInventory()Lorg/spongepowered/api/item/inventory/types/CarriedInventory;
java.lang.NoSuchMethodError: org.spongepowered.api.entity.player.Player.getInventory()Lorg/spongepowered/api/item/inventory/types/CarriedInventory;

I’ve honestly looked a lot into this. I searched the API and traced the path, but can’t figure out why it’s not working. I’ve isolated it to the line Inventory playerInventory = p.getInventory();. My method within CommandCallable is below. Thanks for the help!

public Optional<CommandResult> process(CommandSource source,
			String arguments) throws CommandException {
	if (source instanceof Player) {
		Player p = (Player) source;
		ItemStack itemStack = game.getRegistry().getItemBuilder().itemType(ItemTypes.PLANKS).quantity(6).build();
		Inventory playerInventory = p.getInventory();
		Inventory firstSlot = playerInventory.query(Hotbar.class).query(EmptyInventory.class);
		firstSlot.set(itemStack);
		return Optional.of(CommandResult.success());
	} else {
		return null;
	}
}

It’s right: There isn’t a .getInventory() method on Player: https://spongepowered.github.io/SpongeAPI/

This might be a mistake or it might be called something different.

Try casting it to Human instead: That has .getInventory()

Human extends Carrier and Player extends Human so I’m not sure why .getInventory() isn’t in Player

@WetSponge that’s not how casting works. :stuck_out_tongue: When a Class does indeed extend another Class, it obtains its features, as well. In this situation, casting to Human wouldn’t make much difference.

@soccer66 It sounds as though the API you’re using to develop with is either older or newer than the implementation that you’re using.

Could you post your Sponge/Forge version, as well as what API you’re developing with? For the latest, you should be on 2.1-SNAPSHOT for the API.

You should find this lesson helpful, @WetSponge

What is inheritance?

2 Likes

I do understand inheritance however the Javadocs don’t show Player having getInventory() from Human

That’s because they get it from Carrier

But Human extends Carrier.

I really think you would, Oscar. :stuck_out_tongue:

In my pom.xml I have the following:

Group Id: org.spongepowered
Artifact Id: spongeapi
Version: 2.1-SNAPSHOT
Packaing: jar

I updated TO THIS this at some point yesterday, but no changes were really made. It still auto completes in eclipse that I can do player.getInventory(). Is the 2.1-SNAPSHOT the same as SpongeAPI 2.0 API? Because that’s what the github links to (as well as what @WetSponge linked). How do I properly update my SpongeAPI in eclipse, since I was using an older version? (Using Maven)

And I am currently using sponge-1.8-1446-2.1DEV-507 @FerusGrim. I feel like my API version is behind the sponge version. Is there a more updated API version?

2.1-snapshot is the most current Version, while 2.0 is obviously a bit older.

@Tzk So then why would eclipse be auto completing with a method that doesn’t exist? I guess it means for some reason my Maven SpongeAPI version didn’t update or something? Do you know how to do this?

Cant help you with that, sorry :frowning:

Maybe join #spongedev on IRC.esper.net and ASK there :wink:

Maven downloads from the repo, and then uses the artifact that it stores locally. Have you tried updating your Maven indices? If not, it may not have downloaded the most recent API from the repository.

How would I do that? :smile:

Try this.

Okay, I tried that, but it still states spongeapi-2.0.jar as a Maven Dependency after restarting eclipse. Are my pom.xml settings correct?

spongeapi-2.0.jar != spongeapi-2.1-SNAPSHOT.jar

Eclipse isn’t reading your POM correctly.

1 Like

Yeah it seems really messed up, I’m going to try just rebuilding my plugin workspace and I’ll let you know if that changes anything.

2 Likes

Okay I don’t really know what was wrong (something with the pom.xml), but I now have spongeapi-2.1-SNAPSHOT.jar as a dependency! Except it’s still saying player has a getInventory() method? Is this wrong? And am I doing something wrong with the Player p = (Player) source; casting line?