/
RecipeTransferButton.java
77 lines (66 loc) · 2.59 KB
/
RecipeTransferButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package mezz.jei.gui.recipes;
import com.mojang.blaze3d.matrix.MatrixStack;
import javax.annotation.Nullable;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.Container;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import mezz.jei.gui.TooltipRenderer;
import mezz.jei.gui.elements.GuiIconButtonSmall;
import mezz.jei.recipes.RecipeTransferManager;
import mezz.jei.transfer.RecipeTransferErrorInternal;
import mezz.jei.transfer.RecipeTransferUtil;
import net.minecraft.util.text.TranslationTextComponent;
public class RecipeTransferButton extends GuiIconButtonSmall {
private final RecipeLayout<?> recipeLayout;
@Nullable
private IRecipeTransferError recipeTransferError;
@Nullable
private IOnClickHandler onClickHandler;
public RecipeTransferButton(int xPos, int yPos, int width, int height, IDrawable icon, RecipeLayout<?> recipeLayout) {
super(xPos, yPos, width, height, icon, b -> {});
this.recipeLayout = recipeLayout;
}
public void init(RecipeTransferManager recipeTransferManager, @Nullable Container container, PlayerEntity player) {
if (container != null) {
this.recipeTransferError = RecipeTransferUtil.getTransferRecipeError(recipeTransferManager, container, recipeLayout, player);
} else {
this.recipeTransferError = RecipeTransferErrorInternal.INSTANCE;
}
if (RecipeTransferUtil.allowsTransfer(recipeTransferError)) {
this.active = true;
this.visible = true;
} else {
this.active = false;
IRecipeTransferError.Type type = this.recipeTransferError.getType();
this.visible = (type == IRecipeTransferError.Type.USER_FACING);
}
}
public void drawToolTip(MatrixStack matrixStack, int mouseX, int mouseY) {
if (isMouseOver(mouseX, mouseY)) {
if (recipeTransferError == null) {
TranslationTextComponent tooltipTransfer = new TranslationTextComponent("jei.tooltip.transfer");
TooltipRenderer.drawHoveringText(tooltipTransfer, mouseX, mouseY, matrixStack);
} else {
recipeTransferError.showError(matrixStack, mouseX, mouseY, recipeLayout, recipeLayout.getPosX(), recipeLayout.getPosY());
}
}
}
@Override
public boolean isMouseOver(double mouseX, double mouseY) {
return this.visible &&
mouseX >= this.x &&
mouseY >= this.y &&
mouseX < this.x + this.width &&
mouseY < this.y + this.height;
}
public void setOnClickHandler(IOnClickHandler onClickHandler) {
this.onClickHandler = onClickHandler;
}
@Override
public void onClick(double mouseX, double mouseY) {
if (onClickHandler != null) {
onClickHandler.onClick(mouseX, mouseY);
}
}
}