Files
NotEnoughItems/src/main/java/codechicken/nei/ItemHistoryPanel.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.4 KiB
Java
Raw Normal View History

2024-02-21 18:12:28 +02:00
package codechicken.nei;
import java.util.ArrayList;
import java.util.List;
2024-02-21 18:12:28 +02:00
import net.minecraft.item.ItemStack;
import codechicken.nei.ItemsGrid.ItemsGridSlot;
import codechicken.nei.ItemsGrid.MouseContext;
import codechicken.nei.recipe.Recipe.RecipeId;
2024-02-21 18:12:28 +02:00
import codechicken.nei.recipe.StackInfo;
public class ItemHistoryPanel extends AbstractSubpanel<ItemsGrid<ItemHistoryPanel.HistoryGridSlot, MouseContext>> {
public static class HistoryGridSlot extends ItemsGridSlot {
public HistoryGridSlot(int slotIndex, int itemIndex, ItemStack item) {
super(slotIndex, itemIndex, item);
}
@Override
public RecipeId getRecipeId() {
return FavoriteRecipes.getFavorite(this.item);
}
}
2025-08-22 14:58:41 +03:00
public ItemHistoryPanel() {
this.grid = new ItemsGrid<>() {
protected List<HistoryGridSlot> gridMask;
2025-08-22 14:58:41 +03:00
@Override
protected void onGridChanged() {
this.gridMask = null;
super.onGridChanged();
}
2025-08-22 14:58:41 +03:00
@Override
public List<HistoryGridSlot> getMask() {
2025-08-22 14:58:41 +03:00
if (this.gridMask == null) {
this.gridMask = new ArrayList<>();
for (int slotIndex = 0; slotIndex < Math.min(size(), this.rows * this.columns); slotIndex++) {
this.gridMask.add(new HistoryGridSlot(slotIndex, slotIndex, getItem(slotIndex)));
2025-08-22 14:58:41 +03:00
}
}
2025-08-22 14:58:41 +03:00
return this.gridMask;
}
2025-08-22 14:58:41 +03:00
@Override
protected MouseContext getMouseContext(int mousex, int mousey) {
final HistoryGridSlot hovered = getSlotMouseOver(mousex, mousey);
2024-02-21 18:12:28 +02:00
2025-08-22 14:58:41 +03:00
if (hovered != null) {
return new MouseContext(
hovered.slotIndex,
hovered.slotIndex / this.columns,
hovered.slotIndex % this.columns);
}
2024-02-21 18:12:28 +02:00
2025-08-22 14:58:41 +03:00
return null;
2024-02-21 18:12:28 +02:00
}
2025-08-22 14:58:41 +03:00
};
2024-02-21 18:12:28 +02:00
}
@Override
2025-08-22 14:58:41 +03:00
protected ItemStack getDraggedStackWithQuantity(ItemStack itemStack) {
return ItemQuantityField.prepareStackWithQuantity(itemStack, 0);
2024-02-21 18:12:28 +02:00
}
@Override
2025-08-22 14:58:41 +03:00
public void draw(int mousex, int mousey) {
if (this.grid.size() > 0) {
super.draw(mousex, mousey);
2024-02-21 18:12:28 +02:00
}
}
@Override
2025-08-22 14:58:41 +03:00
public int setPanelWidth(int width) {
final int columns = width / ItemsGrid.SLOT_SIZE;
final int useRows = NEIClientConfig.getIntSetting("inventory.history.useRows");
final int rows = (int) Math.min(Math.ceil(this.grid.size() * 1f / columns), useRows);
2024-02-21 18:12:28 +02:00
2025-08-22 14:58:41 +03:00
this.w = width;
this.h = 8 + ItemsGrid.SLOT_SIZE * Math.max(rows, 1);
2024-02-21 18:12:28 +02:00
2025-08-22 14:58:41 +03:00
return rows;
}
2025-08-22 14:58:41 +03:00
public void update() {
this.splittingLineColor = NEIClientConfig.getSetting("inventory.history.color").getHexValue();
super.update();
}
2025-08-22 14:58:41 +03:00
public void addItem(ItemStack stack) {
if (stack != null) {
ItemStack is = StackInfo.withAmount(stack, 0);
2024-02-21 18:12:28 +02:00
2025-08-22 14:58:41 +03:00
this.grid.realItems.removeIf(historyStack -> StackInfo.equalItemAndNBT(historyStack, stack, true));
this.grid.realItems.add(0, is);
2024-02-21 18:12:28 +02:00
2025-08-22 14:58:41 +03:00
if (this.grid.realItems.size() > Math.max(50, this.grid.rows * this.grid.columns)) {
this.grid.realItems.remove(this.grid.realItems.size() - 1);
2024-02-21 18:12:28 +02:00
}
2025-08-22 14:58:41 +03:00
this.grid.onItemsChanged();
2024-02-21 18:12:28 +02:00
}
}
}