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