Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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);
	}

 

  • Author

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?

DataFixers are used by Vanilla to update old (world/player/...) data to the new DataFormat used by newer versions.

You should not actually be required to use a DataFixType.

  • Author

@Luis_ST Well it requires me to pass that as the parameter.

This is the only way I was able to bypass the error I had originally reported.

Any suggestions on how to do this properly?

I have never used SavedData before, is it explicitly required or would it also be possible to use a capability?

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

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.