Skip to content

Commit

Permalink
Use new Fabric model api
Browse files Browse the repository at this point in the history
Fixes #683
  • Loading branch information
williewillus committed Aug 29, 2023
1 parent 310a1c4 commit 113a209
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dependencies {
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:0.14.21"

modImplementation "net.fabricmc.fabric-api:fabric-api:0.83.1+1.20.1"
modImplementation "net.fabricmc.fabric-api:fabric-api:0.87.0+1.20.1"
compileOnly project(":Xplat")

modCompileOnly "mezz.jei:jei-1.20.1-common-api:15.0.0.12"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelModifier;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
Expand All @@ -11,25 +12,30 @@
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;

import vazkii.patchouli.api.PatchouliAPI;
import vazkii.patchouli.client.base.BookModel;
import vazkii.patchouli.client.base.ClientTicker;
import vazkii.patchouli.client.base.PersistentData;
import vazkii.patchouli.client.book.BookContentResourceListenerLoader;
import vazkii.patchouli.client.book.ClientBookRegistry;
import vazkii.patchouli.client.handler.BookRightClickHandler;
import vazkii.patchouli.client.handler.MultiblockVisualizationHandler;
import vazkii.patchouli.common.book.Book;
import vazkii.patchouli.common.book.BookRegistry;
import vazkii.patchouli.common.item.ItemModBook;
import vazkii.patchouli.common.item.PatchouliItems;
import vazkii.patchouli.fabric.network.FabricMessageOpenBookGui;
import vazkii.patchouli.fabric.network.FabricMessageReloadBookContents;

import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

Expand All @@ -47,9 +53,23 @@ public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(FabricMessageOpenBookGui.ID, FabricMessageOpenBookGui::handle);
ClientPlayNetworking.registerGlobalReceiver(FabricMessageReloadBookContents.ID, FabricMessageReloadBookContents::handle);

ModelLoadingRegistry.INSTANCE.registerModelProvider((manager, register) -> BookRegistry.INSTANCE.books.values().stream()
.map(b -> new ModelResourceLocation(b.model, "inventory"))
.forEach(register));
ModelLoadingPlugin.register(pluginContext -> {
for (Book book : BookRegistry.INSTANCE.books.values()) {
pluginContext.addModels(new ModelResourceLocation(book.model, "inventory"));
}

pluginContext.modifyModelAfterBake().register(
(@Nullable BakedModel oldModel, ModelModifier.AfterBake.Context ctx) -> {
if (ctx.id() instanceof ModelResourceLocation key
&& PatchouliItems.BOOK_ID.equals(key) // checks namespace and path
&& key.getVariant().equals("inventory")
&& oldModel != null) {
return new BookModel(oldModel, ctx.loader());
}
return oldModel;
}
);
});

ItemProperties.register(PatchouliItems.BOOK,
new ResourceLocation(PatchouliAPI.MOD_ID, "completion"),
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion Fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

"depends": {
"fabricloader": ">=0.14.21",
"fabric": ">=0.83.1",
"fabric": ">=0.87.0",
"minecraft": ">=1.20.1 <1.21"
}
}
1 change: 0 additions & 1 deletion Fabric/src/main/resources/patchouli_fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"client": [
"client.MixinGameRenderer",
"client.MixinMinecraft",
"client.MixinModelManager",
"client.MixinGuiGraphics"
],
"injectors": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public static void onInitializeClient(FMLClientSetupEvent evt) {

@SubscribeEvent
public static void replaceBookModel(ModelEvent.ModifyBakingResult evt) {
BookModel.replace(evt.getModels(), evt.getModelBakery());
ModelResourceLocation key = new ModelResourceLocation(PatchouliItems.BOOK_ID, "inventory");
evt.getModels().computeIfPresent(key, (k, oldModel) -> new BookModel(oldModel, evt.getModelBakery()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,19 @@

import vazkii.patchouli.common.book.Book;
import vazkii.patchouli.common.item.ItemModBook;
import vazkii.patchouli.common.item.PatchouliItems;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class BookModel implements BakedModel {
private final BakedModel original;
private final ItemOverrides itemHandler;

public static void replace(Map<ResourceLocation, BakedModel> models, ModelBakery bakery) {
ModelResourceLocation key = new ModelResourceLocation(PatchouliItems.BOOK_ID, "inventory");
models.computeIfPresent(key, (k, oldModel) -> new BookModel(oldModel, bakery));
}

private BookModel(BakedModel original, ModelBakery loader) {
public BookModel(BakedModel original, ModelBakery loader) {
this.original = original;
BlockModel missing = (BlockModel) loader.getModel(ModelBakery.MISSING_MODEL_LOCATION);

Expand Down

0 comments on commit 113a209

Please sign in to comment.