Class CommonUtils

java.lang.Object
net.xun.lib.common.api.util.CommonUtils

public class CommonUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    combineAsNamespacedID(String namespace, String... pathParts)
    Combines a namespace with path components into a namespaced ID string.
    static <T> net.minecraft.resources.ResourceKey<T>
    createKey(net.minecraft.resources.ResourceKey<? extends net.minecraft.core.Registry<T>> registry, String path)
    Creates a resource key from a registry and a path
    static net.minecraft.resources.ResourceLocation
    Retrieves the registry key for a given object by checking known registries.
    static String
    Gets the path component of the registry key for the given object.
    static String
    getRegistryID(net.minecraft.world.level.ItemLike itemLike)
    Gets the path component of the registry key for an ItemLike object.
    static net.minecraft.resources.ResourceLocation
    modLoc(String path)
    Creates a ResourceLocation using the auto-detected or manually set mod ID.
    static String
    namespacedID(String... pathParts)
    Generates a namespaced ID string using the current mod ID.

    Methods inherited from class java.lang.Object

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

    • combineAsNamespacedID

      public static String combineAsNamespacedID(String namespace, String... pathParts)
      Combines a namespace with path components into a namespaced ID string.

      The path components are joined with underscores, forming the path part of the ID. The result is formatted as "namespace:joined_path". For example:

      
       combineAsNamespacedID("mymod", "item", "example") // returns "mymod:item_example"
       
      Parameters:
      namespace - The namespace to use (typically a mod ID)
      pathParts - The components of the path to join with underscores
      Returns:
      The combined namespaced ID in standard "namespace:path" format
    • modLoc

      public static net.minecraft.resources.ResourceLocation modLoc(String path)
      Creates a ResourceLocation using the auto-detected or manually set mod ID.
      Parameters:
      path - The resource path (e.g., "items/example")
      Returns:
      Namespaced ResourceLocation
      Throws:
      IllegalStateException - If the mod ID has not been set or detected
    • namespacedID

      public static String namespacedID(String... pathParts)
      Generates a namespaced ID string using the current mod ID.
      Parameters:
      pathParts - The ID path components to join with underscores
      Returns:
      Combined ID string in "namespace:path" format
      Throws:
      IllegalStateException - If the mod ID has not been set or detected
    • getKey

      public static net.minecraft.resources.ResourceLocation getKey(Object obj)
      Retrieves the registry key for a given object by checking known registries.

      Supports objects registered in common registries such as blocks, items, entities, effects, and more. If the object's type isn't recognized, throws an exception.

      Parameters:
      obj - The registered object to get the key for
      Returns:
      The resource location key of the object
      Throws:
      IllegalArgumentException - If the object's type isn't in the supported registries
    • getRegistryID

      public static String getRegistryID(Object obj)
      Gets the path component of the registry key for the given object.

      Equivalent to calling getKey(obj).getPath().

      Parameters:
      obj - The registered object to get the ID for
      Returns:
      The path portion of the object's registry key
      Throws:
      IllegalArgumentException - If the object's type isn't supported
      See Also:
    • getRegistryID

      public static String getRegistryID(net.minecraft.world.level.ItemLike itemLike)
      Gets the path component of the registry key for an ItemLike object.

      Specifically handles items and blocks through the ItemLike interface.

      Parameters:
      itemLike - The item or block to get the registry ID for
      Returns:
      The path portion of the object's registry key
      Throws:
      IllegalArgumentException - If the object's type isn't supported
      See Also:
    • createKey

      public static <T> net.minecraft.resources.ResourceKey<T> createKey(net.minecraft.resources.ResourceKey<? extends net.minecraft.core.Registry<T>> registry, String path)
      Creates a resource key from a registry and a path
      Type Parameters:
      T - The type of the resource key
      Parameters:
      registry - Resource key of the registry
      path - The key path
      Returns:
      A resource key from the registry and the path