Skip to content

Commit

Permalink
Small Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Dec 30, 2023
1 parent 12db275 commit 313cfb6
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 53 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ yarn_mappings=1.20.4+build.3
loader_version=0.15.3

# Mod Properties
mod_version=1.0.0
mod_version=0.0.1-alpha
maven_group=fr.firstmegagame4.env.driven.assets
archives_base_name=env_driven_assets

# Dependencies
fabric_version=0.91.3+1.20.4
env_json_version=96e07941aa
env_json_version=8e6eb0457d

# Compatibilities
sodium_version=mc1.20.3-0.5.5
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
import fr.firstmegagame4.env.driven.assets.client.impl.env.json.EmptyVisitor;
import fr.firstmegagame4.env.driven.assets.client.impl.sodium.SodiumBlockEnvJsonVisitor;
import fr.firstmegagame4.env.driven.assets.mixin.client.ChunkRendererRegionAccessor;
import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
Expand All @@ -21,12 +22,15 @@ public static EnvJsonVisitor blockVisitor(BlockRenderView view, BlockPos pos) {
if (view instanceof ChunkRendererRegion region) {
return new BlockEnvJsonVisitor(((ChunkRendererRegionAccessor) region).env_driven_assets$getWorld(), pos);
}
else if (view instanceof World world) {
return new BlockEnvJsonVisitor(world, pos);
}
else if (FabricLoader.getInstance().isModLoaded("sodium")) {
if (view instanceof WorldSlice slice) {
return new SodiumBlockEnvJsonVisitor(slice, pos);
}
}
return new BlockEnvJsonVisitor((World) view, pos);
return new EmptyVisitor();
}

public static EnvJsonVisitor clientVisitor(MinecraftClient client) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package fr.firstmegagame4.env.driven.assets.client.impl.env.json;

import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.SkyEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.VoidEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.WaterEnvJsonRule;
import it.unimi.dsi.fastutil.ints.Int2BooleanFunction;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;

public class EmptyVisitor implements EnvJsonVisitor {

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return false;
}

@Override
public boolean applyDimensionTag(TagKey<World> dimensionTag) {
return false;
}

@Override
public boolean applyBiomeKey(RegistryKey<Biome> biomeKey) {
return false;
}

@Override
public boolean applyBiomeTag(TagKey<Biome> biomeTag) {
return false;
}

@Override
public boolean applyXCoord(Int2BooleanFunction operation) {
return false;
}

@Override
public boolean applyYCoord(Int2BooleanFunction operation) {
return false;
}

@Override
public boolean applyZCoord(Int2BooleanFunction operation) {
return false;
}

@Override
public boolean applySubmerged(boolean submerged) {
return false;
}

@Override
public boolean applySky(SkyEnvJsonRule.Localization localization) {
return false;
}

@Override
public boolean applyWater(WaterEnvJsonRule.Localization localization) {
return false;
}

@Override
public boolean applyVoid(VoidEnvJsonRule.Localization localization) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import fr.firstmegagame4.env.driven.assets.client.EDAEnvJsonVisitors;
import fr.firstmegagame4.env.driven.assets.client.EDAUtils;
import fr.firstmegagame4.env.driven.assets.client.impl.env.json.BlockEnvJsonVisitor;
import fr.firstmegagame4.env.driven.assets.mixin.client.WorldSliceAccessor;
import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.SkyEnvJsonRule;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.firstmegagame4.env.driven.assets.client.injected;

import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockRenderView;

import java.util.function.Supplier;

public interface BakedModelRedirection extends FabricBakedModel {

default boolean shouldAllowBlockRedirection(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context, boolean isAlreadyRedirected) {
return true;
}

default boolean shouldAllowItemRedirection(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context, boolean isAlreadyRedirected) {
return true;
}

default void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context, boolean isRedirected) {
if (this.shouldAllowBlockRedirection(blockView, state, pos, randomSupplier, context, isRedirected)) {
this.emitBlockQuads(blockView, state, pos, randomSupplier, context);
}
else {
FabricBakedModel.super.emitBlockQuads(blockView, state, pos, randomSupplier, context);
}
}

default void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context, boolean isRedirected) {
if (this.shouldAllowItemRedirection(stack, randomSupplier, context, isRedirected)) {
this.emitItemQuads(stack, randomSupplier, context);
}
else {
FabricBakedModel.super.emitItemQuads(stack, randomSupplier, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,29 @@

public class EDABakedModel extends ForwardingBakedModel {

private final ModelLoader loader;
private final ModelManager manager;

public EDABakedModel(ModelLoader loader, BakedModel wrapped) {
this.loader = loader;
this.manager = ((ModelLoaderDuckInterface) loader).env_driven_assets$getModelManager();
this.wrapped = wrapped;
}

@Override
public boolean shouldAllowBlockRedirection(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context, boolean isAlreadyRedirected) {
return !isAlreadyRedirected;
}

@Override
public boolean shouldAllowItemRedirection(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context, boolean isAlreadyRedirected) {
return !isAlreadyRedirected;
}

@Override
public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
if (this.getEnvJson() != null) {
Identifier identifier = this.getEnvJson().apply(EDAEnvJsonVisitors.blockVisitor(blockView, pos));
if (identifier != null) {
this.manager.convert(this.loader, identifier).emitBlockQuads(blockView, state, pos, randomSupplier, context);
this.manager.convert(identifier).emitBlockQuads(blockView, state, pos, randomSupplier, context, true);
return;
}
}
Expand All @@ -46,7 +54,7 @@ public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, Rend
if (this.getEnvJson() != null) {
Identifier identifier = this.getEnvJson().apply(EDAEnvJsonVisitors.clientVisitor(MinecraftClient.getInstance()));
if (identifier != null) {
this.manager.convert(this.loader, identifier).emitItemQuads(stack, randomSupplier, context);
this.manager.convert(identifier).emitItemQuads(stack, randomSupplier, context, true);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.firstmegagame4.env.driven.assets.client.model;

import fr.firstmegagame4.env.driven.assets.client.EDAUtils;
import fr.firstmegagame4.env.driven.assets.mixin.client.ModelLoaderAccessor;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
Expand All @@ -14,10 +13,11 @@
public class ModelManager {

private final Map<Identifier, ModelLoader.BakedModelCacheKey> convertor = new Object2ObjectOpenHashMap<>();
private Map<BakedModel, ModelLoader.BakedModelCacheKey> revertor = new Object2ObjectOpenHashMap<>();
private final Map<ModelLoader.BakedModelCacheKey, BakedModel> cache = new Object2ObjectOpenHashMap<>();
private final Map<BakedModel, ModelLoader.BakedModelCacheKey> revertor = new Object2ObjectOpenHashMap<>();

public BakedModel convert(ModelLoader loader, Identifier reference) {
return ((ModelLoaderAccessor) loader).env_driven_assets$getBakedModelCache().get(this.convertor.get(reference));
public BakedModel convert(Identifier reference) {
return this.cache.get(this.convertor.get(reference));
}

public Identifier revert(BakedModel source) {
Expand All @@ -28,11 +28,13 @@ public Identifier revert(BlockState source) {
return this.revert(MinecraftClient.getInstance().getBakedModelManager().getBlockModels().getModel(source));
}

public void appendModelIdentifier(Identifier identifier, ModelLoader.BakedModelCacheKey key) {
public void appendModel(Identifier identifier, ModelLoader.BakedModelCacheKey key, BakedModel model) {
this.convertor.put(identifier, key);
this.cache.put(key, model);
}

public void reloadRevertor(ModelLoader loader) {
this.revertor = EDAUtils.reverseMap(((ModelLoaderAccessor) loader).env_driven_assets$getBakedModelCache(), Object2ObjectOpenHashMap::new);
public void reloadRevertor() {
this.revertor.clear();
this.revertor.putAll(EDAUtils.reverseMap(this.cache, Object2ObjectOpenHashMap::new));
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package fr.firstmegagame4.env.driven.assets.client.model.plugin;

import fr.firstmegagame4.env.driven.assets.client.model.EDABakedModel;
import fr.firstmegagame4.env.driven.assets.client.duck.BakedModelDuckInterface;
import fr.firstmegagame4.env.driven.assets.client.duck.JsonUnbakedModelDuckInterface;
import fr.firstmegagame4.env.driven.assets.client.duck.ModelLoaderDuckInterface;
import fr.firstmegagame4.env.driven.assets.mixin.client.BakedModelCacheKeyAccessor;
import fr.firstmegagame4.env.driven.assets.client.model.EDABakedModel;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelModifier;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.UnbakedModel;
import org.jetbrains.annotations.Nullable;

public class EDAModelLoadingPlugin implements ModelLoadingPlugin {

private static UnbakedModel modifyModelBeforeBake(UnbakedModel model, ModelModifier.BeforeBake.Context context) {
((ModelLoaderDuckInterface) context.loader()).env_driven_assets$getModelManager().appendModelIdentifier(
context.id(),
BakedModelCacheKeyAccessor.env_driven_assets$init(
context.id(),
context.settings().getRotation(),
context.settings().isUvLocked()
)
);
return model;
}

private static BakedModel modifyModelAfterBake(@Nullable BakedModel model, ModelModifier.AfterBake.Context context) {
if (model != null) {
if (context.sourceModel() instanceof JsonUnbakedModelDuckInterface jum && model instanceof BakedModelDuckInterface ducked) {
Expand All @@ -37,7 +22,6 @@ private static BakedModel modifyModelAfterBake(@Nullable BakedModel model, Model

@Override
public void onInitializeModelLoader(Context pluginContext) {
pluginContext.modifyModelBeforeBake().register(EDAModelLoadingPlugin::modifyModelBeforeBake);
pluginContext.modifyModelAfterBake().register(EDAModelLoadingPlugin::modifyModelAfterBake);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ private static <F, S> Pair<F, S> mutateModelValue(F first, S second, Operation<P
@Inject(method = "upload", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/ModelLoader;getBakedModelMap()Ljava/util/Map;"))
private void hookModelLoader(BakedModelManager.BakingResult bakingResult, Profiler profiler, CallbackInfo ci) {
this.modelLoader = bakingResult.modelLoader();
((ModelLoaderDuckInterface) this.modelLoader).env_driven_assets$getModelManager().reloadRevertor(this.modelLoader);
((ModelLoaderDuckInterface) this.modelLoader).env_driven_assets$getModelManager().reloadRevertor();
}

@Override
public BakedModel env_driven_assets$convert(Identifier reference) {
return ((ModelLoaderDuckInterface) this.modelLoader).env_driven_assets$getModelManager().convert(this.modelLoader, reference);
return ((ModelLoaderDuckInterface) this.modelLoader).env_driven_assets$getModelManager().convert(reference);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.firstmegagame4.env.driven.assets.mixin.client;

import fr.firstmegagame4.env.driven.assets.client.injected.BakedModelRedirection;
import net.minecraft.client.render.model.BakedModel;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(BakedModel.class)
public interface BakedModelMixin extends BakedModelRedirection {}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ public class JsonUnbakedModelMixin implements JsonUnbakedModelDuckInterface {
this.envJson = envJson;
}

/* @Inject(method = "deserialize(Ljava/io/Reader;)Lnet/minecraft/client/render/model/json/JsonUnbakedModel;", at = @At("TAIL"), cancellable = true)
private static void retrieveEnvJson(Reader input, CallbackInfoReturnable<JsonUnbakedModel> cir) {
if (FabricLoader.getInstance().isModLoaded("modernfix")) {
JsonUnbakedModelDuckInterface ducked = (JsonUnbakedModelDuckInterface) cir.getReturnValue();
if (input instanceof ExtendedResourceReader reader) {
try {
ducked.env_driven_assets$setEnvJson(reader.getExtendedResource().getEnvJson());
} catch (IOException ioException) {
throw new JsonParseException(ioException);
}
}
cir.setReturnValue((JsonUnbakedModel) ducked);
}
} */

@Inject(method = "getModelDependencies", at = @At("TAIL"), cancellable = true)
private void mutateDependencies(CallbackInfoReturnable<Collection<Identifier>> cir) {
Collection<Identifier> dependencies = cir.getReturnValue();
if (this.envJson != null) {
// this.envJson.members().forEach(member -> dependencies.add(member.result()));
this.envJson.members().forEach(member -> dependencies.add(member.result()));
}
cir.setReturnValue(dependencies);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package fr.firstmegagame4.env.driven.assets.mixin.client;

import com.llamalad7.mixinextras.sugar.Local;
import fr.firstmegagame4.env.driven.assets.client.model.ModelManager;
import fr.firstmegagame4.env.driven.assets.client.duck.ModelLoaderDuckInterface;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.ModelBakeSettings;
import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ModelLoader.class)
public abstract class ModelLoaderMixin implements ModelLoaderDuckInterface {
public class ModelLoaderMixin implements ModelLoaderDuckInterface {

@Unique
private final ModelManager manager = new ModelManager();
Expand All @@ -16,4 +25,17 @@ public abstract class ModelLoaderMixin implements ModelLoaderDuckInterface {
public ModelManager env_driven_assets$getModelManager() {
return this.manager;
}

@Mixin(ModelLoader.BakerImpl.class)
public static class BakerImplMixin {

@Final
@Shadow
ModelLoader field_40571;

@Inject(method = "bake", at = @At("RETURN"))
private void hookToCache(Identifier id, ModelBakeSettings settings, CallbackInfoReturnable<BakedModel> cir, @Local(ordinal = 0) ModelLoader.BakedModelCacheKey bakedModelCacheKey) {
((ModelLoaderDuckInterface) this.field_40571).env_driven_assets$getModelManager().appendModel(id, bakedModelCacheKey, cir.getReturnValue());
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/env_driven_assets.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ accessWidener v1 named

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
Loading

0 comments on commit 313cfb6

Please sign in to comment.