/** * Create a new NBT wrapper from a given type. * * @param <T> Type * @param type - the NBT type. * @param name - the name of the NBT tag. * @return The new wrapped NBT tag. * @throws FieldAccessException If we're unable to create the underlying tag. */ public static <T> NbtWrapper<T> ofWrapper(NbtType type, String name) { if (type == null) throw new IllegalArgumentException("type cannot be NULL."); if (type == NbtType.TAG_END) throw new IllegalArgumentException("Cannot create a TAG_END."); if (methodCreateTag == null) { Class<?> base = MinecraftReflection.getNBTBaseClass(); // Use the base class try { methodCreateTag = findCreateMethod(base, byte.class, String.class); methodCreateWithName = true; } catch (Exception e) { methodCreateTag = findCreateMethod(base, byte.class); methodCreateWithName = false; } } try { // Delegate to the correct version if (methodCreateWithName) return createTagWithName(type, name); else return createTagSetName(type, name); } catch (Exception e) { // Inform the caller throw new FieldAccessException( String.format("Cannot create NBT element %s (type: %s)", name, type), e); } }
/** * Retrieve a structure modifier that automatically marshalls between NBT wrappers and their NMS * counterpart. * * @param stack - the stack that will store the NBT compound. * @return The structure modifier. */ private static StructureModifier<NbtBase<?>> getStackModifier(ItemStack stack) { Object nmsStack = MinecraftReflection.getMinecraftItemStack(stack); if (itemStackModifier == null) { itemStackModifier = new StructureModifier<Object>(nmsStack.getClass(), Object.class, false); } // Use the first and best NBT tag return itemStackModifier .withTarget(nmsStack) .withType(MinecraftReflection.getNBTBaseClass(), BukkitConverters.getNbtConverter()); }
/** * Retrieves a read/write structure for NBT classes. * * @return A modifier for NBT classes. */ public StructureModifier<NbtBase<?>> getNbtModifier() { // Allow access to the NBT class in packet 130 return structureModifier.withType( MinecraftReflection.getNBTBaseClass(), BukkitConverters.getNbtConverter()); }