mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-28 07:35:51 +00:00
Start work on the ModelRegistry
This commit is contained in:
1 parent
3557102ca0
commit
3a366dec39
1 file changed
+53
@@ -0,0 +1,53 @@
|
||||
package com.gtnewhorizon.gtnhlib.client.model;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.gtnewhorizon.gtnhlib.block.BlockState;
|
||||
import com.gtnewhorizon.gtnhlib.block.DynamicModelCache;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.BakedModel;
|
||||
import com.gtnewhorizon.gtnhlib.json.MonopartState;
|
||||
import com.gtnewhorizon.gtnhlib.json.StateDef;
|
||||
import com.gtnewhorizon.gtnhlib.json.StateDeserializer;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
|
||||
public class ModelRegistry {
|
||||
private static final Gson GSON = new GsonBuilder().registerTypeAdapter(MonopartState.class, new StateDeserializer())
|
||||
.create();
|
||||
private static final DynamicModelCache<BlockState> CACHE = new DynamicModelCache<>(s -> bakeModel((BlockState) s), false);
|
||||
|
||||
private static BakedModel bakeModel(BlockState state) {
|
||||
final var block = state.block();
|
||||
final var meta = state.meta();
|
||||
|
||||
// Fetch the blockstate file
|
||||
final var name = BlockName.fromBlock(block);
|
||||
final var stateLocation = new ResourceLocation(name.domain, "blockstates/" + name.name);
|
||||
|
||||
final var resourceManager = Minecraft.getMinecraft().getResourceManager();
|
||||
try {
|
||||
final var stateResource = resourceManager.getResource(stateLocation);
|
||||
final var stateFile = GSON.fromJson(new InputStreamReader(stateResource.getInputStream()), StateDef.class);
|
||||
final var model = stateFile.selectModel()
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private record BlockName(String domain, String name) {
|
||||
private static BlockName fromBlock(Block block) {
|
||||
final String blockName = Block.blockRegistry.getNameForObject(block);
|
||||
|
||||
final int sepIdx = blockName.indexOf(":");
|
||||
final var domain = blockName.substring(0, sepIdx);
|
||||
final var name = blockName.substring(sepIdx);
|
||||
|
||||
return new BlockName(domain, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user