Skip to content

Commit

Permalink
Lots of fixes from the comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Meegooo committed Sep 29, 2024
1 parent b84e99b commit eac0e1d
Show file tree
Hide file tree
Showing 12 changed files with 655 additions and 272 deletions.
149 changes: 111 additions & 38 deletions src/main/java/codechicken/nei/BookmarkPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.fluids.FluidStack;

import org.apache.commons.io.IOUtils;
import org.lwjgl.opengl.GL11;
Expand Down Expand Up @@ -1106,11 +1107,12 @@ protected void beforeDrawSlot(@Nullable ItemPanelSlot focus, int idx, Rectangle4
drawRect(rect.x, rect.y, rect.w, rect.h, 0x6645DA75); // inputs
} else if (groupMeta.crafting.getOutputSlots().contains(idx)) {
drawRect(rect.x, rect.y, rect.w, rect.h, 0x9966CCFF); // exports
} else if (groupMeta.crafting.getConflictingSlots().contains(idx)) {
drawRect(rect.x, rect.y, rect.w, rect.h, 0x99d66f6f); // conflicts
}

} else if (focus != null && meta.equalsRecipe(getMetadata(focus.slotIndex))) {
drawRect(rect.x, rect.y, rect.w, rect.h, meta.ingredient ? 0x6645DA75 : 0x9966CCFF); // highlight
// recipe
} else {
super.beforeDrawSlot(focus, idx, rect);
}
Expand All @@ -1135,6 +1137,9 @@ protected void afterDrawSlot(@Nullable ItemPanelSlot focus, int idx, Rectangle4i

if (groupMeta.crafting != null) {
String text = null;
if (groupMeta.crafting.getConflictingSlots().contains(idx)) {
return;
}

if (NEIClientUtils.shiftKey()) {
int crafts = groupMeta.crafting.getCalculatedCraftCounts().getOrDefault(idx, 0);
Expand Down Expand Up @@ -1174,7 +1179,12 @@ protected void drawItem(Rectangle4i rect, int idx) {
ItemStack drawStack = realStack;

if (groupMeta.crafting != null) {
ItemStack craftingItemStack = groupMeta.crafting.getCalculatedItems().get(idx);
ItemStack craftingItemStack;
if (NEIClientUtils.shiftKey()) {
craftingItemStack = groupMeta.crafting.getCalculatedRemainders().get(idx);
} else {
craftingItemStack = groupMeta.crafting.getCalculatedItems().get(idx);
}
if (craftingItemStack != null) {
drawStack = craftingItemStack;
} else {
Expand Down Expand Up @@ -1359,8 +1369,6 @@ public CraftingChainTooltipLineHandler(int groupId, BookmarkCraftingChain crafti
this.groupId = groupId;
this.craftingChain = craftingChain;

// TODO stuff

this.inputs = new ItemsTooltipLineHandler(
translate("bookmark.crafting_chain.input"),
new ArrayList<>(craftingChain.getInputStacks()),
Expand All @@ -1382,8 +1390,8 @@ public CraftingChainTooltipLineHandler(int groupId, BookmarkCraftingChain crafti
if (!this.inputs.isEmpty() || !this.outputs.isEmpty() || !this.remainder.isEmpty()) {
this.size.height += 2 + fontRenderer.FONT_HEIGHT;
this.size.width = Math.max(
this.inputs.getSize().width,
Math.max(this.outputs.getSize().width, this.remainder.getSize().width));
Math.max(this.inputs.getSize().width, this.outputs.getSize().width),
this.remainder.getSize().width);

this.size.height += this.inputs.getSize().height + this.outputs.getSize().height
+ this.remainder.getSize().height;
Expand Down Expand Up @@ -1434,8 +1442,8 @@ public void draw(int x, int y) {

if (!this.remainder.isEmpty()) {
this.remainder.draw(x, y);
y += this.remainder.getSize().height;
}

}

}
Expand Down Expand Up @@ -2625,6 +2633,59 @@ private List<String> craftingChainTooltip(int groupId, List<String> currenttip)
public List<String> handleItemTooltip(GuiContainer gui, ItemStack itemstack, int mousex, int mousey,
List<String> currenttip) {

final BookmarkGrid BGrid = (BookmarkGrid) grid;
final ItemPanelSlot slot = getSlotMouseOver(mousex, mousey);
if (slot != null) {
final int overRowIndex = BGrid.getHoveredRowIndex(false);
final int groupId = BGrid.getRowGroupId(overRowIndex);
final BookmarkGroup group = BGrid.groups.get(groupId);
if (group != null && group.crafting != null) {
if (group.crafting.getConflictingSlots().contains(slot.slotIndex) && NEIClientUtils.shiftKey()) {
currenttip.add(
EnumChatFormatting.RED + translate("bookmark.conflictingRecipe.tip")
+ EnumChatFormatting.RESET);
return currenttip;
}
if (!NEIClientUtils.shiftKey()) {
ItemStackMetadata metadata = BGrid.getMetadata(slot.slotIndex);
if (!metadata.ingredient) {
int maxStackSize = itemstack.getMaxStackSize();
FluidStack fluid = StackInfo.getFluid(itemstack);
if (fluid != null) {
maxStackSize = 144;
}
String details;
if (metadata.requestedAmount > 0) {
details = GuiContainerManager.countDetails(
metadata.requestedAmount,
maxStackSize,
"Requested: %s = %s * %s + %s",
"Requested: %s = %s * %s");
} else {
details = GuiContainerManager.countDetails(
-metadata.requestedAmount,
maxStackSize,
"Requested: -%s = -%s * %s - %s",
"Requested: -%s = -%s * %s");
}
if (details != null) {
currenttip.add(EnumChatFormatting.GRAY + details + EnumChatFormatting.RESET);
}
}
} else {
ItemStackMetadata metadata = BGrid.getMetadata(slot.slotIndex);
if (!metadata.ingredient) {
int crafts = group.crafting.getCalculatedCraftCounts().get(slot.slotIndex);
currenttip.add(
String.format(
EnumChatFormatting.GRAY + "Crafts: %d" + EnumChatFormatting.RESET,
crafts));
}
}
}

}

if (contains(mousex, mousey) && itemstack != null && this.recipeTooltipLineHandler != null) {
currenttip.add(GuiDraw.TOOLTIP_HANDLER + GuiDraw.getTipLineId(this.recipeTooltipLineHandler));
}
Expand Down Expand Up @@ -2697,40 +2758,38 @@ public boolean onMouseWheel(int shift, int mousex, int mousey) {
if (slot != null) {
final BookmarkGrid BGrid = (BookmarkGrid) grid;
final ItemStackMetadata overMeta = BGrid.getMetadata(slot.slotIndex);
final HashMap<Integer, ItemStack> items = new HashMap<>();
int shiftMultiplier = 1;

if (NEIClientUtils.altKey()) {
shiftMultiplier = NEIClientConfig.showItemQuantityWidget() ? NEIClientConfig.getItemQuantity() : 0;
if (shiftMultiplier == 0) {
shiftMultiplier = slot.item.getMaxStackSize();
}
}
if (shiftMultiplier == 0) {
shiftMultiplier = slot.item.getMaxStackSize();
}

BookmarkGroup group = BGrid.groups.get(overMeta.groupId);
if (group.crafting != null) {
if (NEIClientUtils.shiftKey()) {
for (ItemStackMetadata meta : BGrid.metadata) {
if (Objects.equals(meta.recipeId, overMeta.recipeId) && !meta.ingredient) {
meta.requestedAmount += shift * shiftMultiplier * meta.factor;
}
if (NEIClientUtils.shiftKey()) {
List<ItemStackMetadata> metadata = BGrid.metadata;
for (int i = 0; i < metadata.size(); i++) {
ItemStackMetadata meta = metadata.get(i);
if (!Objects.equals(meta.recipeId, overMeta.recipeId) || meta.groupId != overMeta.groupId) {
continue;
}
} else {
if (!overMeta.ingredient) {
Integer stackSize = StackInfo.getFluidCellSize(BGrid.getItem(slot.slotIndex));
if (overMeta.fluidDisplay && stackSize != null) {
shiftMultiplier *= stackSize;
}
overMeta.requestedAmount += shift * shiftMultiplier;
if (group.crafting != null && !meta.ingredient) {
shiftRequestedAmount(meta, shift, shiftMultiplier * meta.factor);
} else if (group.crafting == null) {
shiftStackSize(BGrid, i, shift, shiftMultiplier * meta.factor);
}
}
} else {
items.put(slot.slotIndex, shiftStackSize(BGrid, slot.slotIndex, shift, shiftMultiplier));

for (int slotIndex : items.keySet()) {
if (items.get(slotIndex) != null) {
BGrid.realItems.set(slotIndex, items.get(slotIndex));
}
Integer stackSize = StackInfo.getFluidCellSize(BGrid.getItem(slot.slotIndex));
if (overMeta.fluidDisplay && stackSize != null) {
shiftMultiplier *= stackSize;
}
if (group.crafting != null && !overMeta.ingredient) {
shiftRequestedAmount(overMeta, shift, shiftMultiplier);
} else if (group.crafting == null) {
shiftStackSize(BGrid, slot.slotIndex, shift, shiftMultiplier);
}
}

Expand Down Expand Up @@ -2758,21 +2817,35 @@ public boolean onMouseWheel(int shift, int mousex, int mousey) {
return false;
}

private ItemStack shiftStackSize(BookmarkGrid BGrid, int slotIndex, int shift, int shiftMultiplier) {
private void shiftStackSize(BookmarkGrid BGrid, int slotIndex, int shift, int shiftMultiplier) {
final NBTTagCompound nbTag = StackInfo.itemStackToNBT(BGrid.getItem(slotIndex));
final ItemStackMetadata meta = BGrid.getMetadata(slotIndex);

if (meta.factor > 0) {
final int multiplier = nbTag.getInteger("Count") / meta.factor;
final long count = ((long) (multiplier + shift * shiftMultiplier) / shiftMultiplier) * shiftMultiplier
* meta.factor;
final int originalCount = nbTag.getInteger("Count");
final long count = calculateShiftedValue(originalCount, shift, shiftMultiplier);
ItemStack newStack = StackInfo.loadFromNBT(nbTag, Math.max(count, 0));
BGrid.realItems.set(slotIndex, newStack);
}
}

if (count <= Integer.MAX_VALUE) {
return StackInfo.loadFromNBT(nbTag, Math.max(count, 0));
}
private int calculateShiftedValue(int originalCount, int shift, int shiftMultiplier) {
long newValue = originalCount + (long) shift * shiftMultiplier;
newValue -= Math.floorMod(newValue, (shift * shiftMultiplier));
if (newValue <= Integer.MAX_VALUE) {
return (int) newValue;
} else {
return originalCount;
}
}

private void shiftRequestedAmount(ItemStackMetadata meta, int shift, int shiftMultiplier) {
long newValue = calculateShiftedValue(meta.requestedAmount, shift, shiftMultiplier);
if (!NEIClientConfig.allowNegativeRequests()) {
newValue = Math.max(0, newValue);
}
meta.requestedAmount = (int) newValue;

return null;
}

public boolean pullBookmarkItems(int groupId, boolean onlyIngredients) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ public boolean onClick(int button) {
tag.getTag("inventory.bookmarks.craftingChainDir").getIntValue(1);
API.addOption(new OptionCycled("inventory.bookmarks.craftingChainDir", 2, true));

tag.getTag("inventory.bookmarks.allowNegativeRequests").getBooleanValue(false);
API.addOption(new OptionToggleButton("inventory.bookmarks.allowNegativeRequests", true));

tag.getTag("inventory.bookmarks.showCatalysts").getIntValue(0);
API.addOption(new OptionCycled("inventory.bookmarks.showCatalysts", 3, true));

tag.getTag("inventory.bookmarks.ignorePotionOverlap").setComment("Ignore overlap with potion effect HUD")
.getBooleanValue(false);
API.addOption(new OptionToggleButton("inventory.bookmarks.ignorePotionOverlap", true));
Expand Down Expand Up @@ -720,6 +726,14 @@ public static boolean showRecipeMarker() {
return getBooleanSetting("inventory.bookmarks.showRecipeMarker");
}

public static boolean allowNegativeRequests() {
return getBooleanSetting("inventory.bookmarks.allowNegativeRequests");
}

public static int showUnusedCatalysts() {
return getIntSetting("inventory.bookmarks.showCatalysts");
}

public static boolean showItemQuantityWidget() {
return getBooleanSetting("inventory.showItemQuantityWidget");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import net.minecraft.item.ItemStack;

import codechicken.nei.BookmarkPanel;
import codechicken.nei.bookmarks.crafts.graph.CraftingGraph;
import codechicken.nei.bookmarks.crafts.graph.CraftingGraphNode;
import codechicken.nei.bookmarks.crafts.graph.ItemGraphNode;
import codechicken.nei.bookmarks.crafts.graph.RecipeGraphNode;
import codechicken.nei.recipe.BookmarkRecipeId;
import codechicken.nei.recipe.StackInfo;

public class BookmarkCraftingChain {

Expand All @@ -26,10 +23,10 @@ public void refresh(ArrayList<ItemStackWithMetadata> groupItems, boolean skipCal
if (groupItems.isEmpty()) {
return;
}
Map<String, CraftingGraphNode> nodes = new HashMap<>();
this.graph = new CraftingGraph();

Map<BookmarkRecipeId, List<ItemStackWithMetadata>> groupedByRecipe = new HashMap<>();
Map<BookmarkRecipeId, BookmarkPanel.BookmarkRecipe> recipes = new HashMap<>();
LinkedHashMap<BookmarkRecipeId, List<ItemStackWithMetadata>> groupedByRecipe = new LinkedHashMap<>();
LinkedHashMap<BookmarkRecipeId, BookmarkPanel.BookmarkRecipe> recipes = new LinkedHashMap<>();
List<ItemStackWithMetadata> itemsWithoutRecipe = new ArrayList<>();

for (ItemStackWithMetadata groupItem : groupItems) {
Expand Down Expand Up @@ -65,10 +62,7 @@ public void refresh(ArrayList<ItemStackWithMetadata> groupItems, boolean skipCal
if (recipe != null) {
RecipeGraphNode node = new RecipeGraphNode(recipe, inputs, outputs);
for (ItemStackWithMetadata output : outputs) {
String key = StackInfo.getItemStackGUID(output.getStack());
if (!nodes.containsKey(key) || !(nodes.get(key) instanceof RecipeGraphNode)) {
nodes.put(key, node);
}
graph.addNode(output, node);
}
}
}
Expand All @@ -77,19 +71,14 @@ public void refresh(ArrayList<ItemStackWithMetadata> groupItems, boolean skipCal
}

for (ItemStackWithMetadata itemWithoutRecipe : itemsWithoutRecipe) {
String key = StackInfo.getItemStackGUID(itemWithoutRecipe.getStack());
ItemGraphNode node = new ItemGraphNode(itemWithoutRecipe);
if (!nodes.containsKey(key) || !(nodes.get(key) instanceof RecipeGraphNode)) {
nodes.put(key, node);
}
graph.addNode(itemWithoutRecipe, node);
}
this.graph = new CraftingGraph(nodes);
List<ItemStackWithMetadata> requestedItems = groupItems.stream().filter(it -> !it.getMeta().ingredient)
.collect(Collectors.toList());
this.graph.preProcess();
if (skipCalculation) {
this.graph.postProcess(Collections.emptyMap());
this.graph.postProcess();
} else {
this.graph.dfs(requestedItems);
this.graph.runAll();
}
}

Expand All @@ -98,6 +87,11 @@ public Map<Integer, ItemStack> getCalculatedItems() {
return this.graph.getCalculatedItems();
}

public Map<Integer, ItemStack> getCalculatedRemainders() {
if (this.graph == null) return Collections.emptyMap();
return this.graph.getCalculatedRemainders();
}

public Map<Integer, Integer> getCalculatedCraftCounts() {
if (this.graph == null) return Collections.emptyMap();
return this.graph.getCalculatedCraftCounts();
Expand All @@ -113,18 +107,23 @@ public Set<Integer> getOutputSlots() {
return this.graph.getCraftedOutputSlots();
}

public Set<ItemStack> getInputStacks() {
public Set<Integer> getConflictingSlots() {
if (this.graph == null) return Collections.emptySet();
return this.graph.getConflictingSlots();
}

public List<ItemStack> getInputStacks() {
if (this.graph == null) return Collections.emptyList();
return this.graph.getInputStacks();
}

public Set<ItemStack> getOutputStacks() {
if (this.graph == null) return Collections.emptySet();
public List<ItemStack> getOutputStacks() {
if (this.graph == null) return Collections.emptyList();
return this.graph.getOutputStacks();
}

public Set<ItemStack> getRemainingStacks() {
if (this.graph == null) return Collections.emptySet();
public List<ItemStack> getRemainingStacks() {
if (this.graph == null) return Collections.emptyList();
return this.graph.getRemainingStacks();
}
}
Loading

0 comments on commit eac0e1d

Please sign in to comment.