NaturesAura/src/main/java/de/ellpeck/naturesaura/compat/jei/AnimalSpawnerCategory.java

130 lines
5.3 KiB
Java
Raw Normal View History

2020-01-21 21:04:44 +01:00
package de.ellpeck.naturesaura.compat.jei;
2021-12-19 17:14:56 +01:00
import com.mojang.blaze3d.platform.Lighting;
2020-01-29 19:04:33 +01:00
import com.mojang.blaze3d.systems.RenderSystem;
2021-12-19 17:14:56 +01:00
import com.mojang.blaze3d.vertex.PoseStack;
2023-07-08 12:32:27 +02:00
import com.mojang.math.Axis;
import de.ellpeck.naturesaura.NaturesAura;
2020-04-29 16:38:50 +02:00
import de.ellpeck.naturesaura.recipes.AnimalSpawnerRecipe;
2022-06-27 15:24:04 +02:00
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
2020-01-21 21:04:44 +01:00
import mezz.jei.api.gui.drawable.IDrawable;
2022-06-27 15:24:04 +02:00
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
2020-01-21 21:04:44 +01:00
import mezz.jei.api.helpers.IGuiHelper;
2022-06-27 15:24:04 +02:00
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import mezz.jei.api.recipe.RecipeType;
2020-01-21 21:04:44 +01:00
import mezz.jei.api.recipe.category.IRecipeCategory;
2020-01-25 16:56:04 +01:00
import net.minecraft.client.Minecraft;
2023-07-08 12:32:27 +02:00
import net.minecraft.client.gui.GuiGraphics;
2021-12-19 17:14:56 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.common.DeferredSpawnEggItem;
import java.util.Arrays;
2020-01-25 16:56:04 +01:00
import java.util.HashMap;
import java.util.Map;
2020-01-21 21:04:44 +01:00
public class AnimalSpawnerCategory implements IRecipeCategory<AnimalSpawnerRecipe> {
private final IDrawable background;
2021-12-19 17:14:56 +01:00
private final Map<EntityType<?>, Entity> entityCache = new HashMap<>();
public AnimalSpawnerCategory(IGuiHelper helper) {
this.background = helper.createDrawable(new ResourceLocation(NaturesAura.MOD_ID, "textures/gui/jei/animal_spawner.png"), 0, 0, 72, 86);
}
2021-12-19 17:14:56 +01:00
private static void renderEntity(PoseStack matrixstack, int x, int y, float scale, float yaw, float pitch, LivingEntity entity) {
var f = (float) Math.atan(yaw / 40.0F);
var f1 = (float) Math.atan(pitch / 40.0F);
var posestack = RenderSystem.getModelViewStack();
posestack.pushPose();
posestack.translate(x, y, 1050.0D);
posestack.scale(1.0F, 1.0F, -1.0F);
RenderSystem.applyModelViewMatrix();
2020-02-07 15:22:30 +01:00
matrixstack.translate(0.0D, 0.0D, 1000.0D);
matrixstack.scale(scale, scale, scale);
2023-07-08 12:32:27 +02:00
var quaternion = Axis.ZP.rotationDegrees(180.0F);
var quaternion1 = Axis.XP.rotationDegrees(f1 * 20.0F);
2021-12-19 17:14:56 +01:00
quaternion.mul(quaternion1);
matrixstack.mulPose(quaternion);
var f2 = entity.yBodyRot;
var f3 = entity.getYRot();
var f4 = entity.getXRot();
var f5 = entity.yHeadRotO;
var f6 = entity.yHeadRot;
entity.yBodyRot = 180.0F + f * 20.0F;
entity.setYRot(180.0F + f * 40.0F);
entity.setXRot(-f1 * 20.0F);
entity.yHeadRot = entity.getYRot();
entity.yHeadRotO = entity.getYRot();
Lighting.setupForEntityInInventory();
var entityrenderermanager = Minecraft.getInstance().getEntityRenderDispatcher();
2023-07-08 12:32:27 +02:00
quaternion1.conjugate();
2021-12-19 17:14:56 +01:00
entityrenderermanager.overrideCameraOrientation(quaternion1);
2020-02-07 15:22:30 +01:00
entityrenderermanager.setRenderShadow(false);
2021-12-19 17:14:56 +01:00
var buff = Minecraft.getInstance().renderBuffers().bufferSource();
entityrenderermanager.render(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F, matrixstack, buff, 15728880);
buff.endBatch();
2020-02-07 15:22:30 +01:00
entityrenderermanager.setRenderShadow(true);
2021-12-19 17:14:56 +01:00
entity.yBodyRot = f2;
entity.setYRot(f3);
entity.setXRot(f4);
entity.yHeadRotO = f5;
entity.yHeadRot = f6;
posestack.popPose();
RenderSystem.applyModelViewMatrix();
Lighting.setupFor3DItems();
2020-02-07 15:22:30 +01:00
}
@Override
2022-06-27 15:24:04 +02:00
public RecipeType<AnimalSpawnerRecipe> getRecipeType() {
return JEINaturesAuraPlugin.SPAWNER;
}
@Override
2021-12-19 17:14:56 +01:00
public Component getTitle() {
return Component.translatable("container." + JEINaturesAuraPlugin.SPAWNER.getUid() + ".name");
}
@Override
public IDrawable getBackground() {
return this.background;
}
@Override
2020-01-21 21:04:44 +01:00
public IDrawable getIcon() {
return null;
}
@Override
2022-06-27 15:24:04 +02:00
public void setRecipe(IRecipeLayoutBuilder builder, AnimalSpawnerRecipe recipe, IFocusGroup focuses) {
for (var i = 0; i < recipe.ingredients.length; i++)
builder.addSlot(RecipeIngredientRole.INPUT, i * 18 + 1, 69).addItemStacks(Arrays.asList(recipe.ingredients[i].getItems()));
2024-02-03 14:56:07 +01:00
builder.addInvisibleIngredients(RecipeIngredientRole.OUTPUT).addItemStack(new ItemStack(DeferredSpawnEggItem.fromEntityType(recipe.entity)));
}
2020-01-25 16:56:04 +01:00
@Override
2023-07-08 12:32:27 +02:00
public void draw(AnimalSpawnerRecipe recipe, IRecipeSlotsView recipeSlotsView, GuiGraphics graphics, double mouseX, double mouseY) {
2021-12-19 17:14:56 +01:00
var minecraft = Minecraft.getInstance();
var entity = this.entityCache.get(recipe.entity);
2020-01-28 18:08:56 +01:00
if (entity == null) {
2021-12-04 15:40:09 +01:00
entity = recipe.makeEntity(minecraft.level, BlockPos.ZERO);
2020-01-28 18:08:56 +01:00
this.entityCache.put(recipe.entity, entity);
2020-01-25 16:56:04 +01:00
}
2023-07-08 12:32:27 +02:00
graphics.pose().pushPose();
2021-12-19 17:14:56 +01:00
var size = Math.max(1F, Math.max(recipe.entity.getWidth(), recipe.entity.getHeight()));
2023-07-08 12:32:27 +02:00
AnimalSpawnerCategory.renderEntity(graphics.pose(), 36, 56, 100F / size * 0.4F, 40, size * 0.5F, (LivingEntity) entity);
graphics.pose().popPose();
2020-01-25 16:56:04 +01:00
2021-12-19 17:14:56 +01:00
var name = recipe.entity.getDescription().getString();
2023-07-08 12:32:27 +02:00
graphics.drawString(minecraft.font, name, 36 - minecraft.font.width(name) / 2F, 55, 0xFFFFFF, true);
2020-09-22 03:17:02 +02:00
2020-01-25 16:56:04 +01:00
}
}