This commit is contained in:
Shadowfacts 2018-12-24 11:36:59 -05:00
parent 46abfcba3b
commit 5718fb5e16
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 27 additions and 53 deletions

View File

@ -3,27 +3,49 @@ package net.shadowfacts.simplemultipart.client;
import net.fabricmc.fabric.api.client.model.ModelProvider;
import net.fabricmc.fabric.api.client.model.ModelProviderException;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.block.BlockModels;
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.render.model.json.ModelVariantMap;
import net.minecraft.client.render.model.json.WeightedUnbakedModel;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
/**
* @author shadowfacts
*/
public class MultipartModelProvider implements ModelProvider {
private static final Set<ModelIdentifier> multipartModels = new HashSet<>();
private final Map<ModelIdentifier, UnbakedModel> unbakedModels = new HashMap<>();
static void registerMultipartModels(ResourceManager resourceManager, Consumer<ModelIdentifier> adder) {
for (Multipart part : SimpleMultipart.MULTIPART) {
Identifier partId = SimpleMultipart.MULTIPART.getId(part);
for (MultipartState state : part.getStateFactory().getStates()) {
String variant = BlockModels.propertyMapToString(state.getEntries());
ModelIdentifier id = new ModelIdentifier(partId, variant);
multipartModels.add(id);
adder.accept(id);
}
}
}
@Override
public UnbakedModel load(Identifier id, Context context) throws ModelProviderException {
if (!(id instanceof ModelIdentifier)) {
@ -32,7 +54,7 @@ public class MultipartModelProvider implements ModelProvider {
ModelIdentifier modelId = (ModelIdentifier)id;
if (!SimpleMultipartClient.multipartModels.contains(modelId)) {
if (!multipartModels.contains(modelId)) {
return null;
}

View File

@ -2,41 +2,16 @@ package net.shadowfacts.simplemultipart.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.impl.client.model.ModelLoadingRegistryImpl;
import net.minecraft.client.render.block.BlockModels;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.shadowfacts.simplemultipart.SimpleMultipart;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
/**
* @author shadowfacts
*/
public class SimpleMultipartClient implements ClientModInitializer {
public static final Set<ModelIdentifier> multipartModels = new HashSet<>();
@Override
public void onInitializeClient() {
ModelLoadingRegistryImpl.INSTANCE.registerAppender(SimpleMultipartClient::registerMultipartModels);
ModelLoadingRegistryImpl.INSTANCE.registerAppender(MultipartModelProvider::registerMultipartModels);
ModelLoadingRegistryImpl.INSTANCE.registerProvider(resourceManager -> new MultipartModelProvider());
}
private static void registerMultipartModels(ResourceManager resourceManager, Consumer<ModelIdentifier> adder) {
for (Multipart part : SimpleMultipart.MULTIPART) {
Identifier partId = SimpleMultipart.MULTIPART.getId(part);
for (MultipartState state : part.getStateFactory().getStates()) {
String variant = BlockModels.propertyMapToString(state.getEntries());
ModelIdentifier id = new ModelIdentifier(partId, variant);
multipartModels.add(id);
adder.accept(id);
}
}
}
}

View File

@ -5,6 +5,8 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ExtendedBlockView;
/**
* Temporary until fabric-api #45 is merged
*
* @author shadowfacts
*/
public interface RenderStateProvider {

View File

@ -25,20 +25,9 @@ public abstract class MixinModelLoader {
@Shadow
private Map<Identifier, BakedModel> bakedModels;
@Shadow
public abstract void addModel(ModelIdentifier id);
@Inject(method = "<init>", at = @At("RETURN"))
public void addMultipartModel(ResourceManager manager, SpriteAtlasTexture texture, CallbackInfo info) {
bakedModels.put(new ModelIdentifier("simplemultipart:container#"), new MultipartContainerBakedModel());
}
// // temporary workaround until fabric-api #39 is merged
// @Inject(method = "addModel", at = @At("HEAD"))
// public void injectMultipartModels(ModelIdentifier id, CallbackInfo info) {
// if (id == ModelLoader.MISSING) {
// SimpleMultipartClient.getMultipartModelIds().forEach(this::addModel);
// }
// }
}

View File

@ -1,14 +0,0 @@
{
"type": "multipart:multipart",
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "minecraft:item",
"name": "multipart_test:red"
}
]
}
]
}

View File

@ -6,7 +6,7 @@
"entries": [
{
"type": "minecraft:item",
"name": "multipart_test:green"
"name": "multipart_test:test_part"
}
]
}