70 lines
3.2 KiB
Java
70 lines
3.2 KiB
Java
package net.minecraft.client.data.models.model;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.common.collect.ImmutableSet;
|
|
import com.google.common.collect.Streams;
|
|
import com.google.gson.JsonObject;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.function.BiConsumer;
|
|
import java.util.function.Function;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.level.block.Block;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class ModelTemplate {
|
|
private final Optional<ResourceLocation> model;
|
|
private final Set<TextureSlot> requiredSlots;
|
|
private final Optional<String> suffix;
|
|
|
|
public ModelTemplate(Optional<ResourceLocation> model, Optional<String> suffix, TextureSlot... requiredSlots) {
|
|
this.model = model;
|
|
this.suffix = suffix;
|
|
this.requiredSlots = ImmutableSet.copyOf(requiredSlots);
|
|
}
|
|
|
|
public ResourceLocation getDefaultModelLocation(Block block) {
|
|
return ModelLocationUtils.getModelLocation(block, (String)this.suffix.orElse(""));
|
|
}
|
|
|
|
public ResourceLocation create(Block block, TextureMapping textureMapping, BiConsumer<ResourceLocation, ModelInstance> output) {
|
|
return this.create(ModelLocationUtils.getModelLocation(block, (String)this.suffix.orElse("")), textureMapping, output);
|
|
}
|
|
|
|
public ResourceLocation createWithSuffix(Block block, String suffix, TextureMapping textureMapping, BiConsumer<ResourceLocation, ModelInstance> output) {
|
|
return this.create(ModelLocationUtils.getModelLocation(block, suffix + (String)this.suffix.orElse("")), textureMapping, output);
|
|
}
|
|
|
|
public ResourceLocation createWithOverride(Block block, String suffix, TextureMapping textureMapping, BiConsumer<ResourceLocation, ModelInstance> output) {
|
|
return this.create(ModelLocationUtils.getModelLocation(block, suffix), textureMapping, output);
|
|
}
|
|
|
|
public ResourceLocation create(Item item, TextureMapping textureMapping, BiConsumer<ResourceLocation, ModelInstance> output) {
|
|
return this.create(ModelLocationUtils.getModelLocation(item, (String)this.suffix.orElse("")), textureMapping, output);
|
|
}
|
|
|
|
public ResourceLocation create(ResourceLocation modelLocation, TextureMapping textureMapping, BiConsumer<ResourceLocation, ModelInstance> output) {
|
|
Map<TextureSlot, ResourceLocation> map = this.createMap(textureMapping);
|
|
output.accept(modelLocation, (ModelInstance)() -> {
|
|
JsonObject jsonObject = new JsonObject();
|
|
this.model.ifPresent(resourceLocation -> jsonObject.addProperty("parent", resourceLocation.toString()));
|
|
if (!map.isEmpty()) {
|
|
JsonObject jsonObject2 = new JsonObject();
|
|
map.forEach((textureSlot, resourceLocation) -> jsonObject2.addProperty(textureSlot.getId(), resourceLocation.toString()));
|
|
jsonObject.add("textures", jsonObject2);
|
|
}
|
|
|
|
return jsonObject;
|
|
});
|
|
return modelLocation;
|
|
}
|
|
|
|
private Map<TextureSlot, ResourceLocation> createMap(TextureMapping textureMapping) {
|
|
return (Map<TextureSlot, ResourceLocation>)Streams.concat(this.requiredSlots.stream(), textureMapping.getForced())
|
|
.collect(ImmutableMap.toImmutableMap(Function.identity(), textureMapping::get));
|
|
}
|
|
}
|