Skip to content

Commit

Permalink
NEI Bookmarks improvements (#188)
Browse files Browse the repository at this point in the history
* Add bookmarks sets; save items count in bookmarks; change counts in bookmarks; save ingredients in bookmarks; fill fluidcontainers in bookmarks;
* change fill container controls
* fix fill container
* rollback public variables

Co-authored-by: Serghei Borovetchi <[email protected]>
  • Loading branch information
slprime and Serghei Borovetchi authored Jan 2, 2022
1 parent ce17ec8 commit 5dde7ca
Show file tree
Hide file tree
Showing 20 changed files with 1,523 additions and 532 deletions.
700 changes: 594 additions & 106 deletions src/main/java/codechicken/nei/BookmarkPanel.java

Large diffs are not rendered by default.

539 changes: 285 additions & 254 deletions src/main/java/codechicken/nei/ItemPanel.java

Large diffs are not rendered by default.

315 changes: 315 additions & 0 deletions src/main/java/codechicken/nei/ItemsGrid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
package codechicken.nei;

import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.api.GuiInfo;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.recipe.StackInfo;
import codechicken.nei.ItemPanel.ItemPanelSlot;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;

import static codechicken.lib.gui.GuiDraw.drawRect;

public class ItemsGrid
{
protected static final int SLOT_SIZE = 18;

protected int width;
protected int height;

protected int marginLeft;
protected int marginTop;

protected ArrayList<ItemStack> realItems = new ArrayList<>();

protected int page;
protected int perPage;

protected int firstIndex;
protected int numPages;

protected int rows;
protected int columns;

protected boolean[] validSlotMap;
protected boolean[] invalidSlotMap;

private int[] guiSize;

protected boolean needRefresh = false;


public ArrayList<ItemStack> getItems()
{
return realItems;
}

public ItemStack getItem(int idx)
{
return realItems.get(idx);
}

public int size()
{
return realItems.size();
}

public int indexOf(ItemStack stackA, boolean useNBT)
{

for (int idx = 0; idx < realItems.size(); idx++) {
if (StackInfo.equalItemAndNBT(stackA, realItems.get(idx), useNBT)) {
return idx;
}
}

return -1;
}

public int getPage()
{
return page + 1;
}

public int getPerPage()
{
return perPage;
}

public int getNumPages()
{
return numPages;
}

public int getRows()
{
return rows;
}

public int getColumns()
{
return columns;
}

public void setGridSize(int mleft, int mtop, int w, int h)
{

//I don't like this big condition
if (marginLeft != mleft || marginTop != mtop || width != w || height != h) {

marginLeft = mleft;
marginTop = mtop;

width = Math.max(0, w);
height = Math.max(0, h);

columns = width / SLOT_SIZE;
rows = height / SLOT_SIZE;

needRefresh = true;
}

}

public void shiftPage(int shift)
{
if (perPage == 0) {
numPages = 0;
page = 0;
return;
}

numPages = (int) Math.ceil((float) realItems.size() / (float) perPage);

page += shift;

if (page >= numPages) {
page = page - numPages;
}

if (page < 0) {
page = numPages + page;
}

page = Math.max(0, Math.min(page, numPages - 1));

needRefresh = true;
}

public void refresh(GuiContainer gui)
{
if (!needRefresh(gui)) {
return;
}

updateGuiOverlapSlots(gui);
shiftPage(0);

needRefresh = false;
}

private boolean needRefresh(GuiContainer gui)
{

if (needRefresh == true) {
return true;
}

if (gui == null && guiSize == null) {
return false;
}

if (gui == null) {
guiSize = null;
return true;
}

if (guiSize == null) {
guiSize = new int[]{ gui.guiLeft, gui.guiTop, gui.width, gui.height };
return true;
}

if (gui.guiLeft != guiSize[0] || gui.guiTop != guiSize[1] || gui.width != guiSize[2] || gui.height != guiSize[3]) {
guiSize = new int[]{ gui.guiLeft, gui.guiTop, gui.width, gui.height };
return true;
}

return false;
}

private void updateGuiOverlapSlots(GuiContainer gui)
{
invalidSlotMap = new boolean[rows * columns];
perPage = columns * rows;

checkGuiOverlap(gui, 0, columns - 2, 1);
checkGuiOverlap(gui, columns - 1, 1, -1);

}

private void checkGuiOverlap(GuiContainer gui, int start, int end, int dir)
{
boolean validColumn = false;

for (int c = start; c != end && !validColumn; c += dir) {
validColumn = true;

for (int r = 0; r < rows; r++) {

if (!slotValid(gui, r, c) && !invalidSlotMap[columns * r + c]) {
invalidSlotMap[columns * r + c] = true;
validColumn = false;
perPage--;
}

}

}

}

private boolean slotValid(GuiContainer gui, int row, int column)
{
Rectangle4i rect = getSlotRect(row, column);

try {
GuiInfo.readLock.lock();
if (GuiInfo.guiHandlers.stream().anyMatch(handler -> handler.hideItemPanelSlot(gui, rect.x, rect.y, rect.w, rect.h))) {
return false;
}
} finally {
GuiInfo.readLock.unlock();
}

return true;
}

public Rectangle4i getSlotRect(int i)
{
return getSlotRect(i / columns, i % columns);
}

public Rectangle4i getSlotRect(int row, int column)
{
return new Rectangle4i(marginLeft + (width % SLOT_SIZE) / 2 + column * SLOT_SIZE, marginTop + row * SLOT_SIZE, SLOT_SIZE, SLOT_SIZE);
}

public boolean isInvalidSlot(int idx)
{
return invalidSlotMap[idx] == true;
}

public void draw(int mousex, int mousey)
{
if (getPerPage() == 0) {
return;
}

ItemPanelSlot slot = getSlotMouseOver(mousex, mousey);

GuiContainerManager.enableMatrixStackLogging();

int idx = page * perPage;
for (int i = 0; i < rows * columns && idx < size(); i++) {

if (!invalidSlotMap[i]) {
drawItem(getSlotRect(i), idx, slot);
idx ++;
}

}

GuiContainerManager.disableMatrixStackLogging();
}

protected void drawItem(Rectangle4i rect, int idx, ItemPanelSlot focus)
{

if (focus != null && focus.slotIndex == idx) {
drawRect(rect.x, rect.y, rect.w, rect.h, 0xee555555);//highlight
}

GuiContainerManager.drawItem(rect.x + 1, rect.y + 1, getItem(idx));
}

public ItemPanelSlot getSlotMouseOver(int mousex, int mousey)
{
if (!contains(mousex, mousey)) {
return null;
}

final int overRow = (int) ((mousey - marginTop) / SLOT_SIZE);
final int overColumn = (int) ((mousex - marginLeft - (width % SLOT_SIZE) / 2) / SLOT_SIZE);
final int slt = columns * overRow + overColumn;
int idx = page * perPage + slt;

if (overRow >= rows || overColumn >= columns) {
return null;
}

for (int i = 0; i < slt; i++) {
if (invalidSlotMap[i]) {
idx--;
}
}

return idx < size()? new ItemPanelSlot(idx, realItems.get(idx)): null;
}

public boolean contains(int px, int py)
{

if (!(new Rectangle4i(marginLeft, marginTop, width, height)).contains(px, py)) {
return false;
}

final int r = (int) ((py - marginTop) / SLOT_SIZE);
final int c = (int) ((px - marginLeft - (width % SLOT_SIZE) / 2) / SLOT_SIZE);
final int slt = columns * r + c;

return r >= rows || c >= columns || !invalidSlotMap[slt];
}

}
2 changes: 1 addition & 1 deletion src/main/java/codechicken/nei/LayoutStyleMinecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ else if (NEIClientUtils.isValidGamemode("adventure"))
bookmarkPanel.resize(gui);

more.w = more.h = less.w = less.h = 16;
less.x = itemPanel.prev.x;
less.x = itemPanel.pagePrev.x;
more.x = gui.width - less.w - 2;
more.y = less.y = gui.height - more.h - 2;

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/codechicken/nei/NEIClientUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import codechicken.lib.util.LangProxy;
import codechicken.nei.api.GuiInfo;
import codechicken.nei.api.IInfiniteItemHandler;
import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.api.ItemInfo;
import com.google.common.collect.Iterables;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -136,10 +135,7 @@ public static void cheatItem(ItemStack stack, int button, int mode) {
if (mode == 1 && stack.stackSize < stack.getMaxStackSize()) {
giveStack(stack, stack.getMaxStackSize() - stack.stackSize);
} else {
int amount = getItemQuantity();
if (amount == 0)
amount = stack.getMaxStackSize();
giveStack(stack, amount);
giveStack(stack);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/codechicken/nei/SearchField.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public static boolean searchInventories() {
}

@Override
public int getTextColour() {
if(ItemPanels.itemPanel.realItems.size() == 0) {
public int getTextColour()
{
if (ItemPanels.itemPanel.getItems().isEmpty()) {
return focused() ? 0xFFcc3300 : 0xFF993300;
} else {
return focused() ? 0xFFE0E0E0 : 0xFF909090;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/codechicken/nei/api/IStackStringifyHandler.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package codechicken.nei.api;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidStack;
import net.minecraft.item.ItemStack;

public interface IStackStringifyHandler
{

public NBTTagCompound convertItemStackToNBT(ItemStack[] stacks);
public NBTTagCompound convertItemStackToNBT(ItemStack stack, boolean saveStackSize);

public ItemStack convertNBTToItemStack(NBTTagCompound nbtTag);

public ItemStack normalize(ItemStack item);
public FluidStack getFluid(ItemStack stack);

}
Loading

0 comments on commit 5dde7ca

Please sign in to comment.