SPIGOT-2782: Custom Colors for Potions

This commit is contained in:
md_5 2016-11-18 09:49:54 +11:00
parent d8c6364c4c
commit 4f63973ebb
2 changed files with 46 additions and 8 deletions

View File

@ -12,7 +12,6 @@ import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.inventory.meta.MapMeta;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
@DelegateDeserialization(SerializableMeta.class)
@ -140,8 +139,6 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
@Override
public void setLocationName(String name) {
Preconditions.checkArgument(name != null, "name");
this.locName = name;
}
@ -157,8 +154,6 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
@Override
public void setColor(Color color) {
Preconditions.checkArgument(color != null, "color");
this.color = color;
}

View File

@ -6,9 +6,11 @@ import java.util.List;
import java.util.Map;
import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.NBTTagInt;
import net.minecraft.server.NBTTagList;
import org.apache.commons.lang.Validate;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.inventory.meta.PotionMeta;
@ -29,6 +31,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
static final ItemMetaKey DURATION = new ItemMetaKey("Duration", "duration");
static final ItemMetaKey SHOW_PARTICLES = new ItemMetaKey("ShowParticles", "has-particles");
static final ItemMetaKey POTION_EFFECTS = new ItemMetaKey("CustomPotionEffects", "custom-effects");
static final ItemMetaKey POTION_COLOR = new ItemMetaKey("CustomPotionColor", "custom-color");
static final ItemMetaKey ID = new ItemMetaKey("Id", "potion-id");
static final ItemMetaKey DEFAULT_POTION = new ItemMetaKey("Potion", "potion-type");
@ -36,6 +39,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
// is treated as the empty form of the meta because it represents an empty potion with no effect
private PotionData type = new PotionData(PotionType.UNCRAFTABLE, false, false);
private List<PotionEffect> customEffects;
private Color color;
CraftMetaPotion(CraftMetaItem meta) {
super(meta);
@ -44,6 +48,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
}
CraftMetaPotion potionMeta = (CraftMetaPotion) meta;
this.type = potionMeta.type;
this.color = potionMeta.color;
if (potionMeta.hasCustomEffects()) {
this.customEffects = new ArrayList<PotionEffect>(potionMeta.customEffects);
}
@ -54,6 +59,9 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
if (tag.hasKey(DEFAULT_POTION.NBT)) {
type = CraftPotionUtil.toBukkit(tag.getString(DEFAULT_POTION.NBT));
}
if (tag.hasKey(POTION_COLOR.NBT)) {
color = Color.fromRGB(tag.getInt(POTION_COLOR.NBT));
}
if (tag.hasKey(POTION_EFFECTS.NBT)) {
NBTTagList list = tag.getList(POTION_EFFECTS.NBT, 10);
int length = list.size();
@ -75,6 +83,11 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
super(map);
type = CraftPotionUtil.toBukkit(SerializableMeta.getString(map, DEFAULT_POTION.BUKKIT, true));
Color color = SerializableMeta.getObject(Color.class, map, POTION_COLOR.BUKKIT, true);
if (color != null) {
setColor(color);
}
Iterable<?> rawEffectList = SerializableMeta.getObject(Iterable.class, map, POTION_EFFECTS.BUKKIT, true);
if (rawEffectList == null) {
return;
@ -91,7 +104,13 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
@Override
void applyToItem(NBTTagCompound tag) {
super.applyToItem(tag);
tag.setString(DEFAULT_POTION.NBT, CraftPotionUtil.fromBukkit(type));
if (hasColor()) {
tag.setInt(POTION_COLOR.NBT, color.asRGB());
}
if (customEffects != null) {
NBTTagList effectList = new NBTTagList();
tag.set(POTION_EFFECTS.NBT, effectList);
@ -114,7 +133,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
}
boolean isPotionEmpty() {
return (type.getType() == PotionType.UNCRAFTABLE) && !(hasCustomEffects());
return (type.getType() == PotionType.UNCRAFTABLE) && !(hasCustomEffects() || hasColor());
}
@Override
@ -245,12 +264,30 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
return changed;
}
@Override
public boolean hasColor() {
return color != null;
}
@Override
public Color getColor() {
return color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
int applyHash() {
final int original;
int hash = original = super.applyHash();
if (type.getType() != PotionType.UNCRAFTABLE) {
hash = 73 * hash + type.hashCode();
hash = 73 * hash + type.hashCode();
}
if (hasColor()) {
hash = 73 * hash + color.hashCode();
}
if (hasCustomEffects()) {
hash = 73 * hash + customEffects.hashCode();
@ -266,7 +303,9 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
if (meta instanceof CraftMetaPotion) {
CraftMetaPotion that = (CraftMetaPotion) meta;
return type.equals(that.type) && (this.hasCustomEffects() ? that.hasCustomEffects() && this.customEffects.equals(that.customEffects) : !that.hasCustomEffects());
return type.equals(that.type)
&& (this.hasCustomEffects() ? that.hasCustomEffects() && this.customEffects.equals(that.customEffects) : !that.hasCustomEffects())
&& (this.hasColor() ? that.hasColor() && this.color.equals(that.color) : !that.hasColor());
}
return true;
}
@ -283,6 +322,10 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
builder.put(DEFAULT_POTION.BUKKIT, CraftPotionUtil.fromBukkit(type));
}
if (hasColor()) {
builder.put(POTION_COLOR.BUKKIT, getColor());
}
if (hasCustomEffects()) {
builder.put(POTION_EFFECTS.BUKKIT, ImmutableList.copyOf(this.customEffects));
}