Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
items models configurable generator
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealEmissions committed Mar 28, 2024
1 parent f92fd0a commit 44c190c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

import net.dumbcode.projectnublar.core.items.DumbItems;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraftforge.client.model.generators.ItemModelBuilder;
import net.minecraftforge.client.model.generators.ItemModelProvider;
import net.minecraftforge.client.model.generators.ModelFile;
import net.minecraftforge.common.data.ExistingFileHelper;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;

import java.util.function.UnaryOperator;

import static net.dumbcode.projectnublar.ProjectNublar.MOD_ID;

Expand All @@ -12,10 +23,47 @@ public ModItemModelProvider(PackOutput output, ExistingFileHelper existingFileHe
super(output, MOD_ID, existingFileHelper);
}

public final class Generator {
private final ModItemModelProvider provider;
private final RegistryObject<Item> item;

public Generator(ModItemModelProvider provider, RegistryObject<Item> item) {
this.provider = provider;
this.item = item;
}

private ResourceLocation location() {
return ForgeRegistries.ITEMS.getKey(item.get());
}

public Generator basic() {
provider.basicItem(item.get());
return this;
}

public Generator basic(@Range(from=1, to=5) int layers) {
ResourceLocation location = location();
ItemModelBuilder builder = getBuilder(location.toString())
.parent(new ModelFile.UncheckedModelFile("item/generated"));
for (int i = 0; i < layers; i++) {
builder.texture("layer" + i, "item/" + location.getPath() + "_layer" + i);
}
return this;
}

@Contract("_ -> this")
public Generator builder(@NotNull UnaryOperator<ItemModelBuilder> builder) {
ItemModelBuilder localBuilder = provider.getBuilder(location().toString());
builder.apply(localBuilder);
return this;
}
}

@Override
protected void registerModels() {
for (DumbItems.Items item : DumbItems.Items.values()) {
basicItem(item.getRegistry().item().get());
UnaryOperator<Generator> modelGeneratorOperator = item.getModelGeneratorOperator();
modelGeneratorOperator.apply(new Generator(this, item.getRegistry().item()));
}
}
}
34 changes: 29 additions & 5 deletions src/main/java/net/dumbcode/projectnublar/core/items/DumbItems.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.dumbcode.projectnublar.core.items;

import net.dumbcode.projectnublar.core.data.ModItemModelProvider;
import net.dumbcode.projectnublar.core.data.ModRecipeProvider;
import net.dumbcode.projectnublar.core.items.elements.AcornItem;
import net.dumbcode.projectnublar.core.items.elements.OreDetectorItem;
Expand All @@ -25,13 +26,31 @@

public final class DumbItems {
public enum Items {
TEST_ITEM(TestItem::new, metadata -> metadata),
ORE_DETECTOR(OreDetectorItem::new, metadata -> metadata),
STRAWBERRY(StrawberryItem::new, metadata -> metadata),
ACORN(AcornItem::new, metadata -> metadata);
TEST_ITEM(
TestItem::new,
ModItemModelProvider.Generator::basic,
metadata -> metadata
),
ORE_DETECTOR(
OreDetectorItem::new,
ModItemModelProvider.Generator::basic,
metadata -> metadata
),
STRAWBERRY(
StrawberryItem::new,
ModItemModelProvider.Generator::basic,
metadata -> metadata
),
ACORN(
AcornItem::new,
ModItemModelProvider.Generator::basic,
metadata -> metadata
);

private final Supplier<DumbItem> itemConstructor;

private final UnaryOperator<ModItemModelProvider.Generator> modelGeneratorOperator;

private final Registry registry = Registry.of(
RegistryObject.create(new ResourceLocation(MOD_ID, getRegisterName()), Registrar.ITEMS.getRegistryKey(), MOD_ID)
);
Expand All @@ -41,8 +60,9 @@ public enum Items {
return Arrays.stream(Items.values()).map(x -> x.registry.item).toList();
}

Items(Supplier<DumbItem> itemConstructor, @NotNull UnaryOperator<Metadata.Builder> metadataBuilder) {
Items(Supplier<DumbItem> itemConstructor, UnaryOperator<ModItemModelProvider.Generator> modelGeneratorOperator, @NotNull UnaryOperator<Metadata.Builder> metadataBuilder) {
this.itemConstructor = itemConstructor;
this.modelGeneratorOperator = modelGeneratorOperator;
this.metadata = metadataBuilder.apply(new Metadata.Builder()).build();
}

Expand All @@ -54,6 +74,10 @@ public Registry getRegistry() {
return this.registry;
}

public UnaryOperator<ModItemModelProvider.Generator> getModelGeneratorOperator() {
return this.modelGeneratorOperator;
}

public Metadata getMetadata() {
return this.metadata;
}
Expand Down

0 comments on commit 44c190c

Please sign in to comment.