Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions src/main/java/codechicken/nei/PositionedStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@

import codechicken.nei.api.ItemFilter;
import codechicken.nei.api.ItemInfo;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.recipe.Badge;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.StackInfo;

/**
* Simply an {@link ItemStack} with position. Mainly used in the recipe handlers.
*/
public class PositionedStack {
public class PositionedStack implements Cloneable {

/** Maximum chance value representing 100% probability (1 unit = 0.01%). */
public static final int CHANCE_FULL = 10_000;

public int relx;
public int rely;
public int width = 16;
public int height = 16;
public ItemStack[] items;
// compatibility dummy
public ItemStack item;

protected int chance = CHANCE_FULL;
private boolean permutated = false;
protected boolean permutated = false;

public PositionedStack(Object object, int x, int y, boolean genPerms) {
items = NEIServerUtils.extractRecipeItems(object);
Expand Down Expand Up @@ -91,6 +94,10 @@ public List<Badge> getBadges() {
return null;
}

public List<String> getTooltip() {
return null;
}

public int getChance() {
return this.chance;
}
Expand All @@ -100,14 +107,20 @@ public void setChance(int chance) {
}

public PositionedStack copy() {
PositionedStack pStack = new PositionedStack(
Arrays.stream(this.items).map(ItemStack::copy).toArray(ItemStack[]::new),
relx,
rely,
false);
pStack.permutated = this.permutated;
pStack.chance = this.chance;
return pStack;
try {
PositionedStack pStack = (PositionedStack) super.clone();
pStack.items = Arrays.stream(this.items).map(ItemStack::copy).toArray(ItemStack[]::new);
pStack.item = this.item == null ? null : this.item.copy();
return pStack;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}

public void draw(int mousex, int mousey) {
final int x = this.relx + (this.width - 16) / 2;
final int y = this.rely + (this.height - 16) / 2;
GuiContainerManager.drawItem(x, y, this.item);
}

public List<ItemStack> getFilteredPermutations() {
Expand Down Expand Up @@ -164,7 +177,9 @@ public void setPermutationToRender(int index) {
}

public boolean contains(int mx, int my) {
return mx >= this.relx - 1 && mx < this.relx + 17 && my >= this.rely - 1 && my < this.rely + 17;
return mx >= this.relx - 1 && mx < this.relx + this.width + 1
&& my >= this.rely - 1
&& my < this.rely + this.height + 1;
}

public boolean contains(ItemStack ingredient) {
Expand Down Expand Up @@ -192,4 +207,21 @@ public boolean contains(Item ingred) {
public String toString() {
return "PositionedStack(output='" + item.toString() + "')";
}

/**
* A {@link PositionedStack} that keeps its items but never draws them.
*/
public static class Placeholder extends PositionedStack {

public Placeholder(Object object, int x, int y, boolean genPerms) {
super(object, x, y, genPerms);
}

public Placeholder(Object object, int x, int y) {
this(object, x, y, true);
}

@Override
public void draw(int mousex, int mousey) {}
}
}
11 changes: 8 additions & 3 deletions src/main/java/codechicken/nei/recipe/GuiFavoriteButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,16 @@ public void drawItemOverlay() {
if (this.selectedResult == null) return;

NEIClientUtils.gl2DRenderContext(
() -> GuiDraw.drawRect(this.selectedResult.relx, this.selectedResult.rely, 16, 16, 0x66333333));
() -> GuiDraw.drawRect(
this.selectedResult.relx,
this.selectedResult.rely,
this.selectedResult.width,
this.selectedResult.height,
0x66333333));

final Image icon = isFavorite() ? ICON_STATE_ON : ICON_STATE_OFF;
final int iconX = this.selectedResult.relx + (16 - icon.width) / 2;
final int iconY = this.selectedResult.rely + (18 - icon.height) / 2;
final int iconX = this.selectedResult.relx + (this.selectedResult.width - icon.width) / 2;
final int iconY = this.selectedResult.rely + (this.selectedResult.height + 2 - icon.height) / 2;

LayoutManager.drawIcon(iconX, iconY, icon);
}
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/codechicken/nei/recipe/NEIRecipeWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,15 @@ public void draw(int mouseX, int mouseY) {
}

protected void drawItem(PositionedStack pStack, int mouseX, int mouseY, int yShift, boolean input) {
GuiContainerManager.drawItem(pStack.relx, pStack.rely, pStack.item);
pStack.draw(mouseX - this.x, mouseY - this.y - yShift);

if (this.handlerInfo.getShowBadge()) {
drawBadge(pStack, input);
}

if (pStack.contains(mouseX - this.x, mouseY - this.y - yShift)) {
NEIClientUtils.gl2DRenderContext(() -> GuiDraw.drawRect(pStack.relx, pStack.rely, 16, 16, 0x80FFFFFF));
NEIClientUtils.gl2DRenderContext(
() -> GuiDraw.drawRect(pStack.relx, pStack.rely, pStack.width, pStack.height, 0x80FFFFFF));
}
}

Expand Down Expand Up @@ -291,7 +292,7 @@ protected void drawBadge(PositionedStack pStack, boolean input) {
final List<Badge> badges = getBadges(pStack, input);

for (Badge badge : badges) {
badge.draw(new Rectangle4i(pStack.relx, pStack.rely, 16, 16));
badge.draw(new Rectangle4i(pStack.relx, pStack.rely, pStack.width, pStack.height));
}
}

Expand Down Expand Up @@ -409,14 +410,22 @@ public List<String> handleItemTooltip(ItemStack itemstack, int mousex, int mouse
if (itemstack != null) {
final PositionedStack hovered = getPositionedStackMouseOver(mousex, mousey);

if (hovered != null && this.handlerInfo.getShowBadge()) {
final List<Badge> badges = getBadges(hovered, getOutputs().indexOf(hovered) == -1);
if (hovered != null) {
if (this.handlerInfo.getShowBadge()) {
final List<Badge> badges = getBadges(hovered, getOutputs().indexOf(hovered) == -1);

for (Badge badge : badges) {
if (badge.getTooltip() != null && !badge.getTooltip().isEmpty()) {
tooltip.addAll(badge.getTooltip());
for (Badge badge : badges) {
if (badge.getTooltip() != null && !badge.getTooltip().isEmpty()) {
tooltip.addAll(badge.getTooltip());
}
}
}

final List<String> customTooltip = hovered.getTooltip();

if (customTooltip != null && !customTooltip.isEmpty()) {
tooltip.addAll(customTooltip);
}
}

if (hovered == null || !NEIClientConfig.showCycledIngredientsTooltip()
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/codechicken/nei/recipe/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,18 @@ public static class RecipeIngredient {

protected int relx;
protected int rely;
protected int width;
protected int height;

public RecipeIngredient(int relx, int rely, List<ItemStack> items, int chance, int activeIndex) {
public RecipeIngredient(int relx, int rely, int width, int height, List<ItemStack> items, int chance,
int activeIndex) {
this.items = items.stream().map(ItemStack::copy).toArray(size -> new ItemStack[size]);
this.amount = StackInfo.getAmount(this.items[activeIndex]);
this.activeIndex = activeIndex;
this.relx = relx;
this.rely = rely;
this.width = width;
this.height = height;
this.chance = chance;
}

Expand All @@ -314,11 +319,19 @@ public static RecipeIngredient of(PositionedStack positionedStack) {
}
}

return of(positionedStack.relx, positionedStack.rely, stacks, positionedStack.getChance(), activeIndex);
return of(
positionedStack.relx,
positionedStack.rely,
positionedStack.width,
positionedStack.height,
stacks,
positionedStack.getChance(),
activeIndex);
}

public static RecipeIngredient of(int relx, int rely, List<ItemStack> stacks, int chance, int activeIndex) {
return new RecipeIngredient(relx, rely, stacks, chance, activeIndex);
public static RecipeIngredient of(int relx, int rely, int width, int height, List<ItemStack> stacks, int chance,
int activeIndex) {
return new RecipeIngredient(relx, rely, width, height, stacks, chance, activeIndex);
}

public int getAmount() {
Expand Down Expand Up @@ -352,6 +365,8 @@ public RecipeIngredient copy() {
return new RecipeIngredient(
this.relx,
this.rely,
this.width,
this.height,
getPermutations().stream().map(ItemStack::copy).collect(Collectors.toList()),
this.chance,
this.activeIndex);
Expand Down Expand Up @@ -436,7 +451,7 @@ protected static RecipeIngredient extractItem(Object item) {
}

if (item instanceof ItemStack stack) {
return RecipeIngredient.of(0, 0, Arrays.asList(stack), PositionedStack.CHANCE_FULL, 0);
return RecipeIngredient.of(0, 0, 16, 16, Arrays.asList(stack), PositionedStack.CHANCE_FULL, 0);
}

if (item instanceof RecipeIngredient ingr) {
Expand Down