Merge branch 'main' into feature/networking

This commit is contained in:
Frank Bauer 2021-08-01 14:24:28 +02:00
commit 433aa7a1c7
20 changed files with 241 additions and 36 deletions

View file

@ -2,9 +2,13 @@ package ru.bclib.mixin.client;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.renderer.block.BlockModelShaper;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.multipart.MultiPart;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.client.resources.model.UnbakedModel;
@ -13,6 +17,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
@ -21,9 +26,13 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.bclib.api.ModIntegrationAPI;
import ru.bclib.client.render.EmissiveTextureInfo;
import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.ItemModelProvider;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@ -58,24 +67,25 @@ public abstract class ModelBakeryMixin {
BlockModelProvider provider = (BlockModelProvider) block;
if (!resourceManager.hasResource(storageID)) {
BlockState defaultState = block.defaultBlockState();
ResourceLocation defaultStateID = BlockModelShaper.stateToModelLocation(blockID, defaultState);
UnbakedModel defaultModel = provider.getModelVariant(defaultStateID, defaultState, cache);
cache.put(blockID, defaultModel);
topLevel.put(blockID, defaultModel);
ImmutableList<BlockState> states = block.getStateDefinition().getPossibleStates();
if (states.size() == 1) {
ResourceLocation stateID = BlockModelShaper.stateToModelLocation(blockID, block.defaultBlockState());
cache.put(stateID, defaultModel);
topLevel.put(stateID, defaultModel);
BlockState defaultState = block.defaultBlockState();
ResourceLocation defaultStateID = BlockModelShaper.stateToModelLocation(blockID, defaultState);
UnbakedModel defaultModel = provider.getModelVariant(defaultStateID, defaultState, cache);
if (defaultModel instanceof MultiPart) {
states.forEach(blockState -> {
ResourceLocation stateID = BlockModelShaper.stateToModelLocation(blockID, blockState);
topLevel.put(stateID, defaultModel);
cache.put(stateID, defaultModel);
});
}
else {
states.forEach(blockState -> {
ResourceLocation stateID = BlockModelShaper.stateToModelLocation(blockID, blockState);
BlockModel model = provider.getBlockModel(stateID, blockState);
cache.put(stateID, model != null ? model : defaultModel);
UnbakedModel model = stateID.equals(defaultStateID) ? defaultModel : provider.getModelVariant(stateID, blockState, cache);
topLevel.put(stateID, model);
cache.put(stateID, model);
});
}
}
@ -85,8 +95,8 @@ public abstract class ModelBakeryMixin {
if (!resourceManager.hasResource(storageID)) {
ResourceLocation itemID = new ModelResourceLocation(blockID.getNamespace(), blockID.getPath(), "inventory");
BlockModel model = provider.getItemModel(itemID);
cache.put(itemID, model);
topLevel.put(itemID, model);
cache.put(itemID, model);
}
}
});
@ -98,8 +108,8 @@ public abstract class ModelBakeryMixin {
ResourceLocation itemID = new ModelResourceLocation(registryID.getNamespace(), registryID.getPath(), "inventory");
ItemModelProvider provider = (ItemModelProvider) item;
BlockModel model = provider.getItemModel(registryID);
cache.put(itemID, model);
topLevel.put(itemID, model);
cache.put(itemID, model);
}
});
@ -110,4 +120,44 @@ public abstract class ModelBakeryMixin {
topLevelModels.putAll(topLevel);
unbakedCache.putAll(cache);
}
@Inject(method = "<init>*", at = @At("TAIL"))
private void bclib_findEmissiveModels(ResourceManager resourceManager, BlockColors blockColors, ProfilerFiller profiler, int mipmap, CallbackInfo info) {
if (!ModIntegrationAPI.hasCanvas()) {
return;
}
Map<ResourceLocation, UnbakedModel> cacheCopy = new HashMap<>(unbakedCache);
Set<Pair<String, String>> strings = Sets.newConcurrentHashSet();
Registry.BLOCK.keySet().forEach(blockID -> {
Block block = Registry.BLOCK.get(blockID);
ImmutableList<BlockState> states = block.getStateDefinition().getPossibleStates();
boolean addBlock = false;
for (BlockState state: states) {
ResourceLocation stateID = BlockModelShaper.stateToModelLocation(blockID, state);
UnbakedModel model = cacheCopy.get(stateID);
if (model == null) {
continue;
}
Collection<Material> materials = model.getMaterials(cacheCopy::get, strings);
if (materials == null) {
continue;
}
for (Material material: materials) {
if (EmissiveTextureInfo.isEmissiveTexture(material.texture())) {
addBlock = true;
break;
}
}
if (addBlock) {
break;
}
}
if (addBlock) {
EmissiveTextureInfo.addBlock(blockID);
}
});
}
}

View file

@ -0,0 +1,58 @@
package ru.bclib.mixin.client;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.FallbackResourceManager;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.SimpleReloadableResourceManager;
import net.minecraft.server.packs.resources.SimpleResource;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.bclib.BCLib;
import ru.bclib.api.ModIntegrationAPI;
import ru.bclib.client.render.EmissiveTextureInfo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
@Mixin(SimpleReloadableResourceManager.class)
public class SimpleReloadableResourceManagerMixin {
@Final
@Shadow
private Map<String, FallbackResourceManager> namespacedPacks;
private ResourceLocation bclib_alphaEmissionMaterial = BCLib.makeID("materialmaps/block/alpha_emission.json");
@Inject(method = "getResource", at = @At("HEAD"), cancellable = true)
private void bclib_getResource(ResourceLocation resourceLocation, CallbackInfoReturnable<Resource> info) throws IOException {
if (!ModIntegrationAPI.hasCanvas()) {
return;
}
if (!resourceLocation.getPath().startsWith("materialmaps")) {
return;
}
if (!resourceLocation.getPath().contains("/block/")) {
return;
}
String name = resourceLocation.getPath().replace("materialmaps/block/", "").replace(".json", "");
ResourceLocation blockID = new ResourceLocation(resourceLocation.getNamespace(), name);
if (!EmissiveTextureInfo.isEmissiveBlock(blockID)) {
return;
}
ResourceManager resourceManager = this.namespacedPacks.get(resourceLocation.getNamespace());
if (resourceManager != null && !resourceManager.hasResource(resourceLocation)) {
info.setReturnValue(resourceManager.getResource(bclib_alphaEmissionMaterial));
}
}
}

View file

@ -14,18 +14,22 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ru.bclib.BCLib;
import ru.bclib.client.render.EmissiveTextureInfo;
import java.io.IOException;
@Mixin(TextureAtlas.class)
public class TextureAtlasMixin {
private static final int EMISSIVE_ALPHA = 253 << 24;
private static final int EMISSIVE_ALPHA = 254 << 24;
private boolean bclib_modifyAtlas;
@Inject(method = "<init>*", at = @At("TAIL"))
private void bclib_onAtlasInit(ResourceLocation resourceLocation, CallbackInfo info) {
boolean hasOptifine = FabricLoader.getInstance().isModLoaded("optifabric");
bclib_modifyAtlas = !hasOptifine && resourceLocation.toString().equals("minecraft:textures/atlas/blocks.png");
if (bclib_modifyAtlas) {
EmissiveTextureInfo.clear();
}
}
@Inject(method = "load(Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite$Info;IIIII)Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;", at = @At("HEAD"), cancellable = true)
@ -91,6 +95,7 @@ public class TextureAtlasMixin {
posY,
sprite
);
EmissiveTextureInfo.addTexture(location);
info.setReturnValue(result);
}
}