Mirror von
https://github.com/PaperMC/Paper.git
synchronisiert 2024-12-18 04:20:08 +01:00
Create NBT TypeId helper class
Dieser Commit ist enthalten in:
Ursprung
7b044defc0
Commit
c19c2932bf
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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++) {
|
||||
|
@ -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++) {
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren