Add CraftMetaTileEntity, fixes the previously broken tests

This commit is contained in:
Thinkofdeath 2014-12-07 13:22:56 +00:00
parent fb445e5337
commit 44ea9d88c2
6 changed files with 156 additions and 26 deletions

View file

@ -83,6 +83,26 @@ public final class CraftItemFactory implements ItemFactory {
return meta instanceof CraftMetaEnchantedBook ? meta : new CraftMetaEnchantedBook(meta); return meta instanceof CraftMetaEnchantedBook ? meta : new CraftMetaEnchantedBook(meta);
case BANNER: case BANNER:
return meta instanceof CraftMetaBanner ? meta : new CraftMetaBanner(meta); return meta instanceof CraftMetaBanner ? meta : new CraftMetaBanner(meta);
case COMMAND:
case CHEST:
case TRAPPED_CHEST:
case FURNACE:
case HOPPER:
case MOB_SPAWNER:
case SIGN:
case BREWING_STAND_ITEM:
case JUKEBOX:
case FLOWER_POT_ITEM:
case DISPENSER:
case DROPPER:
if (meta instanceof CraftMetaTileEntity) {
CraftMetaTileEntity te = (CraftMetaTileEntity) meta;
if (te.material != material) {
return new CraftMetaTileEntity((CraftMetaTileEntity) null, material);
}
return meta;
}
return new CraftMetaTileEntity(meta, material);
default: default:
return new CraftMetaItem(meta); return new CraftMetaItem(meta);
} }

View file

@ -347,6 +347,19 @@ public final class CraftItemStack extends ItemStack {
return new CraftMetaEnchantedBook(item.getTag()); return new CraftMetaEnchantedBook(item.getTag());
case BANNER: case BANNER:
return new CraftMetaBanner(item.getTag()); return new CraftMetaBanner(item.getTag());
case COMMAND:
case CHEST:
case TRAPPED_CHEST:
case FURNACE:
case HOPPER:
case MOB_SPAWNER:
case SIGN:
case BREWING_STAND_ITEM:
case JUKEBOX:
case FLOWER_POT_ITEM:
case DISPENSER:
case DROPPER:
return new CraftMetaTileEntity(item.getTag(), CraftMagicNumbers.getMaterial(item.getItem()));
default: default:
return new CraftMetaItem(item.getTag()); return new CraftMetaItem(item.getTag());
} }

View file

@ -27,7 +27,6 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
CraftMetaBanner(CraftMetaItem meta) { CraftMetaBanner(CraftMetaItem meta) {
super(meta); super(meta);
blockEntityTag = null;
if (!(meta instanceof CraftMetaBanner)) { if (!(meta instanceof CraftMetaBanner)) {
return; return;
@ -40,7 +39,6 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
CraftMetaBanner(NBTTagCompound tag) { CraftMetaBanner(NBTTagCompound tag) {
super(tag); super(tag);
blockEntityTag = null;
if (!tag.hasKey("BlockEntityTag")) { if (!tag.hasKey("BlockEntityTag")) {
return; return;
@ -61,7 +59,6 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
CraftMetaBanner(Map<String, Object> map) { CraftMetaBanner(Map<String, Object> map) {
super(map); super(map);
blockEntityTag = null;
String baseStr = SerializableMeta.getString(map, BASE.BUKKIT, true); String baseStr = SerializableMeta.getString(map, BASE.BUKKIT, true);
if (baseStr != null) { if (baseStr != null) {

View file

@ -97,6 +97,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
static { static {
classMap = ImmutableMap.<Class<? extends CraftMetaItem>, String>builder() classMap = ImmutableMap.<Class<? extends CraftMetaItem>, String>builder()
.put(CraftMetaBanner.class, "BANNER") .put(CraftMetaBanner.class, "BANNER")
.put(CraftMetaTileEntity.class, "TILE_ENTITY")
.put(CraftMetaBook.class, "BOOK") .put(CraftMetaBook.class, "BOOK")
.put(CraftMetaSkull.class, "SKULL") .put(CraftMetaSkull.class, "SKULL")
.put(CraftMetaLeatherArmor.class, "LEATHER_ARMOR") .put(CraftMetaLeatherArmor.class, "LEATHER_ARMOR")
@ -196,20 +197,16 @@ class CraftMetaItem implements ItemMeta, Repairable {
static final ItemMetaKey ATTRIBUTES_UUID_HIGH = new ItemMetaKey("UUIDMost"); static final ItemMetaKey ATTRIBUTES_UUID_HIGH = new ItemMetaKey("UUIDMost");
@Specific(Specific.To.NBT) @Specific(Specific.To.NBT)
static final ItemMetaKey ATTRIBUTES_UUID_LOW = new ItemMetaKey("UUIDLeast"); static final ItemMetaKey ATTRIBUTES_UUID_LOW = new ItemMetaKey("UUIDLeast");
@Specific(Specific.To.NBT)
static final ItemMetaKey BLOCK_ENTITY_TAG = new ItemMetaKey("BlockEntityTag");
private String displayName; private String displayName;
private List<String> lore; private List<String> lore;
private Map<Enchantment, Integer> enchantments; private Map<Enchantment, Integer> enchantments;
private int repairCost; private int repairCost;
private final NBTTagList attributes; private final NBTTagList attributes;
protected NBTTagCompound blockEntityTag;
CraftMetaItem(CraftMetaItem meta) { CraftMetaItem(CraftMetaItem meta) {
if (meta == null) { if (meta == null) {
attributes = null; attributes = null;
blockEntityTag = null;
return; return;
} }
@ -225,7 +222,6 @@ class CraftMetaItem implements ItemMeta, Repairable {
this.repairCost = meta.repairCost; this.repairCost = meta.repairCost;
this.attributes = meta.attributes; this.attributes = meta.attributes;
this.blockEntityTag = meta.blockEntityTag;
} }
CraftMetaItem(NBTTagCompound tag) { CraftMetaItem(NBTTagCompound tag) {
@ -301,12 +297,6 @@ class CraftMetaItem implements ItemMeta, Repairable {
} else { } else {
attributes = null; attributes = null;
} }
if (tag.hasKeyOfType(BLOCK_ENTITY_TAG.NBT, 10)) {
blockEntityTag = tag.getCompound(BLOCK_ENTITY_TAG.NBT);
} else {
blockEntityTag = null;
}
} }
static Map<Enchantment, Integer> buildEnchantments(NBTTagCompound tag, ItemMetaKey key) { static Map<Enchantment, Integer> buildEnchantments(NBTTagCompound tag, ItemMetaKey key) {
@ -343,7 +333,6 @@ class CraftMetaItem implements ItemMeta, Repairable {
} }
attributes = null; attributes = null;
blockEntityTag = null;
} }
static Map<Enchantment, Integer> buildEnchantments(Map<String, Object> map, ItemMetaKey key) { static Map<Enchantment, Integer> buildEnchantments(Map<String, Object> map, ItemMetaKey key) {
@ -383,10 +372,6 @@ class CraftMetaItem implements ItemMeta, Repairable {
if (attributes != null) { if (attributes != null) {
itemTag.set(ATTRIBUTES.NBT, attributes); itemTag.set(ATTRIBUTES.NBT, attributes);
} }
if (blockEntityTag != null) {
itemTag.set(BLOCK_ENTITY_TAG.NBT, blockEntityTag);
}
} }
static NBTTagList createStringList(List<String> list) { static NBTTagList createStringList(List<String> list) {
@ -438,7 +423,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
@Overridden @Overridden
boolean isEmpty() { boolean isEmpty() {
return !(hasDisplayName() || hasEnchants() || hasLore() || hasAttributes() || hasRepairCost() || blockEntityTag != null); return !(hasDisplayName() || hasEnchants() || hasLore() || hasAttributes() || hasRepairCost());
} }
public String getDisplayName() { public String getDisplayName() {
@ -555,8 +540,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
&& (this.hasEnchants() ? that.hasEnchants() && this.enchantments.equals(that.enchantments) : !that.hasEnchants()) && (this.hasEnchants() ? that.hasEnchants() && this.enchantments.equals(that.enchantments) : !that.hasEnchants())
&& (this.hasLore() ? that.hasLore() && this.lore.equals(that.lore) : !that.hasLore()) && (this.hasLore() ? that.hasLore() && this.lore.equals(that.lore) : !that.hasLore())
&& (this.hasAttributes() ? that.hasAttributes() && this.attributes.equals(that.attributes) : !that.hasAttributes()) && (this.hasAttributes() ? that.hasAttributes() && this.attributes.equals(that.attributes) : !that.hasAttributes())
&& (this.hasRepairCost() ? that.hasRepairCost() && this.repairCost == that.repairCost : !that.hasRepairCost()) && (this.hasRepairCost() ? that.hasRepairCost() && this.repairCost == that.repairCost : !that.hasRepairCost());
&& (this.blockEntityTag != null ? that.blockEntityTag != null && this.blockEntityTag.equals(that.blockEntityTag) : that.blockEntityTag == null);
} }
/** /**
@ -582,7 +566,6 @@ class CraftMetaItem implements ItemMeta, Repairable {
hash = 61 * hash + (hasEnchants() ? this.enchantments.hashCode() : 0); hash = 61 * hash + (hasEnchants() ? this.enchantments.hashCode() : 0);
hash = 61 * hash + (hasAttributes() ? this.attributes.hashCode() : 0); hash = 61 * hash + (hasAttributes() ? this.attributes.hashCode() : 0);
hash = 61 * hash + (hasRepairCost() ? this.repairCost : 0); hash = 61 * hash + (hasRepairCost() ? this.repairCost : 0);
hash = 61 * hash + (blockEntityTag != null ? this.blockEntityTag.hashCode() : 0);
return hash; return hash;
} }

View file

@ -0,0 +1,117 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import net.minecraft.server.NBTTagCompound;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
public class CraftMetaTileEntity extends CraftMetaItem {
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKey BLOCK_ENTITY_TAG = new ItemMetaKey("BlockEntityTag");
final Material material;
private final NBTTagCompound blockEntityTag;
CraftMetaTileEntity(CraftMetaItem meta, Material material) {
super(meta);
this.material = material;
if (!(meta instanceof CraftMetaTileEntity)) {
blockEntityTag = null;
return;
}
CraftMetaTileEntity te = (CraftMetaTileEntity) meta;
this.blockEntityTag = te.blockEntityTag;
}
CraftMetaTileEntity(NBTTagCompound tag, Material material) {
super(tag);
this.material = material;
if (tag.hasKeyOfType(BLOCK_ENTITY_TAG.NBT, 10)) {
blockEntityTag = tag.getCompound(BLOCK_ENTITY_TAG.NBT);
} else {
blockEntityTag = null;
}
}
CraftMetaTileEntity(Map<String, Object> map) {
super(map);
material = Material.AIR; // TODO
blockEntityTag = null;
}
@Override
void applyToItem(NBTTagCompound tag) {
super.applyToItem(tag);
if (blockEntityTag != null) {
tag.set(BLOCK_ENTITY_TAG.NBT, blockEntityTag);
}
}
@Override
ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Object> builder) {
super.serialize(builder);
return builder;
}
@Override
int applyHash() {
final int original;
int hash = original = super.applyHash();
if (blockEntityTag != null) {
hash = 61 * hash + this.blockEntityTag.hashCode();
}
return original != hash ? CraftMetaTileEntity.class.hashCode() ^ hash : hash;
}
@Override
public boolean equalsCommon(CraftMetaItem meta) {
if (!super.equalsCommon(meta)) {
return false;
}
if (meta instanceof CraftMetaTileEntity) {
CraftMetaTileEntity that = (CraftMetaTileEntity) meta;
return Objects.equal(this.blockEntityTag, that.blockEntityTag);
}
return true;
}
@Override
boolean notUncommon(CraftMetaItem meta) {
return super.notUncommon(meta) && (meta instanceof CraftMetaTileEntity || blockEntityTag == null);
}
@Override
boolean isEmpty() {
return super.isEmpty() && blockEntityTag == null;
}
@Override
boolean applicableTo(Material type) {
switch(type){
case COMMAND:
case CHEST:
case TRAPPED_CHEST:
case FURNACE:
case HOPPER:
case MOB_SPAWNER:
case SIGN:
case BREWING_STAND_ITEM:
case JUKEBOX:
case FLOWER_POT_ITEM:
case DISPENSER:
case DROPPER:
return true;
}
return false;
}
}

View file

@ -207,11 +207,11 @@ public class ItemMetaTest extends AbstractTestingBase {
} }
); );
assertThat("Forgotten test?", providers, hasSize(ItemStackTest.COMPOUND_MATERIALS.length - 2 /* Normal item meta and skulls */)); assertThat("Forgotten test?", providers, hasSize(ItemStackTest.COMPOUND_MATERIALS.length - 3/* Normal item meta, skulls and tile entities */));
for (final StackProvider provider : providers) { for (final StackProvider provider : providers) {
// downCastTest(new BukkitWrapper(provider)); downCastTest(new BukkitWrapper(provider));
// downCastTest(new CraftWrapper(provider)); downCastTest(new CraftWrapper(provider));
} }
} }