Is it possible to add potion effects to a player?

I’m rewriting one of my old Bukkit plugins and one of the features it had was allowing the sender to apply potion effects to a targetwith a command shown here:
target.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1200, 1));

So is there a way to do this with Sponge? Thanks in advance!

There is a PotionEffectBuilder in the sponge api

https://github.com/SpongePowered/SpongeAPI/blob/master/src/main/java/org/spongepowered/api/potion/PotionEffectBuilder.java

and then you can add it via the DataAPI for PotionEffects

Excuse my noobiness but how would I go about doing this?

Get the GameRegistry (with game.getRegistry()) - This contains methods to get all the various data builders, blocks, data types, etc.

GameRegistry has a method that allows you to get an instance of a builder, in this case registry.getBuilderOf(PotionEffectBuilder.class), which will return a live instance of PotionEffectBuilder.

Go through and customise the builder, and get yourself an instance of PotionEffect with builder.build() that you can apply.

Then get the entity you want to apply the effect to. All Entities, Blocks and Items are “data holders”, in that they have objects that give information about them.

So to get the PotionEffectData for the player, for example, use player.getOrCreate(PotionEffectData.class) - This means you want to get the potion effects currently applied.

Then go through, add the potion effect(s) like you would normally, but instead passing it to the data.

Finally, you want to appy this to the player. Use player.offer(effectData) to “offer” the data to the player - this is to safe guard against data sources (such as blocks) that don’t support the data type that you give it.

3 Likes

Here is a gist link. Line 46 is throwing a NullPointerException error and nothing looks null to me.

@Impervious

It’s possible that game is null, as that would cause an NPE at that line. Where are you passing in the game variable in your constructor from?

My game is not null. I did System.out.println(this.game == null); and it returned false in my console.

Could you post all of the code on GitHub?

@Zirconium actually, I belive the reason is that the getBuilderOf method is not yet implemented. However, I realised there’s actually an easier way - registry.getPotionEffectBuilder(), which AFAIK is implemented.

A lot of work is yet to be done implementing SpongeAPI. As such, there’s a lot of stuff that will throw errors when it’s not supposed to.

This is where Optional is supposed to come in, hinting to developers that the returned object could be null, so you can use optional.isPresent() to check whether it’s null or not.

Good point. I haven’t been keeping up with what’s implemented and what’s not lately.

Don’t forget the @Inject on the Game variable.

@ZephireNZ, I tried using the PotionEffectBuilder and I did this:

PotionEffectBuilder examplePotionEffect = gameRegistry.createPotionEffectBuilder()
    .potionType(PotionEffectTypes.SPEED)
    .duration(4)
    .amplifier(3)
    .build();

However, when I type .build(), my IDE says that they are “incompatible types”. Am I doing something wrong?

That’s because you’re trying to save the created PotionEffect into a variable for a PotionEffectBuilder.

1 Like