Skip to content

Commit

Permalink
unify convertibles
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Sep 15, 2023
1 parent 27c57d1 commit fdc5f07
Show file tree
Hide file tree
Showing 27 changed files with 502 additions and 457 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

import com.mojang.serialization.Codec;
import de.teamlapen.vampirism.api.VampirismRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.function.Function;

public interface Converter {

Codec<Converter> CODEC = ExtraCodecs.lazyInitializedCodec(() -> VampirismRegistries.ENTITY_CONVERTER.get().getCodec()).dispatch(Converter::codec, Function.identity());

@Deprecated
IConvertingHandler<?> createHandler();

default IConvertingHandler<?> createHandler(@Nullable ResourceLocation texture){
return createHandler();
}

Codec<? extends Converter> codec();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.teamlapen.vampirism.api.entity.convertible;

import de.teamlapen.vampirism.api.entity.vampire.IVampireMob;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.PathfinderMob;
import org.jetbrains.annotations.Nullable;

/**
* Interface for entities that were bitten and then converted to a vampire.
Expand All @@ -10,5 +12,11 @@
*/
public interface IConvertedCreature<T extends PathfinderMob> extends IVampireMob {

class Data<T> {

@Nullable
public ResourceLocation texture;
}

Data<T> data();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"handler": {
"type": "vampirism:cow"
"type": "vampirism:special",
"converted_type": "vampirism:converted_cow"
},
"overlay": "vampirism:textures/entity/vanilla/cow_overlay.png"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"handler": {
"type": "vampirism:sheep"
"type": "vampirism:special",
"converted_type": "vampirism:converted_sheep"
},
"overlay": "vampirism:textures/entity/vanilla/sheep_overlay.png"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"handler": {
"type": "vampirism:villager"
}
"type": "vampirism:special",
"converted_type": "vampirism:villager_converted"
},
"overlay": "vampirism:textures/entity/vanilla/villager_overlay_overlay.png"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@

import de.teamlapen.lib.lib.client.render.RenderAreaParticleCloud;
import de.teamlapen.vampirism.REFERENCE;
import de.teamlapen.vampirism.api.VampirismAPI;
import de.teamlapen.vampirism.client.model.*;
import de.teamlapen.vampirism.client.model.armor.*;
import de.teamlapen.vampirism.client.renderer.entity.*;
import de.teamlapen.vampirism.client.renderer.entity.layers.VampireEntityLayer;
import de.teamlapen.vampirism.client.renderer.entity.layers.ConvertedVampireEntityLayer;
import de.teamlapen.vampirism.client.renderer.entity.layers.VampirePlayerHeadLayer;
import de.teamlapen.vampirism.core.ModEntities;
import de.teamlapen.vampirism.entity.IVampirismBoat;
import de.teamlapen.vampirism.mixin.client.LivingEntityRendererAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.*;
import net.minecraft.client.model.geom.LayerDefinitions;
import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.model.geom.builders.CubeDeformation;
import net.minecraft.client.model.geom.builders.LayerDefinition;
import net.minecraft.client.renderer.entity.BatRenderer;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.HorseRenderer;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
Expand All @@ -34,9 +26,6 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

/**
* Handles entity render registration
*/
Expand Down Expand Up @@ -72,15 +61,11 @@ static void onRegisterRenderers(EntityRenderersEvent.@NotNull RegisterRenderers
event.registerEntityRenderer(ModEntities.BLINDING_BAT.get(), BatRenderer::new);
event.registerEntityRenderer(ModEntities.CONVERTED_CREATURE_IMOB.get(), ConvertedCreatureRenderer::new);
event.registerEntityRenderer(ModEntities.CONVERTED_CREATURE.get(), (ConvertedCreatureRenderer::new));
event.registerEntityRenderer(ModEntities.CONVERTED_HORSE.get(), renderingManager -> {
HorseRenderer renderer = new HorseRenderer(renderingManager);
renderer.addLayer(new VampireEntityLayer<>(renderer, new ResourceLocation(REFERENCE.MODID, "textures/entity/vanilla/horse_overlay.png"), false));
return renderer;
});
event.registerEntityRenderer(ModEntities.CONVERTED_DONKEY.get(), (context) -> new ConvertedChestedHorseRenderer<>(context, ModelLayers.DONKEY));
event.registerEntityRenderer(ModEntities.CONVERTED_MULE.get(), (context -> new ConvertedChestedHorseRenderer<>(context, ModelLayers.MULE)));
event.registerEntityRenderer(ModEntities.CONVERTED_SHEEP.get(), (ConvertedCreatureRenderer::new));
event.registerEntityRenderer(ModEntities.CONVERTED_COW.get(), (ConvertedCreatureRenderer::new));
event.registerEntityRenderer(ModEntities.CONVERTED_HORSE.get(), convertedRenderer(HorseRenderer::new));
event.registerEntityRenderer(ModEntities.CONVERTED_DONKEY.get(), convertedRenderer(context -> new ConvertedChestedHorseRenderer<>(context, 0.87f, ModelLayers.DONKEY)));
event.registerEntityRenderer(ModEntities.CONVERTED_MULE.get(), convertedRenderer(context -> new ConvertedChestedHorseRenderer<>(context, 0.92F, ModelLayers.MULE)));
event.registerEntityRenderer(ModEntities.CONVERTED_SHEEP.get(), convertedRenderer(SheepRenderer::new));
event.registerEntityRenderer(ModEntities.CONVERTED_COW.get(), convertedRenderer(CowRenderer::new));
event.registerEntityRenderer(ModEntities.HUNTER.get(), (BasicHunterRenderer::new));
event.registerEntityRenderer(ModEntities.HUNTER_IMOB.get(), (BasicHunterRenderer::new));
event.registerEntityRenderer(ModEntities.VAMPIRE.get(), (BasicVampireRenderer::new));
Expand All @@ -91,7 +76,7 @@ static void onRegisterRenderers(EntityRenderersEvent.@NotNull RegisterRenderers
event.registerEntityRenderer(ModEntities.ADVANCED_HUNTER_IMOB.get(), (AdvancedHunterRenderer::new));
event.registerEntityRenderer(ModEntities.ADVANCED_VAMPIRE.get(), (AdvancedVampireRenderer::new));
event.registerEntityRenderer(ModEntities.ADVANCED_VAMPIRE_IMOB.get(), (AdvancedVampireRenderer::new));
event.registerEntityRenderer(ModEntities.VILLAGER_CONVERTED.get(), (ConvertedVillagerRenderer::new));
event.registerEntityRenderer(ModEntities.VILLAGER_CONVERTED.get(), convertedRenderer(VillagerRenderer::new));
event.registerEntityRenderer(ModEntities.VILLAGER_ANGRY.get(), HunterVillagerRenderer::new);
event.registerEntityRenderer(ModEntities.CROSSBOW_ARROW.get(), (CrossbowArrowRenderer::new));
event.registerEntityRenderer(ModEntities.PARTICLE_CLOUD.get(), (RenderAreaParticleCloud::new));
Expand All @@ -107,8 +92,8 @@ static void onRegisterRenderers(EntityRenderersEvent.@NotNull RegisterRenderers
event.registerEntityRenderer(ModEntities.dummy_sit_entity.get(), DummyRenderer::new);
event.registerEntityRenderer(ModEntities.BOAT.get(), context -> new VampirismBoatRenderer(context, false));
event.registerEntityRenderer(ModEntities.CHEST_BOAT.get(), context -> new VampirismBoatRenderer(context, true));
event.registerEntityRenderer(ModEntities.CONVERTED_FOX.get(), ConvertedFoxRenderer::new);
event.registerEntityRenderer(ModEntities.CONVERTED_GOAT.get(), ConvertedGoatRenderer::new);
event.registerEntityRenderer(ModEntities.CONVERTED_FOX.get(), convertedRenderer(FoxRenderer::new));
event.registerEntityRenderer(ModEntities.CONVERTED_GOAT.get(), convertedRenderer(GoatRenderer::new));
}

static void onRegisterLayers(EntityRenderersEvent.@NotNull RegisterLayerDefinitions event) {
Expand Down Expand Up @@ -148,26 +133,6 @@ static void onAddLayers(EntityRenderersEvent.@NotNull AddLayers event) {
_onAddLayers(event);
}

private static Map<LivingEntityRenderer<?, ?>, VampireEntityLayer> ENTITY_VAMPIRE_LAYER = new HashMap<>();

public static <T extends Player, Q extends EntityModel<T>, Z extends HumanoidModel<T>, I extends LivingEntity, U extends EntityModel<I>> void applyConvertibleOverlayUnsafe(Map<EntityType<? extends PathfinderMob>, ResourceLocation> overlays) {
Map<EntityType<?>, EntityRenderer<?>> renderers = Minecraft.getInstance().getEntityRenderDispatcher().renderers;
ENTITY_VAMPIRE_LAYER.forEach((renderer, layer) -> ((LivingEntityRendererAccessor<?, ?>) renderer).getLayers().remove(layer));
ENTITY_VAMPIRE_LAYER.clear();
overlays.forEach((type, overlay) -> {
EntityRenderer<?> entityRenderer = renderers.get(type);
if (entityRenderer != null) {
if (entityRenderer instanceof LivingEntityRenderer<?, ?> livingRenderer) {
livingRenderer.addLayer(new VampireEntityLayer(livingRenderer, overlay, true));
} else {
LOGGER.error("Renderer for {} is not a LivingEntityRenderer", type);
}
} else {
LOGGER.error("Did not find renderer for {}", type);
}
});
}

@SuppressWarnings("unchecked")
private static <T extends Player, Q extends EntityModel<T>, Z extends HumanoidModel<T>, I extends LivingEntity, U extends EntityModel<I>> void _onAddLayers(EntityRenderersEvent.@NotNull AddLayers event) {

Expand All @@ -178,14 +143,6 @@ private static <T extends Player, Q extends EntityModel<T>, Z extends HumanoidMo
renderPlayer2.addLayer(new VampirePlayerHeadLayer<>(renderPlayer2));
}
}
for (Map.Entry<EntityType<? extends PathfinderMob>, ResourceLocation> entry : VampirismAPI.entityRegistry().getConvertibleOverlay().entrySet()) {
EntityType<? extends PathfinderMob> type = entry.getKey();
LivingEntityRenderer<I, U> render = (LivingEntityRenderer<I, U>) event.getRenderer(type);
if (render == null) {
continue;
}
render.addLayer(new VampireEntityLayer<>(render, entry.getValue(), true));
}
}

public static @NotNull ModelLayerLocation createBoatModelName(IVampirismBoat.@NotNull BoatType type) {
Expand All @@ -196,4 +153,18 @@ private static <T extends Player, Q extends EntityModel<T>, Z extends HumanoidMo
return new ModelLayerLocation(new ResourceLocation(REFERENCE.MODID, "chest_boat/" + type.getName()), "main");
}

private static @NotNull <T extends LivingEntity, Z extends EntityModel<T>> EntityRendererProvider<T> convertedRenderer(LivingEntityRendererProvider<T,Z> provider) {
return context -> {
LivingEntityRenderer<T, Z> renderer = provider.create(context);
renderer.addLayer(new ConvertedVampireEntityLayer<>(renderer, false));
return renderer;
};
}

private interface LivingEntityRendererProvider<T extends LivingEntity,Z extends EntityModel<T>> extends EntityRendererProvider<T> {
@Override
@NotNull
LivingEntityRenderer<T,Z> create(EntityRendererProvider.@NotNull Context pContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import de.teamlapen.vampirism.REFERENCE;
import de.teamlapen.vampirism.client.renderer.entity.layers.VampireEntityLayer;
import de.teamlapen.vampirism.client.renderer.entity.layers.ConvertedVampireEntityLayer;
import de.teamlapen.vampirism.core.ModEntities;
import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.renderer.entity.ChestedHorseRenderer;
Expand All @@ -18,9 +17,8 @@
public class ConvertedChestedHorseRenderer<T extends AbstractChestedHorse> extends ChestedHorseRenderer<T> {
private static final Map<EntityType<?>, ResourceLocation> entityTypes = Maps.newHashMap(ImmutableMap.of(ModEntities.CONVERTED_DONKEY.get(), new ResourceLocation("textures/entity/horse/donkey.png"), ModEntities.CONVERTED_MULE.get(), new ResourceLocation("textures/entity/horse/mule.png")));

public ConvertedChestedHorseRenderer(EntityRendererProvider.@NotNull Context context, @NotNull ModelLayerLocation type) {
super(context, 0.87f, type);
this.addLayer(new VampireEntityLayer<>(this, new ResourceLocation(REFERENCE.MODID, "textures/entity/vanilla/horse_overlay.png"), false));
public ConvertedChestedHorseRenderer(EntityRendererProvider.@NotNull Context context, float pScale, @NotNull ModelLayerLocation type) {
super(context, pScale, type);
}

@NotNull
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.teamlapen.vampirism.REFERENCE;
import de.teamlapen.vampirism.client.core.ModEntitiesRender;
import de.teamlapen.vampirism.client.renderer.entity.layers.TaskMasterTypeLayer;
import de.teamlapen.vampirism.client.renderer.entity.layers.ConvertedVampireEntityLayer;
import de.teamlapen.vampirism.client.renderer.entity.layers.VampireEntityLayer;
import de.teamlapen.vampirism.entity.vampire.VampireTaskMasterEntity;
import net.minecraft.client.model.VillagerModel;
Expand All @@ -28,7 +29,7 @@ public class VampireTaskMasterRenderer extends MobRenderer<VampireTaskMasterEnti
public VampireTaskMasterRenderer(EntityRendererProvider.@NotNull Context context) {
super(context, new VillagerModel<>(context.bakeLayer(ModEntitiesRender.TASK_MASTER)), 0.5F);
// this.addLayer(new HeldItemLayer<>(this));
this.addLayer(new VampireEntityLayer<>(this, vampireOverlay, false));
this.addLayer(new VampireEntityLayer<>(this, vampireOverlay));
this.addLayer(new TaskMasterTypeLayer<>(this, overlay));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.teamlapen.vampirism.client.renderer.entity.layers;

import com.mojang.blaze3d.vertex.PoseStack;
import de.teamlapen.vampirism.client.renderer.entity.ConvertedCreatureRenderer;
import de.teamlapen.vampirism.entity.ConvertedCreature;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;

/**
* Render the vampire overlay for converted creatures
*/
@OnlyIn(Dist.CLIENT)
public class ConvertedVampireEntityLayer<T extends LivingEntity, U extends EntityModel<T>> extends RenderLayer<T, U> {

private final boolean checkIfRender;

/**
* @param checkIfRender If it should check if {@link ConvertedCreatureRenderer#renderOverlay} is true
*/
public ConvertedVampireEntityLayer(@NotNull RenderLayerParent<T, U> entityRendererIn, boolean checkIfRender) {
super(entityRendererIn);
this.checkIfRender = checkIfRender;
}

@Override
public void render(@NotNull PoseStack matrixStack, @NotNull MultiBufferSource iRenderTypeBuffer, int i, @NotNull T entity, float v, float v1, float v2, float v3, float v4, float v5) {
if (entity instanceof ConvertedCreature<?> converted && !entity.isInvisible() && (!checkIfRender || ConvertedCreatureRenderer.renderOverlay)) {
ResourceLocation texture = converted.data().texture;
//noinspection ConstantValue,DataFlowIssue
if (texture != null && Minecraft.getInstance().textureManager.getTexture(texture, null) != null) {
renderColoredCutoutModel(this.getParentModel(), texture, matrixStack, iRenderTypeBuffer, i, entity, 1, 1, 1);
}
}
}
}
Loading

0 comments on commit fdc5f07

Please sign in to comment.