Skip to content

Commit

Permalink
Fixes to Torch Models EnvJson
Browse files Browse the repository at this point in the history
Fix a Crash for DimensionEnvJsonRule
Work a Bit More on Axiom Compat
  • Loading branch information
FirstMegaGame4 committed Feb 8, 2024
1 parent 8c2e69f commit e209555
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.firstmegagame4.env.driven.assets.client;

import com.moulberry.axiom.render.regions.ChunkedBlockRegion;
import com.moulberry.axiom.render.regions.MapBlockAndTintGetter;
import fr.firstmegagame4.env.driven.assets.client.impl.axiom.AxiomBlockEnvJsonVisitor;
import fr.firstmegagame4.env.driven.assets.client.impl.axiom.MappedBlockAndTintGetterInstance;
import fr.firstmegagame4.env.driven.assets.client.impl.env.json.BlockEnvJsonVisitor;
import fr.firstmegagame4.env.driven.assets.client.impl.env.json.ClientEnvJsonVisitor;
import fr.firstmegagame4.env.driven.assets.client.impl.env.json.EntityEnvJsonVisitor;
Expand Down Expand Up @@ -36,6 +38,12 @@ else if (FabricLoader.getInstance().isModLoaded("axiom")) {
if (view instanceof ChunkedBlockRegion region) {
return new AxiomBlockEnvJsonVisitor.ChunkedBlockVisitor(region, pos);
}
else if (view instanceof MapBlockAndTintGetter map) {
return new AxiomBlockEnvJsonVisitor.MapBlockVisitor(map, pos);
}
else if (view instanceof MappedBlockAndTintGetterInstance mapped) {
return new AxiomBlockEnvJsonVisitor.MappedBlockVisitor(mapped, pos);
}
}
return new EmptyVisitor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ public class EDAUtils {

public static final Logger LOGGER = LoggerFactory.getLogger("env_driven_assets");

public static boolean worldIsOf(World entry, RegistryKey<World> key) {
return EDAUtils.isOf(entry.getRegistryManager().get(RegistryKeys.WORLD).getEntry(entry), key);
}

public static <T> boolean isOf(RegistryEntry<T> entry, RegistryKey<T> key) {
return entry.matchesKey(key);
public static <T> boolean compareKeys(RegistryKey<T> left, RegistryKey<T> right) {
return left.getRegistry() == right.getRegistry() && left.getValue() == right.getValue();
}

public static boolean worldIsIn(World entry, TagKey<World> tag) {
return EDAUtils.isIn(entry.getRegistryManager(), RegistryKeys.WORLD, tag, entry.getRegistryManager().get(RegistryKeys.WORLD).getEntry(entry));
}

public static <T> boolean isIn(DynamicRegistryManager manager, RegistryKey<? extends Registry<T>> registry, TagKey<T> tag, RegistryEntry<T> entry) {
return manager.get(registry).getEntryList(tag).orElseThrow().contains(entry);
return manager.getOptional(registry).map(r -> r.getEntryList(tag).orElseThrow().contains(entry)).orElse(false);
}

public static boolean lookupSubmerged(BlockView world, BlockPos pos, Function<BlockPos, BlockState> stateFinder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static int produceBakingOutput(CommandContext<FabricClientCommandSource>
try {
FileWriter writer = new FileWriter(new File(
FabricLoader.getInstance().getGameDir().toFile(),
new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(Calendar.getInstance()) + ".eda-output.txt")
new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance()) + ".eda-output.txt")
);
MinecraftClient client = MinecraftClient.getInstance();
if (client == null) return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;

Expand All @@ -36,7 +37,7 @@ public ChunkedBlockVisitor(ChunkedBlockRegion region, BlockPos pos) {

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return EDAUtils.worldIsOf(this.fallback, dimensionKey);
return EDAUtils.compareKeys(this.fallback.getRegistryKey(), dimensionKey);
}

@Override
Expand Down Expand Up @@ -120,7 +121,7 @@ public MapBlockVisitor(MapBlockAndTintGetter map, BlockPos pos) {

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return EDAUtils.worldIsOf(this.fallback, dimensionKey);
return EDAUtils.compareKeys(this.fallback.getRegistryKey(), dimensionKey);
}

@Override
Expand Down Expand Up @@ -189,4 +190,88 @@ public boolean applyVoid(VoidEnvJsonRule.Localization localization) {
};
}
}

public static class MappedBlockVisitor implements EnvJsonVisitor {

private final BlockRenderView view;
private final World world;
private final BlockPos pos;

public MappedBlockVisitor(MappedBlockAndTintGetterInstance mapped, BlockPos pos) {
this.view = mapped.getView();
this.world = mapped.getWorld();
this.pos = pos;
}

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return EDAUtils.compareKeys(this.world.getRegistryKey(), dimensionKey);
}

@Override
public boolean applyDimensionTag(TagKey<World> dimensionTag) {
return EDAUtils.worldIsIn(this.world, dimensionTag);
}

@Override
public boolean applyBiomeKey(RegistryKey<Biome> biomeKey) {
return this.world.getBiome(this.pos).matchesKey(biomeKey);
}

@Override
public boolean applyBiomeTag(TagKey<Biome> biomeTag) {
return this.world.getBiome(this.pos).isIn(biomeTag);
}

@Override
public boolean applyXCoord(Int2BooleanFunction operation) {
return operation.get(this.pos.getX());
}

@Override
public boolean applyYCoord(Int2BooleanFunction operation) {
return operation.get(this.pos.getY());
}

@Override
public boolean applyZCoord(Int2BooleanFunction operation) {
return operation.get(this.pos.getZ());
}

@Override
public boolean applySubmerged(boolean submerged) {
if (submerged) {
return EDAUtils.lookupSubmerged(this.view, this.pos, this.view::getBlockState);
} else {
return !EDAUtils.lookupSubmerged(this.view, this.pos, this.view::getBlockState);
}
}

@Override
public boolean applySky(SkyEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.pos.getY() < this.view.getTopY() - 1;
case AT -> this.pos.getY() == this.view.getTopY() - 1;
case ABOVE -> this.pos.getY() > this.view.getTopY() - 1;
};
}

@Override
public boolean applyWater(WaterEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.pos.getY() < this.world.getSeaLevel() - 1;
case AT -> this.pos.getY() == this.world.getSeaLevel() - 1;
case ABOVE -> this.pos.getY() > this.world.getSeaLevel() - 1;
};
}

@Override
public boolean applyVoid(VoidEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.pos.getY() < this.view.getBottomY();
case AT -> this.pos.getY() == this.view.getBottomY();
case ABOVE -> this.pos.getY() > this.view.getBottomY();
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.firstmegagame4.env.driven.assets.client.impl.axiom;

import net.minecraft.world.BlockRenderView;
import net.minecraft.world.World;

public interface MappedBlockAndTintGetterInstance {

BlockRenderView getView();

World getWorld();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public BlockEnvJsonVisitor(World world, BlockPos pos) {

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return EDAUtils.worldIsOf(this.world, dimensionKey);
return EDAUtils.compareKeys(this.world.getRegistryKey(), dimensionKey);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ClientEnvJsonVisitor(MinecraftClient client) {

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return this.player().isPresent() && EDAUtils.worldIsOf(this.player().get().clientWorld, dimensionKey);
return this.player().isPresent() && EDAUtils.compareKeys(this.player().get().clientWorld.getRegistryKey(), dimensionKey);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public EntityEnvJsonVisitor(Entity entity) {

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return EDAUtils.worldIsOf(this.entity.getWorld(), dimensionKey);
return EDAUtils.compareKeys(this.entity.getWorld().getRegistryKey(), dimensionKey);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SodiumBlockEnvJsonVisitor(WorldSlice slice, BlockPos pos) {

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return EDAUtils.worldIsOf(((WorldSliceAccessor) (Object) this.slice).env_driven_assets$getWorld(), dimensionKey);
return EDAUtils.compareKeys(((WorldSliceAccessor) (Object) this.slice).env_driven_assets$getWorld().getRegistryKey(), dimensionKey);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(BlockModelRenderer.class)
public class BlockModelRendererMixin {

@ModifyVariable(method = "render(Lnet/minecraft/world/BlockRenderView;Lnet/minecraft/client/render/model/BakedModel;Lnet/minecraft/block/BlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;ZLnet/minecraft/util/math/random/Random;JI)V", at = @At("HEAD"), argsOnly = true)
private BakedModel mutateBakedModel(BakedModel original, @Local BlockRenderView world, @Local BlockPos pos) {
@ModifyVariable(method = "renderSmooth", at = @At("HEAD"), argsOnly = true, index = 2)
private BakedModel mutateSmooth(BakedModel original, @Local(argsOnly = true) BlockRenderView world, @Local(argsOnly = true) BlockPos pos) {
return this.mutateBakedModel(original, world, pos);
}

@ModifyVariable(method = "renderFlat", at = @At("HEAD"), argsOnly = true, index = 2)
private BakedModel mutateFlat(BakedModel original, @Local(argsOnly = true) BlockRenderView world, @Local(argsOnly = true) BlockPos pos) {
return this.mutateBakedModel(original, world, pos);
}

@Unique
private BakedModel mutateBakedModel(BakedModel original, BlockRenderView world, BlockPos pos) {
BakedModelDuckInterface ducked = (BakedModelDuckInterface) original;
if (ducked.env_driven_assets$getEnvJson() != null) {
EnvJson envJson = ducked.env_driven_assets$getEnvJson();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.firstmegagame4.env.driven.assets.mixin.client;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.moulberry.axiom.render.regions.ChunkedBlockRegion;
import fr.firstmegagame4.env.driven.assets.client.EDAEnvJsonVisitors;
import fr.firstmegagame4.env.driven.assets.client.duck.BakedModelDuckInterface;
import fr.firstmegagame4.env.driven.assets.client.injected.ModelManagerContainer;
import fr.firstmegagame4.env.driven.assets.client.model.ModelManager;
import fr.firstmegagame4.env.json.api.EnvJson;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.block.BlockRenderManager;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockRenderView;
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(ChunkedBlockRegion.class)
public class ChunkedBlockRegionMixin {

@ModifyExpressionValue(method = "renderBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/block/BlockRenderManager;getModel(Lnet/minecraft/block/BlockState;)Lnet/minecraft/client/render/model/BakedModel;"))
private static BakedModel mutateBakedModel(BakedModel original, BufferBuilder blockBuilder, BlockRenderManager renderManager, BlockPos.Mutable blockPos, Random rand, MatrixStack matrices, BlockRenderView blockAndTintGetter, Matrix4f currentPoseMatrix, Matrix4f basePoseMatrix, int x, int y, int z, BlockState dataState, boolean useAmbientOcclusion) {
BakedModelDuckInterface ducked = (BakedModelDuckInterface) original;
if (ducked.env_driven_assets$getEnvJson() != null) {
EnvJson envJson = ducked.env_driven_assets$getEnvJson();
Identifier identifier = envJson.apply(EDAEnvJsonVisitors.blockVisitor(blockAndTintGetter, new BlockPos(blockPos)));
if (identifier != null) {
ModelManager manager = ((ModelManagerContainer) MinecraftClient.getInstance().getBakedModelManager()).getModelManager();
return manager.changeModelWithoutSettings(identifier);
}
}
return original;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.firstmegagame4.env.driven.assets.mixin.client;

import fr.firstmegagame4.env.driven.assets.client.impl.axiom.MappedBlockAndTintGetterInstance;
import net.minecraft.world.BlockRenderView;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;

@Pseudo
@Mixin(targets = "com.moulberry.axiom.render.ChunkRenderOverrider$MappedBlockAndTintGetter")
public class MappedBlockAndTintGetterMixin implements MappedBlockAndTintGetterInstance {

@Shadow
@Final
private World level;

@Override
public BlockRenderView getView() {
return (BlockRenderView) this;
}

@Override
public World getWorld() {
return this.level;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,11 @@ private void bakeEnvJsonModels(Identifier id, ModelBakeSettings settings, Callba
if (unbakedModel instanceof JsonUnbakedModelDuckInterface jum && jum.env_driven_assets$getEnvJson() != null) {
jum.env_driven_assets$getEnvJson().members().forEach(member -> {
UnbakedModel envJsonModel = this.field_40571.getOrLoadModel(member.result());
// It's currently not a way I like to apply the rotations, need to find a better way in the future
for (ModelRotation rotation : ModelRotation.values()) {
BakedModel rotatedModel = envJsonModel.bake((Baker) this, this.textureGetter, rotation, member.result());
((ModelLoaderDuckInterface) this.field_40571).env_driven_assets$getModelManager().appendModel(
BakedModelCacheKeyAccessor.env_driven_assets$init(member.result(), rotation.getRotation(), rotation.isUvLocked()),
rotatedModel
);
}
BakedModel bakedModel = envJsonModel.bake((Baker) this, this.textureGetter, settings, member.result());
((ModelLoaderDuckInterface) this.field_40571).env_driven_assets$getModelManager().appendModel(
BakedModelCacheKeyAccessor.env_driven_assets$init(member.result(), settings.getRotation(), settings.isUvLocked()),
bakedModel
);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Set;
import java.util.function.BooleanSupplier;

public class EDAMixinPlugin implements IMixinConfigPlugin {

Expand All @@ -20,7 +21,15 @@ public String getRefMapperConfig() {

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return !mixinClassName.equals("fr.firstmegagame4.env.driven.assets.mixin.client.WorldSliceAccessor") || FabricLoader.getInstance().isModLoaded("sodium");
if (!FabricLoader.getInstance().isModLoaded("sodium")) {
return !mixinClassName.equals("fr.firstmegagame4.env.driven.assets.mixin.client.WorldSliceAccessor");
}
if (!FabricLoader.getInstance().isModLoaded("axiom")) {
BooleanSupplier mapped = () -> mixinClassName.equals("fr.firstmegagame4.env.driven.assets.mixin.client.MappedBlockAndTintGetterMixin");
BooleanSupplier chunked = () -> mixinClassName.equals("fr.firstmegagame4.env.driven.assets.mixin.client.ChunkedBlockRegionMixin");
return !(mapped.getAsBoolean() || chunked.getAsBoolean());
}
return true;
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/env_driven_assets.accesswidener
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
accessWidener v1 named

accessible class com/moulberry/axiom/render/ChunkRenderOverrider$MappedBlockAndTintGetter
accessible class net/minecraft/client/render/model/BakedModelManager$BakingResult
accessible class net/minecraft/client/render/model/ModelLoader$BakedModelCacheKey
accessible class net/minecraft/client/render/model/ModelLoader$BakerImpl
2 changes: 2 additions & 0 deletions src/main/resources/env_driven_assets.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"compatibilityLevel": "JAVA_17",
"plugin": "fr.firstmegagame4.env.driven.assets.plugin.mixin.client.EDAMixinPlugin",
"mixins": [
"client.ChunkedBlockRegionMixin",
"client.MappedBlockAndTintGetterMixin",
"client.WorldSliceAccessor"
],
"client": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
},
{
"type": "biome",
"rule": "minecraft:space_jungle"
"rule": "minecraft:sparse_jungle"
}
],
"result": "env_driven_new_default:block/jungle_torch"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
},
{
"type": "biome",
"rule": "minecraft:space_jungle"
"rule": "minecraft:sparse_jungle"
}
],
"result": "env_driven_new_default:block/jungle_wall_torch"
Expand Down

0 comments on commit e209555

Please sign in to comment.