Skip to content

Commit

Permalink
ClientExtension registration helper for item / block (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcy0x1 authored Jul 25, 2024
1 parent 04746f6 commit 94ef225
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ org.gradle.caching=true

mod_version=1.3.x
minecraft_version=1.21
neo_version=21.0.87-beta
neo_version=21.0.133-beta
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ protected void onRegisterLate(RegisterEvent event) {
* The event
*/
protected void onBuildCreativeModeTabContents(BuildCreativeModeTabContentsEvent event) {
var modifier = new CreativeModeTabModifier(event::getFlags, event::hasPermissions, event::accept);
var modifier = new CreativeModeTabModifier(event::getFlags, event::hasPermissions, event::accept, event::getParameters);

creativeModeTabModifiers.forEach((key, value) -> {
if(event.getTabKey().equals(key)) value.accept(modifier);
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/tterrag/registrate/builders/BlockBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;
import net.neoforged.neoforge.client.extensions.common.IClientBlockExtensions;
import net.neoforged.neoforge.client.extensions.common.IClientItemExtensions;
import net.neoforged.neoforge.client.extensions.common.RegisterClientExtensionsEvent;
import net.neoforged.neoforge.client.model.generators.BlockStateProvider;
import net.neoforged.neoforge.registries.DeferredHolder;

Expand Down Expand Up @@ -357,6 +360,33 @@ public BlockBuilder<T, P> recipe(NonNullBiConsumer<DataGenContext<Block, T>, Reg
return setData(ProviderType.RECIPE, cons);
}

@Nullable
private NonNullSupplier<Supplier<IClientBlockExtensions>> clientExtension;

/**
* Register a client extension for this block. The {@link IClientBlockExtensions} instance can be shared across many items.
*
* @param clientExtension
* The client extension to register for this block
* @return this {@link BlockBuilder}
*/
public BlockBuilder<T, P> clientExtension(NonNullSupplier<Supplier<IClientBlockExtensions>> clientExtension) {
if (this.clientExtension == null) {
RegistrateDistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> this::registerClientExtension);
}
this.clientExtension = clientExtension;
return this;
}

protected void registerClientExtension() {
OneTimeEventReceiver.addModListener(getOwner(), RegisterClientExtensionsEvent.class, e -> {
NonNullSupplier<Supplier<IClientBlockExtensions>> clientExtension = this.clientExtension;
if (clientExtension != null) {
e.registerBlock(clientExtension.get().get(), getEntry());
}
});
}

/**
* Assign {@link TagKey}{@code s} to this block. Multiple calls will add additional tags.
*
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/com/tterrag/registrate/builders/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.client.extensions.common.IClientItemExtensions;
import net.neoforged.neoforge.client.extensions.common.RegisterClientExtensionsEvent;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import net.neoforged.neoforge.registries.datamaps.builtin.Compostable;
import net.neoforged.neoforge.registries.datamaps.builtin.FurnaceFuel;
import net.neoforged.neoforge.registries.datamaps.builtin.NeoForgeDataMaps;
Expand Down Expand Up @@ -290,6 +288,33 @@ public ItemBuilder<T, P> compostable(float chance) {
return dataMap(NeoForgeDataMaps.COMPOSTABLES, new Compostable(chance));
}

@Nullable
private NonNullSupplier<Supplier<IClientItemExtensions>> clientExtension;

/**
* Register a client extension for this item. The {@link IClientItemExtensions} instance can be shared across many items.
*
* @param clientExtension
* The client extension to register for this item
* @return this {@link ItemBuilder}
*/
public ItemBuilder<T, P> clientExtension(NonNullSupplier<Supplier<IClientItemExtensions>> clientExtension) {
if (this.clientExtension == null) {
RegistrateDistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> this::registerClientExtension);
}
this.clientExtension = clientExtension;
return this;
}

protected void registerClientExtension() {
OneTimeEventReceiver.addModListener(getOwner(), RegisterClientExtensionsEvent.class, e -> {
NonNullSupplier<Supplier<IClientItemExtensions>> clientExtension = this.clientExtension;
if (clientExtension != null) {
e.registerItem(clientExtension.get().get(), getEntry());
}
});
}

/**
* Assign {@link TagKey}{@code s} to this item. Multiple calls will add additional tags.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ public final class CreativeModeTabModifier implements CreativeModeTab.Output {
private final Supplier<FeatureFlagSet> flags;
private final BooleanSupplier hasPermissions;
private final BiConsumer<ItemStack, CreativeModeTab.TabVisibility> acceptFunc;
private final Supplier<CreativeModeTab.ItemDisplayParameters> parameters;

@ApiStatus.Internal
public CreativeModeTabModifier(Supplier<FeatureFlagSet> flags, BooleanSupplier hasPermissions, BiConsumer<ItemStack, CreativeModeTab.TabVisibility> acceptFunc) {
public CreativeModeTabModifier(Supplier<FeatureFlagSet> flags, BooleanSupplier hasPermissions, BiConsumer<ItemStack, CreativeModeTab.TabVisibility> acceptFunc, Supplier<CreativeModeTab.ItemDisplayParameters> parameters) {
this.flags = flags;
this.hasPermissions = hasPermissions;
this.acceptFunc = acceptFunc;
}
this.parameters = parameters;
}

public FeatureFlagSet getFlags() {
return flags.get();
}

public CreativeModeTab.ItemDisplayParameters getParameters(){
return parameters.get();
}

public boolean hasPermissions() {
return hasPermissions.getAsBoolean();
}
Expand Down

0 comments on commit 94ef225

Please sign in to comment.