| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.navigation.ScreenPosition;
|
| 8 | import net.minecraft.client.gui.screens.recipebook.CraftingRecipeBookComponent;
|
| 9 | import net.minecraft.client.input.MouseButtonEvent;
|
| 10 | import net.minecraft.client.renderer.RenderPipelines;
|
| 11 | import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
|
| 12 | import net.minecraft.client.renderer.entity.EntityRenderer;
|
| 13 | import net.minecraft.client.renderer.entity.state.EntityRenderState;
|
| 14 | import net.minecraft.client.renderer.entity.state.LivingEntityRenderState;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.Pose;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.inventory.InventoryMenu;
|
| 20 | import org.joml.Quaternionf;
|
| 21 | import org.joml.Vector3f;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class InventoryScreen extends AbstractRecipeBookScreen<InventoryMenu> {
|
| 25 | private float xMouse;
|
| 26 | private float yMouse;
|
| 27 | private boolean buttonClicked;
|
| 28 | private final EffectsInInventory effects;
|
| 29 |
|
| 30 | public InventoryScreen(final Player player) {
|
| 31 | super(player.inventoryMenu, new CraftingRecipeBookComponent(player.inventoryMenu), player.getInventory(), Component.translatable("container.crafting"));
|
| 32 | this.titleLabelX = 97;
|
| 33 | this.effects = new EffectsInInventory(this);
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void containerTick() {
|
| 38 | super.containerTick();
|
| 39 | if (this.minecraft.player.hasInfiniteMaterials()) {
|
| 40 | this.minecraft
|
| 41 | .gui
|
| 42 | .setScreen(
|
| 43 | new CreativeModeInventoryScreen(this.minecraft.player, this.minecraft.player.connection.enabledFeatures(), this.minecraft.options.operatorItemsTab().get())
|
| 44 | );
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected void init() {
|
| 50 | if (this.minecraft.player.hasInfiniteMaterials()) {
|
| 51 | this.minecraft
|
| 52 | .gui
|
| 53 | .setScreen(
|
| 54 | new CreativeModeInventoryScreen(this.minecraft.player, this.minecraft.player.connection.enabledFeatures(), this.minecraft.options.operatorItemsTab().get())
|
| 55 | );
|
| 56 | } else {
|
| 57 | super.init();
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected ScreenPosition getRecipeBookButtonPosition() {
|
| 63 | return new ScreenPosition(this.leftPos + 104, this.height / 2 - 22);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void onRecipeBookButtonClick() {
|
| 68 | this.buttonClicked = true;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected void extractLabels(final GuiGraphicsExtractor graphics, final int xm, final int ym) {
|
| 73 | graphics.text(this.font, this.title, this.titleLabelX, this.titleLabelY, -12566464, false);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 78 | this.effects.extractRenderState(graphics, mouseX, mouseY);
|
| 79 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 80 | this.xMouse = mouseX;
|
| 81 | this.yMouse = mouseY;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public boolean showsActiveEffects() {
|
| 86 | return this.effects.canSeeEffects();
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected boolean isBiggerResultSlot() {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 96 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 97 | int xo = this.leftPos;
|
| 98 | int yo = this.topPos;
|
| 99 | graphics.blit(RenderPipelines.GUI_TEXTURED, INVENTORY_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
|
| 100 | extractEntityInInventoryFollowsMouse(graphics, xo + 26, yo + 8, xo + 75, yo + 78, 30, 0.0625F, this.xMouse, this.yMouse, this.minecraft.player);
|
| 101 | }
|
| 102 |
|
| 103 | public static void extractEntityInInventoryFollowsMouse(
|
| 104 | final GuiGraphicsExtractor graphics,
|
| 105 | final int x0,
|
| 106 | final int y0,
|
| 107 | final int x1,
|
| 108 | final int y1,
|
| 109 | final int size,
|
| 110 | final float offsetY,
|
| 111 | final float mouseX,
|
| 112 | final float mouseY,
|
| 113 | final LivingEntity entity
|
| 114 | ) {
|
| 115 | float centerX = (x0 + x1) / 2.0F;
|
| 116 | float centerY = (y0 + y1) / 2.0F;
|
| 117 | float xAngle = (float)Math.atan((centerX - mouseX) / 40.0F);
|
| 118 | float yAngle = (float)Math.atan((centerY - mouseY) / 40.0F);
|
| 119 | Quaternionf rotation = new Quaternionf().rotateZ((float) Math.PI);
|
| 120 | Quaternionf xRotation = new Quaternionf().rotateX(yAngle * 20.0F * (float) (Math.PI / 180.0));
|
| 121 | rotation.mul(xRotation);
|
| 122 | EntityRenderState renderState = extractRenderState(entity);
|
| 123 | if (renderState instanceof LivingEntityRenderState livingRenderState) {
|
| 124 | livingRenderState.bodyRot = 180.0F + xAngle * 20.0F;
|
| 125 | livingRenderState.yRot = xAngle * 20.0F;
|
| 126 | if (livingRenderState.pose != Pose.FALL_FLYING) {
|
| 127 | livingRenderState.xRot = -yAngle * 20.0F;
|
| 128 | } else {
|
| 129 | livingRenderState.xRot = 0.0F;
|
| 130 | }
|
| 131 |
|
| 132 | livingRenderState.boundingBoxWidth = livingRenderState.boundingBoxWidth / livingRenderState.scale;
|
| 133 | livingRenderState.boundingBoxHeight = livingRenderState.boundingBoxHeight / livingRenderState.scale;
|
| 134 | livingRenderState.scale = 1.0F;
|
| 135 | }
|
| 136 |
|
| 137 | Vector3f translation = new Vector3f(0.0F, renderState.boundingBoxHeight / 2.0F + offsetY, 0.0F);
|
| 138 | graphics.entity(renderState, size, translation, rotation, xRotation, x0, y0, x1, y1);
|
| 139 | }
|
| 140 |
|
| 141 | private static EntityRenderState extractRenderState(final LivingEntity entity) {
|
| 142 | EntityRenderDispatcher entityRenderDispatcher = Minecraft.getInstance().getEntityRenderDispatcher();
|
| 143 | EntityRenderer<? super LivingEntity, ?> renderer = entityRenderDispatcher.getRenderer(entity);
|
| 144 | EntityRenderState renderState = renderer.createRenderState(entity, 1.0F);
|
| 145 | renderState.shadowPieces.clear();
|
| 146 | renderState.outlineColor = 0;
|
| 147 | return renderState;
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public boolean mouseReleased(final MouseButtonEvent event) {
|
| 152 | if (this.buttonClicked) {
|
| 153 | this.buttonClicked = false;
|
| 154 | return true;
|
| 155 | } else {
|
| 156 | return super.mouseReleased(event);
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 |
|