SuperMartijn642's Core Lib
SuperMartijn642's Core Lib adds lots of basic implementations that allow for similar code between different Minecraft versions!
SuperMartijn642's Core Lib is the foundation of all of SuperMartijn642's mods. It provides easy-to-use implementations for registration, networking, GUIs, blocks and block entities, data generation and more.
The main goal of Core Lib is to allow for the same code across Minecraft versions and mod loaders. Core Lib is available for Minecraft 1.12 all the way up to the latest versions, on Forge, NeoForge, Fabric and Quilt, and its API stays as similar as possible between all of them. This makes it a lot easier to keep a mod updated for many versions at once.
Players only need to install Core Lib when a mod requires it.
Features
Getting started
Core Lib can be added to a project from Modrinth's Maven repository.
Replace the version with the one for your Minecraft version and mod loader from the
versions page, e.g. 1..
repositories {
maven { url = "https://api.modrinth.com/maven" }
}
dependencies {
implementation "maven.modrinth:supermartijn642s-core-lib:<version>"
}
Registration
Blocks, items, block entity types, menu types and other registry entries are registered through a
RegistrationHandler, which takes care of the differences between mod loaders. Fields annotated with
@RegistryEntryAcceptor are filled with the registered objects automatically.
public class ExampleMod {
@RegistryEntryAcceptor(namespace = "examplemod", identifier = "example_block", registry = RegistryEntryAcceptor.Registry.BLOCKS)
public static BaseBlock example_block;
public ExampleMod(){
RegistrationHandler handler = RegistrationHandler.get("examplemod");
handler.registerBlock("example_block", () -> new ExampleBlock());
handler.registerItem("example_block", () -> new BaseBlockItem(example_block, ItemProperties.create().group(CreativeItemGroup.getBuildingBlocks())));
}
}
Networking
Packets are simple classes implementing BasePacket, which write and read their own data. Packets are registered
to a PacketChannel together with the direction they may be sent in, and can then be sent to the server, a single
player, all players in a dimension, players near a position, and more.
public class ExamplePacket implements BasePacket {
private int value;
public ExamplePacket(int value){
this.value = value;
}
public ExamplePacket(){
}
@Override
public void write(FriendlyByteBuf buffer){
buffer.writeInt(this.value);
}
@Override
public void read(FriendlyByteBuf buffer){
this.value = buffer.readInt();
}
@Override
public void handle(PacketContext context){
context.getSendingPlayer().displayClientMessage(TextComponents.string("Received " + this.value).get(), true);
}
}
public static final PacketChannel CHANNEL = PacketChannel.create("examplemod");
CHANNEL.registerMessage(ExamplePacket.class, ExamplePacket::new, PacketDirection.CLIENT_TO_SERVER, true);
CHANNEL.sendToServer(new ExamplePacket(42));
For packets about a block entity, BlockEntityBasePacket sends the block's position along and looks up the block
entity before the packet is handled.
GUIs
Screens are built from widgets. A widget draws itself and its child widgets, and handles input. Core Lib comes with
premade widgets such as buttons, text fields, labels and scrollbars, and any widget can be shown as a screen with
WidgetScreen.. Container screens for menus work the same way, through BaseContainer and WidgetContainerScreen.
public class ExampleScreen extends BaseWidget {
public ExampleScreen(){
super(0, 0, 176, 80);
}
@Override
public Component getNarrationMessage(){
return TextComponents.translation("examplemod.screen.title").get();
}
@Override
protected void addWidgets(){
this.addWidget(new ButtonWidget(38, 50, 100, 20, TextComponents.string("Send").get(), () -> {
ExampleMod.CHANNEL.sendToServer(new ExamplePacket(42));
}));
}
@Override
public void renderBackground(WidgetRenderContext context, GuiGraphicsHelper graphics, int mouseX, int mouseY){
graphics.submitDefaultScreenBackground(this.left(), this.top(), this.width, this.height);
super.renderBackground(context, graphics, mouseX, mouseY);
}
}
ClientUtils.displayScreen(WidgetScreen.of(new ExampleScreen()));
Blocks and block entities
BaseBlock and BaseBlockEntity take care of the boilerplate around blocks with data. Block entities only define how
their data is saved and loaded, and calling dataChanged() saves the data and syncs it to clients. Blocks can keep their
block entity's data when broken, and simply override interact to respond to players.
public class ExampleBlockEntity extends BaseBlockEntity {
private int count = 0;
public ExampleBlockEntity(BlockPos pos, BlockState state){
super(ExampleMod.example_block_entity, pos, state);
}
public void increment(){
this.count++;
this.dataChanged();
}
@Override
protected void writeData(ValueOutput output){
output.putInt("count", this.count);
}
@Override
protected void readData(ValueInput input){
this.count = input.getIntOr("count", 0);
}
}
Data generators
Generators for block states, models, item info, loot tables, recipes, tags, advancements, atlas sources and
translations, which can all be registered through a GeneratorRegistrationHandler.
public class ExampleLanguageGenerator extends LanguageGenerator {
public ExampleLanguageGenerator(ResourceCache cache){
super("examplemod", cache, "en_us");
}
@Override
public void generate(){
this.block(ExampleMod.example_block, "Example Block");
this.translation("examplemod.screen.title", "Example Screen");
}
}
GeneratorRegistrationHandler handler = GeneratorRegistrationHandler.get("examplemod");
handler.addGenerator(ExampleLanguageGenerator::new);
And more
TextComponentsfor building text with formatting, e.g.TextComponents.translation("key"). color(ChatFormatting. GOLD). bold(). get() ClientUtilsandCommonUtilsfor common things like the client's player, the server, opening screens and containers, and checking which mods are loaded- Custom block entity and item renderers, and rendering helpers for drawing shapes in the world
- Teleportation utilities for moving entities between dimensions
- Helpers for formatting energy values
Frequently asked questions
Why do I need to install Core Lib?
Many of SuperMartijn642's mods are built on top of Core Lib, so it is required for those mods to work. Core Lib does not add any content to the game by itself.
Can I use Core Lib in my modpack?
Yes, feel free to use Core Lib in your modpack.
Can I use Core Lib for my own mods?
Yes, Core Lib is meant to be used by other mods as well.