Commits
Doc authored and md_5 committed ade46cc79fe
1 + | package org.bukkit.craftbukkit.inventory.components.consumable.effects; |
2 + | |
3 + | import com.google.common.base.Preconditions; |
4 + | import com.google.common.collect.ImmutableList; |
5 + | import java.util.ArrayList; |
6 + | import java.util.LinkedHashMap; |
7 + | import java.util.List; |
8 + | import java.util.Map; |
9 + | import net.minecraft.world.effect.MobEffect; |
10 + | import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect; |
11 + | import org.bukkit.craftbukkit.inventory.ItemMetaKey; |
12 + | import org.bukkit.craftbukkit.inventory.SerializableMeta; |
13 + | import org.bukkit.craftbukkit.potion.CraftPotionUtil; |
14 + | import org.bukkit.inventory.meta.components.consumable.effects.ConsumableApplyEffects; |
15 + | import org.bukkit.potion.PotionEffect; |
16 + | |
17 + | public class CraftConsumableApplyEffects extends CraftConsumableEffect<ApplyStatusEffectsConsumeEffect> implements ConsumableApplyEffects { |
18 + | |
19 + | static final ItemMetaKey POTIONS = new ItemMetaKey("effects"); |
20 + | |
21 + | public CraftConsumableApplyEffects(ApplyStatusEffectsConsumeEffect consumeEffect) { |
22 + | super(consumeEffect); |
23 + | } |
24 + | |
25 + | public CraftConsumableApplyEffects(CraftConsumableApplyEffects consumeEffect) { |
26 + | super(consumeEffect); |
27 + | } |
28 + | |
29 + | public CraftConsumableApplyEffects(Map<String, Object> map) { |
30 + | super(map); |
31 + | |
32 + | List<PotionEffect> effectList = new ArrayList<>(); |
33 + | Iterable<?> rawEffectTypeList = SerializableMeta.getObject(Iterable.class, map, POTIONS, true); |
34 + | if (rawEffectTypeList == null) { |
35 + | return; |
36 + | } |
37 + | |
38 + | for (Object obj : rawEffectTypeList) { |
39 + | Preconditions.checkArgument(obj instanceof PotionEffect, "Object (%s) in effect list is not valid", obj.getClass()); |
40 + | effectList.add((PotionEffect) obj); |
41 + | } |
42 + | |
43 + | Float probability = SerializableMeta.getObject(Float.class, map, "probability", false); |
44 + | |
45 + | this.handle = new ApplyStatusEffectsConsumeEffect(effectList.stream().map(CraftPotionUtil::fromBukkit).toList(), probability); |
46 + | } |
47 + | |
48 + | |
49 + | public List<PotionEffect> getEffects() { |
50 + | List<MobEffect> mobEffectList = this.getHandle().effects(); |
51 + | return mobEffectList.stream().map(CraftPotionUtil::toBukkit).toList(); |
52 + | } |
53 + | |
54 + | |
55 + | public void setEffects(List<PotionEffect> list) { |
56 + | this.handle = new ApplyStatusEffectsConsumeEffect(list.stream().map(CraftPotionUtil::fromBukkit).toList()); |
57 + | } |
58 + | |
59 + | |
60 + | public PotionEffect addEffect(PotionEffect potionEffect) { |
61 + | List<MobEffect> mobEffectList = this.getHandle().effects(); |
62 + | mobEffectList.add(CraftPotionUtil.fromBukkit(potionEffect)); |
63 + | this.handle = new ApplyStatusEffectsConsumeEffect(mobEffectList, this.handle.probability()); |
64 + | return potionEffect; |
65 + | } |
66 + | |
67 + | |
68 + | public float getProbability() { |
69 + | return this.getHandle().probability(); |
70 + | } |
71 + | |
72 + | |
73 + | public void setProbability(float probability) { |
74 + | Preconditions.checkArgument(probability >= 0.0f && probability <= 1.0f, "Probability must be between 0.0f and 1.0f but is %s", probability); |
75 + | this.handle = new ApplyStatusEffectsConsumeEffect(this.getHandle().effects(), probability); |
76 + | } |
77 + | |
78 + | |
79 + | public Map<String, Object> serialize() { |
80 + | Map<String, Object> result = new LinkedHashMap<>(); |
81 + | result.put(POTIONS.BUKKIT, ImmutableList.copyOf(this.getEffects())); |
82 + | |
83 + | return result; |
84 + | } |
85 + | } |