Example #1
0
 /**
  * Retrieves a read/write structure for collections of watchable objects.
  *
  * <p>This modifier will automatically marshall between the visible WrappedWatchableObject and the
  * internal Minecraft WatchableObject.
  *
  * @return A modifier for watchable object list fields.
  */
 public StructureModifier<List<WrappedWatchableObject>> getWatchableCollectionModifier() {
   // Convert to and from the ProtocolLib wrapper
   return structureModifier.withType(
       Collection.class,
       BukkitConverters.getListConverter(
           MinecraftReflection.getWatchableObjectClass(),
           BukkitConverters.getWatchableObjectConverter()));
 }
Example #2
0
 /**
  * Retrieves a read/write structure for collections of chunk positions.
  *
  * <p>This modifier will automatically marshall between the visible ProtocolLib ChunkPosition and
  * the internal Minecraft ChunkPosition.
  *
  * @return A modifier for ChunkPosition list fields.
  */
 public StructureModifier<List<ChunkPosition>> getPositionCollectionModifier() {
   // Convert to and from the ProtocolLib wrapper
   return structureModifier.withType(
       Collection.class,
       BukkitConverters.getListConverter(
           MinecraftReflection.getChunkPositionClass(), ChunkPosition.getConverter()));
 }
Example #3
0
  /**
   * 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());
  }
Example #4
0
  /**
   * Retrieves a read/write structure for arrays of ItemStacks.
   *
   * <p>This modifier will automatically marshall between the Bukkit ItemStack and the internal
   * Minecraft ItemStack.
   *
   * @return A modifier for ItemStack array fields.
   */
  public StructureModifier<ItemStack[]> getItemArrayModifier() {

    final EquivalentConverter<ItemStack> stackConverter = BukkitConverters.getItemStackConverter();

    // Convert to and from the Bukkit wrapper
    return structureModifier.<ItemStack[]>withType(
        MinecraftReflection.getItemStackArrayClass(),
        BukkitConverters.getIgnoreNull(
            new EquivalentConverter<ItemStack[]>() {

              public Object getGeneric(Class<?> genericType, ItemStack[] specific) {
                Class<?> nmsStack = MinecraftReflection.getItemStackClass();
                Object[] result = (Object[]) Array.newInstance(nmsStack, specific.length);

                // Unwrap every item
                for (int i = 0; i < result.length; i++) {
                  result[i] = stackConverter.getGeneric(nmsStack, specific[i]);
                }
                return result;
              }

              @Override
              public ItemStack[] getSpecific(Object generic) {
                Object[] input = (Object[]) generic;
                ItemStack[] result = new ItemStack[input.length];

                // Add the wrapper
                for (int i = 0; i < result.length; i++) {
                  result[i] = stackConverter.getSpecific(input[i]);
                }
                return result;
              }

              @Override
              public Class<ItemStack[]> getSpecificType() {
                return ItemStack[].class;
              }
            }));
  }
Example #5
0
 /**
  * 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());
 }
Example #6
0
 /**
  * Retrieves a read/write structure for entity objects.
  *
  * <p>Note that entities are transmitted by integer ID, and the type may not be enough to
  * distinguish between entities and other values. Thus, this structure modifier MAY return null or
  * invalid entities for certain fields. Using the correct index is essential.
  *
  * @return A modifier entity types.
  */
 public StructureModifier<Entity> getEntityModifier(World world) {
   // Convert to and from the Bukkit wrapper
   return structureModifier.<Entity>withType(
       int.class, BukkitConverters.getEntityConverter(world));
 }
Example #7
0
 /**
  * Retrieves a read/write structure for data watchers.
  *
  * @return A modifier for data watchers.
  */
 public StructureModifier<WrappedDataWatcher> getDataWatcherModifier() {
   // Convert to and from the Bukkit wrapper
   return structureModifier.<WrappedDataWatcher>withType(
       MinecraftReflection.getDataWatcherClass(), BukkitConverters.getDataWatcherConverter());
 }
Example #8
0
 /**
  * Retrieves a read/write structure for the world type enum.
  *
  * <p>This modifier will automatically marshall between the Bukkit world type and the internal
  * Minecraft world type.
  *
  * @return A modifier for world type fields.
  */
 public StructureModifier<WorldType> getWorldTypeModifier() {
   // Convert to and from the Bukkit wrapper
   return structureModifier.<WorldType>withType(
       MinecraftReflection.getWorldTypeClass(), BukkitConverters.getWorldTypeConverter());
 }
Example #9
0
 /**
  * Retrieves a read/write structure for ItemStack.
  *
  * <p>This modifier will automatically marshall between the Bukkit ItemStack and the internal
  * Minecraft ItemStack.
  *
  * @return A modifier for ItemStack fields.
  */
 public StructureModifier<ItemStack> getItemModifier() {
   // Convert to and from the Bukkit wrapper
   return structureModifier.<ItemStack>withType(
       MinecraftReflection.getItemStackClass(), BukkitConverters.getItemStackConverter());
 }