|
| 1 | +package com.devdyna.cakesticklib.api.compat.jei; |
| 2 | + |
| 3 | +import java.util.function.Function; |
| 4 | + |
| 5 | +import net.minecraft.client.Minecraft; |
| 6 | +import net.minecraft.client.gui.GuiGraphicsExtractor; |
| 7 | +import net.minecraft.client.gui.screens.inventory.InventoryScreen; |
| 8 | +import net.minecraft.util.Mth; |
| 9 | +import net.minecraft.world.entity.EntitySpawnReason; |
| 10 | +import net.minecraft.world.entity.EntityType; |
| 11 | +import net.minecraft.world.entity.LivingEntity; |
| 12 | + |
| 13 | +public class EntityJei { |
| 14 | + |
| 15 | + public static <ENTITY extends LivingEntity> void renderEntity(EntityType<ENTITY> type, |
| 16 | + GuiGraphicsExtractor g, int x, int y, int xo, int yo, double mouseX, |
| 17 | + double mouseY) { |
| 18 | + renderEntity(type, g, x, y, xo, yo, e -> e, mouseX, mouseY); |
| 19 | + } |
| 20 | + |
| 21 | + public static <ENTITY extends LivingEntity> void renderEntity(EntityType<ENTITY> type, |
| 22 | + GuiGraphicsExtractor g, int x, int y, int xo, int yo, Function<ENTITY, ENTITY> c, double mouseX, |
| 23 | + double mouseY) { |
| 24 | + |
| 25 | + var level = Minecraft.getInstance().level; |
| 26 | + |
| 27 | + if (level == null) |
| 28 | + return; |
| 29 | + |
| 30 | + var entity = type.create(level, EntitySpawnReason.EVENT); |
| 31 | + |
| 32 | + if (entity == null) |
| 33 | + return; |
| 34 | + |
| 35 | + entity = c.apply(entity); |
| 36 | + |
| 37 | + xo = Math.max(xo, 1); |
| 38 | + yo = Math.max(yo, 1); |
| 39 | + |
| 40 | + var pose = g.pose(); |
| 41 | + var left = pose.m20(); |
| 42 | + var top = pose.m21(); |
| 43 | + |
| 44 | + var x1 = (int) left + x; |
| 45 | + var y1 = (int) top + y; |
| 46 | + var x2 = (int) left + xo + x; |
| 47 | + var y2 = (int) top + yo + y; |
| 48 | + |
| 49 | + var entH = entity.getBbHeight(); |
| 50 | + var entW = entity.getBbWidth(); |
| 51 | + |
| 52 | + var scale = Mth.clamp( |
| 53 | + Math.min((y2 - y1) / entH, (x2 - x1) / Mth.sqrt(entW * entW + entH * entH)), 6.0F, 18.0F); |
| 54 | + |
| 55 | + InventoryScreen.extractEntityInInventoryFollowsMouse( |
| 56 | + g, |
| 57 | + x1, y1, x2, y2, |
| 58 | + (int) scale, |
| 59 | + ((y2 - y1) - entH * scale) / 2 / scale, |
| 60 | + (int) (mouseX + left), (int) (mouseY + top), |
| 61 | + entity); |
| 62 | + } |
| 63 | +} |
0 commit comments