Download TCDCommons API — Minecraft Mods — MetaMods

TCDCommons API

Active

Downloads

27

Last update

3 months ago

Versions

1.18.2 — 1.21.11
Client
Fabric
Forge
Neoforge
Quilt
Libraries
Utils

TCDCommons API

TCDCommons is an API library for Minecraft modding that features its own graphical interface system, various game events and hooks, as well as useful utilities for mod developers. The main purpose of this API is to optimize and accelerate the mod creation process, eliminating the need to repeatedly write the same code for similar tasks. It's important to note that this mod is a library and does not provide users with visible front-end features.

Dependencies

The table below shows the installation requirements for the mod. The term "client" refers to installing mods in the game through the Minecraft launcher, while "server" refers to installation on a server that players connect to through the multiplayer menu.

Requirements Client Server
Fabric API Required as of version 1.20.5 Required as of version 1.20.5

Purpose

The main goal of this API is to improve and speed up Minecraft mod development by eliminating repetitive code writing. Initially, the API was created to provide a more convenient graphical interface system, but over time it has expanded and now includes various events, hooks, and utilities commonly used in development.

Mods Using This API

This API is primarily used by developer TheCSDev for creating the Better Statistics Screen mod.

API Features

Client Side

  • GUI System: A unique and user-friendly interface system offering more customization options and flexibility compared to the vanilla system.
  • HUD Screens: Allows displaying screens on the in-game HUD for visual enhancement. Important: these screens are for display only and do not support user interaction.

Common Side

  • Auto-config System: Simplifies the creation of JSON configuration files for your mod. The system automatically serializes primitive type fields defined in the configuration class when saving and loading settings.
  • Event System: Allows tracking various game events, as well as creating custom events that can be listened to by other mods.
  • Hooks: Provides easy access to various game components.
  • Networking: The API includes its own network with custom packets, allowing mods to exchange data in client-to-server and server-to-client directions through the game's network protocol.
  • Utilities: Includes various interfaces, enumerations, exception types, input-output utilities, threading tools, and more. One such utility is a caching system that allows mods to perform asynchronous operations to fetch/load resources and cache them.

Simple Introduction to the GUI System

The graphical interface system allows you to create custom menus in a simpler way while providing greater flexibility and customization options. The vanilla GUI system uses Screen elements, while this API uses so-called TScreen elements.

All GUI components, including TScreen and widgets, are located in the package: io.github.thecsdev.tcdcommons.api.client.gui.

Below is an example implementation of TScreen with a simple button that performs an action when clicked:

import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreen;
import io.github.thecsdev.tcdcommons.api.client.gui.widget.TButtonWidget;
import net.minecraft.text.Text;

public final class ExampleTScreen extends TScreen
{
    /
      super takes a Text argument for the screen title
     /
    public ExampleTScreen() { super(Text.literal("Example TScreen")); }

    /
      This is where the screen initializes its GUI elements.
      We will add the button widget here.
     /
    protected final @Override void init()
    {
        //create the button and center it on the screen
        final var button = new TButtonWidget(
            (getWidth() / 2) - 50,  //button X position. we center it
            (getHeight() / 2) - 10, //button Y position. we center it
            100, //button width
            20   //button height
        );

        //define text for the button
        button.setText(Text.literal("Click me"));

        //now let's do something when the button is clicked
        button.setOnClick(btn ->
        {
            //print a simple message to the console
            //when the button is clicked
            System.out.println("Hello World!");
        });

        //and finally, add the button to the screen
        addChild(button);
    }
}

Now we need to open the screen and show it to the user. For this, we use MinecraftClient#setScreen(Screen) (in Fabric) and TScreen#getAsScreen() to get the Screen reference. The final code will look like this:

final var tscreen = new ExampleTScreen();
final var screen = tscreen.getAsScreen();
MinecraftClient.getInstance().setScreen(screen);

Simple Introduction to the Event System

The event system allows you to not only track various events already provided by this API but also create and invoke your own custom events. Below is an example of how to create a custom event, register and unregister listeners, and invoke the event:

import io.github.thecsdev.tcdcommons.api.event.TEvent;
import io.github.thecsdev.tcdcommons.api.event.TEventFactory;

public final class ExampleEvents
{
    /
      This is how we create a custom event using this API.
      Note that this event system is similar to Architectury API's event system,
      but with some refinements and additions for greater flexibility.
     /
    public static final TEvent<Runnable> eSomething = TEventFactory.createLoop();

    public static final void registerAndUnregisterListener()
    {
        //first define the listener that will be called when the event is invoked
        final Runnable listener = () -> { System.out.println("The event was invoked."); };

        //then this is how we can register it
        eSomething.register(listener);

        //and this is how we can unregister it
        eSomething.unregister(listener);
    }

    public static final void invokeEvent()
    {
        //we invoke the event by obtaining the "invoker",
        //and calling the main function of the functional interface
        //(in Runnable's case, the main function is run)
        eSomething.invoker().run();
    }
}

Contributing

I appreciate everyone's contribution to the development of this mod, whether it's translation, feedback, or valuable suggestions. If you want to help, here are several ways to participate:

  1. If you speak multiple languages, consider helping with translating the mod. Your efforts will make the mod accessible to a wider audience.
  2. Your ideas and suggestions are invaluable! Feel free to share your thoughts on how to improve the mod. Whether it's a bug report, feature request, or improvement idea - I'd be happy to receive feedback from the community.
  3. If you encounter issues or have ideas for improvements, create an issue on this API mod's repository page.
Project members
TheCSDev

TheCSDev

Developer

Created: 25 Nov 2022

ID: 219