Jump to content

[1.20.2] The target type of this expression must be a functional interface


Adil Yilan

Recommended Posts

I have updated mod that has previously worked with Forge for 1.20.1 to latest version of Forge for 1.20.2, and then code that was previously compiling properly started to show unusual message:

PlayerManager.java:107: error: incompatible types: Factory is not a functional interface PlayersSeenSavedData savedData = storage.get(PlayersSeenSavedData::load, SEENS_KEY);

The code looks like this:

	private static PlayersSeenSavedData tryLoadPlayersSeenData(DimensionDataStorage storage) {

		// Load saved data based on the key.
		PlayersSeenSavedData savedData = storage.get(PlayersSeenSavedData::load, SEENS_KEY);
		
		// IF: Data was never saved before.
		if(savedData == null) {
			savedData = PlayersSeenSavedData.create();
		}
		
		return savedData;
	}

I have checked method signature for get method of DimensionDataStorage for 1.20.1 and 1.20.2 and I cannot find any difference.

Any hints on what exactly has changed and why is my load method now rejected?

Here is the code for load method:

	public static PlayersSeenSavedData load(CompoundTag compoundTag) {
		
		// Load list of NBTs from server data.
		ListTag listTag = compoundTag.getList(KEY, Tag.TAG_COMPOUND);
		
		// Create new empty list that will hold all data.
		ArrayList<PlayerSeenData> playersData = new ArrayList<PlayerSeenData>();
		
		// Iterate through all NBTs.
		for(Tag tag : listTag) {
			
			// Deserialize NBT back to regular object.
			PlayerSeenData playerData = PlayerSeenData.deserialize((CompoundTag)tag);
			
			// Add object to array of data.
			playersData.add(playerData);
		}
		
		// Create new instance of saved data class and provide data that was loaded to it.
		return new PlayersSeenSavedData(playersData);
	}

 

Link to comment
Share on other sites

Alright, so the key for this is to implement a factory method in the class that extends SavedData:

public static SavedData.Factory<PlayersSeenSavedData> factory() {
    return new SavedData.Factory<>(PlayersSeenSavedData::new, PlayersSeenSavedData::load, DataFixTypes.PLAYER);
}

Once the factory method is implemented, it should be provided to .get method of DimensionDataStorage:

PlayersSeenSavedData savedData = storage.get(PlayersSeenSavedData.factory(), SEENS_KEY);

The only thing I am not sure of is usage of DataFixTypes.PLAYER.

What is this used for?

How to select proper DataFixType?

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.