Skip to content

Commit 19650af

Browse files
committed
custom in hand model registry
1 parent 66f0482 commit 19650af

17 files changed

Lines changed: 154 additions & 53 deletions

File tree

common/src/main/java/net/lcc/sollib/SolTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package net.lcc.sollib;
22

3+
import net.lcc.sollib.api.client.SolClientRegistries;
34
import net.lcc.sollib.api.common.registry.SolModContainer;
5+
import net.minecraft.world.item.Items;
46

57
public class SolTest {
68
public static final SolModContainer MOD = new SolModContainer("SolTest", "soltest");
79

810
public static void lyof() {
11+
SolClientRegistries.ITEM_MODEL.register(Items.DIAMOND);
912
}
1013

1114

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package net.lcc.sollib.api.client;
22

3+
import net.lcc.sollib.api.client.model.item.SItemModelRegistry;
34
import net.lcc.sollib.api.client.render.block.SBlockRendererRegistry;
45
import net.lcc.sollib.api.client.render.item.SItemRendererRegistry;
56
import net.lcc.sollib.api.client.ui.bossbar.SBossBarRegistry;
67

78
public class SolClientRegistries {
9+
public static final SItemModelRegistry ITEM_MODEL = SItemModelRegistry.INSTANCE;
810
public static class Render {
9-
public static final SItemRendererRegistry ITEM = new SItemRendererRegistry();
10-
public static final SBlockRendererRegistry BLOCK = new SBlockRendererRegistry();
11+
public static final SItemRendererRegistry ITEM = SItemRendererRegistry.INSTANCE;
12+
public static final SBlockRendererRegistry BLOCK = SBlockRendererRegistry.INSTANCE;
1113
}
12-
public static final SBossBarRegistry BOSS_BAR = new SBossBarRegistry();
14+
public static final SBossBarRegistry BOSS_BAR = SBossBarRegistry.INSTANCE;
1315
}

common/src/main/java/net/lcc/sollib/api/client/model/ModelPartUtils.java renamed to common/src/main/java/net/lcc/sollib/api/client/model/entity/ModelPartUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package net.lcc.sollib.api.client.model;
1+
package net.lcc.sollib.api.client.model.entity;
22

3-
import net.lcc.sollib.api.client.model.inject.IModelPartExtension;
3+
import net.lcc.sollib.api.client.model.entity.inject.IModelPartExtension;
44
import net.lcc.sollib.api.common.logger.SolLogger;
55
import net.minecraft.client.model.geom.ModelPart;
66
import net.minecraft.client.model.geom.builders.LayerDefinition;
@@ -16,7 +16,7 @@ public class ModelPartUtils {
1616
/**
1717
* @param texturedData LayerDefinition to extract model parts from
1818
* @return {@code List<ModelPart>}
19-
* @since 1.0.0
19+
* @since 1.0
2020
*/
2121
public static List<ModelPart> collectAllModelParts(LayerDefinition texturedData) {
2222
ModelPart root = texturedData.bakeRoot();
@@ -30,7 +30,7 @@ public static List<ModelPart> collectAllModelParts(LayerDefinition texturedData)
3030
* @param parts List of model parts to look into
3131
* @return {@link ModelPart}
3232
* @throws NullPointerException in cases of absence of looking part, method may return null
33-
* @since 1.0.0
33+
* @since 1.0
3434
*/
3535
@Nullable
3636
public static ModelPart getPart(Triple<Float, Float, Float> partSize, List<ModelPart> parts) {
@@ -46,7 +46,7 @@ public static ModelPart getPart(Triple<Float, Float, Float> partSize, List<Model
4646
* @param parts List of model parts to look into
4747
* @return {@link ModelPart}
4848
* @throws NullPointerException in cases of absence of looking part, method may return null
49-
* @since 1.0.0
49+
* @since 1.0
5050
*/
5151
@Nullable
5252
public static ModelPart getPart(String partName, List<ModelPart> parts) {
@@ -60,7 +60,7 @@ public static ModelPart getPart(String partName, List<ModelPart> parts) {
6060
/**
6161
* @param current ModelPart to extract child parts from
6262
* @param out Result of model parts extraction
63-
* @since 1.0.0
63+
* @since 1.0
6464
*/
6565
private static void collectRecursive(ModelPart current, List<ModelPart> out) {
6666
out.add(current);

common/src/main/java/net/lcc/sollib/api/client/model/inject/IModelPartExtension.java renamed to common/src/main/java/net/lcc/sollib/api/client/model/entity/inject/IModelPartExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lcc.sollib.api.client.model.inject;
1+
package net.lcc.sollib.api.client.model.entity.inject;
22

33
import net.minecraft.client.model.geom.ModelPart;
44

common/src/main/java/net/lcc/sollib/api/client/model/inject/IModelPartsAccessor.java renamed to common/src/main/java/net/lcc/sollib/api/client/model/entity/inject/IModelPartsAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lcc.sollib.api.client.model.inject;
1+
package net.lcc.sollib.api.client.model.entity.inject;
22

33
import net.minecraft.client.model.geom.ModelPart;
44

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.lcc.sollib.api.client.model.item;
2+
3+
import net.minecraft.client.renderer.entity.ItemRenderer;
4+
import net.minecraft.client.resources.model.ModelBakery;
5+
import net.minecraft.client.resources.model.ModelResourceLocation;
6+
import net.minecraft.core.registries.BuiltInRegistries;
7+
import net.minecraft.resources.ResourceLocation;
8+
import net.minecraft.world.item.Item;
9+
import net.minecraft.world.item.ItemStack;
10+
import net.minecraft.world.level.ItemLike;
11+
import org.jetbrains.annotations.ApiStatus;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Set;
18+
import java.util.function.Consumer;
19+
20+
public class SItemModelRegistry {
21+
public static final SItemModelRegistry INSTANCE = new SItemModelRegistry();
22+
private SItemModelRegistry() {}
23+
24+
private final Map<Item, ModelResourceLocation> INSTANCES = new HashMap<>();
25+
26+
/**
27+
* Registers an item to have a different held model
28+
*/
29+
public void register(ItemLike item) {
30+
ResourceLocation id = BuiltInRegistries.ITEM.getKey(item.asItem());
31+
INSTANCES.putIfAbsent(item.asItem(),
32+
new ModelResourceLocation(id.getNamespace(), id.getPath() + "_in_hand", "inventory"));
33+
}
34+
35+
@ApiStatus.Internal
36+
public void load(Consumer<ModelResourceLocation> loader) {
37+
for (ModelResourceLocation model : INSTANCES.values())
38+
loader.accept(model);
39+
}
40+
41+
@ApiStatus.Internal
42+
@Nullable
43+
public ModelResourceLocation getModel(ItemStack stack) {
44+
return INSTANCES.get(stack.getItem());
45+
}
46+
}

common/src/main/java/net/lcc/sollib/api/client/render/block/IBlockRenderer.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,16 @@
99
import net.minecraft.world.level.block.state.BlockState;
1010
import org.jetbrains.annotations.Nullable;
1111

12+
import java.util.function.BiConsumer;
13+
1214
@FunctionalInterface
1315
public interface IBlockRenderer {
14-
/**
15-
* Renders a BlockState at the specified BlockPos <br/>
16-
* Implementation varies depending on Sodium presence, prefer using this over {@link BlockRenderDispatcher} or something else
17-
*/
18-
@FunctionalInterface
19-
interface ARenderer {
20-
void renderBlock(BlockPos pos, BlockState state);
21-
}
22-
2316
/**
2417
*
25-
* @param renderer An {@link ARenderer}, whose implementation depends on rendering mods present
18+
* @param renderer A block renderer
2619
* @param poseStack Will be null if Sodium is present!
2720
* @param vertexConsumer Will be null if Sodium is present!
2821
*/
29-
void render(ARenderer renderer, BlockState state, BlockPos pos, BlockAndTintGetter getter,
22+
void render(BiConsumer<BlockPos, BlockState> renderer, BlockState state, BlockPos pos, BlockAndTintGetter getter,
3023
RandomSource random, @Nullable PoseStack poseStack, @Nullable VertexConsumer vertexConsumer);
3124
}

common/src/main/java/net/lcc/sollib/api/client/render/block/ISodiumBlockRenderer.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

common/src/main/java/net/lcc/sollib/api/client/render/block/SBlockRendererRegistry.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111

1212
import java.util.LinkedHashMap;
1313
import java.util.Map;
14+
import java.util.function.BiConsumer;
1415
import java.util.function.Predicate;
1516

1617
public class SBlockRendererRegistry {
18+
public static final SBlockRendererRegistry INSTANCE = new SBlockRendererRegistry();
19+
private SBlockRendererRegistry() {}
20+
1721
private final Map<Predicate<BlockState>, IBlockRenderer> INSTANCES = new LinkedHashMap<>();
1822

1923
/**
@@ -53,12 +57,12 @@ public void apply(BlockRenderDispatcher instance, BlockState state, BlockPos pos
5357
* @since 1.0
5458
*/
5559
@ApiStatus.Internal
56-
public void apply(IBlockRenderer.ARenderer blockRenderer, BlockAndTintGetter view, BlockState state, BlockPos pos, long seed) {
60+
public void apply(BiConsumer<BlockPos, BlockState> renderer, BlockAndTintGetter view, BlockState state, BlockPos pos, long seed) {
5761
if (state == null) return;
5862

5963
for (Map.Entry<Predicate<BlockState>, IBlockRenderer> entry : INSTANCES.entrySet()) {
6064
if (!entry.getKey().test(state)) continue;
61-
entry.getValue().render(blockRenderer, state, pos, view, RandomSource.create(seed), null, null);
65+
entry.getValue().render(renderer, state, pos, view, RandomSource.create(seed), null, null);
6266
}
6367
}
6468
}

common/src/main/java/net/lcc/sollib/api/client/render/item/SItemRendererRegistry.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import java.util.function.Predicate;
1212

1313
public class SItemRendererRegistry {
14+
public static final SItemRendererRegistry INSTANCE = new SItemRendererRegistry();
15+
private SItemRendererRegistry() {}
16+
1417
private final Map<Predicate<ItemStack>, IItemRenderer> INSTANCES = new LinkedHashMap<>();
1518

1619
/**

0 commit comments

Comments
 (0)