Skip to content

Split Sources Support

Tslat edited this page Dec 17, 2024 · 3 revisions

By default, GeckoLib is built to safely encapsulate all of its code in one place, preventing the need to spread your code across different classes for a single usage.

However on environments which are built using split-sources (in which the client sources are in a completely isolated sourceset from the common sources), this poses a problem because the client code GeckoLib uses to render doesn't exist in the common sourceset.

This only really affects Items and Armour, since Entities and BlockEntities already have their renderers registered in client-only code.

Implementation

To utilise the GeoRenderProvider system with split-sources, we have to take an extra step in the implementation. First, create your GeoItem class just like you normally would any GeoItem class (see here).

Then, follow the below steps:

  1. Create a public instanceof MutableObject<GeoRenderProvider> in your item class as a final field
  2. In your createGeoRenderer method, pass the getValue() call from the MutableObject to the consumer
  3. In your client setup (ClientModInitializer on Fabric, or your client registration class on Neo/Forge), call setValue() on your registered item instance's cached MutableObject, passing your GeoRenderer instance as explained in the GeckoLib Items wiki page

Example

Item Class

public class ExampleItem extends Item implements GeoItem {
    // Create the object to store the RenderProvider
    public final MutableObject<GeoRenderProvider> renderProviderHolder = new MutableObject<>();
    private final AnimatableInstanceCache geoCache = GeckoLibUtil.createInstanceCache(this);

    public ExampleItem(Settings settings) {
        super(settings);
    }

    @Override
    public void createGeoRenderer(Consumer<GeoRenderProvider> consumer) {
        // Return the cached RenderProvider
        consumer.accept(this.renderProviderHolder.getValue());
    }

    @Override
    public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {}

    @Override
    public AnimatableInstanceCache getAnimatableInstanceCache() {
        return this.geoCache;
    }
}

Client Setup

@Override
public void onInitializeClient() {
    ExampleModItems.EXAMPLE_ITEM.renderProviderHolder.setValue(new GeoRenderProvider() {
            private ExampleItemRenderer renderer;

            @Override
            public BlockEntityWithoutLevelRenderer getGeoItemRenderer() {
                if (this.renderer == null)
                    this.renderer = new ExampleItemRenderer();

                return this.renderer;
            }
    });
}

Table of Contents

Geckolib 3
Geckolib 4

Hosted By: Cloudsmith

Package repository hosting is graciously provided by Cloudsmith.

Cloudsmith is the only fully hosted, cloud-native, universal package management solution that enables your organization to create, store and share packages in any format, to any place, with total confidence.

Clone this wiki locally