Skip to content

Commit

Permalink
Attempt to convert counts between crafting mode and regular mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Meegooo committed Oct 18, 2024
1 parent 18adaa4 commit 97defd7
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions src/main/java/codechicken/nei/BookmarkPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,26 @@ public void setCraftingMode(int groupId, boolean on) {
}

public void toggleCraftingMode(int groupId) {
this.groups.get(groupId).toggleCraftingMode();
BookmarkGroup group = this.groups.get(groupId);
if (group.crafting != null) {
copyStackSizesFromCrafting(groupId, group.crafting);
}
group.toggleCraftingMode();
onItemsChanged();
}

public void copyStackSizesFromCrafting(int groupId, BookmarkCraftingChain craftingChain) {
for (int i = 0; i < metadata.size(); i++) {
ItemStackMetadata metadata = getMetadata(i);
if (metadata.groupId == groupId) {
ItemStack calculatedStack = craftingChain.getCalculatedItems().get(i);
if (calculatedStack != null) {
realItems.set(i, calculatedStack);
}
}
}
}

@Override
public int getNumPages() {

Expand Down Expand Up @@ -880,6 +896,26 @@ protected void createGroup(GroupingItem groupingItem) {
}
}

if (groupIdA == DEFAULT_GROUP_ID) {
int start = Math.min(groupingItem.startItemIndexTop, groupingItem.endItemIndexTop);
int end = Math.min(groupingItem.startItemIndexBottom, groupingItem.endItemIndexBottom);
boolean skip = false;
for (int i = start; i <= bottomItemIndex; i++) {
if (this.metadata.get(i).requestedAmount != 0) {
skip = true;
break;
}
}
if (!skip) {
for (int i = start; i <= end; i++) {
ItemStackMetadata meta = this.metadata.get(i);
if (!meta.ingredient) {
meta.requestedAmount = StackInfo.itemStackToNBT(getItem(i)).getInteger("Count");
}
}
}
}

final HashSet<Integer> usedSetIds = new HashSet<>();
for (ItemStackMetadata meta : this.metadata) {
usedSetIds.add(meta.groupId);
Expand Down Expand Up @@ -1624,11 +1660,22 @@ public void addRecipe(BookmarkRecipe recipe, boolean saveSize, int groupId) {

Map<String, NBTTagCompound> uniqueResults = getUniqueItems(recipe.result);

boolean isGroupEmpty = true;
for (int rowGroupId : BGrid.gridGroupMask) {
if (rowGroupId == groupId) {
isGroupEmpty = false;
break;
}
}

for (String GUID : uniqueResults.keySet()) {
final NBTTagCompound nbTag = uniqueResults.get(GUID);
final ItemStack normalized = StackInfo.loadFromNBT(nbTag, saveSize ? nbTag.getInteger("Count") : 0);
final ItemStackMetadata metadata = new ItemStackMetadata(recipeId, nbTag, false, groupId);
metadata.fullRecipe = recipe;
if (isGroupEmpty && saveSize) {
metadata.requestedAmount = nbTag.getInteger("Count");
}
BGrid.addItem(normalized, metadata);
}

Expand Down Expand Up @@ -2279,7 +2326,7 @@ public void mouseDragged(int mousex, int mousey, int button, long heldTime) {
}

} else if (mouseOverSlot != null
&& this.sortableItem.items.indexOf(BGrid.realItems.get(mouseOverSlot.slotIndex)) == -1) {
&& !this.sortableItem.items.contains(BGrid.realItems.get(mouseOverSlot.slotIndex))) {
final ItemStackMetadata meta = BGrid.getMetadata(mouseOverSlot.slotIndex);

if (meta.groupId == sortMeta.groupId) {
Expand Down Expand Up @@ -2786,7 +2833,10 @@ public boolean onMouseWheel(int shift, int mousex, int mousey) {
if (group.crafting != null && !meta.ingredient) {
shiftRequestedAmount(meta, shift, shiftMultiplier * meta.factor);
} else if (group.crafting == null) {
shiftStackSize(BGrid, i, shift, shiftMultiplier * meta.factor);
int newSize = shiftStackSize(BGrid, i, shift, shiftMultiplier * meta.factor);
if (!meta.ingredient && overMeta.requestedAmount != 0) {
meta.requestedAmount = newSize;
}
}
}
} else {
Expand All @@ -2797,7 +2847,10 @@ public boolean onMouseWheel(int shift, int mousex, int mousey) {
if (group.crafting != null && !overMeta.ingredient) {
shiftRequestedAmount(overMeta, shift, shiftMultiplier);
} else if (group.crafting == null) {
shiftStackSize(BGrid, slot.slotIndex, shift, shiftMultiplier);
int newSize = shiftStackSize(BGrid, slot.slotIndex, shift, shiftMultiplier);
if (!overMeta.ingredient && overMeta.requestedAmount != 0) {
overMeta.requestedAmount = newSize;
}
}
}

Expand Down Expand Up @@ -2825,16 +2878,18 @@ public boolean onMouseWheel(int shift, int mousex, int mousey) {
return false;
}

private void shiftStackSize(BookmarkGrid BGrid, int slotIndex, int shift, int shiftMultiplier) {
private int 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 originalCount = nbTag.getInteger("Count");
final long count = calculateShiftedValue(originalCount, shift, shiftMultiplier);
ItemStack newStack = StackInfo.loadFromNBT(nbTag, Math.max(count, 0));
final int count = Math.max(calculateShiftedValue(originalCount, shift, shiftMultiplier), 0);
ItemStack newStack = StackInfo.loadFromNBT(nbTag, count);
BGrid.realItems.set(slotIndex, newStack);
return count;
}
return 0;
}

private int calculateShiftedValue(int originalCount, int shift, int shiftMultiplier) {
Expand Down

0 comments on commit 97defd7

Please sign in to comment.