Fixed crash when toggling pony rendering of certain mob types

This commit is contained in:
Sollace 2021-08-09 16:01:56 +02:00
parent fcbc95e14e
commit 844bced2db
4 changed files with 29 additions and 21 deletions

View file

@ -0,0 +1,17 @@
package com.minelittlepony.client.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import java.util.Map;
import net.minecraft.client.render.entity.EntityRendererFactory;
import net.minecraft.client.render.entity.EntityRenderers;
import net.minecraft.entity.EntityType;
@Mixin(EntityRenderers.class)
public interface MixinEntityRenderers {
@Accessor
public static Map<EntityType<?>, EntityRendererFactory<?>> getRendererFactories() {
return null;
}
}

View file

@ -60,13 +60,13 @@ public final class MobRenderers {
pony.switchRenderer(state, EntityType.STRIDER, ParaspriteRenderer::new);
});
private final BiConsumer<Boolean, PonyRenderDispatcher> changer;
private final BiConsumer<MobRenderers, PonyRenderDispatcher> changer;
public final String name;
private boolean lastState;
private MobRenderers(String name, BiConsumer<Boolean, PonyRenderDispatcher> changer) {
private MobRenderers(String name, BiConsumer<MobRenderers, PonyRenderDispatcher> changer) {
this.name = name;
this.changer = changer;
}
@ -85,16 +85,15 @@ public final class MobRenderers {
return option().get();
}
public static MobRenderers register(String name, BiConsumer<Boolean, PonyRenderDispatcher> changer) {
public static MobRenderers register(String name, BiConsumer<MobRenderers, PonyRenderDispatcher> changer) {
return REGISTRY.computeIfAbsent(name, n -> new MobRenderers(name, changer));
}
void apply(PonyRenderDispatcher dispatcher) {
boolean state = get();
if (state != lastState) {
MineLittlePony.logger.info(String.format("Ponify %s [%B] -> [%B]", name, lastState, state));
lastState = state;
changer.accept(state, dispatcher);
changer.accept(this, dispatcher);
}
}
}

View file

@ -1,15 +1,13 @@
package com.minelittlepony.client.render;
import java.util.Map;
import java.util.function.Function;
import com.google.common.collect.Maps;
import com.minelittlepony.client.mixin.MixinEntityRenderers;
import com.minelittlepony.client.model.IPonyModel;
import com.minelittlepony.client.model.entity.race.PlayerModels;
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.common.mixin.MixinEntityRenderDispatcher;
import com.minelittlepony.mson.api.Mson;
import net.minecraft.client.MinecraftClient;
@ -38,8 +36,6 @@ public class PonyRenderDispatcher {
private LevitatingItemRenderer magicRenderer = new LevitatingItemRenderer();
private final Map<EntityType<?>, EntityRenderer<?>> renderMap = Maps.newHashMap();
/**
* Registers all new player skin types. (currently only pony and slimpony).
*/
@ -71,18 +67,13 @@ public class PonyRenderDispatcher {
* @param factory The replacement value
* @param <T> The entity type
*/
@SuppressWarnings("unchecked")
<T extends Entity, V extends T> void switchRenderer(boolean state, EntityType<V> type, Function<EntityRendererFactory.Context, EntityRenderer<T>> factory) {
if (state) {
if (!renderMap.containsKey(type)) {
renderMap.put(type, ((MixinEntityRenderDispatcher)MinecraftClient.getInstance().getEntityRenderDispatcher()).getEntityRenderers().get(type));
}
Mson.getInstance().getEntityRendererRegistry().registerEntityRenderer(type, factory);
} else {
if (renderMap.containsKey(type)) {
Mson.getInstance().getEntityRendererRegistry().registerEntityRenderer(type, m -> (EntityRenderer<T>)renderMap.get(type));
}
<T extends Entity, V extends T> void switchRenderer(MobRenderers state, EntityType<V> type, Function<EntityRendererFactory.Context, EntityRenderer<T>> factory) {
Mson.getInstance().getEntityRendererRegistry().registerEntityRenderer(type, ctx -> {
if (!state.get()) {
return MixinEntityRenderers.getRendererFactories().get(type).create(ctx);
}
return factory.apply(ctx);
});
}
public LevitatingItemRenderer getMagicRenderer() {

View file

@ -9,6 +9,7 @@
"MixinCamera",
"MixinDefaultPlayerSkin",
"MixinEntityRenderDispatcher",
"MixinEntityRenderers",
"MixinSkullBlockEntityRenderer",
"MixinFirstPersonRenderer",
"MixinItemRenderer",