Commits
15 15 | * potion effect has a duration that it will last for, an amplifier that will |
16 16 | * enhance its effects, and a {@link PotionEffectType}, that represents its |
17 17 | * effect on an entity. |
18 18 | */ |
19 19 | "PotionEffect") | (
20 20 | public class PotionEffect implements ConfigurationSerializable { |
21 21 | private static final String AMPLIFIER = "amplifier"; |
22 22 | private static final String DURATION = "duration"; |
23 23 | private static final String TYPE = "effect"; |
24 24 | private static final String AMBIENT = "ambient"; |
25 + | private static final String PARTICLES = "has-particles"; |
25 26 | private final int amplifier; |
26 27 | private final int duration; |
27 28 | private final PotionEffectType type; |
28 29 | private final boolean ambient; |
29 30 | private final boolean particles; |
30 31 | |
31 32 | /** |
32 33 | * Creates a potion effect. |
33 34 | * @param type effect type |
34 35 | * @param duration measured in ticks, see {@link |
70 71 | public PotionEffect(PotionEffectType type, int duration, int amplifier) { |
71 72 | this(type, duration, amplifier, true); |
72 73 | } |
73 74 | |
74 75 | /** |
75 76 | * Constructor for deserialization. |
76 77 | * |
77 78 | * @param map the map to deserialize from |
78 79 | */ |
79 80 | public PotionEffect(Map<String, Object> map) { |
80 - | this(getEffectType(map), getInt(map, DURATION), getInt(map, AMPLIFIER), getBool(map, AMBIENT)); |
81 + | this(getEffectType(map), getInt(map, DURATION), getInt(map, AMPLIFIER), getBool(map, AMBIENT, false), getBool(map, PARTICLES, true)); |
81 82 | } |
82 83 | |
83 84 | private static PotionEffectType getEffectType(Map<?,?> map) { |
84 85 | int type = getInt(map, TYPE); |
85 86 | PotionEffectType effect = PotionEffectType.getById(type); |
86 87 | if (effect != null) { |
87 88 | return effect; |
88 89 | } |
89 90 | throw new NoSuchElementException(map + " does not contain " + TYPE); |
90 91 | } |
91 92 | |
92 93 | private static int getInt(Map<?,?> map, Object key) { |
93 94 | Object num = map.get(key); |
94 95 | if (num instanceof Integer) { |
95 96 | return (Integer) num; |
96 97 | } |
97 98 | throw new NoSuchElementException(map + " does not contain " + key); |
98 99 | } |
99 100 | |
100 - | private static boolean getBool(Map<?,?> map, Object key) { |
101 + | private static boolean getBool(Map<?,?> map, Object key, boolean def) { |
101 102 | Object bool = map.get(key); |
102 103 | if (bool instanceof Boolean) { |
103 104 | return (Boolean) bool; |
104 105 | } |
105 - | throw new NoSuchElementException(map + " does not contain " + key); |
106 + | return def; |
106 107 | } |
107 108 | |
108 109 | public Map<String, Object> serialize() { |
109 110 | return ImmutableMap.<String, Object>of( |
110 111 | TYPE, type.getId(), |
111 112 | DURATION, duration, |
112 113 | AMPLIFIER, amplifier, |
113 - | AMBIENT, ambient |
114 + | AMBIENT, ambient, |
115 + | PARTICLES, particles |
114 116 | ); |
115 117 | } |
116 118 | |
117 119 | /** |
118 120 | * Attempts to add the effect represented by this object to the given |
119 121 | * {@link LivingEntity}. |
120 122 | * |
121 123 | * @see LivingEntity#addPotionEffect(PotionEffect) |
122 124 | * @param entity The entity to add this effect to |
123 125 | * @return Whether the effect could be added |
128 130 | |
129 131 | |
130 132 | public boolean equals(Object obj) { |
131 133 | if (this == obj) { |
132 134 | return true; |
133 135 | } |
134 136 | if (!(obj instanceof PotionEffect)) { |
135 137 | return false; |
136 138 | } |
137 139 | PotionEffect that = (PotionEffect) obj; |
138 - | return this.type.equals(that.type) && this.ambient == that.ambient && this.amplifier == that.amplifier && this.duration == that.duration; |
140 + | return this.type.equals(that.type) && this.ambient == that.ambient && this.amplifier == that.amplifier && this.duration == that.duration && this.particles == that.particles; |
139 141 | } |
140 142 | |
141 143 | /** |
142 144 | * Returns the amplifier of this effect. A higher amplifier means the |
143 145 | * potion effect happens more often over its duration and in some cases |
144 146 | * has more effect on its target. |
145 147 | * |
146 148 | * @return The effect amplifier |
147 149 | */ |
148 150 | public int getAmplifier() { |
184 186 | return particles; |
185 187 | } |
186 188 | |
187 189 | |
188 190 | public int hashCode() { |
189 191 | int hash = 1; |
190 192 | hash = hash * 31 + type.hashCode(); |
191 193 | hash = hash * 31 + amplifier; |
192 194 | hash = hash * 31 + duration; |
193 195 | hash ^= 0x22222222 >> (ambient ? 1 : -1); |
196 + | hash ^= 0x22222222 >> (particles ? 1 : -1); |
194 197 | return hash; |
195 198 | } |
196 199 | |
197 200 | |
198 201 | public String toString() { |
199 202 | return type.getName() + (ambient ? ":(" : ":") + duration + "t-x" + amplifier + (ambient ? ")" : ""); |
200 203 | } |
201 204 | } |