Class NMSWrapper<A,B>

java.lang.Object
com.kamikazejam.kamicommon.nms.wrappers.NMSWrapper<A,B>
Type Parameters:
A - the type of NMS wrapper to create
B - the type of context object required for wrapper creation
Direct Known Subclasses:
NMSWorldWrapper

public abstract class NMSWrapper<A,B> extends Object
Abstract base class for version-specific NMS wrapper factories.

This class extends the provider pattern to support NMS wrapper creation that depends on both the current Minecraft version and a context object. It provides lazy initialization of wrappers based on the current NMS version, while accepting an additional parameter for wrapper creation.

Unlike the simpler Provider<T> pattern, this wrapper factory pattern is designed for scenarios where the wrapper creation requires external context, such as wrapping existing Bukkit objects into their NMS equivalents or creating wrappers that depend on world state or configuration.

The wrapper is cached after first creation to avoid repeated version checks and reflection operations, improving performance for subsequent access.

Example usage:


 public class ChunkWrapper extends NMSWrapper<NMSChunk, Chunk> {
     @Override
     protected NMSChunk provide(int formattedNmsInteger, Chunk bukkitChunk) {
         if (formattedNmsInteger >= f("1.17")) {
             return new NMSChunk1_17(bukkitChunk);
         } else if (formattedNmsInteger >= f("1.13")) {
             return new NMSChunk1_13(bukkitChunk);
         } else {
             return new NMSChunkLegacy(bukkitChunk);
         }
     }
 }
 

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final int
    f(String mcVersion)
    Convenience method to convert a Minecraft version string to a formatted integer.
    final A
    get(B b)
    Retrieves the version-specific NMS wrapper, creating it if necessary.
    protected abstract A
    provide(int formattedNmsInteger, B b)
    Creates the appropriate NMS wrapper for the specified version and context.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NMSWrapper

      public NMSWrapper()
  • Method Details

    • get

      @NotNull public final A get(@NotNull B b)
      Retrieves the version-specific NMS wrapper, creating it if necessary.

      This method uses lazy initialization to create the wrapper on first access using the provided context object. Subsequent calls return the cached instance. The wrapper implementation is determined by calling provide(int, Object) with the current NMS version and the provided context.

      Note: The wrapper is cached after first creation, so the context object is only used during initial creation. If you need different wrappers for different contexts, use separate wrapper instances.

      Parameters:
      b - the context object required for wrapper creation
      Returns:
      the version-specific NMS wrapper instance
    • provide

      @NotNull protected abstract A provide(int formattedNmsInteger, @NotNull B b)
      Creates the appropriate NMS wrapper for the specified version and context.

      This method must be implemented by subclasses to provide version-specific logic for creating the appropriate wrapper implementation. The method receives both a formatted NMS integer for version comparison and a context object that provides necessary information for wrapper creation.

      Use the f(String) helper method to convert Minecraft version strings to formatted integers for comparison.

      Parameters:
      formattedNmsInteger - the formatted NMS version integer from NmsVersion.getFormattedNmsInteger()
      b - the context object containing information needed for wrapper creation
      Returns:
      the wrapper instance appropriate for the given version and context
    • f

      public final int f(String mcVersion)
      Convenience method to convert a Minecraft version string to a formatted integer.

      This helper method converts a Minecraft version string (e.g., "1.17.1", "1.13.2") to a formatted integer that can be used for version comparisons in the provide(int, Object) method.

      This method is primarily intended for use within provide(int, Object) implementations to create readable version threshold comparisons.

      Parameters:
      mcVersion - the Minecraft version string to convert (e.g., "1.17.1")
      Returns:
      the formatted integer representation of the version