-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ClientProxy.java
168 lines (152 loc) · 6.41 KB
/
ClientProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package snownee.snow.util;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.mojang.blaze3d.vertex.PoseStack;
import net.fabricmc.api.ClientModInitializer;
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.rendering.v1.EntityRendererRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockModelShaper;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import snownee.kiwi.loader.Platform;
import snownee.kiwi.util.GameObjectLookup;
import snownee.snow.CoreModule;
import snownee.snow.SnowRealMagic;
import snownee.snow.client.FallingSnowRenderer;
import snownee.snow.client.SnowClient;
import snownee.snow.client.SnowVariantMetadataSectionSerializer;
import snownee.snow.client.model.ModelDefinition;
import snownee.snow.client.model.SnowCoveredModel;
import snownee.snow.client.model.SnowVariantModel;
import snownee.snow.client.model.WrapperUnbakedModel;
public class ClientProxy implements ClientModInitializer {
public static BakedModel getBlockModel(BlockState state) {
return Minecraft.getInstance().getBlockRenderer().getBlockModel(state);
}
public static BakedModel getBlockModel(ResourceLocation location) {
return Minecraft.getInstance().getModelManager().getModel(location);
}
public static void onPlayerJoin() {
LocalPlayer player = Minecraft.getInstance().player;
if (player != null && Platform.isModLoaded("sodium") && !Platform.isModLoaded("indium")) {
player.sendSystemMessage(Component.literal("Please install §lIndium§r mod to make Snow! Real Magic! work with Sodium."));
}
}
public static void renderFallingBlock(
Entity entity,
BlockState state,
BlockPos pos,
PoseStack poseStack,
MultiBufferSource bufferSource) {
BlockPos blockpos = BlockPos.containing(entity.getX(), entity.getBoundingBox().maxY, entity.getZ());
poseStack.translate(-0.5D, 0.0D, -0.5D);
BlockRenderDispatcher dispatcher = Minecraft.getInstance().getBlockRenderer();
RenderType type = ItemBlockRenderTypes.getMovingBlockRenderType(state);
dispatcher.getModelRenderer().tesselateBlock(
entity.level(),
getBlockModel(state),
state,
blockpos,
poseStack,
bufferSource.getBuffer(type),
false,
RandomSource.create(42),
state.getSeed(pos),
OverlayTexture.NO_OVERLAY);
}
@Override
public void onInitializeClient() {
EntityRendererRegistry.register(CoreModule.ENTITY.getOrCreate(), FallingSnowRenderer::new);
ModelLoadingPlugin.register(ctx -> {
List<ResourceLocation> extraModels = Lists.newArrayList(SnowClient.OVERLAY_MODEL);
SnowClient.snowVariantMapping.clear();
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
ModelBakery.MODEL_LISTER.listMatchingResources(resourceManager).forEach((key, resource) -> {
ModelDefinition def;
try {
def = resource.metadata().getSection(SnowVariantMetadataSectionSerializer.SERIALIZER).orElse(null);
} catch (IOException e) {
return;
}
if (def == null || def.model == null) {
return;
}
SnowClient.snowVariantMapping.put(ModelBakery.MODEL_LISTER.fileToId(key), def);
extraModels.add(def.model);
if (def.overrideBlocks != null) {
for (ResourceLocation id : def.overrideBlocks) {
Block block = BuiltInRegistries.BLOCK.get(id);
if (block != Blocks.AIR) {
SnowClient.overrideBlocks.add(block);
}
}
}
});
ctx.addModels(extraModels);
Set<ModelResourceLocation> snowCoveredModelIds = Sets.newHashSet();
Map<UnbakedModel, UnbakedModel> transform = Maps.newHashMap();
for (var block : allSnowBlocks()) {
for (BlockState state : block.getStateDefinition().getPossibleStates()) {
ModelResourceLocation modelId = BlockModelShaper.stateToModelLocation(BuiltInRegistries.BLOCK.getKey(block), state);
snowCoveredModelIds.add(modelId);
}
}
ctx.modifyModelOnLoad().register(ModelModifier.WRAP_LAST_PHASE, (model, context) -> {
if (snowCoveredModelIds.contains(context.topLevelId())) {
return transform.computeIfAbsent(model, $ -> new WrapperUnbakedModel($, SnowCoveredModel::new));
}
return model;
});
ctx.modifyModelAfterBake().register(ModelModifier.WRAP_LAST_PHASE, (model, context) -> {
ModelState modelState = context.settings();
if (model == null || modelState.getClass() != Variant.class) {
return model;
}
ModelDefinition def = SnowClient.snowVariantMapping.get(context.resourceId());
if (def == null) {
return model;
}
Variant variantState = (Variant) modelState;
variantState = new Variant(def.model, variantState.getRotation(), variantState.isUvLocked(), variantState.getWeight());
BakedModel variantModel = context.baker().bake(def.model, variantState);
if (variantModel == null) {
return model;
}
return new SnowVariantModel(model, variantModel);
});
SnowClient.cachedOverlayModel = null;
SnowClient.cachedSnowModel = null;
});
}
public static List<Block> allSnowBlocks() {
return GameObjectLookup.all(Registries.BLOCK, SnowRealMagic.ID).toList();
}
}