From 6265b6d2957824cd550c03a16d548fe2f6cce69b Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Wed, 22 Feb 2023 20:34:37 -0800 Subject: [PATCH] Refactor custom rendered items - CustomRenderedItemModel no longer holds partials - Store partials as PartialModels in static fields in renderers - Remove all CustomRenderedItemModel subclasses - Remove CustomRenderedItemModelRenderer#createModel - CustomRenderedItemModelRenderer is no longer generic - Store items with custom renderers in CustomRenderedItems instead of CustomRenderedItemModelRenderer --- .../wrench/WrenchItemRenderer.java | 17 +++-- .../contraptions/wrench/WrenchModel.java | 14 ---- .../client/SymmetryWandItemRenderer.java | 23 +++---- .../symmetry/client/SymmetryWandModel.java | 14 ---- .../tools/ExtendoGripItemRenderer.java | 65 ++++++++++--------- .../curiosities/tools/ExtendoGripModel.java | 14 ---- .../tools/SandPaperItemRenderer.java | 20 +----- .../weapons/PotatoCannonItemRenderer.java | 17 +++-- .../weapons/PotatoCannonModel.java | 14 ---- .../zapper/ZapperItemRenderer.java | 4 +- .../WorldshaperItemRenderer.java | 23 +++---- .../terrainzapper/WorldshaperModel.java | 14 ---- .../item/LecternControllerRenderer.java | 3 +- .../item/LinkedControllerItemRenderer.java | 25 +++---- .../logistics/item/LinkedControllerModel.java | 14 ---- .../block/render/CustomBlockModels.java | 13 ++-- .../render/CreateCustomRenderedItemModel.java | 13 ---- .../item/render/CustomItemModels.java | 5 +- .../item/render/CustomRenderedItemModel.java | 53 ++------------- .../CustomRenderedItemModelRenderer.java | 41 +----------- .../item/render/CustomRenderedItems.java | 63 +++++++++--------- .../item/render/SimpleCustomRenderer.java | 10 +-- .../foundation/utility/ModelSwapper.java | 22 +------ 23 files changed, 150 insertions(+), 351 deletions(-) delete mode 100644 src/main/java/com/simibubi/create/content/contraptions/wrench/WrenchModel.java delete mode 100644 src/main/java/com/simibubi/create/content/curiosities/symmetry/client/SymmetryWandModel.java delete mode 100644 src/main/java/com/simibubi/create/content/curiosities/tools/ExtendoGripModel.java delete mode 100644 src/main/java/com/simibubi/create/content/curiosities/weapons/PotatoCannonModel.java delete mode 100644 src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperModel.java delete mode 100644 src/main/java/com/simibubi/create/content/logistics/item/LinkedControllerModel.java delete mode 100644 src/main/java/com/simibubi/create/foundation/item/render/CreateCustomRenderedItemModel.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/wrench/WrenchItemRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/wrench/WrenchItemRenderer.java index 1caf40a38..bf626deb8 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/wrench/WrenchItemRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/wrench/WrenchItemRenderer.java @@ -1,21 +1,25 @@ package com.simibubi.create.content.contraptions.wrench; +import com.jozufozu.flywheel.core.PartialModel; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Vector3f; +import com.simibubi.create.Create; import com.simibubi.create.foundation.blockEntity.behaviour.scrollvalue.ScrollValueHandler; +import com.simibubi.create.foundation.item.render.CustomRenderedItemModel; import com.simibubi.create.foundation.item.render.CustomRenderedItemModelRenderer; import com.simibubi.create.foundation.item.render.PartialItemModelRenderer; import com.simibubi.create.foundation.utility.AnimationTickHolder; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.block.model.ItemTransforms; -import net.minecraft.client.resources.model.BakedModel; import net.minecraft.world.item.ItemStack; -public class WrenchItemRenderer extends CustomRenderedItemModelRenderer { +public class WrenchItemRenderer extends CustomRenderedItemModelRenderer { + + protected static final PartialModel GEAR = new PartialModel(Create.asResource("item/wrench/gear")); @Override - protected void render(ItemStack stack, WrenchModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { renderer.render(model.getOriginalModel(), light); @@ -24,12 +28,7 @@ public class WrenchItemRenderer extends CustomRenderedItemModelRenderer { +public class SymmetryWandItemRenderer extends CustomRenderedItemModelRenderer { + + protected static final PartialModel BITS = new PartialModel(Create.asResource("item/wand_of_symmetry/bits")); + protected static final PartialModel CORE = new PartialModel(Create.asResource("item/wand_of_symmetry/core")); + protected static final PartialModel CORE_GLOW = new PartialModel(Create.asResource("item/wand_of_symmetry/core_glow")); @Override - protected void render(ItemStack stack, SymmetryWandModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { float worldTime = AnimationTickHolder.getRenderTime() / 20; int maxLight = LightTexture.FULL_BRIGHT; renderer.render(model.getOriginalModel(), light); - renderer.renderSolidGlowing(model.getPartial("core"), maxLight); - renderer.renderGlowing(model.getPartial("core_glow"), maxLight); + renderer.renderSolidGlowing(CORE.get(), maxLight); + renderer.renderGlowing(CORE_GLOW.get(), maxLight); float floating = Mth.sin(worldTime) * .05f; float angle = worldTime * -10 % 360; @@ -31,12 +37,7 @@ public class SymmetryWandItemRenderer extends CustomRenderedItemModelRenderer { +public class ExtendoGripItemRenderer extends CustomRenderedItemModelRenderer { - private static final Vec3 rotationOffset = new Vec3(0, 1 / 2f, 1 / 2f); - private static final Vec3 cogRotationOffset = new Vec3(0, 1 / 16f, 0); + protected static final PartialModel COG = new PartialModel(Create.asResource("item/extendo_grip/cog")); + protected static final PartialModel THIN_SHORT = new PartialModel(Create.asResource("item/extendo_grip/thin_short")); + protected static final PartialModel WIDE_SHORT = new PartialModel(Create.asResource("item/extendo_grip/wide_short")); + protected static final PartialModel THIN_LONG = new PartialModel(Create.asResource("item/extendo_grip/thin_long")); + protected static final PartialModel WIDE_LONG = new PartialModel(Create.asResource("item/extendo_grip/wide_long")); + + private static final Vec3 ROTATION_OFFSET = new Vec3(0, 1 / 2f, 1 / 2f); + private static final Vec3 COG_ROTATION_OFFSET = new Vec3(0, 1 / 16f, 0); @Override - protected void render(ItemStack stack, ExtendoGripModel model, PartialItemModelRenderer renderer, TransformType transformType, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { TransformStack stacker = TransformStack.cast(ms); float animation = 0.25f; @@ -45,43 +53,43 @@ public class ExtendoGripItemRenderer extends CustomRenderedItemModelRenderer { +public class SandPaperItemRenderer extends CustomRenderedItemModelRenderer { @Override - protected void render(ItemStack stack, SandPaperModel model, PartialItemModelRenderer renderer, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer(); LocalPlayer player = Minecraft.getInstance().player; @@ -79,17 +78,4 @@ public class SandPaperItemRenderer extends CustomRenderedItemModelRenderer { +public class PotatoCannonItemRenderer extends CustomRenderedItemModelRenderer { + + protected static final PartialModel COG = new PartialModel(Create.asResource("item/potato_cannon/cog")); @Override - protected void render(ItemStack stack, PotatoCannonModel model, PartialItemModelRenderer renderer, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { ItemRenderer itemRenderer = Minecraft.getInstance() .getItemRenderer(); @@ -46,7 +50,7 @@ public class PotatoCannonItemRenderer extends CustomRenderedItemModelRenderer extends CustomRenderedItemModelRenderer { +public abstract class ZapperItemRenderer extends CustomRenderedItemModelRenderer { @Override - protected void render(ItemStack stack, M model, PartialItemModelRenderer renderer, TransformType transformType, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { // Block indicator if (transformType == TransformType.GUI && stack.hasTag() && stack.getTag() diff --git a/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItemRenderer.java b/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItemRenderer.java index a163d209c..05855609b 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItemRenderer.java +++ b/src/main/java/com/simibubi/create/content/curiosities/zapper/terrainzapper/WorldshaperItemRenderer.java @@ -2,9 +2,12 @@ package com.simibubi.create.content.curiosities.zapper.terrainzapper; import static java.lang.Math.max; +import com.jozufozu.flywheel.core.PartialModel; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Vector3f; +import com.simibubi.create.Create; import com.simibubi.create.content.curiosities.zapper.ZapperItemRenderer; +import com.simibubi.create.foundation.item.render.CustomRenderedItemModel; import com.simibubi.create.foundation.item.render.PartialItemModelRenderer; import com.simibubi.create.foundation.utility.AnimationTickHolder; @@ -13,15 +16,18 @@ import net.minecraft.client.player.LocalPlayer; import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.block.model.ItemTransforms; -import net.minecraft.client.resources.model.BakedModel; import net.minecraft.util.Mth; import net.minecraft.world.entity.HumanoidArm; import net.minecraft.world.item.ItemStack; -public class WorldshaperItemRenderer extends ZapperItemRenderer { +public class WorldshaperItemRenderer extends ZapperItemRenderer { + + protected static final PartialModel CORE = new PartialModel(Create.asResource("item/handheld_worldshaper/core")); + protected static final PartialModel CORE_GLOW = new PartialModel(Create.asResource("item/handheld_worldshaper/core_glow")); + protected static final PartialModel ACCELERATOR = new PartialModel(Create.asResource("item/handheld_worldshaper/accelerator")); @Override - protected void render(ItemStack stack, WorldshaperModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { super.render(stack, model, renderer, transformType, ms, buffer, light, overlay); @@ -45,8 +51,8 @@ public class WorldshaperItemRenderer extends ZapperItemRenderer { +public class LinkedControllerItemRenderer extends CustomRenderedItemModelRenderer { + + protected static final PartialModel POWERED = new PartialModel(Create.asResource("item/linked_controller/powered")); + protected static final PartialModel BUTTON = new PartialModel(Create.asResource("item/linked_controller/button")); static LerpedFloat equipProgress; static Vector buttons; @@ -61,25 +67,25 @@ public class LinkedControllerItemRenderer extends CustomRenderedItemModelRendere } @Override - protected void render(ItemStack stack, LinkedControllerModel model, PartialItemModelRenderer renderer, + protected void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { renderNormal(stack, model, renderer, transformType, ms, light); } - protected static void renderNormal(ItemStack stack, LinkedControllerModel model, + protected static void renderNormal(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, PoseStack ms, int light) { render(stack, model, renderer, transformType, ms, light, RenderType.NORMAL, false, false); } - public static void renderInLectern(ItemStack stack, LinkedControllerModel model, + public static void renderInLectern(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, PoseStack ms, int light, boolean active, boolean renderDepression) { render(stack, model, renderer, transformType, ms, light, RenderType.LECTERN, active, renderDepression); } - protected static void render(ItemStack stack, LinkedControllerModel model, + protected static void render(ItemStack stack, CustomRenderedItemModel model, PartialItemModelRenderer renderer, ItemTransforms.TransformType transformType, PoseStack ms, int light, RenderType renderType, boolean active, boolean renderDepression) { float pt = AnimationTickHolder.getPartialTicks(); @@ -119,14 +125,14 @@ public class LinkedControllerItemRenderer extends CustomRenderedItemModelRendere renderDepression = true; } - renderer.render(active ? model.getPartial("powered") : model.getOriginalModel(), light); + renderer.render(active ? POWERED.get() : model.getOriginalModel(), light); if (!active) { ms.popPose(); return; } - BakedModel button = model.getPartial("button"); + BakedModel button = BUTTON.get(); float s = 1 / 16f; float b = s * -.75f; int index = 0; @@ -168,11 +174,6 @@ public class LinkedControllerItemRenderer extends CustomRenderedItemModelRendere ms.popPose(); } - @Override - public LinkedControllerModel createModel(BakedModel originalModel) { - return new LinkedControllerModel(originalModel); - } - protected enum RenderType { NORMAL, LECTERN; } diff --git a/src/main/java/com/simibubi/create/content/logistics/item/LinkedControllerModel.java b/src/main/java/com/simibubi/create/content/logistics/item/LinkedControllerModel.java deleted file mode 100644 index 7b3784d6e..000000000 --- a/src/main/java/com/simibubi/create/content/logistics/item/LinkedControllerModel.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.simibubi.create.content.logistics.item; - -import com.simibubi.create.foundation.item.render.CreateCustomRenderedItemModel; - -import net.minecraft.client.resources.model.BakedModel; - -public class LinkedControllerModel extends CreateCustomRenderedItemModel { - - public LinkedControllerModel(BakedModel template) { - super(template, "linked_controller"); - addPartials("powered", "button"); - } - -} diff --git a/src/main/java/com/simibubi/create/foundation/block/render/CustomBlockModels.java b/src/main/java/com/simibubi/create/foundation/block/render/CustomBlockModels.java index 8dc43b692..a6abf0101 100644 --- a/src/main/java/com/simibubi/create/foundation/block/render/CustomBlockModels.java +++ b/src/main/java/com/simibubi/create/foundation/block/render/CustomBlockModels.java @@ -16,7 +16,8 @@ import net.minecraftforge.registries.ForgeRegistries; public class CustomBlockModels { private final Multimap> modelFuncs = MultimapBuilder.hashKeys().arrayListValues().build(); - private final Map> finalModelFunc = new IdentityHashMap<>(); + private final Map> finalModelFuncs = new IdentityHashMap<>(); + private boolean funcsLoaded = false; public void register(ResourceLocation block, NonNullFunction func) { modelFuncs.put(block, func); @@ -24,16 +25,18 @@ public class CustomBlockModels { public void forEach(NonNullBiConsumer> consumer) { loadEntriesIfMissing(); - finalModelFunc.forEach(consumer); + finalModelFuncs.forEach(consumer); } private void loadEntriesIfMissing() { - if (finalModelFunc.isEmpty()) + if (!funcsLoaded) { loadEntries(); + funcsLoaded = true; + } } private void loadEntries() { - finalModelFunc.clear(); + finalModelFuncs.clear(); modelFuncs.asMap().forEach((location, funcList) -> { Block block = ForgeRegistries.BLOCKS.getValue(location); if (block == null) { @@ -49,7 +52,7 @@ public class CustomBlockModels { } } - finalModelFunc.put(block, finalFunc); + finalModelFuncs.put(block, finalFunc); }); } diff --git a/src/main/java/com/simibubi/create/foundation/item/render/CreateCustomRenderedItemModel.java b/src/main/java/com/simibubi/create/foundation/item/render/CreateCustomRenderedItemModel.java deleted file mode 100644 index b87d39b5e..000000000 --- a/src/main/java/com/simibubi/create/foundation/item/render/CreateCustomRenderedItemModel.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.simibubi.create.foundation.item.render; - -import com.simibubi.create.Create; - -import net.minecraft.client.resources.model.BakedModel; - -public abstract class CreateCustomRenderedItemModel extends CustomRenderedItemModel { - - public CreateCustomRenderedItemModel(BakedModel template, String basePath) { - super(template, Create.ID, basePath); - } - -} diff --git a/src/main/java/com/simibubi/create/foundation/item/render/CustomItemModels.java b/src/main/java/com/simibubi/create/foundation/item/render/CustomItemModels.java index 289281528..0bfd5836f 100644 --- a/src/main/java/com/simibubi/create/foundation/item/render/CustomItemModels.java +++ b/src/main/java/com/simibubi/create/foundation/item/render/CustomItemModels.java @@ -17,6 +17,7 @@ public class CustomItemModels { private final Multimap> modelFuncs = MultimapBuilder.hashKeys().arrayListValues().build(); private final Map> finalModelFuncs = new IdentityHashMap<>(); + private boolean funcsLoaded = false; public void register(ResourceLocation item, NonNullFunction func) { modelFuncs.put(item, func); @@ -28,8 +29,10 @@ public class CustomItemModels { } private void loadEntriesIfMissing() { - if (finalModelFuncs.isEmpty()) + if (!funcsLoaded) { loadEntries(); + funcsLoaded = true; + } } private void loadEntries() { diff --git a/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModel.java b/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModel.java index 21e6664c2..79cd72773 100644 --- a/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModel.java +++ b/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModel.java @@ -1,30 +1,15 @@ package com.simibubi.create.foundation.item.render; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.client.renderer.block.model.ItemTransforms; import net.minecraft.client.resources.model.BakedModel; -import net.minecraft.client.resources.model.BlockModelRotation; -import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.client.event.ModelBakeEvent; import net.minecraftforge.client.model.BakedModelWrapper; -import net.minecraftforge.client.model.ForgeModelBakery; -public abstract class CustomRenderedItemModel extends BakedModelWrapper { +public class CustomRenderedItemModel extends BakedModelWrapper { - protected String namespace; - protected String basePath; - protected Map partials = new HashMap<>(); - - public CustomRenderedItemModel(BakedModel template, String namespace, String basePath) { - super(template); - this.namespace = namespace; - this.basePath = basePath; + public CustomRenderedItemModel(BakedModel originalModel) { + super(originalModel); } @Override @@ -34,42 +19,14 @@ public abstract class CustomRenderedItemModel extends BakedModelWrapper getModelLocations() { - return partials.keySet().stream().map(this::getPartialModelLocation).collect(Collectors.toList()); - } - - protected void addPartials(String... partials) { - for (String name : partials) - this.partials.put(name, null); - } - - public void loadPartials(ModelBakeEvent event) { - ForgeModelBakery modelLoader = event.getModelLoader(); - for (String name : partials.keySet()) - partials.put(name, loadPartial(modelLoader, name)); - } - - @SuppressWarnings("deprecation") - protected BakedModel loadPartial(ForgeModelBakery modelLoader, String name) { - return modelLoader.bake(getPartialModelLocation(name), BlockModelRotation.X0_Y0); - } - - protected ResourceLocation getPartialModelLocation(String name) { - return new ResourceLocation(namespace, "item/" + basePath + "/" + name); - } - } diff --git a/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModelRenderer.java b/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModelRenderer.java index 08f7a990a..b7a46610d 100644 --- a/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModelRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItemModelRenderer.java @@ -1,34 +1,22 @@ package com.simibubi.create.foundation.item.render; -import java.util.HashSet; -import java.util.Set; - import com.mojang.blaze3d.vertex.PoseStack; -import com.tterrag.registrate.util.nullness.NonNullBiConsumer; -import com.tterrag.registrate.util.nullness.NonNullFunction; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.block.model.ItemTransforms; -import net.minecraft.client.resources.model.BakedModel; -import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import net.minecraftforge.client.RenderProperties; -import net.minecraftforge.registries.ForgeRegistries; -public abstract class CustomRenderedItemModelRenderer extends BlockEntityWithoutLevelRenderer { - - private static final Set ITEMS = new HashSet<>(); +public abstract class CustomRenderedItemModelRenderer extends BlockEntityWithoutLevelRenderer { public CustomRenderedItemModelRenderer() { super(null, null); } @Override - @SuppressWarnings("unchecked") public void renderByItem(ItemStack stack, ItemTransforms.TransformType transformType, PoseStack ms, MultiBufferSource buffer, int light, int overlay) { - M mainModel = (M) Minecraft.getInstance() + CustomRenderedItemModel mainModel = (CustomRenderedItemModel) Minecraft.getInstance() .getItemRenderer() .getModel(stack, null, null, 0); PartialItemModelRenderer renderer = PartialItemModelRenderer.of(stack, transformType, ms, buffer, overlay); @@ -39,30 +27,7 @@ public abstract class CustomRenderedItemModelRenderer> consumer) { - for (Item item : ITEMS) { - if (ForgeRegistries.ITEMS.containsValue(item)) { - BlockEntityWithoutLevelRenderer renderer = RenderProperties.get(item).getItemStackRenderer(); - if (renderer instanceof CustomRenderedItemModelRenderer customRenderer) { - consumer.accept(item, customRenderer::createModel); - } - } - } - } - } diff --git a/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItems.java b/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItems.java index 7a29d0d14..0c7052ec8 100644 --- a/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItems.java +++ b/src/main/java/com/simibubi/create/foundation/item/render/CustomRenderedItems.java @@ -1,48 +1,45 @@ package com.simibubi.create.foundation.item.render; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.Map; +import java.util.Iterator; +import java.util.Set; +import java.util.function.Consumer; -import com.tterrag.registrate.util.nullness.NonNullBiConsumer; -import com.tterrag.registrate.util.nullness.NonNullFunction; - -import net.minecraft.client.resources.model.BakedModel; -import net.minecraft.resources.ResourceLocation; +import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; import net.minecraft.world.item.Item; +import net.minecraftforge.client.RenderProperties; import net.minecraftforge.registries.ForgeRegistries; public class CustomRenderedItems { - private final Map> modelFuncs = new HashMap<>(); - private final Map> finalModelFuncs = new IdentityHashMap<>(); + private static final Set ITEMS = new ReferenceOpenHashSet<>(); + private static boolean itemsFiltered = false; - public void register(ResourceLocation item, - NonNullFunction func) { - modelFuncs.put(item, func); + /** + * Track an item that uses a subclass of {@link CustomRenderedItemModelRenderer} as its custom renderer + * to automatically register {@link #createModel} to {@link CustomRenderedItems} on client setup so that + * its model can be swapped. + * @param item The item that should have its model swapped. + */ + public static void register(Item item) { + ITEMS.add(item); } - public void forEach( - NonNullBiConsumer> consumer) { - loadEntriesIfMissing(); - finalModelFuncs.forEach(consumer); - } - - private void loadEntriesIfMissing() { - if (finalModelFuncs.isEmpty()) - loadEntries(); - } - - private void loadEntries() { - finalModelFuncs.clear(); - CustomRenderedItemModelRenderer.acceptModelFuncs(finalModelFuncs::put); - modelFuncs.forEach((location, func) -> { - Item item = ForgeRegistries.ITEMS.getValue(location); - if (item == null) { - return; + /** + * This method must not be called before item registration is finished! + */ + public static void forEach(Consumer consumer) { + if (!itemsFiltered) { + Iterator iterator = ITEMS.iterator(); + while (iterator.hasNext()) { + Item item = iterator.next(); + if (!ForgeRegistries.ITEMS.containsValue(item) || + !(RenderProperties.get(item).getItemStackRenderer() instanceof CustomRenderedItemModelRenderer)) { + iterator.remove(); + } } - finalModelFuncs.put(item, func); - }); + itemsFiltered = true; + } + ITEMS.forEach(consumer); } } diff --git a/src/main/java/com/simibubi/create/foundation/item/render/SimpleCustomRenderer.java b/src/main/java/com/simibubi/create/foundation/item/render/SimpleCustomRenderer.java index 539dd5b60..c1d897554 100644 --- a/src/main/java/com/simibubi/create/foundation/item/render/SimpleCustomRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/item/render/SimpleCustomRenderer.java @@ -5,19 +5,19 @@ import net.minecraftforge.client.IItemRenderProperties; public class SimpleCustomRenderer implements IItemRenderProperties { - protected CustomRenderedItemModelRenderer renderer; + protected CustomRenderedItemModelRenderer renderer; - protected SimpleCustomRenderer(CustomRenderedItemModelRenderer renderer) { + protected SimpleCustomRenderer(CustomRenderedItemModelRenderer renderer) { this.renderer = renderer; } - public static SimpleCustomRenderer create(Item item, CustomRenderedItemModelRenderer renderer) { - CustomRenderedItemModelRenderer.registerForSwapping(item); + public static SimpleCustomRenderer create(Item item, CustomRenderedItemModelRenderer renderer) { + CustomRenderedItems.register(item); return new SimpleCustomRenderer(renderer); } @Override - public CustomRenderedItemModelRenderer getItemStackRenderer() { + public CustomRenderedItemModelRenderer getItemStackRenderer() { return renderer; } diff --git a/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java b/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java index 25c4338d8..417ecc078 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java +++ b/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java @@ -17,15 +17,12 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraftforge.client.event.ModelBakeEvent; -import net.minecraftforge.client.event.ModelRegistryEvent; -import net.minecraftforge.client.model.ForgeModelBakery; import net.minecraftforge.eventbus.api.IEventBus; public class ModelSwapper { protected CustomBlockModels customBlockModels = new CustomBlockModels(); protected CustomItemModels customItemModels = new CustomItemModels(); - protected CustomRenderedItems customRenderedItems = new CustomRenderedItems(); public CustomBlockModels getCustomBlockModels() { return customBlockModels; @@ -35,32 +32,15 @@ public class ModelSwapper { return customItemModels; } - public CustomRenderedItems getCustomRenderedItems() { - return customRenderedItems; - } - - public void onModelRegistry(ModelRegistryEvent event) { - customRenderedItems.forEach((item, modelFunc) -> modelFunc.apply(null) - .getModelLocations() - .forEach(ForgeModelBakery::addSpecialModel)); - } - public void onModelBake(ModelBakeEvent event) { Map modelRegistry = event.getModelRegistry(); customBlockModels.forEach((block, modelFunc) -> swapModels(modelRegistry, getAllBlockStateModelLocations(block), modelFunc)); customItemModels.forEach((item, modelFunc) -> swapModels(modelRegistry, getItemModelLocation(item), modelFunc)); - customRenderedItems.forEach((item, modelFunc) -> { - swapModels(modelRegistry, getItemModelLocation(item), m -> { - CustomRenderedItemModel swapped = modelFunc.apply(m); - swapped.loadPartials(event); - return swapped; - }); - }); + CustomRenderedItems.forEach(item -> swapModels(modelRegistry, getItemModelLocation(item), CustomRenderedItemModel::new)); } public void registerListeners(IEventBus modEventBus) { - modEventBus.addListener(this::onModelRegistry); modEventBus.addListener(this::onModelBake); }