mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-27 06:50:12 +01:00
SPIGOT-5158: Add SuspiciousStewMeta
This commit is contained in:
parent
9aa743049f
commit
03b145b300
5 changed files with 254 additions and 1 deletions
|
@ -263,6 +263,8 @@ public final class CraftItemFactory implements ItemFactory {
|
|||
return meta instanceof CraftMetaTropicalFishBucket ? meta : new CraftMetaTropicalFishBucket(meta);
|
||||
case CROSSBOW:
|
||||
return meta instanceof CraftMetaCrossbow ? meta : new CraftMetaCrossbow(meta);
|
||||
case SUSPICIOUS_STEW:
|
||||
return meta instanceof CraftMetaSuspiciousStew ? meta : new CraftMetaSuspiciousStew(meta);
|
||||
default:
|
||||
return new CraftMetaItem(meta);
|
||||
}
|
||||
|
|
|
@ -514,6 +514,8 @@ public final class CraftItemStack extends ItemStack {
|
|||
return new CraftMetaTropicalFishBucket(item.getTag());
|
||||
case CROSSBOW:
|
||||
return new CraftMetaCrossbow(item.getTag());
|
||||
case SUSPICIOUS_STEW:
|
||||
return new CraftMetaSuspiciousStew(item.getTag());
|
||||
default:
|
||||
return new CraftMetaItem(item.getTag());
|
||||
}
|
||||
|
|
|
@ -155,6 +155,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
|
|||
.put(CraftMetaKnowledgeBook.class, "KNOWLEDGE_BOOK")
|
||||
.put(CraftMetaTropicalFishBucket.class, "TROPICAL_FISH_BUCKET")
|
||||
.put(CraftMetaCrossbow.class, "CROSSBOW")
|
||||
.put(CraftMetaSuspiciousStew.class, "SUSPICIOUS_STEW")
|
||||
.put(CraftMetaItem.class, "UNSPECIFIC")
|
||||
.build();
|
||||
|
||||
|
@ -1410,7 +1411,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
|
|||
CraftMetaKnowledgeBook.BOOK_RECIPES.NBT,
|
||||
CraftMetaTropicalFishBucket.VARIANT.NBT,
|
||||
CraftMetaCrossbow.CHARGED.NBT,
|
||||
CraftMetaCrossbow.CHARGED_PROJECTILES.NBT
|
||||
CraftMetaCrossbow.CHARGED_PROJECTILES.NBT,
|
||||
CraftMetaSuspiciousStew.EFFECTS.NBT
|
||||
));
|
||||
}
|
||||
return HANDLED_TAGS;
|
||||
|
|
|
@ -0,0 +1,239 @@
|
|||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap.Builder;
|
||||
import net.minecraft.server.NBTTagCompound;
|
||||
import net.minecraft.server.NBTTagList;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.serialization.DelegateDeserialization;
|
||||
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
|
||||
import org.bukkit.inventory.meta.SuspiciousStewMeta;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
|
||||
public class CraftMetaSuspiciousStew extends CraftMetaItem implements SuspiciousStewMeta {
|
||||
|
||||
static final ItemMetaKey DURATION = new ItemMetaKey("EffectDuration", "duration");
|
||||
static final ItemMetaKey EFFECTS = new ItemMetaKey("Effects", "effects");
|
||||
static final ItemMetaKey ID = new ItemMetaKey("EffectId", "id");
|
||||
|
||||
private List<PotionEffect> customEffects;
|
||||
|
||||
CraftMetaSuspiciousStew(CraftMetaItem meta) {
|
||||
super(meta);
|
||||
if (!(meta instanceof CraftMetaSuspiciousStew)) {
|
||||
return;
|
||||
}
|
||||
CraftMetaSuspiciousStew stewMeta = ((CraftMetaSuspiciousStew) meta);
|
||||
if (stewMeta.hasCustomEffects()) {
|
||||
this.customEffects = new ArrayList<PotionEffect>(stewMeta.customEffects);
|
||||
}
|
||||
}
|
||||
|
||||
CraftMetaSuspiciousStew(NBTTagCompound tag) {
|
||||
super(tag);
|
||||
if (tag.hasKey(EFFECTS.NBT)) {
|
||||
NBTTagList list = tag.getList(EFFECTS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
|
||||
int length = list.size();
|
||||
customEffects = new ArrayList<PotionEffect>(length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
NBTTagCompound effect = list.getCompound(i);
|
||||
PotionEffectType type = PotionEffectType.getById(effect.getByte(ID.NBT));
|
||||
if (type == null) {
|
||||
continue;
|
||||
}
|
||||
int duration = effect.getInt(DURATION.NBT);
|
||||
customEffects.add(new PotionEffect(type, duration, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CraftMetaSuspiciousStew(Map<String, Object> map) {
|
||||
super(map);
|
||||
|
||||
Iterable<?> rawEffectList = SerializableMeta.getObject(Iterable.class, map, EFFECTS.BUKKIT, true);
|
||||
if (rawEffectList == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Object obj : rawEffectList) {
|
||||
if (!(obj instanceof PotionEffect)) {
|
||||
throw new IllegalArgumentException("Object in effect list is not valid. " + obj.getClass());
|
||||
}
|
||||
addCustomEffect((PotionEffect) obj, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void applyToItem(NBTTagCompound tag) {
|
||||
super.applyToItem(tag);
|
||||
|
||||
if (customEffects != null) {
|
||||
NBTTagList effectList = new NBTTagList();
|
||||
tag.set(EFFECTS.NBT, effectList);
|
||||
|
||||
for (PotionEffect effect : customEffects) {
|
||||
NBTTagCompound effectData = new NBTTagCompound();
|
||||
effectData.setByte(ID.NBT, ((byte) effect.getType().getId()));
|
||||
effectData.setInt(DURATION.NBT, effect.getDuration());
|
||||
effectList.add(effectData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean isEmpty() {
|
||||
return super.isEmpty() && isStewEmpty();
|
||||
}
|
||||
|
||||
boolean isStewEmpty() {
|
||||
return !hasCustomEffects();
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean applicableTo(Material type) {
|
||||
return type == Material.SUSPICIOUS_STEW;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CraftMetaSuspiciousStew clone() {
|
||||
CraftMetaSuspiciousStew clone = ((CraftMetaSuspiciousStew) super.clone());
|
||||
if (this.customEffects != null) {
|
||||
clone.customEffects = new ArrayList<PotionEffect>(this.customEffects);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomEffects() {
|
||||
return customEffects != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PotionEffect> getCustomEffects() {
|
||||
if (hasCustomEffects()) {
|
||||
return ImmutableList.copyOf(customEffects);
|
||||
}
|
||||
return ImmutableList.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addCustomEffect(PotionEffect effect, boolean overwrite) {
|
||||
Validate.notNull(effect, "Potion effect must not be null");
|
||||
|
||||
int index = indexOfEffect(effect.getType());
|
||||
if (index != -1) {
|
||||
if (overwrite) {
|
||||
PotionEffect old = customEffects.get(index);
|
||||
if (old.getDuration() == effect.getDuration()) {
|
||||
return false;
|
||||
}
|
||||
customEffects.set(index, effect);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (customEffects == null) {
|
||||
customEffects = new ArrayList<PotionEffect>();
|
||||
}
|
||||
customEffects.add(effect);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeCustomEffect(PotionEffectType type) {
|
||||
Validate.notNull(type, "Potion effect type must not be null");
|
||||
|
||||
if (!hasCustomEffects()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean changed = false;
|
||||
Iterator<PotionEffect> iterator = customEffects.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
PotionEffect effect = iterator.next();
|
||||
if (type.equals(effect.getType())) {
|
||||
iterator.remove();
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (customEffects.isEmpty()) {
|
||||
customEffects = null;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomEffect(PotionEffectType type) {
|
||||
Validate.notNull(type, "Potion effect type must not be null");
|
||||
return indexOfEffect(type) != -1;
|
||||
}
|
||||
|
||||
private int indexOfEffect(PotionEffectType type) {
|
||||
if (!hasCustomEffects()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < customEffects.size(); i++) {
|
||||
if (customEffects.get(i).getType().equals(type)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearCustomEffects() {
|
||||
boolean changed = hasCustomEffects();
|
||||
customEffects = null;
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
int applyHash() {
|
||||
final int original;
|
||||
int hash = original = super.applyHash();
|
||||
if (hasCustomEffects()) {
|
||||
hash = 73 * hash + customEffects.hashCode();
|
||||
}
|
||||
return original != hash ? CraftMetaSuspiciousStew.class.hashCode() ^ hash : hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean equalsCommon(CraftMetaItem meta) {
|
||||
if (!super.equalsCommon(meta)) {
|
||||
return false;
|
||||
}
|
||||
if (meta instanceof CraftMetaSuspiciousStew) {
|
||||
CraftMetaSuspiciousStew that = (CraftMetaSuspiciousStew) meta;
|
||||
|
||||
return (this.hasCustomEffects() ? that.hasCustomEffects() && this.customEffects.equals(that.customEffects) : !that.hasCustomEffects());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean notUncommon(CraftMetaItem meta) {
|
||||
return super.notUncommon(meta) && (meta instanceof CraftMetaPotion || isStewEmpty());
|
||||
}
|
||||
|
||||
@Override
|
||||
Builder<String, Object> serialize(Builder<String, Object> builder) {
|
||||
super.serialize(builder);
|
||||
|
||||
if (hasCustomEffects()) {
|
||||
builder.put(EFFECTS.BUKKIT, ImmutableList.copyOf(this.customEffects));
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
|
@ -354,6 +354,14 @@ public class ItemMetaTest extends AbstractTestingBase {
|
|||
cleanStack.setItemMeta(meta);
|
||||
return cleanStack;
|
||||
}
|
||||
},
|
||||
new StackProvider(Material.SUSPICIOUS_STEW) {
|
||||
@Override ItemStack operate(ItemStack cleanStack) {
|
||||
final CraftMetaSuspiciousStew meta = ((CraftMetaSuspiciousStew) cleanStack.getItemMeta());
|
||||
meta.addCustomEffect(PotionEffectType.CONFUSION.createEffect(1, 0), false);
|
||||
cleanStack.setItemMeta(meta);
|
||||
return cleanStack;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in a new issue