sandbox/src/main/java/com/youpe/test/client/CustomInventoryScreen.java

136 lines
5.8 KiB
Java

package com.youpe.test.client;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.render.entity.EntityRenderDispatcher;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
public class CustomInventoryScreen extends CustomAbstractInventoryScreen<PlayerScreenHandler> {
private float mouseX;
private float mouseY;
private boolean narrow;
private boolean mouseDown;
public CustomInventoryScreen(PlayerEntity player) {
super(player.playerScreenHandler, player.getInventory(), Text.translatable("container.crafting"));
this.titleX = 97;
}
public void handledScreenTick() {
if (this.client.interactionManager.hasCreativeInventory()) {
this.client.setScreen(new CreativeInventoryScreen(this.client.player, this.client.player.networkHandler.getEnabledFeatures(), (Boolean)this.client.options.getOperatorItemsTab().getValue()));
}
}
protected void init() {
if (this.client.interactionManager.hasCreativeInventory()) {
this.client.setScreen(new CreativeInventoryScreen(this.client.player, this.client.player.networkHandler.getEnabledFeatures(), (Boolean)this.client.options.getOperatorItemsTab().getValue()));
} else {
super.init();
this.narrow = this.width < 379;
}
}
protected void drawForeground(DrawContext context, int mouseX, int mouseY) {
context.drawText(this.textRenderer, this.title, this.titleX, this.titleY, 4210752, false);
}
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
this.renderBackground(context);
if (this.narrow) {
this.drawBackground(context, delta, mouseX, mouseY);
} else {
super.render(context, mouseX, mouseY, delta);
}
this.drawMouseoverTooltip(context, mouseX, mouseY);
this.mouseX = (float)mouseX;
this.mouseY = (float)mouseY;
}
protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
int i = this.x;
int j = this.y;
context.drawTexture(BACKGROUND_TEXTURE, i, j, 0, 0, this.backgroundWidth, this.backgroundHeight);
drawEntity(context, i + 51, j + 75, 30, (float)(i + 51) - this.mouseX, (float)(j + 75 - 50) - this.mouseY, this.client.player);
}
public static void drawEntity(DrawContext context, int x, int y, int size, float mouseX, float mouseY, LivingEntity entity) {
float f = (float)Math.atan((double)(mouseX / 40.0F));
float g = (float)Math.atan((double)(mouseY / 40.0F));
Quaternionf quaternionf = (new Quaternionf()).rotateZ(3.1415927F);
Quaternionf quaternionf2 = (new Quaternionf()).rotateX(g * 20.0F * 0.017453292F);
quaternionf.mul(quaternionf2);
float h = entity.bodyYaw;
float i = entity.getYaw();
float j = entity.getPitch();
float k = entity.prevHeadYaw;
float l = entity.headYaw;
entity.bodyYaw = 180.0F + f * 20.0F;
entity.setYaw(180.0F + f * 40.0F);
entity.setPitch(-g * 20.0F);
entity.headYaw = entity.getYaw();
entity.prevHeadYaw = entity.getYaw();
drawEntity(context, x, y, size, quaternionf, quaternionf2, entity);
entity.bodyYaw = h;
entity.setYaw(i);
entity.setPitch(j);
entity.prevHeadYaw = k;
entity.headYaw = l;
}
public static void drawEntity(DrawContext context, int x, int y, int size, Quaternionf quaternionf, @Nullable Quaternionf quaternionf2, LivingEntity entity) {
context.getMatrices().push();
context.getMatrices().translate((double)x, (double)y, 50.0);
context.getMatrices().multiplyPositionMatrix((new Matrix4f()).scaling((float)size, (float)size, (float)(-size)));
context.getMatrices().multiply(quaternionf);
DiffuseLighting.method_34742();
EntityRenderDispatcher entityRenderDispatcher = MinecraftClient.getInstance().getEntityRenderDispatcher();
if (quaternionf2 != null) {
quaternionf2.conjugate();
entityRenderDispatcher.setRotation(quaternionf2);
}
entityRenderDispatcher.setRenderShadows(false);
RenderSystem.runAsFancy(() -> {
entityRenderDispatcher.render(entity, 0.0, 0.0, 0.0, 0.0F, 1.0F, context.getMatrices(), context.getVertexConsumers(), 15728880);
});
context.draw();
entityRenderDispatcher.setRenderShadows(true);
context.getMatrices().pop();
DiffuseLighting.enableGuiDepthLighting();
}
protected boolean isPointWithinBounds(int x, int y, int width, int height, double pointX, double pointY) {
return (!this.narrow) && super.isPointWithinBounds(x, y, width, height, pointX, pointY);
}
public boolean mouseClicked(double mouseX, double mouseY, int button) {
return this.narrow ? false : super.mouseClicked(mouseX, mouseY, button);
}
public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (this.mouseDown) {
this.mouseDown = false;
return true;
} else {
return super.mouseReleased(mouseX, mouseY, button);
}
}
protected void onMouseClick(Slot slot, int slotId, int button, SlotActionType actionType) {
super.onMouseClick(slot, slotId, button, actionType);
}
}