Offering data to a player

I would like to offer a player jump boost potion effect to a player:

@Plugin(id = "testplugin", name = "TestPlugin", version = "v1.0")
public class TestPlugin {
    @Listener
    public void onRespawnPlayer(RespawnPlayerEvent event) {
        event.getTargetEntity().getOrCreate(PotionEffectData.class).ifPresent(potionEffectData -> {
            PotionEffectData data = potionEffectData.addElement(PotionEffect.of(PotionEffectTypes.JUMP_BOOST, 1, 2));
            DataTransactionResult result = event.getTargetEntity().offer(data);
            System.out.println(result.toString());
        });
    }
}

The result is success. What am I doing wrong? I am aware that with the data api everything is a copy so I reassign the data back to the player. Even in the DataTransactionResult the potion effect is mentioned(toString()).

What is the problem if the result is success?

Actually, everything is a copy only if it’s immutable. The data can be operated on separately, and it applies back to the player.

Assuming your problem is the effect is not applying, my guess is that the time argument in PotionEffect.of is far too small for you to open your inventory / jump before it wears off. It’s also possible that the power argument is too small. Try increasing those two numbers.

The time argument is measured in ticks (1/20ths of a second). 1 tick is probably too short for you to notice anything. You’ll need to increase it.

Man… It was too late as I created this topic! Thanks for all your help! :slight_smile:

@Flibio @Socratic_Phoenix Yes, the effect wasn’t visible.
@pie_flavor Thanks for the clarification! Again, it was very late, sorry.
@JBYoshi @Socratic_Phoenix Yes, you are right! That was the problem. I assumed it would be seconds as the vanilla command /effect ... has. This should be stated somewhere in the JavaDocs, I will open an issue.

Now the offering works(To be precise: It worked before but now the data is appropriate).

The next problem is: The potion effect only takes effect, when the player rejoins! I know, the potion effect is given to player in the respawn event. But what way I try(death screen → direct respawn or death screen → title menu → rejoin → death screen → respawn), the effect is only applied when the player rejoins the server after respawning!
The interesting fact is: The potion effect counts down when it is applied. I assume this because when rejoining and getting the effect, the potion doesn’t have the “full given” time, it is always less than the given time! When I wait long enough before rejoining I don’t get the effect at all.

So I assume: The server gives the player the effect and counts the remaining time down. When the player rejoins the server sends for the first time the “potion effect packet” to the player and so is visible on client side for the first time.

I hope you understand the problem.

To try to workaround the above described bug, I used the command /effect .... Here the behavior is almost the same. The only difference in offering the data with the SpongeAPI is that the particles appear, when the “effect command” is executed by the plugin. That the actual effect is only visible for the client after a rejoin, is the same.

I had issues of a similar nature. What I did was delayed giving player the Potion effect 10 ticks.

1 Like

Thanks, that works! My code

I created an issue.