Change models loading (still WIP)
This commit is contained in:
parent
2d23ca72ea
commit
744fe40a00
64 changed files with 437 additions and 269 deletions
|
@ -1,25 +1,45 @@
|
|||
package ru.betterend.client.models;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.minecraft.client.renderer.block.model.BlockModel;
|
||||
import net.minecraft.client.renderer.block.model.MultiVariant;
|
||||
import net.minecraft.client.resources.model.UnbakedModel;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import ru.betterend.BetterEnd;
|
||||
|
||||
import static net.minecraft.client.resources.model.ModelBakery.MISSING_MODEL_LOCATION;
|
||||
|
||||
public interface BlockModelProvider extends ModelProvider {
|
||||
String getStatesPattern(Reader data);
|
||||
ResourceLocation statePatternId();
|
||||
|
||||
@Nullable
|
||||
default BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
|
||||
String pattern = Patterns.createBlockSimple(resourceLocation.getPath());
|
||||
return BlockModel.fromString(pattern);
|
||||
Optional<String> pattern = Patterns.createBlockSimple(resourceLocation.getPath());
|
||||
return pattern.map(BlockModel::fromString).orElse(null);
|
||||
}
|
||||
|
||||
default MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState) {
|
||||
default MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
|
||||
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
|
||||
"block/" + resourceLocation.getPath());
|
||||
ModelsHelper.addBlockState(blockState, modelId);
|
||||
registerBlockModel(resourceLocation, modelId, blockState, modelCache);
|
||||
return ModelsHelper.createBlockSimple(modelId);
|
||||
}
|
||||
|
||||
default void registerBlockModel(ResourceLocation stateId, ResourceLocation modelId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
|
||||
if (!modelCache.containsKey(modelId)) {
|
||||
BlockModel model = getBlockModel(stateId, blockState);
|
||||
if (model != null) {
|
||||
modelCache.put(modelId, model);
|
||||
} else {
|
||||
BetterEnd.LOGGER.warning("Error loading model: {}", modelId);
|
||||
modelCache.put(modelId, modelCache.get(MISSING_MODEL_LOCATION));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,17 @@ package ru.betterend.client.models;
|
|||
import net.minecraft.client.renderer.block.model.BlockModel;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ModelProvider {
|
||||
String getModelString(String name);
|
||||
Optional<String> getModelString(String name);
|
||||
|
||||
default BlockModel getModel(ResourceLocation resourceLocation) {
|
||||
return createItemModel(resourceLocation.getPath());
|
||||
}
|
||||
|
||||
static BlockModel createItemModel(String name) {
|
||||
String pattern = Patterns.createItemGenerated("item/" + name);
|
||||
return BlockModel.fromString(pattern);
|
||||
Optional<String> pattern = Patterns.createItemGenerated("item/" + name);
|
||||
return pattern.map(BlockModel::fromString).orElse(null);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,9 @@ package ru.betterend.client.models;
|
|||
|
||||
import com.google.common.collect.*;
|
||||
import com.mojang.math.Transformation;
|
||||
import javafx.print.Collation;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.client.renderer.block.model.BlockModel;
|
||||
import net.minecraft.client.renderer.block.model.MultiVariant;
|
||||
import net.minecraft.client.renderer.block.model.Variant;
|
||||
|
@ -11,28 +14,15 @@ import net.minecraft.resources.ResourceLocation;
|
|||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class ModelsHelper {
|
||||
|
||||
private final static BiMap<BlockState, ResourceLocation> STATES_MAP;
|
||||
|
||||
@Nullable
|
||||
public static BlockState getBlockState(ResourceLocation resourceLocation) {
|
||||
BlockState blockState = STATES_MAP.inverse().get(resourceLocation);
|
||||
if (blockState != null) {
|
||||
STATES_MAP.remove(blockState);
|
||||
}
|
||||
return blockState;
|
||||
}
|
||||
|
||||
public static void addBlockState(BlockState blockState, ResourceLocation resourceLocation) {
|
||||
STATES_MAP.put(blockState, resourceLocation);
|
||||
}
|
||||
|
||||
public static BlockModel createBlockItem(ResourceLocation resourceLocation) {
|
||||
String pattern = Patterns.createJson(Patterns.ITEM_BLOCK, resourceLocation.getPath());
|
||||
return BlockModel.fromString(pattern);
|
||||
Optional<String> pattern = Patterns.createJson(Patterns.ITEM_BLOCK, resourceLocation.getPath());
|
||||
return pattern.map(BlockModel::fromString).orElse(null);
|
||||
}
|
||||
|
||||
public static MultiVariant createBlockSimple(ResourceLocation resourceLocation) {
|
||||
|
@ -41,7 +31,7 @@ public class ModelsHelper {
|
|||
}
|
||||
|
||||
public static MultiVariant createFacingModel(ResourceLocation resourceLocation, Direction facing) {
|
||||
BlockModelRotation rotation = BlockModelRotation.by(0, (int) facing.toYRot());
|
||||
BlockModelRotation rotation = BlockModelRotation.by(0, (int) facing.getOpposite().toYRot());
|
||||
Variant variant = new Variant(resourceLocation, rotation.getRotation(), false, 1);
|
||||
return new MultiVariant(Lists.newArrayList(variant));
|
||||
}
|
||||
|
@ -50,19 +40,15 @@ public class ModelsHelper {
|
|||
BlockModelRotation rotation = BlockModelRotation.X0_Y0;
|
||||
switch (axis) {
|
||||
case X: {
|
||||
rotation = BlockModelRotation.by(90, 0);
|
||||
rotation = BlockModelRotation.by(90, 90);
|
||||
break;
|
||||
}
|
||||
case Z: {
|
||||
rotation = BlockModelRotation.by(90, 90);
|
||||
rotation = BlockModelRotation.by(90, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Variant variant = new Variant(resourceLocation, rotation.getRotation(), false, 1);
|
||||
return new MultiVariant(Lists.newArrayList(variant));
|
||||
}
|
||||
|
||||
static {
|
||||
STATES_MAP = HashBiMap.create();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import java.io.Reader;
|
|||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
@ -117,14 +118,14 @@ public class Patterns {
|
|||
public final static ResourceLocation ITEM_HANDHELD = BetterEnd.makeID("patterns/item/pattern_item_handheld.json");
|
||||
public final static ResourceLocation ITEM_SPAWN_EGG = BetterEnd.makeID("patterns/item/pattern_item_spawn_egg.json");
|
||||
|
||||
public static String createItemGenerated(String name) {
|
||||
public static Optional<String> createItemGenerated(String name) {
|
||||
return createJson(ITEM_GENERATED, name);
|
||||
}
|
||||
|
||||
public static String createBlockSimple(String name) {
|
||||
public static Optional<String> createBlockSimple(String name) {
|
||||
return Patterns.createJson(Patterns.BLOCK_BASE, name, name);
|
||||
}
|
||||
public static String createBlockPillar(String name) {
|
||||
public static Optional<String> createBlockPillar(String name) {
|
||||
return Patterns.createJson(Patterns.BLOCK_PILLAR, name, name);
|
||||
}
|
||||
|
||||
|
@ -138,22 +139,22 @@ public class Patterns {
|
|||
}
|
||||
}
|
||||
|
||||
public static String createJson(ResourceLocation patternId, String parent, String block) {
|
||||
public static Optional<String> createJson(ResourceLocation patternId, String parent, String block) {
|
||||
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
|
||||
try (InputStream input = resourceManager.getResource(patternId).getInputStream()) {
|
||||
return createJson(new InputStreamReader(input, StandardCharsets.UTF_8), parent, block);
|
||||
return Optional.ofNullable(createJson(new InputStreamReader(input, StandardCharsets.UTF_8), parent, block));
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static String createJson(ResourceLocation patternId, String texture) {
|
||||
public static Optional<String> createJson(ResourceLocation patternId, String texture) {
|
||||
Map<String, String> textures = Maps.newHashMap();
|
||||
textures.put("%texture%", texture);
|
||||
return createJson(patternId, textures);
|
||||
}
|
||||
|
||||
public static String createJson(ResourceLocation patternId, Map<String, String> textures) {
|
||||
public static Optional<String> createJson(ResourceLocation patternId, Map<String, String> textures) {
|
||||
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();
|
||||
try (InputStream input = resourceManager.getResource(patternId).getInputStream()) {
|
||||
String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))
|
||||
|
@ -161,9 +162,9 @@ public class Patterns {
|
|||
for (Entry<String, String> texture : textures.entrySet()) {
|
||||
json = json.replace(texture.getKey(), texture.getValue());
|
||||
}
|
||||
return json;
|
||||
return Optional.of(json);
|
||||
} catch (Exception ex) {
|
||||
return "";
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue