test gui init
This commit is contained in:
parent
cc09ee124b
commit
ccca7f55dd
|
@ -0,0 +1,123 @@
|
|||
package com.youpe.test.client;
|
||||
|
||||
import com.google.common.collect.Ordering;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.texture.Sprite;
|
||||
import net.minecraft.client.texture.StatusEffectSpriteManager;
|
||||
import net.minecraft.entity.effect.StatusEffect;
|
||||
import net.minecraft.entity.effect.StatusEffectInstance;
|
||||
import net.minecraft.entity.effect.StatusEffectUtil;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.screen.ScreenHandler;
|
||||
import net.minecraft.screen.ScreenTexts;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class CustomAbstractInventoryScreen <T extends ScreenHandler> extends CustomHandledScreen<T> {
|
||||
|
||||
public CustomAbstractInventoryScreen(T screenHandler, PlayerInventory inventory, Text title) {
|
||||
super(screenHandler, inventory, title);
|
||||
}
|
||||
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
|
||||
super.render(context, mouseX, mouseY, delta);
|
||||
this.drawStatusEffects(context, mouseX, mouseY);
|
||||
}
|
||||
|
||||
public boolean hideStatusEffectHud() {
|
||||
int i = this.x + this.backgroundWidth + 2;
|
||||
int j = this.width - i;
|
||||
return j >= 32;
|
||||
}
|
||||
|
||||
private void drawStatusEffects(DrawContext context, int mouseX, int mouseY) {
|
||||
int i = this.x + this.backgroundWidth + 2;
|
||||
int j = this.width - i;
|
||||
Collection<StatusEffectInstance> collection = this.client.player.getStatusEffects();
|
||||
if (!collection.isEmpty() && j >= 32) {
|
||||
boolean bl = j >= 120;
|
||||
int k = 33;
|
||||
if (collection.size() > 5) {
|
||||
k = 132 / (collection.size() - 1);
|
||||
}
|
||||
|
||||
Iterable<StatusEffectInstance> iterable = Ordering.natural().sortedCopy(collection);
|
||||
this.drawStatusEffectBackgrounds(context, i, k, iterable, bl);
|
||||
this.drawStatusEffectSprites(context, i, k, iterable, bl);
|
||||
if (bl) {
|
||||
this.drawStatusEffectDescriptions(context, i, k, iterable);
|
||||
} else if (mouseX >= i && mouseX <= i + 33) {
|
||||
int l = this.y;
|
||||
StatusEffectInstance statusEffectInstance = null;
|
||||
|
||||
for(Iterator var12 = iterable.iterator(); var12.hasNext(); l += k) {
|
||||
StatusEffectInstance statusEffectInstance2 = (StatusEffectInstance)var12.next();
|
||||
if (mouseY >= l && mouseY <= l + k) {
|
||||
statusEffectInstance = statusEffectInstance2;
|
||||
}
|
||||
}
|
||||
|
||||
if (statusEffectInstance != null) {
|
||||
List<Text> list = List.of(this.getStatusEffectDescription(statusEffectInstance), StatusEffectUtil.getDurationText(statusEffectInstance, 1.0F));
|
||||
context.drawTooltip(this.textRenderer, list, Optional.empty(), mouseX, mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void drawStatusEffectBackgrounds(DrawContext context, int x, int height, Iterable<StatusEffectInstance> statusEffects, boolean wide) {
|
||||
int i = this.y;
|
||||
|
||||
for(Iterator var7 = statusEffects.iterator(); var7.hasNext(); i += height) {
|
||||
StatusEffectInstance statusEffectInstance = (StatusEffectInstance)var7.next();
|
||||
if (wide) {
|
||||
context.drawTexture(BACKGROUND_TEXTURE, x, i, 0, 166, 120, 32);
|
||||
} else {
|
||||
context.drawTexture(BACKGROUND_TEXTURE, x, i, 0, 198, 32, 32);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void drawStatusEffectSprites(DrawContext context, int x, int height, Iterable<StatusEffectInstance> statusEffects, boolean wide) {
|
||||
StatusEffectSpriteManager statusEffectSpriteManager = this.client.getStatusEffectSpriteManager();
|
||||
int i = this.y;
|
||||
|
||||
for(Iterator var8 = statusEffects.iterator(); var8.hasNext(); i += height) {
|
||||
StatusEffectInstance statusEffectInstance = (StatusEffectInstance)var8.next();
|
||||
StatusEffect statusEffect = statusEffectInstance.getEffectType();
|
||||
Sprite sprite = statusEffectSpriteManager.getSprite(statusEffect);
|
||||
context.drawSprite(x + (wide ? 6 : 7), i + 7, 0, 18, 18, sprite);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void drawStatusEffectDescriptions(DrawContext context, int x, int height, Iterable<StatusEffectInstance> statusEffects) {
|
||||
int i = this.y;
|
||||
|
||||
for(Iterator var6 = statusEffects.iterator(); var6.hasNext(); i += height) {
|
||||
StatusEffectInstance statusEffectInstance = (StatusEffectInstance)var6.next();
|
||||
Text text = this.getStatusEffectDescription(statusEffectInstance);
|
||||
context.drawTextWithShadow(this.textRenderer, text, x + 10 + 18, i + 6, 16777215);
|
||||
Text text2 = StatusEffectUtil.getDurationText(statusEffectInstance, 1.0F);
|
||||
context.drawTextWithShadow(this.textRenderer, text2, x + 10 + 18, i + 6 + 10, 8355711);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Text getStatusEffectDescription(StatusEffectInstance statusEffect) {
|
||||
MutableText mutableText = statusEffect.getEffectType().getName().copy();
|
||||
if (statusEffect.getAmplifier() >= 1 && statusEffect.getAmplifier() <= 9) {
|
||||
MutableText var10000 = mutableText.append(ScreenTexts.SPACE);
|
||||
int var10001 = statusEffect.getAmplifier();
|
||||
var10000.append(Text.translatable("enchantment.level." + (var10001 + 1)));
|
||||
}
|
||||
|
||||
return mutableText;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,617 @@
|
|||
package com.youpe.test.client;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
|
||||
import net.minecraft.client.render.RenderLayer;
|
||||
import net.minecraft.client.texture.Sprite;
|
||||
import net.minecraft.client.util.InputUtil;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.screen.ScreenHandler;
|
||||
import net.minecraft.screen.slot.Slot;
|
||||
import net.minecraft.screen.slot.SlotActionType;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Formatting;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.Util;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public abstract class CustomHandledScreen<T extends ScreenHandler> extends Screen implements ScreenHandlerProvider<T> {
|
||||
public static final Identifier BACKGROUND_TEXTURE = new Identifier("textures/gui/container/inventory.png");
|
||||
private static final float field_32318 = 100.0F;
|
||||
private static final int field_32319 = 500;
|
||||
public static final int field_32322 = 100;
|
||||
private static final int field_32321 = 200;
|
||||
protected int backgroundWidth = 176;
|
||||
protected int backgroundHeight = 166;
|
||||
protected int titleX;
|
||||
protected int titleY;
|
||||
protected int playerInventoryTitleX;
|
||||
protected int playerInventoryTitleY;
|
||||
protected final T handler;
|
||||
protected final Text playerInventoryTitle;
|
||||
@Nullable
|
||||
protected Slot focusedSlot;
|
||||
@Nullable
|
||||
private Slot touchDragSlotStart;
|
||||
@Nullable
|
||||
private Slot touchDropOriginSlot;
|
||||
@Nullable
|
||||
private Slot touchHoveredSlot;
|
||||
@Nullable
|
||||
private Slot lastClickedSlot;
|
||||
protected int x;
|
||||
protected int y;
|
||||
private boolean touchIsRightClickDrag;
|
||||
private ItemStack touchDragStack;
|
||||
private int touchDropX;
|
||||
private int touchDropY;
|
||||
private long touchDropTime;
|
||||
private ItemStack touchDropReturningStack;
|
||||
private long touchDropTimer;
|
||||
protected final Set<Slot> cursorDragSlots;
|
||||
protected boolean cursorDragging;
|
||||
private int heldButtonType;
|
||||
private int heldButtonCode;
|
||||
private boolean cancelNextRelease;
|
||||
private int draggedStackRemainder;
|
||||
private long lastButtonClickTime;
|
||||
private int lastClickedButton;
|
||||
private boolean doubleClicking;
|
||||
private ItemStack quickMovingStack;
|
||||
|
||||
public CustomHandledScreen(T handler, PlayerInventory inventory, Text title) {
|
||||
super(title);
|
||||
this.touchDragStack = ItemStack.EMPTY;
|
||||
this.touchDropReturningStack = ItemStack.EMPTY;
|
||||
this.cursorDragSlots = Sets.newHashSet();
|
||||
this.quickMovingStack = ItemStack.EMPTY;
|
||||
this.handler = handler;
|
||||
this.playerInventoryTitle = inventory.getDisplayName();
|
||||
this.cancelNextRelease = true;
|
||||
this.titleX = 8;
|
||||
this.titleY = 6;
|
||||
this.playerInventoryTitleX = 8;
|
||||
this.playerInventoryTitleY = this.backgroundHeight - 94;
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
this.x = (this.width - this.backgroundWidth) / 2;
|
||||
this.y = (this.height - this.backgroundHeight) / 2;
|
||||
}
|
||||
|
||||
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
|
||||
int i = this.x;
|
||||
int j = this.y;
|
||||
this.drawBackground(context, delta, mouseX, mouseY);
|
||||
RenderSystem.disableDepthTest();
|
||||
super.render(context, mouseX, mouseY, delta);
|
||||
context.getMatrices().push();
|
||||
context.getMatrices().translate((float)i, (float)j, 0.0F);
|
||||
this.focusedSlot = null;
|
||||
|
||||
int l;
|
||||
int m;
|
||||
for(int k = 0; k < this.handler.slots.size(); ++k) {
|
||||
Slot slot = (Slot)this.handler.slots.get(k);
|
||||
if (slot.isEnabled()) {
|
||||
this.drawSlot(context, slot);
|
||||
}
|
||||
|
||||
if (this.isPointOverSlot(slot, (double)mouseX, (double)mouseY) && slot.isEnabled()) {
|
||||
this.focusedSlot = slot;
|
||||
l = slot.x;
|
||||
m = slot.y;
|
||||
if (this.focusedSlot.method_51306()) {
|
||||
drawSlotHighlight(context, l, m, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.drawForeground(context, mouseX, mouseY);
|
||||
ItemStack itemStack = this.touchDragStack.isEmpty() ? this.handler.getCursorStack() : this.touchDragStack;
|
||||
if (!itemStack.isEmpty()) {
|
||||
boolean n = true;
|
||||
l = this.touchDragStack.isEmpty() ? 8 : 16;
|
||||
String string = null;
|
||||
if (!this.touchDragStack.isEmpty() && this.touchIsRightClickDrag) {
|
||||
itemStack = itemStack.copyWithCount(MathHelper.ceil((float)itemStack.getCount() / 2.0F));
|
||||
} else if (this.cursorDragging && this.cursorDragSlots.size() > 1) {
|
||||
itemStack = itemStack.copyWithCount(this.draggedStackRemainder);
|
||||
if (itemStack.isEmpty()) {
|
||||
string = Formatting.YELLOW + "0";
|
||||
}
|
||||
}
|
||||
|
||||
this.drawItem(context, itemStack, mouseX - i - 8, mouseY - j - l, string);
|
||||
}
|
||||
|
||||
if (!this.touchDropReturningStack.isEmpty()) {
|
||||
float f = (float)(Util.getMeasuringTimeMs() - this.touchDropTime) / 100.0F;
|
||||
if (f >= 1.0F) {
|
||||
f = 1.0F;
|
||||
this.touchDropReturningStack = ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
l = this.touchDropOriginSlot.x - this.touchDropX;
|
||||
m = this.touchDropOriginSlot.y - this.touchDropY;
|
||||
int o = this.touchDropX + (int)((float)l * f);
|
||||
int p = this.touchDropY + (int)((float)m * f);
|
||||
this.drawItem(context, this.touchDropReturningStack, o, p, (String)null);
|
||||
}
|
||||
|
||||
context.getMatrices().pop();
|
||||
RenderSystem.enableDepthTest();
|
||||
}
|
||||
|
||||
public static void drawSlotHighlight(DrawContext context, int x, int y, int z) {
|
||||
context.fillGradient(RenderLayer.getGuiOverlay(), x, y, x + 16, y + 16, -2130706433, -2130706433, z);
|
||||
}
|
||||
|
||||
protected void drawMouseoverTooltip(DrawContext context, int x, int y) {
|
||||
if (this.handler.getCursorStack().isEmpty() && this.focusedSlot != null && this.focusedSlot.hasStack()) {
|
||||
ItemStack itemStack = this.focusedSlot.getStack();
|
||||
context.drawTooltip(this.textRenderer, this.getTooltipFromItem(itemStack), itemStack.getTooltipData(), x, y);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected List<Text> getTooltipFromItem(ItemStack stack) {
|
||||
return getTooltipFromItem(this.client, stack);
|
||||
}
|
||||
|
||||
private void drawItem(DrawContext context, ItemStack stack, int x, int y, String amountText) {
|
||||
context.getMatrices().push();
|
||||
context.getMatrices().translate(0.0F, 0.0F, 232.0F);
|
||||
context.drawItem(stack, x, y);
|
||||
context.drawItemInSlot(this.textRenderer, stack, x, y - (this.touchDragStack.isEmpty() ? 0 : 8), amountText);
|
||||
context.getMatrices().pop();
|
||||
}
|
||||
|
||||
protected void drawForeground(DrawContext context, int mouseX, int mouseY) {
|
||||
context.drawText(this.textRenderer, this.title, this.titleX, this.titleY, 4210752, false);
|
||||
context.drawText(this.textRenderer, this.playerInventoryTitle, this.playerInventoryTitleX, this.playerInventoryTitleY, 4210752, false);
|
||||
}
|
||||
|
||||
protected abstract void drawBackground(DrawContext context, float delta, int mouseX, int mouseY);
|
||||
|
||||
private void drawSlot(DrawContext context, Slot slot) {
|
||||
int i = slot.x;
|
||||
int j = slot.y;
|
||||
ItemStack itemStack = slot.getStack();
|
||||
boolean bl = false;
|
||||
boolean bl2 = slot == this.touchDragSlotStart && !this.touchDragStack.isEmpty() && !this.touchIsRightClickDrag;
|
||||
ItemStack itemStack2 = this.handler.getCursorStack();
|
||||
String string = null;
|
||||
if (slot == this.touchDragSlotStart && !this.touchDragStack.isEmpty() && this.touchIsRightClickDrag && !itemStack.isEmpty()) {
|
||||
itemStack = itemStack.copyWithCount(itemStack.getCount() / 2);
|
||||
} else if (this.cursorDragging && this.cursorDragSlots.contains(slot) && !itemStack2.isEmpty()) {
|
||||
if (this.cursorDragSlots.size() == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ScreenHandler.canInsertItemIntoSlot(slot, itemStack2, true) && this.handler.canInsertIntoSlot(slot)) {
|
||||
bl = true;
|
||||
int k = Math.min(itemStack2.getMaxCount(), slot.getMaxItemCount(itemStack2));
|
||||
int l = slot.getStack().isEmpty() ? 0 : slot.getStack().getCount();
|
||||
int m = ScreenHandler.calculateStackSize(this.cursorDragSlots, this.heldButtonType, itemStack2) + l;
|
||||
if (m > k) {
|
||||
m = k;
|
||||
String var10000 = Formatting.YELLOW.toString();
|
||||
string = var10000 + k;
|
||||
}
|
||||
|
||||
itemStack = itemStack2.copyWithCount(m);
|
||||
} else {
|
||||
this.cursorDragSlots.remove(slot);
|
||||
this.calculateOffset();
|
||||
}
|
||||
}
|
||||
|
||||
context.getMatrices().push();
|
||||
context.getMatrices().translate(0.0F, 0.0F, 100.0F);
|
||||
if (itemStack.isEmpty() && slot.isEnabled()) {
|
||||
Pair<Identifier, Identifier> pair = slot.getBackgroundSprite();
|
||||
if (pair != null) {
|
||||
Sprite sprite = (Sprite)this.client.getSpriteAtlas((Identifier)pair.getFirst()).apply((Identifier)pair.getSecond());
|
||||
context.drawSprite(i, j, 0, 16, 16, sprite);
|
||||
bl2 = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bl2) {
|
||||
if (bl) {
|
||||
context.fill(i, j, i + 16, j + 16, -2130706433);
|
||||
}
|
||||
|
||||
context.drawItem(itemStack, i, j, slot.x + slot.y * this.backgroundWidth);
|
||||
context.drawItemInSlot(this.textRenderer, itemStack, i, j, string);
|
||||
}
|
||||
|
||||
context.getMatrices().pop();
|
||||
}
|
||||
|
||||
private void calculateOffset() {
|
||||
ItemStack itemStack = this.handler.getCursorStack();
|
||||
if (!itemStack.isEmpty() && this.cursorDragging) {
|
||||
if (this.heldButtonType == 2) {
|
||||
this.draggedStackRemainder = itemStack.getMaxCount();
|
||||
} else {
|
||||
this.draggedStackRemainder = itemStack.getCount();
|
||||
|
||||
int i;
|
||||
int k;
|
||||
for(Iterator var2 = this.cursorDragSlots.iterator(); var2.hasNext(); this.draggedStackRemainder -= k - i) {
|
||||
Slot slot = (Slot)var2.next();
|
||||
ItemStack itemStack2 = slot.getStack();
|
||||
i = itemStack2.isEmpty() ? 0 : itemStack2.getCount();
|
||||
int j = Math.min(itemStack.getMaxCount(), slot.getMaxItemCount(itemStack));
|
||||
k = Math.min(ScreenHandler.calculateStackSize(this.cursorDragSlots, this.heldButtonType, itemStack) + i, j);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Slot getSlotAt(double x, double y) {
|
||||
for(int i = 0; i < this.handler.slots.size(); ++i) {
|
||||
Slot slot = (Slot)this.handler.slots.get(i);
|
||||
if (this.isPointOverSlot(slot, x, y) && slot.isEnabled()) {
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean mouseClicked(double mouseX, double mouseY, int button) {
|
||||
if (super.mouseClicked(mouseX, mouseY, button)) {
|
||||
return true;
|
||||
} else {
|
||||
boolean bl = this.client.options.pickItemKey.matchesMouse(button) && this.client.interactionManager.hasCreativeInventory();
|
||||
Slot slot = this.getSlotAt(mouseX, mouseY);
|
||||
long l = Util.getMeasuringTimeMs();
|
||||
this.doubleClicking = this.lastClickedSlot == slot && l - this.lastButtonClickTime < 250L && this.lastClickedButton == button;
|
||||
this.cancelNextRelease = false;
|
||||
if (button != 0 && button != 1 && !bl) {
|
||||
this.onMouseClick(button);
|
||||
} else {
|
||||
int i = this.x;
|
||||
int j = this.y;
|
||||
boolean bl2 = this.isClickOutsideBounds(mouseX, mouseY, i, j, button);
|
||||
int k = -1;
|
||||
if (slot != null) {
|
||||
k = slot.id;
|
||||
}
|
||||
|
||||
if (bl2) {
|
||||
k = -999;
|
||||
}
|
||||
|
||||
if ((Boolean)this.client.options.getTouchscreen().getValue() && bl2 && this.handler.getCursorStack().isEmpty()) {
|
||||
this.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (k != -1) {
|
||||
if ((Boolean)this.client.options.getTouchscreen().getValue()) {
|
||||
if (slot != null && slot.hasStack()) {
|
||||
this.touchDragSlotStart = slot;
|
||||
this.touchDragStack = ItemStack.EMPTY;
|
||||
this.touchIsRightClickDrag = button == 1;
|
||||
} else {
|
||||
this.touchDragSlotStart = null;
|
||||
}
|
||||
} else if (!this.cursorDragging) {
|
||||
if (this.handler.getCursorStack().isEmpty()) {
|
||||
if (bl) {
|
||||
this.onMouseClick(slot, k, button, SlotActionType.CLONE);
|
||||
} else {
|
||||
boolean bl3 = k != -999 && (InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), 340) || InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), 344));
|
||||
SlotActionType slotActionType = SlotActionType.PICKUP;
|
||||
if (bl3) {
|
||||
this.quickMovingStack = slot != null && slot.hasStack() ? slot.getStack().copy() : ItemStack.EMPTY;
|
||||
slotActionType = SlotActionType.QUICK_MOVE;
|
||||
} else if (k == -999) {
|
||||
slotActionType = SlotActionType.THROW;
|
||||
}
|
||||
|
||||
this.onMouseClick(slot, k, button, slotActionType);
|
||||
}
|
||||
|
||||
this.cancelNextRelease = true;
|
||||
} else {
|
||||
this.cursorDragging = true;
|
||||
this.heldButtonCode = button;
|
||||
this.cursorDragSlots.clear();
|
||||
if (button == 0) {
|
||||
this.heldButtonType = 0;
|
||||
} else if (button == 1) {
|
||||
this.heldButtonType = 1;
|
||||
} else if (bl) {
|
||||
this.heldButtonType = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.lastClickedSlot = slot;
|
||||
this.lastButtonClickTime = l;
|
||||
this.lastClickedButton = button;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private void onMouseClick(int button) {
|
||||
if (this.focusedSlot != null && this.handler.getCursorStack().isEmpty()) {
|
||||
if (this.client.options.swapHandsKey.matchesMouse(button)) {
|
||||
this.onMouseClick(this.focusedSlot, this.focusedSlot.id, 40, SlotActionType.SWAP);
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 9; ++i) {
|
||||
if (this.client.options.hotbarKeys[i].matchesMouse(button)) {
|
||||
this.onMouseClick(this.focusedSlot, this.focusedSlot.id, i, SlotActionType.SWAP);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected boolean isClickOutsideBounds(double mouseX, double mouseY, int left, int top, int button) {
|
||||
return mouseX < (double)left || mouseY < (double)top || mouseX >= (double)(left + this.backgroundWidth) || mouseY >= (double)(top + this.backgroundHeight);
|
||||
}
|
||||
|
||||
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
|
||||
Slot slot = this.getSlotAt(mouseX, mouseY);
|
||||
ItemStack itemStack = this.handler.getCursorStack();
|
||||
if (this.touchDragSlotStart != null && (Boolean)this.client.options.getTouchscreen().getValue()) {
|
||||
if (button == 0 || button == 1) {
|
||||
if (this.touchDragStack.isEmpty()) {
|
||||
if (slot != this.touchDragSlotStart && !this.touchDragSlotStart.getStack().isEmpty()) {
|
||||
this.touchDragStack = this.touchDragSlotStart.getStack().copy();
|
||||
}
|
||||
} else if (this.touchDragStack.getCount() > 1 && slot != null && ScreenHandler.canInsertItemIntoSlot(slot, this.touchDragStack, false)) {
|
||||
long l = Util.getMeasuringTimeMs();
|
||||
if (this.touchHoveredSlot == slot) {
|
||||
if (l - this.touchDropTimer > 500L) {
|
||||
this.onMouseClick(this.touchDragSlotStart, this.touchDragSlotStart.id, 0, SlotActionType.PICKUP);
|
||||
this.onMouseClick(slot, slot.id, 1, SlotActionType.PICKUP);
|
||||
this.onMouseClick(this.touchDragSlotStart, this.touchDragSlotStart.id, 0, SlotActionType.PICKUP);
|
||||
this.touchDropTimer = l + 750L;
|
||||
this.touchDragStack.decrement(1);
|
||||
}
|
||||
} else {
|
||||
this.touchHoveredSlot = slot;
|
||||
this.touchDropTimer = l;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (this.cursorDragging && slot != null && !itemStack.isEmpty() && (itemStack.getCount() > this.cursorDragSlots.size() || this.heldButtonType == 2) && ScreenHandler.canInsertItemIntoSlot(slot, itemStack, true) && slot.canInsert(itemStack) && this.handler.canInsertIntoSlot(slot)) {
|
||||
this.cursorDragSlots.add(slot);
|
||||
this.calculateOffset();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean mouseReleased(double mouseX, double mouseY, int button) {
|
||||
Slot slot = this.getSlotAt(mouseX, mouseY);
|
||||
int i = this.x;
|
||||
int j = this.y;
|
||||
boolean bl = this.isClickOutsideBounds(mouseX, mouseY, i, j, button);
|
||||
int k = -1;
|
||||
if (slot != null) {
|
||||
k = slot.id;
|
||||
}
|
||||
|
||||
if (bl) {
|
||||
k = -999;
|
||||
}
|
||||
|
||||
Slot slot2;
|
||||
Iterator var13;
|
||||
if (this.doubleClicking && slot != null && button == 0 && this.handler.canInsertIntoSlot(ItemStack.EMPTY, slot)) {
|
||||
if (hasShiftDown()) {
|
||||
if (!this.quickMovingStack.isEmpty()) {
|
||||
var13 = this.handler.slots.iterator();
|
||||
|
||||
while(var13.hasNext()) {
|
||||
slot2 = (Slot)var13.next();
|
||||
if (slot2 != null && slot2.canTakeItems(this.client.player) && slot2.hasStack() && slot2.inventory == slot.inventory && ScreenHandler.canInsertItemIntoSlot(slot2, this.quickMovingStack, true)) {
|
||||
this.onMouseClick(slot2, slot2.id, button, SlotActionType.QUICK_MOVE);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.onMouseClick(slot, k, button, SlotActionType.PICKUP_ALL);
|
||||
}
|
||||
|
||||
this.doubleClicking = false;
|
||||
this.lastButtonClickTime = 0L;
|
||||
} else {
|
||||
if (this.cursorDragging && this.heldButtonCode != button) {
|
||||
this.cursorDragging = false;
|
||||
this.cursorDragSlots.clear();
|
||||
this.cancelNextRelease = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.cancelNextRelease) {
|
||||
this.cancelNextRelease = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean bl2;
|
||||
if (this.touchDragSlotStart != null && (Boolean)this.client.options.getTouchscreen().getValue()) {
|
||||
if (button == 0 || button == 1) {
|
||||
if (this.touchDragStack.isEmpty() && slot != this.touchDragSlotStart) {
|
||||
this.touchDragStack = this.touchDragSlotStart.getStack();
|
||||
}
|
||||
|
||||
bl2 = ScreenHandler.canInsertItemIntoSlot(slot, this.touchDragStack, false);
|
||||
if (k != -1 && !this.touchDragStack.isEmpty() && bl2) {
|
||||
this.onMouseClick(this.touchDragSlotStart, this.touchDragSlotStart.id, button, SlotActionType.PICKUP);
|
||||
this.onMouseClick(slot, k, 0, SlotActionType.PICKUP);
|
||||
if (this.handler.getCursorStack().isEmpty()) {
|
||||
this.touchDropReturningStack = ItemStack.EMPTY;
|
||||
} else {
|
||||
this.onMouseClick(this.touchDragSlotStart, this.touchDragSlotStart.id, button, SlotActionType.PICKUP);
|
||||
this.touchDropX = MathHelper.floor(mouseX - (double)i);
|
||||
this.touchDropY = MathHelper.floor(mouseY - (double)j);
|
||||
this.touchDropOriginSlot = this.touchDragSlotStart;
|
||||
this.touchDropReturningStack = this.touchDragStack;
|
||||
this.touchDropTime = Util.getMeasuringTimeMs();
|
||||
}
|
||||
} else if (!this.touchDragStack.isEmpty()) {
|
||||
this.touchDropX = MathHelper.floor(mouseX - (double)i);
|
||||
this.touchDropY = MathHelper.floor(mouseY - (double)j);
|
||||
this.touchDropOriginSlot = this.touchDragSlotStart;
|
||||
this.touchDropReturningStack = this.touchDragStack;
|
||||
this.touchDropTime = Util.getMeasuringTimeMs();
|
||||
}
|
||||
|
||||
this.endTouchDrag();
|
||||
}
|
||||
} else if (this.cursorDragging && !this.cursorDragSlots.isEmpty()) {
|
||||
this.onMouseClick((Slot)null, -999, ScreenHandler.packQuickCraftData(0, this.heldButtonType), SlotActionType.QUICK_CRAFT);
|
||||
var13 = this.cursorDragSlots.iterator();
|
||||
|
||||
while(var13.hasNext()) {
|
||||
slot2 = (Slot)var13.next();
|
||||
this.onMouseClick(slot2, slot2.id, ScreenHandler.packQuickCraftData(1, this.heldButtonType), SlotActionType.QUICK_CRAFT);
|
||||
}
|
||||
|
||||
this.onMouseClick((Slot)null, -999, ScreenHandler.packQuickCraftData(2, this.heldButtonType), SlotActionType.QUICK_CRAFT);
|
||||
} else if (!this.handler.getCursorStack().isEmpty()) {
|
||||
if (this.client.options.pickItemKey.matchesMouse(button)) {
|
||||
this.onMouseClick(slot, k, button, SlotActionType.CLONE);
|
||||
} else {
|
||||
bl2 = k != -999 && (InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), 340) || InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), 344));
|
||||
if (bl2) {
|
||||
this.quickMovingStack = slot != null && slot.hasStack() ? slot.getStack().copy() : ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
this.onMouseClick(slot, k, button, bl2 ? SlotActionType.QUICK_MOVE : SlotActionType.PICKUP);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.handler.getCursorStack().isEmpty()) {
|
||||
this.lastButtonClickTime = 0L;
|
||||
}
|
||||
|
||||
this.cursorDragging = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void endTouchDrag() {
|
||||
this.touchDragStack = ItemStack.EMPTY;
|
||||
this.touchDragSlotStart = null;
|
||||
}
|
||||
|
||||
private boolean isPointOverSlot(Slot slot, double pointX, double pointY) {
|
||||
return this.isPointWithinBounds(slot.x, slot.y, 16, 16, pointX, pointY);
|
||||
}
|
||||
|
||||
protected boolean isPointWithinBounds(int x, int y, int width, int height, double pointX, double pointY) {
|
||||
int i = this.x;
|
||||
int j = this.y;
|
||||
pointX -= (double)i;
|
||||
pointY -= (double)j;
|
||||
return pointX >= (double)(x - 1) && pointX < (double)(x + width + 1) && pointY >= (double)(y - 1) && pointY < (double)(y + height + 1);
|
||||
}
|
||||
|
||||
protected void onMouseClick(Slot slot, int slotId, int button, SlotActionType actionType) {
|
||||
if (slot != null) {
|
||||
slotId = slot.id;
|
||||
}
|
||||
|
||||
this.client.interactionManager.clickSlot(this.handler.syncId, slotId, button, actionType, this.client.player);
|
||||
}
|
||||
|
||||
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
|
||||
if (super.keyPressed(keyCode, scanCode, modifiers)) {
|
||||
return true;
|
||||
} else if (this.client.options.inventoryKey.matchesKey(keyCode, scanCode)) {
|
||||
this.close();
|
||||
return true;
|
||||
} else {
|
||||
this.handleHotbarKeyPressed(keyCode, scanCode);
|
||||
if (this.focusedSlot != null && this.focusedSlot.hasStack()) {
|
||||
if (this.client.options.pickItemKey.matchesKey(keyCode, scanCode)) {
|
||||
this.onMouseClick(this.focusedSlot, this.focusedSlot.id, 0, SlotActionType.CLONE);
|
||||
} else if (this.client.options.dropKey.matchesKey(keyCode, scanCode)) {
|
||||
this.onMouseClick(this.focusedSlot, this.focusedSlot.id, hasControlDown() ? 1 : 0, SlotActionType.THROW);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean handleHotbarKeyPressed(int keyCode, int scanCode) {
|
||||
if (this.handler.getCursorStack().isEmpty() && this.focusedSlot != null) {
|
||||
if (this.client.options.swapHandsKey.matchesKey(keyCode, scanCode)) {
|
||||
this.onMouseClick(this.focusedSlot, this.focusedSlot.id, 40, SlotActionType.SWAP);
|
||||
return true;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 9; ++i) {
|
||||
if (this.client.options.hotbarKeys[i].matchesKey(keyCode, scanCode)) {
|
||||
this.onMouseClick(this.focusedSlot, this.focusedSlot.id, i, SlotActionType.SWAP);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removed() {
|
||||
if (this.client.player != null) {
|
||||
this.handler.onClosed(this.client.player);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean shouldPause() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public final void tick() {
|
||||
super.tick();
|
||||
if (this.client.player.isAlive() && !this.client.player.isRemoved()) {
|
||||
this.handledScreenTick();
|
||||
} else {
|
||||
this.client.player.closeHandledScreen();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void handledScreenTick() {
|
||||
}
|
||||
|
||||
public T getScreenHandler() {
|
||||
return this.handler;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.client.player.closeHandledScreen();
|
||||
super.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.youpe.test.event;
|
||||
|
||||
import com.youpe.test.Testing;
|
||||
import com.youpe.test.client.CustomInventoryScreen;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import com.youpe.test.client.GUI;
|
||||
|
@ -24,8 +25,8 @@ public class KeyInputHandler {
|
|||
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||
if (modkey.wasPressed()) {
|
||||
Testing.LOGGER.info("NAZHAL PIZDEC BLYA");
|
||||
client.player.sendMessage(Text.literal("PIZDEC"));
|
||||
MinecraftClient.getInstance().setScreen(new GUI(Text.empty()));
|
||||
client.player.sendMessage(Text.literal("PIZDEC2"));
|
||||
MinecraftClient.getInstance().setScreen(new CustomInventoryScreen(MinecraftClient.getInstance().player));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue