Add ItemMeta factory and interfaces. This adds BUKKIT-15

Included with ItemMeta is a new serializable class Color.

PotionEffects are now serializable.

By: Wesley Wolfe <weswolf@aol.com>
This commit is contained in:
Bukkit/Spigot
2012-12-17 01:16:28 -06:00
parent 2bf21e7e18
commit ac66053f35
25 changed files with 1656 additions and 189 deletions

View File

@@ -1,24 +1,101 @@
package org.bukkit.potion;
import java.util.Map;
import java.util.NoSuchElementException;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.entity.LivingEntity;
import com.google.common.collect.ImmutableMap;
/**
* Represents a potion effect, that can be added to a {@link LivingEntity}. A
* potion effect has a duration that it will last for, an amplifier that will
* enhance its effects, and a {@link PotionEffectType}, that represents its
* effect on an entity.
*/
public class PotionEffect {
@SerializableAs("PotionEffect")
public class PotionEffect implements ConfigurationSerializable {
private static final String AMPLIFIER = "amplifier";
private static final String DURATION = "duration";
private static final String TYPE = "effect";
private static final String AMBIENT = "ambient";
private final int amplifier;
private final int duration;
private final PotionEffectType type;
private final boolean ambient;
public PotionEffect(PotionEffectType type, int duration, int amplifier) {
/**
* Creates a potion effect.
*
* @param type effect type
* @param duration measured in ticks, see {@link PotionEffect#getDuration()}
* @param amplifier the amplifier, see {@link PotionEffect#getAmplifier()}
* @param ambient the ambient status, see {@link PotionEffect#isAmbient()}
*/
public PotionEffect(PotionEffectType type, int duration, int amplifier, boolean ambient) {
Validate.notNull(type, "effect type cannot be null");
this.type = type;
this.duration = duration;
this.amplifier = amplifier;
this.ambient = ambient;
}
/**
* Creates a potion affect. Assumes ambient is true.
*
* @param type Effect type
* @param duration measured in ticks
* @param amplifier the amplifier for the affect
* @see PotionEffect#PotionEffect(PotionEffectType, int, int, boolean)
*/
public PotionEffect(PotionEffectType type, int duration, int amplifier) {
this(type, duration, amplifier, true);
}
/**
* Constructor for deserialization.
*
* @param map the map to deserialize from
*/
public PotionEffect(Map<String, Object> map) {
this(getEffectType(map), getInt(map, DURATION), getInt(map, AMPLIFIER), getBool(map, AMBIENT));
}
private static PotionEffectType getEffectType(Map<?,?> map) {
int type = getInt(map, TYPE);
PotionEffectType effect = PotionEffectType.getById(type);
if (effect != null) {
return effect;
}
throw new NoSuchElementException(map + " does not contain " + TYPE);
}
private static int getInt(Map<?,?> map, Object key) {
Object num = map.get(key);
if (num instanceof Integer) {
return (Integer) num;
}
throw new NoSuchElementException(map + " does not contain " + key);
}
private static boolean getBool(Map<?,?> map, Object key) {
Object bool = map.get(key);
if (bool instanceof Boolean) {
return (Boolean) bool;
}
throw new NoSuchElementException(map + " does not contain " + key);
}
public Map<String, Object> serialize() {
return ImmutableMap.<String, Object>of(
TYPE, type.getId(),
DURATION, duration,
AMPLIFIER, amplifier,
AMBIENT, ambient
);
}
/**
@@ -38,18 +115,11 @@ public class PotionEffect {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
if (!(obj instanceof PotionEffect)) {
return false;
}
PotionEffect other = (PotionEffect) obj;
if (type == null) {
if (other.type != null) {
return false;
}
} else if (!type.equals(other.type)) {
return false;
}
return true;
PotionEffect that = (PotionEffect) obj;
return this.type.equals(that.type) && this.ambient == that.ambient && this.amplifier == that.amplifier && this.duration == that.duration;
}
/**
@@ -80,8 +150,27 @@ public class PotionEffect {
return type;
}
/**
* Makes potion effect produce more, translucent, particles.
*
* @return if this effect is ambient
*/
public boolean isAmbient() {
return ambient;
}
@Override
public int hashCode() {
return 31 + ((type == null) ? 0 : type.hashCode());
};
int hash = 1;
hash = hash * 31 + type.hashCode();
hash = hash * 31 + amplifier;
hash = hash * 31 + duration;
hash ^= 0x22222222 >> (ambient ? 1 : -1);
return hash;
}
@Override
public String toString() {
return type.getName() + (ambient ? ":(" : ":") + duration + "t-x" + amplifier + (ambient ? ")" : "");
}
}

View File

@@ -115,6 +115,14 @@ public abstract class PotionEffectType {
this.id = id;
}
/**
* Creates a PotionEffect from this PotionEffectType, applying duration modifiers and checks.
*
* @see PotionBrewer#createEffect(PotionEffectType, int, int)
* @param duration time in ticks
* @param amplifier the effect's amplifier
* @return a resulting potion effect
*/
public PotionEffect createEffect(int duration, int amplifier) {
return Potion.getBrewer().createEffect(this, duration, amplifier);
}