Skip to content

Commit

Permalink
Migrate NEI-Utilities (#461)
Browse files Browse the repository at this point in the history
Co-authored-by: slprime <[email protected]>
  • Loading branch information
slprime and slprime authored Feb 21, 2024
1 parent db1fd25 commit f7ba34c
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 14 deletions.
206 changes: 206 additions & 0 deletions src/main/java/codechicken/nei/ItemHistoryPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package codechicken.nei;

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

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;

import org.lwjgl.opengl.GL11;

import codechicken.nei.ItemPanel.ItemPanelSlot;
import codechicken.nei.recipe.GuiCraftingRecipe;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.GuiUsageRecipe;
import codechicken.nei.recipe.StackInfo;

public class ItemHistoryPanel extends Widget {

protected int mouseDownSlot = -1;
protected ItemsGrid grid;

public ItemHistoryPanel() {
grid = new ItemsGrid();
}

public void draw(int mousex, int mousey) {

if (NEIClientConfig.getIntSetting("inventory.history.splittingMode") == 0) {
drawRect(x, y, w, h, NEIClientConfig.getSetting("inventory.history.historyColor").getHexValue());
} else {
drawSplittingArea(x, y, w, h, NEIClientConfig.getSetting("inventory.history.historyColor").getHexValue());
}

grid.draw(mousex, mousey);
}

public void update() {
grid.update();
}

public void addItem(ItemStack stack) {
if (stack != null) {
ItemStack is = stack.copy();
is.stackSize = 1;

grid.realItems.removeIf(historyStack -> StackInfo.equalItemAndNBT(historyStack, stack, true));
grid.realItems.add(0, is);

if (grid.realItems.size() > (grid.rows * grid.columns)) {
grid.realItems.remove(grid.rows * grid.columns);
}

grid.onItemsChanged();
}
}

public void resize(GuiContainer gui) {
grid.setGridSize(x, y, w, h);
grid.refresh(gui);
}

@Override
public ItemStack getStackMouseOver(int mousex, int mousey) {
ItemPanelSlot slot = getSlotMouseOver(mousex, mousey);
return slot == null ? null : slot.item;
}

public ItemPanelSlot getSlotMouseOver(int mousex, int mousey) {
return grid.getSlotMouseOver(mousex, mousey);
}

private void drawSplittingArea(int x, int y, int width, int height, int color) {

float alpha = (float) (color >> 24 & 255) / 255.0F;
float red = (float) (color >> 16 & 255) / 255.0F;
float green = (float) (color >> 8 & 255) / 255.0F;
float blue = (float) (color & 255) / 255.0F;

GL11.glPushMatrix();

GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_LINE_STIPPLE);
GL11.glColor4f(red, green, blue, alpha);
GL11.glLineWidth(2F);
GL11.glLineStipple(2, (short) 0x00FF);

GL11.glBegin(GL11.GL_LINE_LOOP);

GL11.glVertex2i(x, y);
GL11.glVertex2i(x + width, y);
GL11.glVertex2i(x + width, y + height);
GL11.glVertex2i(x, y + height);

GL11.glEnd();

GL11.glLineStipple(1, (short) 0xFFFF);
GL11.glLineWidth(1F);
GL11.glDisable(GL11.GL_LINE_STIPPLE);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glColor4f(1F, 1F, 1F, 1F);

GL11.glPopMatrix();

}

@Override
public void mouseDragged(int mousex, int mousey, int button, long heldTime) {

if (mouseDownSlot >= 0 && ItemPanels.itemPanel.draggedStack == null
&& NEIClientUtils.getHeldItem() == null
&& NEIClientConfig.hasSMPCounterPart()) {

ItemPanelSlot mouseOverSlot = getSlotMouseOver(mousex, mousey);

if (mouseOverSlot == null || mouseOverSlot.slotIndex != mouseDownSlot || heldTime > 500) {
ItemPanels.itemPanel.draggedStack = getDraggedStackWithQuantity(mouseDownSlot);
mouseDownSlot = -1;
}
}

}

@Override
public boolean handleClick(int mousex, int mousey, int button) {

if (handleClickExt(mousex, mousey, button)) return true;

if (NEIClientUtils.getHeldItem() != null) {

if (!grid.contains(mousex, mousey)) {
return false;
}

if (NEIClientConfig.canPerformAction("delete") && NEIClientConfig.canPerformAction("item")) {
if (button == 1) {
NEIClientUtils.decreaseSlotStack(-999);
} else {
NEIClientUtils.deleteHeldItem();
}
} else {
NEIClientUtils.dropHeldItem();
}

return true;
}

ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
if (hoverSlot != null) {

if (button == 2) {

if (hoverSlot.item != null) {
ItemPanels.itemPanel.draggedStack = getDraggedStackWithQuantity(hoverSlot.slotIndex);
}

} else {
mouseDownSlot = hoverSlot.slotIndex;
}

return true;
}

return false;
}

@Override
public void mouseUp(int mousex, int mousey, int button) {
ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);

if (hoverSlot != null && hoverSlot.slotIndex == mouseDownSlot && ItemPanels.itemPanel.draggedStack == null) {
ItemStack item = hoverSlot.item.copy();

if (NEIController.manager.window instanceof GuiRecipe || !NEIClientConfig.canCheatItem(item)) {

if (button == 0) {
GuiCraftingRecipe.openRecipeGui("item", item);
} else if (button == 1) {
GuiUsageRecipe.openRecipeGui("item", item);
}

mouseDownSlot = -1;
return;
}

NEIClientUtils.cheatItem(getDraggedStackWithQuantity(hoverSlot.slotIndex), button, -1);
}

mouseDownSlot = -1;
}

protected ItemStack getDraggedStackWithQuantity(int mouseDownSlot) {
ItemStack stack = grid.getItem(mouseDownSlot);

if (stack != null) {
int amount = NEIClientConfig.showItemQuantityWidget() ? NEIClientConfig.getItemQuantity() : 0;

if (amount == 0) {
amount = stack.getMaxStackSize();
}

return NEIServerUtils.copyStack(stack, amount);
}

return null;
}

}
32 changes: 27 additions & 5 deletions src/main/java/codechicken/nei/ItemPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ItemStack getStackMouseOver(int mousex, int mousey) {
public Button more;
public Button less;
public ItemQuantityField quantity;
public ItemHistoryPanel historyPanel;

public static class ItemPanelSlot {

Expand Down Expand Up @@ -134,6 +135,7 @@ public boolean onButtonPress(boolean rightclick) {
};

quantity = new ItemQuantityField("quantity");
historyPanel = new ItemHistoryPanel();
}

@Deprecated
Expand Down Expand Up @@ -191,20 +193,40 @@ protected int resizeFooter(GuiContainer gui) {
less.y = more.y + more.h;
}

return BUTTON_SIZE + 2;
if (NEIClientConfig.showHistoryPanelWidget()) {
historyPanel.x = x;
historyPanel.w = w;
historyPanel.h = ItemsGrid.SLOT_SIZE * NEIClientConfig.getIntSetting("inventory.history.useRows");
historyPanel.y = quantity.y - PanelWidget.PADDING - historyPanel.h;
return quantity.h + historyPanel.h + PanelWidget.PADDING * 2;
}

return quantity.h + PanelWidget.PADDING;
}

@Override
public void setVisible() {
super.setVisible();

if (grid.getPerPage() > 0 && NEIClientConfig.showItemQuantityWidget()) {
LayoutManager.addWidget(more);
LayoutManager.addWidget(less);
LayoutManager.addWidget(quantity);
if (grid.getPerPage() > 0) {
if (NEIClientConfig.showItemQuantityWidget()) {
LayoutManager.addWidget(more);
LayoutManager.addWidget(less);
LayoutManager.addWidget(quantity);
}

if (NEIClientConfig.showHistoryPanelWidget()) {
LayoutManager.addWidget(historyPanel);
}
}
}

@Override
public void resize(GuiContainer gui) {
super.resize(gui);
historyPanel.resize(gui);
}

protected ItemStack getDraggedStackWithQuantity(int mouseDownSlot) {
ItemStack stack = grid.getItem(mouseDownSlot);

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import codechicken.nei.config.GuiPanelSettings;
import codechicken.nei.config.OptionCycled;
import codechicken.nei.config.OptionGamemodes;
import codechicken.nei.config.OptionIntegerField;
import codechicken.nei.config.OptionList;
import codechicken.nei.config.OptionOpenGui;
import codechicken.nei.config.OptionTextField;
Expand Down Expand Up @@ -162,6 +163,19 @@ public boolean optionValid(int index) {

ItemSorter.initConfig(tag);

tag.getTag("inventory.history.enabled").setComment("Enable/disable History Panel").getBooleanValue(true);
API.addOption(new OptionToggleButton("inventory.history.enabled", true));

tag.getTag("inventory.history.historyColor").setComment("Color of the history area display")
.getHexValue(0xee555555);
API.addOption(new OptionIntegerField("inventory.history.historyColor"));

tag.getTag("inventory.history.useRows").setComment("Rows used in historical areas").getIntValue(2);
API.addOption(new OptionIntegerField("inventory.history.useRows", 1, 5));

tag.getTag("inventory.history.splittingMode").getIntValue(1);
API.addOption(new OptionCycled("inventory.history.splittingMode", 2, true));

tag.getTag("inventory.itemIDs").getIntValue(1);
API.addOption(new OptionCycled("inventory.itemIDs", 3, true));

Expand Down Expand Up @@ -386,6 +400,8 @@ private static void setDefaultKeyBindings() {
API.addKeyBind("world.rain", 0);
API.addKeyBind("world.heal", 0);
API.addKeyBind("world.creative", 0);
API.addHashBind("gui.copy_name", Keyboard.KEY_C + NEIClientUtils.CTRL_HASH);
API.addHashBind("gui.copy_oredict", Keyboard.KEY_D + NEIClientUtils.CTRL_HASH);
}

public static OptionList getOptionList() {
Expand Down Expand Up @@ -725,6 +741,10 @@ public static boolean shouldInvertMouseScrollTransfer() {
return !getBooleanSetting("inventory.invertMouseScrollTransfer");
}

public static boolean showHistoryPanelWidget() {
return getBooleanSetting("inventory.history.enabled");
}

public static boolean shouldCacheItemRendering() {
return getBooleanSetting("inventory.cacheItemRendering") && OpenGlHelper.framebufferSupported;
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/codechicken/nei/api/ShortcutInputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import static codechicken.lib.gui.GuiDraw.getMousePosition;

import java.awt.Point;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.util.List;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

import org.lwjgl.input.Mouse;

Expand Down Expand Up @@ -60,6 +63,14 @@ public static boolean handleKeyEvent(ItemStack stackover) {
return openOverlayRecipe(stackover, false);
}

if (NEIClientConfig.isKeyHashDown("gui.copy_name")) {
return copyItemStackName(stackover);
}

if (NEIClientConfig.isKeyHashDown("gui.copy_oredict")) {
return copyItemStackOreDictionary(stackover);
}

if (NEIClientConfig.isKeyHashDown("gui.overlay_use")) {
return openOverlayRecipe(stackover, true);
}
Expand Down Expand Up @@ -120,6 +131,30 @@ private static boolean hideOverlayRecipe() {
return false;
}

private static boolean copyItemStackName(ItemStack stackover) {
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(stackover.getDisplayName()), null);
return true;
}

private static boolean copyItemStackOreDictionary(ItemStack stackover) {
StringBuilder builder = new StringBuilder();

for (int id : OreDictionary.getOreIDs(stackover)) {
String oreDictionaryName = OreDictionary.getOreName(id);
if (!"Unknown".equals(oreDictionaryName)) {
builder.append(oreDictionaryName).append(",");
}
}

if (builder.length() > 0) {
builder.deleteCharAt(builder.length() - 1);
}

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(builder.toString()), null);
return true;
}

private static boolean openOverlayRecipe(ItemStack stackover, boolean shift) {
final GuiContainer gui = NEIClientUtils.getGuiContainer();

Expand Down
Loading

0 comments on commit f7ba34c

Please sign in to comment.