Skip to content

Common API

Crystal Spider edited this page Jun 9, 2024 · 16 revisions

Unified Registration System

TODO

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.19.4 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

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.