Class NMSWrapper<A,B>
- Type Parameters:
A
- the type of NMS wrapper to createB
- the type of context object required for wrapper creation
- Direct Known Subclasses:
NMSWorldWrapper
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 -
Method Summary
Modifier and TypeMethodDescriptionfinal int
Convenience method to convert a Minecraft version string to a formatted integer.final A
Retrieves the version-specific NMS wrapper, creating it if necessary.protected abstract A
Creates the appropriate NMS wrapper for the specified version and context.
-
Constructor Details
-
NMSWrapper
public NMSWrapper()
-
-
Method Details
-
get
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
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 fromNmsVersion.getFormattedNmsInteger()
b
- the context object containing information needed for wrapper creation- Returns:
- the wrapper instance appropriate for the given version and context
-
f
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
-