summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorSenmori <thesenmori@gmail.com>2017-04-21 23:10:22 -0400
committermd_5 <git@md-5.net>2017-04-22 15:50:30 +1000
commitc19c2932bf14b62a6d31266c8ccd9003bfe05e91 (patch)
tree638c46b592c7c9d260e1d836ffb733a0da00aad2 /src/main
parent7b044defc0a7ab4dbf5fe22cdf3d885ce5de7572 (diff)
downloadcraftbukkit-c19c2932bf14b62a6d31266c8ccd9003bfe05e91.tar
craftbukkit-c19c2932bf14b62a6d31266c8ccd9003bfe05e91.tar.gz
craftbukkit-c19c2932bf14b62a6d31266c8ccd9003bfe05e91.tar.lz
craftbukkit-c19c2932bf14b62a6d31266c8ccd9003bfe05e91.tar.xz
craftbukkit-c19c2932bf14b62a6d31266c8ccd9003bfe05e91.zip
Create NBT TypeId helper class
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java29
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java5
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java3
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java5
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java3
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java15
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java3
-rw-r--r--src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java7
-rw-r--r--src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java22
9 files changed, 60 insertions, 32 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java
index 80f9ffa9..aa1a210a 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java
@@ -12,11 +12,12 @@ import org.bukkit.Material;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.configuration.serialization.DelegateDeserialization;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BannerMeta;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
-
+
static final ItemMetaKey BASE = new ItemMetaKey("Base", "base-color");
static final ItemMetaKey PATTERNS = new ItemMetaKey("Patterns", "patterns");
static final ItemMetaKey COLOR = new ItemMetaKey("Color", "color");
@@ -39,7 +40,7 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
CraftMetaBanner(NBTTagCompound tag) {
super(tag);
-
+
if (!tag.hasKey("BlockEntityTag")) {
return;
}
@@ -49,7 +50,7 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
base = entityTag.hasKey(BASE.NBT) ? DyeColor.getByDyeData((byte) entityTag.getInt(BASE.NBT)) : null;
if (entityTag.hasKey(PATTERNS.NBT)) {
- NBTTagList patterns = entityTag.getList(PATTERNS.NBT, 10);
+ NBTTagList patterns = entityTag.getList(PATTERNS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
for (int i = 0; i < Math.min(patterns.size(), 20); i++) {
NBTTagCompound p = patterns.get(i);
this.patterns.add(new Pattern(DyeColor.getByDyeData((byte) p.getInt(COLOR.NBT)), PatternType.getByIdentifier(p.getString(PATTERN.NBT))));
@@ -59,17 +60,17 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
CraftMetaBanner(Map<String, Object> map) {
super(map);
-
+
String baseStr = SerializableMeta.getString(map, BASE.BUKKIT, true);
if (baseStr != null) {
base = DyeColor.valueOf(baseStr);
}
-
+
Iterable<?> rawPatternList = SerializableMeta.getObject(Iterable.class, map, PATTERNS.BUKKIT, true);
if (rawPatternList == null) {
return;
}
-
+
for (Object obj : rawPatternList) {
if (!(obj instanceof Pattern)) {
throw new IllegalArgumentException("Object in pattern list is not valid. " + obj.getClass());
@@ -80,12 +81,12 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
@Override
void applyToItem(NBTTagCompound tag) {
super.applyToItem(tag);
-
+
NBTTagCompound entityTag = new NBTTagCompound();
if (base != null) {
entityTag.setInt(BASE.NBT, base.getDyeData());
}
-
+
NBTTagList newPatterns = new NBTTagList();
for (Pattern p : patterns) {
@@ -95,7 +96,7 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
newPatterns.add(compound);
}
entityTag.set(PATTERNS.NBT, newPatterns);
-
+
tag.set("BlockEntityTag", entityTag);
}
@@ -143,11 +144,11 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
public int numberOfPatterns() {
return patterns.size();
}
-
+
@Override
ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Object> builder) {
super.serialize(builder);
-
+
if(base != null){
builder.put(BASE.BUKKIT, base.toString());
}
@@ -189,14 +190,12 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
boolean notUncommon(CraftMetaItem meta) {
return super.notUncommon(meta) && (meta instanceof CraftMetaBanner || (patterns.isEmpty() && base == null));
}
-
-
+
@Override
boolean isEmpty() {
return super.isEmpty() && patterns.isEmpty() && base == null;
}
-
-
+
@Override
boolean applicableTo(Material type) {
return type == Material.BANNER;
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
index a90ac5df..8896bd46 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
@@ -55,6 +55,7 @@ import org.bukkit.craftbukkit.block.CraftShulkerBox;
import org.bukkit.craftbukkit.block.CraftSign;
import org.bukkit.craftbukkit.block.CraftSkull;
import org.bukkit.craftbukkit.block.CraftStructureBlock;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BlockStateMeta;
@DelegateDeserialization(CraftMetaItem.SerializableMeta.class)
@@ -84,7 +85,7 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
super(tag);
this.material = material;
- if (tag.hasKeyOfType(BLOCK_ENTITY_TAG.NBT, 10)) {
+ if (tag.hasKeyOfType(BLOCK_ENTITY_TAG.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
blockEntityTag = tag.getCompound(BLOCK_ENTITY_TAG.NBT);
} else {
blockEntityTag = null;
@@ -113,7 +114,7 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
@Override
void deserializeInternal(NBTTagCompound tag) {
- if (tag.hasKeyOfType(BLOCK_ENTITY_TAG.NBT, 10)) {
+ if (tag.hasKeyOfType(BLOCK_ENTITY_TAG.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
blockEntityTag = tag.getCompound(BLOCK_ENTITY_TAG.NBT);
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
index 520e7f0f..e67877d3 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
@@ -11,6 +11,7 @@ import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BookMeta;
import com.google.common.base.Strings;
@@ -75,7 +76,7 @@ public class CraftMetaBook extends CraftMetaItem implements BookMeta {
}
if (tag.hasKey(BOOK_PAGES.NBT) && handlePages) {
- NBTTagList pages = tag.getList(BOOK_PAGES.NBT, 8);
+ NBTTagList pages = tag.getList(BOOK_PAGES.NBT, CraftMagicNumbers.NBT.TAG_STRING);
for (int i = 0; i < pages.size(); i++) {
String page = pages.getString(i);
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java
index ae3341c4..05c13d30 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java
@@ -10,6 +10,7 @@ import net.minecraft.server.NBTTagList;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BookMeta;
import com.google.common.collect.ImmutableMap.Builder;
@@ -34,7 +35,7 @@ class CraftMetaBookSigned extends CraftMetaBook implements BookMeta {
}
if (tag.hasKey(BOOK_PAGES.NBT)) {
- NBTTagList pages = tag.getList(BOOK_PAGES.NBT, 8);
+ NBTTagList pages = tag.getList(BOOK_PAGES.NBT, CraftMagicNumbers.NBT.TAG_STRING);
for (int i = 0; i < pages.size(); i++) {
String page = pages.getString(i);
@@ -79,7 +80,7 @@ class CraftMetaBookSigned extends CraftMetaBook implements BookMeta {
));
}
itemData.set(BOOK_PAGES.NBT, list);
- }
+ }
itemData.setBoolean(RESOLVED.NBT, true);
if (generation != null) {
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
index 0f7da6b7..8bcc476e 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
@@ -17,6 +17,7 @@ import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific.To;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.FireworkMeta;
import com.google.common.collect.ImmutableList;
@@ -88,7 +89,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
return;
}
- NBTTagList fireworkEffects = fireworks.getList(EXPLOSIONS.NBT, 10);
+ NBTTagList fireworkEffects = fireworks.getList(EXPLOSIONS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
List<FireworkEffect> effects = this.effects = new ArrayList<FireworkEffect>(fireworkEffects.size());
for (int i = 0; i < fireworkEffects.size(); i++) {
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
index 2a93e40b..0ad60e84 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
@@ -25,6 +25,7 @@ import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.craftbukkit.Overridden;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.ItemMeta;
@@ -268,7 +269,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
}
if (display.hasKey(LORE.NBT)) {
- NBTTagList list = display.getList(LORE.NBT, 8);
+ NBTTagList list = display.getList(LORE.NBT, CraftMagicNumbers.NBT.TAG_STRING);
lore = new ArrayList<String>(list.size());
for (int index = 0; index < list.size(); index++) {
@@ -293,7 +294,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
if (tag.get(ATTRIBUTES.NBT) instanceof NBTTagList) {
NBTTagList save = null;
- NBTTagList nbttaglist = tag.getList(ATTRIBUTES.NBT, 10);
+ NBTTagList nbttaglist = tag.getList(ATTRIBUTES.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
for (int i = 0; i < nbttaglist.size(); ++i) {
if (!(nbttaglist.get(i) instanceof NBTTagCompound)) {
@@ -301,10 +302,10 @@ class CraftMetaItem implements ItemMeta, Repairable {
}
NBTTagCompound nbttagcompound = (NBTTagCompound) nbttaglist.get(i);
- if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_UUID_HIGH.NBT, 99)) {
+ if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_UUID_HIGH.NBT, CraftMagicNumbers.NBT.TAG_ANY_NUMBER)) {
continue;
}
- if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_UUID_LOW.NBT, 99)) {
+ if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_UUID_LOW.NBT, CraftMagicNumbers.NBT.TAG_ANY_NUMBER)) {
continue;
}
if (!(nbttagcompound.get(ATTRIBUTES_IDENTIFIER.NBT) instanceof NBTTagString) || !CraftItemFactory.KNOWN_NBT_ATTRIBUTE_NAMES.contains(nbttagcompound.getString(ATTRIBUTES_IDENTIFIER.NBT))) {
@@ -313,10 +314,10 @@ class CraftMetaItem implements ItemMeta, Repairable {
if (!(nbttagcompound.get(ATTRIBUTES_NAME.NBT) instanceof NBTTagString) || nbttagcompound.getString(ATTRIBUTES_NAME.NBT).isEmpty()) {
continue;
}
- if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_VALUE.NBT, 99)) {
+ if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_VALUE.NBT, CraftMagicNumbers.NBT.TAG_ANY_NUMBER)) {
continue;
}
- if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_TYPE.NBT, 99) || nbttagcompound.getInt(ATTRIBUTES_TYPE.NBT) < 0 || nbttagcompound.getInt(ATTRIBUTES_TYPE.NBT) > 2) {
+ if (!nbttagcompound.hasKeyOfType(ATTRIBUTES_TYPE.NBT, CraftMagicNumbers.NBT.TAG_ANY_NUMBER) || nbttagcompound.getInt(ATTRIBUTES_TYPE.NBT) < 0 || nbttagcompound.getInt(ATTRIBUTES_TYPE.NBT) > 2) {
continue;
}
@@ -350,7 +351,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
return null;
}
- NBTTagList ench = tag.getList(key.NBT, 10);
+ NBTTagList ench = tag.getList(key.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
Map<Enchantment, Integer> enchantments = new HashMap<Enchantment, Integer>(ench.size());
for (int i = 0; i < ench.size(); i++) {
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
index a90c6da3..4fec1676 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
@@ -13,6 +13,7 @@ import org.apache.commons.lang.Validate;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
@@ -63,7 +64,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
color = Color.fromRGB(tag.getInt(POTION_COLOR.NBT));
}
if (tag.hasKey(POTION_EFFECTS.NBT)) {
- NBTTagList list = tag.getList(POTION_EFFECTS.NBT, 10);
+ NBTTagList list = tag.getList(POTION_EFFECTS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
int length = list.size();
customEffects = new ArrayList<PotionEffect>(length);
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
index 8a11b878..b9c54472 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
@@ -9,6 +9,7 @@ import net.minecraft.server.NBTTagCompound;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.SkullMeta;
import com.google.common.collect.ImmutableMap.Builder;
@@ -37,9 +38,9 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
CraftMetaSkull(NBTTagCompound tag) {
super(tag);
- if (tag.hasKeyOfType(SKULL_OWNER.NBT, 10)) {
+ if (tag.hasKeyOfType(SKULL_OWNER.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
profile = GameProfileSerializer.deserialize(tag.getCompound(SKULL_OWNER.NBT));
- } else if (tag.hasKeyOfType(SKULL_OWNER.NBT, 8) && !tag.getString(SKULL_OWNER.NBT).isEmpty()) {
+ } else if (tag.hasKeyOfType(SKULL_OWNER.NBT, CraftMagicNumbers.NBT.TAG_STRING) && !tag.getString(SKULL_OWNER.NBT).isEmpty()) {
profile = new GameProfile(null, tag.getString(SKULL_OWNER.NBT));
}
}
@@ -53,7 +54,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
@Override
void deserializeInternal(NBTTagCompound tag) {
- if (tag.hasKeyOfType(SKULL_PROFILE.NBT, 10)) {
+ if (tag.hasKeyOfType(SKULL_PROFILE.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
profile = GameProfileSerializer.deserialize(tag.getCompound(SKULL_PROFILE.NBT));
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 492c4b55..574ec0ab 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -146,4 +146,26 @@ public final class CraftMagicNumbers implements UnsafeValues {
}
return matches;
}
+
+ /**
+ * This helper class represents the different NBT Tags.
+ * <p>
+ * These should match NBTBase#getTypeId
+ */
+ public static class NBT {
+
+ public static final int TAG_END = 0;
+ public static final int TAG_BYTE = 1;
+ public static final int TAG_SHORT = 2;
+ public static final int TAG_INT = 3;
+ public static final int TAG_LONG = 4;
+ public static final int TAG_FLOAT = 5;
+ public static final int TAG_DOUBLE = 6;
+ public static final int TAG_BYTE_ARRAY = 7;
+ public static final int TAG_STRING = 8;
+ public static final int TAG_LIST = 9;
+ public static final int TAG_COMPOUND = 10;
+ public static final int TAG_INT_ARRAY = 11;
+ public static final int TAG_ANY_NUMBER = 99;
+ }
}