Skip to content

Commit

Permalink
Fix model loading
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces committed Jun 13, 2024
1 parent 4eba6b1 commit beb89a1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.dhyces.trimmed.api.client.override.provider;

import dev.dhyces.trimmed.modhelper.services.Services;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
Expand All @@ -13,8 +15,22 @@
public abstract class SimpleItemOverrideProvider implements ItemOverrideProvider {
@Override
public Optional<BakedModel> getModel(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
return getModelLocation(itemStack, world, entity, seed).map(Minecraft.getInstance().getModelManager()::getModel);
ModelPair pair = getModelLocation(itemStack, world, entity, seed);
return pair.getTopLevelModelId().map(Minecraft.getInstance().getModelManager()::getModel)
.filter(model -> model != Minecraft.getInstance().getModelManager().getMissingModel())
.or(() -> pair.getResourceId().map(Services.CLIENT_HELPER::getModel));
}

public abstract Optional<ModelResourceLocation> getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed);
public abstract ModelPair getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed);

public record ModelPair(@Nullable ResourceLocation resourceId, @Nullable ModelResourceLocation topLevelModelId) {
public static final ModelPair EMPTY = new ModelPair(null, null);

public Optional<ResourceLocation> getResourceId() {
return Optional.ofNullable(resourceId);
}
public Optional<ModelResourceLocation> getTopLevelModelId() {
return Optional.ofNullable(topLevelModelId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public Optional<BakedModel> getModel(ItemStack itemStack, @Nullable ClientLevel
}

@Override
public Optional<ModelResourceLocation> getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
public ModelPair getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
if (!(itemStack.getItem() instanceof ArmorItem armorItem)) {
return Optional.empty();
return ModelPair.EMPTY;
}

Optional<ResourceLocation> materialIdOptional = Optional.ofNullable(itemStack.get(DataComponents.TRIM))
Expand All @@ -55,10 +55,11 @@ public Optional<ModelResourceLocation> getModelLocation(ItemStack itemStack, @Nu
} else {
return null;
}
})).withPath(s -> s.substring(s.indexOf("/")+1));
return Optional.of(new ModelResourceLocation(id, "inventory"));
}));
// TODO: Fabric is silly and forces custom models to be saved in top level as namespace:resource_id#fabric_resource
return new ModelPair(id, new ModelResourceLocation(id.withPath(s -> s.substring(s.indexOf("/")+1)), "inventory"));
}
return Optional.empty();
return ModelPair.EMPTY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ public ComponentItemOverrideProvider(DataComponentPatch componentPatch, ModelRes
}

@Override
public Optional<ModelResourceLocation> getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
public ModelPair getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
if (!itemStack.getComponentsPatch().isEmpty()) {
DataComponentPatch stackPatch = itemStack.getComponentsPatch();
for (Map.Entry<DataComponentType<?>, Optional<?>> entry : componentPatch.entrySet()) {
Optional<?> stackData = stackPatch.get(entry.getKey());
Optional<?> testData = componentPatch.get(entry.getKey());
if ((stackData == null) != (testData == null)) {
return Optional.empty();
return ModelPair.EMPTY;
}
if (stackData.isEmpty() != testData.isEmpty()) {
return Optional.empty();
return ModelPair.EMPTY;
}
if (stackData.isPresent() && testData.isPresent() && !stackData.get().equals(testData.get())) {
return Optional.empty();
return ModelPair.EMPTY;
}
}
return Optional.of(model);
return new ModelPair(null, model);
}
return Optional.empty();
return ModelPair.EMPTY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package dev.dhyces.trimmed.impl.client.models.source;

import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

public record NamedModel(ResourceLocation id, Supplier<BlockModel> model, @Nullable ModelResourceLocation modelId) {
public record NamedModel(ResourceLocation id, Supplier<BlockModel> model) {
public static NamedModel item(ResourceLocation fileId, Supplier<BlockModel> model) {
return new NamedModel(fileId, model, convertToItemModelId(fileId));
}

public static ModelResourceLocation convertToItemModelId(ResourceLocation modelFileId) {
return new ModelResourceLocation(modelFileId.withPath(s -> s.substring(s.indexOf("/")+1)), "inventory");
return new NamedModel(fileId, model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import dev.dhyces.trimmed.api.client.TrimmedClientApiEntrypoint;
import dev.dhyces.trimmed.impl.ModApiConsumer;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.resources.ResourceLocation;

import java.util.List;

public interface ClientHelper {
List<ModApiConsumer<TrimmedClientApiEntrypoint>> getClientApiConsumers();
BakedModel getModel(ResourceLocation resourceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public void onInitializeClient() {
(data, pluginContext) -> {
Map<ResourceLocation, NamedModel> modelMapByFileId = new Object2ObjectOpenHashMap<>();
for (NamedModel namedModel : data) {
// Need to add as ModelResourceLocations
pluginContext.addModels(namedModel.id());
// But the context ids are in regular ResourceLocations
modelMapByFileId.put(namedModel.id(), namedModel);
}
pluginContext.resolveModel().register(context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import dev.dhyces.trimmed.api.client.TrimmedClientApiEntrypoint;
import dev.dhyces.trimmed.impl.ModApiConsumer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.resources.ResourceLocation;

import java.util.List;

Expand All @@ -12,4 +15,9 @@ public List<ModApiConsumer<TrimmedClientApiEntrypoint>> getClientApiConsumers()
return FabricLoader.getInstance().getEntrypointContainers("trimmed:client_api_entrypoint", TrimmedClientApiEntrypoint.class)
.stream().map(container -> new ModApiConsumer<>(container.getProvider().getMetadata().getId(), container.getEntrypoint())).toList();
}

@Override
public BakedModel getModel(ResourceLocation resourceId) {
return Minecraft.getInstance().getModelManager().getModel(resourceId);
}
}
6 changes: 3 additions & 3 deletions neo/src/main/java/dev/dhyces/trimmed/NeoTrimmedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@SuppressWarnings("unused")
@Mod(value = Trimmed.MODID, dist = Dist.CLIENT)
public class NeoTrimmedClient {
private static Set<ModelResourceLocation> additionalGeneratedModels;
private static Set<ResourceLocation> additionalGeneratedModels;

public NeoTrimmedClient(IEventBus modBus, ModContainer container) {
TrimmedClient.init();
Expand All @@ -44,7 +44,7 @@ private void registerClientReloadListener(final RegisterClientReloadListenersEve
}

private void addModels(final ModelEvent.RegisterAdditional event) {
additionalGeneratedModels.forEach(event::register);
additionalGeneratedModels.forEach(resourceId -> event.register(ModelResourceLocation.standalone(resourceId)));
}

private void tagsSynced(final TagsUpdatedEvent event) {
Expand All @@ -59,7 +59,7 @@ private void onLogout(final ClientPlayerNetworkEvent.LoggingOut event) {
TrimmedClient.resetSyncedStatus();
}

public static void setModels(Set<ModelResourceLocation> models) {
public static void setModels(Set<ResourceLocation> models) {
additionalGeneratedModels = models;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ private static CompletableFuture<Map<ResourceLocation, BlockModel>> injectGenera
return original.thenCombineAsync(TrimmedClient.startGeneratingModels(resourceManager, executor),
(originalMap, generatedModels) -> {
Object2ObjectMap<ResourceLocation, BlockModel> newMap = new Object2ObjectOpenHashMap<>();
Set<ModelResourceLocation> generatedModelIds = new ObjectOpenHashSet<>();
Set<ResourceLocation> generatedModelIds = new ObjectOpenHashSet<>();
generatedModels.forEach(namedModel -> {
ResourceLocation path = namedModel.id().withPrefix("models/").withSuffix(".json");
if (!originalMap.containsKey(path)) {
newMap.put(path, namedModel.model().get());
generatedModelIds.add(namedModel.modelId());
generatedModelIds.add(namedModel.id());
}
});
NeoTrimmedClient.setModels(generatedModelIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import dev.dhyces.trimmed.api.TrimmedClientApiConsumer;
import dev.dhyces.trimmed.api.client.TrimmedClientApiEntrypoint;
import dev.dhyces.trimmed.impl.ModApiConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.fml.ModList;

import java.lang.annotation.ElementType;
Expand Down Expand Up @@ -34,4 +38,9 @@ public List<ModApiConsumer<TrimmedClientApiEntrypoint>> getClientApiConsumers()
return new ModApiConsumer<>(modid, apiEntrypoint);
}).toList();
}

@Override
public BakedModel getModel(ResourceLocation resourceId) {
return Minecraft.getInstance().getModelManager().getModel(ModelResourceLocation.standalone(resourceId));
}
}

0 comments on commit beb89a1

Please sign in to comment.