minecraft-src/net/minecraft/client/gui/screens/inventory/InventoryScreen.java
2025-07-04 03:45:38 +03:00

168 lines
5.9 KiB
Java

package net.minecraft.client.gui.screens.inventory;
import com.mojang.blaze3d.platform.Lighting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.navigation.ScreenPosition;
import net.minecraft.client.gui.screens.recipebook.CraftingRecipeBookComponent;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.InventoryMenu;
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;
import org.joml.Vector3f;
@Environment(EnvType.CLIENT)
public class InventoryScreen extends AbstractRecipeBookScreen<InventoryMenu> {
/**
* The old x position of the mouse pointer
*/
private float xMouse;
/**
* The old y position of the mouse pointer
*/
private float yMouse;
private boolean buttonClicked;
private final EffectsInInventory effects;
public InventoryScreen(Player player) {
super(player.inventoryMenu, new CraftingRecipeBookComponent(player.inventoryMenu), player.getInventory(), Component.translatable("container.crafting"));
this.titleLabelX = 97;
this.effects = new EffectsInInventory(this);
}
@Override
public void containerTick() {
super.containerTick();
if (this.minecraft.player.hasInfiniteMaterials()) {
this.minecraft
.setScreen(
new CreativeModeInventoryScreen(this.minecraft.player, this.minecraft.player.connection.enabledFeatures(), this.minecraft.options.operatorItemsTab().get())
);
}
}
@Override
protected void init() {
if (this.minecraft.player.hasInfiniteMaterials()) {
this.minecraft
.setScreen(
new CreativeModeInventoryScreen(this.minecraft.player, this.minecraft.player.connection.enabledFeatures(), this.minecraft.options.operatorItemsTab().get())
);
} else {
super.init();
}
}
@Override
protected ScreenPosition getRecipeBookButtonPosition() {
return new ScreenPosition(this.leftPos + 104, this.height / 2 - 22);
}
@Override
protected void onRecipeBookButtonClick() {
this.buttonClicked = true;
}
@Override
protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
guiGraphics.drawString(this.font, this.title, this.titleLabelX, this.titleLabelY, 4210752, false);
}
@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
super.render(guiGraphics, mouseX, mouseY, partialTick);
this.effects.render(guiGraphics, mouseX, mouseY, partialTick);
this.xMouse = mouseX;
this.yMouse = mouseY;
}
@Override
public boolean showsActiveEffects() {
return this.effects.canSeeEffects();
}
@Override
protected boolean isBiggerResultSlot() {
return false;
}
@Override
protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
int i = this.leftPos;
int j = this.topPos;
guiGraphics.blit(RenderType::guiTextured, INVENTORY_LOCATION, i, j, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
renderEntityInInventoryFollowsMouse(guiGraphics, i + 26, j + 8, i + 75, j + 78, 30, 0.0625F, this.xMouse, this.yMouse, this.minecraft.player);
}
public static void renderEntityInInventoryFollowsMouse(
GuiGraphics guiGraphics, int x1, int y1, int x2, int y2, int scale, float yOffset, float mouseX, float mouseY, LivingEntity entity
) {
float f = (x1 + x2) / 2.0F;
float g = (y1 + y2) / 2.0F;
guiGraphics.enableScissor(x1, y1, x2, y2);
float h = (float)Math.atan((f - mouseX) / 40.0F);
float i = (float)Math.atan((g - mouseY) / 40.0F);
Quaternionf quaternionf = new Quaternionf().rotateZ((float) Math.PI);
Quaternionf quaternionf2 = new Quaternionf().rotateX(i * 20.0F * (float) (Math.PI / 180.0));
quaternionf.mul(quaternionf2);
float j = entity.yBodyRot;
float k = entity.getYRot();
float l = entity.getXRot();
float m = entity.yHeadRotO;
float n = entity.yHeadRot;
entity.yBodyRot = 180.0F + h * 20.0F;
entity.setYRot(180.0F + h * 40.0F);
entity.setXRot(-i * 20.0F);
entity.yHeadRot = entity.getYRot();
entity.yHeadRotO = entity.getYRot();
float o = entity.getScale();
Vector3f vector3f = new Vector3f(0.0F, entity.getBbHeight() / 2.0F + yOffset * o, 0.0F);
float p = scale / o;
renderEntityInInventory(guiGraphics, f, g, p, vector3f, quaternionf, quaternionf2, entity);
entity.yBodyRot = j;
entity.setYRot(k);
entity.setXRot(l);
entity.yHeadRotO = m;
entity.yHeadRot = n;
guiGraphics.disableScissor();
}
public static void renderEntityInInventory(
GuiGraphics guiGraphics, float x, float y, float scale, Vector3f translate, Quaternionf pose, @Nullable Quaternionf cameraOrientation, LivingEntity entity
) {
guiGraphics.pose().pushPose();
guiGraphics.pose().translate((double)x, (double)y, 50.0);
guiGraphics.pose().scale(scale, scale, -scale);
guiGraphics.pose().translate(translate.x, translate.y, translate.z);
guiGraphics.pose().mulPose(pose);
guiGraphics.flush();
Lighting.setupForEntityInInventory();
EntityRenderDispatcher entityRenderDispatcher = Minecraft.getInstance().getEntityRenderDispatcher();
if (cameraOrientation != null) {
entityRenderDispatcher.overrideCameraOrientation(cameraOrientation.conjugate(new Quaternionf()).rotateY((float) Math.PI));
}
entityRenderDispatcher.setRenderShadow(false);
guiGraphics.drawSpecial(multiBufferSource -> entityRenderDispatcher.render(entity, 0.0, 0.0, 0.0, 1.0F, guiGraphics.pose(), multiBufferSource, 15728880));
guiGraphics.flush();
entityRenderDispatcher.setRenderShadow(true);
guiGraphics.pose().popPose();
Lighting.setupFor3DItems();
}
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (this.buttonClicked) {
this.buttonClicked = false;
return true;
} else {
return super.mouseReleased(mouseX, mouseY, button);
}
}
}