Skip to content

Common API

Crystal Spider edited this page Jul 7, 2024 · 12 revisions

Unified Registration System

Cobweb simplifies the process of registering custom game objects by allowing you to register them directly in the common module, eliminating the need to register them separately in each of your mod loader submodules.

This system is designed to resemble the one provided by NeoForge. To use it, instantiate a register for each type of game object you want to register. This register will be unique to your mod. Then, use this register to add each of your custom game objects of that type. Unlike NeoForge, you don't need to register the register itself to the event bus, Cobweb handles this automatically when you instantiate your register!

Registration example
public final class ItemRegistry {
  // Cobweb provides a utility to get a register for items and a few others.
  private static final CobwebRegister<Item> ITEMS = CobwebRegistry.ofItems(Constants.MOD_ID);

  // Use the Item register above to register any item you need, in this example a new SwordItem.
  public static final Supplier<SwordItem> CUSTOM_SWORD = ITEMS.register("custom_sword", () -> new SwordItem(Tiers.DIAMOND, 5, 0, new Item.Properties()));

  public static void register() {
    // Need to call this method in your CommonModLoader class to load this class.
  }
}
public final class EnchantmentRegistry {
  // Get a register without a specific utility method.
  private static final CobwebRegister<Enchantment> ENCHANTMENTS = CobwebRegistry.of(Registries.ENCHANTMENT, Constants.MOD_ID);

  // Use the Enchantment register above to register any enchantment you need, in this example a new ProtectionEnchantment.
  public static final Supplier<Enchantment> CUSTOM_ENCHANTMENT = ENCHANTMENTS.register("custom_enchantment", () -> new ProtectionEnchantment(Enchantment.Rarity.VERY_RARE, ProtectionEnchantment.Type.ALL, EquipmentSlot.OFFHAND));

  public static void register() {
    // Need to call this method in your CommonModLoader class to load this class.
  }
}
public final class CommonModLoader {
  public static void init() {
    ItemRegistry.register();
    EnchantmentRegistry.register();
  }
}

Unified Configuration System

If you create your mod with our Mod Generator, a configuration example using Cobweb system will already be included!

Cobweb provides three different classes depending on the side you wish to register your configuration on: CommonConfig, ClientConfig, and ServerConfig. However, it's highly suggested that you only use the CommonConfig.

A simple configuration class looks like this:

import it.crystalnest.cobweb.api.config.CommonConfig;
import net.neoforged.neoforge.common.ModConfigSpec;
// Use the import below instead of the one above for 1.20.1 and lower.
// import net.minecraftforge.common.ForgeConfigSpec;


public final class ModConfig extends CommonConfig {
  /**
   * Register your configuration within Cobweb system.
   */
  public static final ModConfig CONFIG = register(Constants.MOD_ID, ModConfig::new);

  /**
   * Config example value.
   * Make it private but not final.
   */
  private ModConfigSpec.BooleanValue example;

  private ModConfig(ModConfigSpec.Builder builder) {
    super(builder);
  }

  /**
   * This is the way to read any configuration value you declared within your config.
   *
   * @return the value of {@link #example} as read from the configuration file.
   */
  public static Boolean getExample() {
    return CONFIG.example.get();
  }

  /**
   * Define all your configuration items here.
   * Make sure you define all of your configuration items declared above, otherwise you will get a NPE!
   */
  @Override
  protected void define(ModConfigSpec.Builder builder) {
    example = builder.comment(" Config example value").define("example", true);
  }
}

And then it needs to be registered within the game:

public final class CommonModLoader {
  private CommonModLoader() {}

  /**
   * Register your configuration here.
   */
  public static void init() {
    ModConfig.CONFIG.register();
  }
}

That's it! You now have a working configuration for your end users to tweak!
To learn how to define your configuration values, refer to the NeoForge configuration documentation, as this system is built upon it.

Tool tiers

The Tool Tiers API is available only for Minecraft < 1.21, since Minecraft removed the concept of levels from tool tiers afterwards.

Forge and NeoForge offer a tool tier API, but it lacks some utility methods. Fabric, on the other hand, doesn't have an API at all.
To address these gaps, Cobweb introduces the TierUtils utility class, which provides the lacking utility methods and allows handling tool tiers within the common module.

  • NO_TIER
    Special Tier to represent no tier.
  • NO_TIER_REFERENCE
    Special string reference for NO_TIER.
  • getAllTiers()
    Returns a Collection<Tier> of all the tiers in the game.
  • getTier(ResourceLocation) / getTier(String)
    Returns the tool tier from the given reference.
    Returns null if not tier could be found.
    The string must be parsable into a valid ResourceLocation.
  • compare(Item, Item) / compare(TieredItem, TieredItem) / compare(Tier, Tier) / compare(ResourceLocation, ResourceLocation) / compare(String, String)
    Compares the two tiers, abiding to the usual compare semantics: 0 if the tiers are equal, < 0 if the first tier is lower than the second one, > 0 vice versa.
  • isIn(Collection<Tier>, Tier) / isIn(Collection<Tier>, ResourceLocation) / isIn(Collection<Tier>, String)
    Checks whether the given tier is in the given tier list.
  • getLevel(Item) / getLevel(TieredItem) / getLevel(Tier) / getLevel(ResourceLocation) / getLevel(String)
    Returns the numeric level of the given tool tier.
    The level can be the special value -1 if the reference is for NO_TIER or if the Item is not an instance of TieredItem.
  • matches(Tier, String)
    Checks whether the given reference represents the given tier.

In-game ID retrival

For both blocks and items, Cobweb provides a shorthand to retrieve their in-game IDs (e.g. Blocks.DIRT -> minecraft:dirt).

Blocks:

  • BlockUtils#getKey(Block)
    Returns the in-game ID of the given block as a ResourceLocation.
  • BlockUtils#getStringKey(Block)
    Returns the in-game ID of the given block as a string.

Items:

  • ItemUtils#getKey(Item)
    Returns the in-game ID of the given item as a ResourceLocation.
  • ItemUtils#getStringKey(Item)
    Returns the in-game ID of the given item as a string.

Platform model

To prevent code duplication across various projects, Cobweb also provides enums for the Environment and the Platform.