Change models loading (still WIP)

This commit is contained in:
Aleksey 2021-05-18 16:51:41 +03:00
parent 2d23ca72ea
commit 744fe40a00
64 changed files with 437 additions and 269 deletions

View file

@ -23,9 +23,9 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.betterend.BetterEnd;
import ru.betterend.client.models.BlockModelProvider;
import ru.betterend.client.models.ModelProvider;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.world.generator.GeneratorOptions;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
@ -47,13 +47,13 @@ public abstract class ModelLoaderMixin {
protected abstract BlockModel loadBlockModel(ResourceLocation resourceLocation);
@Inject(method = "loadModel", at = @At("HEAD"), cancellable = true)
private void be_loadModels(ResourceLocation resourceLocation, CallbackInfo info) {
private void be_loadModels(ResourceLocation resourceLocation, CallbackInfo info) throws IOException {
if (resourceLocation instanceof ModelResourceLocation) {
String modId = resourceLocation.getNamespace();
String path = resourceLocation.getPath();
ResourceLocation clearLoc = new ResourceLocation(modId, path);
ModelResourceLocation modelLoc = (ModelResourceLocation) resourceLocation;
if (Objects.equals(modelLoc.getVariant(), "inventory")) {
ModelResourceLocation modelId = (ModelResourceLocation) resourceLocation;
if (Objects.equals(modelId.getVariant(), "inventory")) {
ResourceLocation itemLoc = new ResourceLocation(modId, "item/" + path);
ResourceLocation itemModelLoc = new ResourceLocation(modId, "models/" + itemLoc.getPath() + ".json");
if (!resourceManager.hasResource(itemModelLoc)) {
@ -62,30 +62,31 @@ public abstract class ModelLoaderMixin {
BlockModel model = ((ModelProvider) item).getModel(clearLoc);
if (model != null) {
model.name = itemLoc.toString();
cacheAndQueueDependencies(modelId, model);
unbakedCache.put(itemLoc, model);
} else {
model = loadBlockModel(itemLoc);
BetterEnd.LOGGER.warning("Error loading model: {}", itemLoc);
}
cacheAndQueueDependencies(modelLoc, model);
unbakedCache.put(itemLoc, model);
info.cancel();
}
}
} else {
ResourceLocation blockstateId = new ResourceLocation(modId, "blockstates/" + path + ".json");
if (!resourceManager.hasResource(blockstateId)) {
ResourceLocation stateLoc = new ResourceLocation(modId, "blockstates/" + path + ".json");
if (!resourceManager.hasResource(stateLoc)) {
Block block = Registry.BLOCK.get(clearLoc);
if (block instanceof BlockModelProvider) {
block.getStateDefinition().getPossibleStates().forEach(blockState -> {
System.out.println(blockState);
ModelResourceLocation stateLoc = BlockModelShaper.stateToModelLocation(clearLoc, blockState);
MultiVariant modelVariant = ((BlockModelProvider) block).getModelVariant(stateLoc, blockState);
Optional<BlockState> stateOptional = block.getStateDefinition().getPossibleStates().stream()
.filter(state -> modelId.equals(BlockModelShaper.stateToModelLocation(clearLoc, state)))
.findFirst();
if (stateOptional.isPresent()) {
MultiVariant modelVariant = ((BlockModelProvider) block).getModelVariant(modelId, stateOptional.get(), unbakedCache);
if (modelVariant != null) {
cacheAndQueueDependencies(stateLoc, modelVariant);
cacheAndQueueDependencies(modelId, modelVariant);
} else {
BetterEnd.LOGGER.warning("Error loading variant: {}", stateLoc);
BetterEnd.LOGGER.warning("Error loading variant: {}", modelId);
}
});
info.cancel();
info.cancel();
}
}
}
}
@ -93,39 +94,32 @@ public abstract class ModelLoaderMixin {
}
@Inject(method = "loadBlockModel", at = @At("HEAD"), cancellable = true)
private void be_loadModelPattern(ResourceLocation modelId, CallbackInfoReturnable<BlockModel> info) {
private void be_loadModelPattern(ResourceLocation modelId, CallbackInfoReturnable<BlockModel> info) throws IOException {
ResourceLocation modelLocation = new ResourceLocation(modelId.getNamespace(), "models/" + modelId.getPath() + ".json");
if (!resourceManager.hasResource(modelLocation)) {
BlockState blockState = ModelsHelper.getBlockState(modelId);
if (blockState != null) {
Block block = blockState.getBlock();
if (block instanceof BlockModelProvider) {
ResourceLocation blockId = Registry.BLOCK.getKey(block);
BlockModel model = ((BlockModelProvider) block).getBlockModel(blockId, blockState);
if (model != null) {
model.name = modelId.toString();
info.setReturnValue(model);
} else {
BetterEnd.LOGGER.warning("Error loading model: {}", modelId);
}
}
} else {
String[] data = modelId.getPath().split("/");
if (data.length > 1) {
ResourceLocation itemId = new ResourceLocation(modelId.getNamespace(), data[1]);
Optional<Block> block = Registry.BLOCK.getOptional(itemId);
if (block.isPresent()) {
if (block.get() instanceof ModelProvider) {
ModelProvider modelProvider = (ModelProvider) block.get();
BlockModel model = be_getModel(data, modelId, modelProvider);
info.setReturnValue(model);
String[] data = modelId.getPath().split("/");
if (data.length > 1) {
ResourceLocation itemId = new ResourceLocation(modelId.getNamespace(), data[1]);
Optional<Block> block = Registry.BLOCK.getOptional(itemId);
if (block.isPresent()) {
if (block.get() instanceof ModelProvider) {
ModelProvider modelProvider = (ModelProvider) block.get();
Optional<BlockModel> model = be_getModel(data, modelId, modelProvider);
if (model.isPresent()) {
info.setReturnValue(model.get());
} else {
throw new FileNotFoundException("Error loading model: " + modelId);
}
} else {
Optional<Item> item = Registry.ITEM.getOptional(itemId);
if (item.isPresent() && item.get() instanceof ModelProvider) {
ModelProvider modelProvider = (ModelProvider) item.get();
BlockModel model = be_getModel(data, modelId, modelProvider);
info.setReturnValue(model);
}
} else {
Optional<Item> item = Registry.ITEM.getOptional(itemId);
if (item.isPresent() && item.get() instanceof ModelProvider) {
ModelProvider modelProvider = (ModelProvider) item.get();
Optional<BlockModel> model = be_getModel(data, modelId, modelProvider);
if (model.isPresent()) {
info.setReturnValue(model.get());
} else {
throw new FileNotFoundException("Error loading model: " + modelId);
}
}
}
@ -133,8 +127,8 @@ public abstract class ModelLoaderMixin {
}
}
private BlockModel be_getModel(String[] data, ResourceLocation id, ModelProvider modelProvider) {
String pattern;
private Optional<BlockModel> be_getModel(String[] data, ResourceLocation id, ModelProvider modelProvider) {
Optional<String> pattern;
if (id.getPath().contains("item")) {
pattern = modelProvider.getModelString(id.getPath());
} else {
@ -144,10 +138,12 @@ public abstract class ModelLoaderMixin {
pattern = modelProvider.getModelString(data[1]);
}
}
BlockModel model = BlockModel.fromString(pattern);
model.name = id.toString();
return model;
if (pattern.isPresent()) {
BlockModel model = BlockModel.fromString(pattern.get());
model.name = id.toString();
return Optional.of(model);
}
return Optional.empty();
}
@ModifyVariable(method = "loadModel", ordinal = 2, at = @At(value = "INVOKE"))

View file

@ -10,7 +10,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.advancements.Advancement;
import net.minecraft.server.PlayerAdvancements;
import net.minecraft.server.level.ServerPlayer;
import ru.betterend.events.PlayerAdvancementsEvents;
import ru.betterend.events.PlayerAdvancementsCallback;
@Mixin(PlayerAdvancements.class)
public abstract class PlayerAdvancementsMixin {
@ -22,6 +22,6 @@ public abstract class PlayerAdvancementsMixin {
target = "Lnet/minecraft/advancements/AdvancementRewards;grant(Lnet/minecraft/server/level/ServerPlayer;)V",
shift = Shift.AFTER))
public void be_award(Advancement advancement, String criterionName, CallbackInfoReturnable<Boolean> info) {
PlayerAdvancementsEvents.PLAYER_ADVENCEMENT_COMPLETE.invoker().onAdvancementComplete(player, advancement, criterionName);
PlayerAdvancementsCallback.PLAYER_ADVANCEMENT_COMPLETE.invoker().onAdvancementComplete(player, advancement, criterionName);
}
}