mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-20 20:22:17 +00:00
Create Favorites Widget; More hotkeys; More Tooltips; (#617)
Co-authored-by: slprime <[email protected]> Co-authored-by: Maya <[email protected]>
This commit is contained in:
63 files changed
+8441
-3975
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,384 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import codechicken.nei.BookmarkPanel.BookmarkViewMode;
|
||||
import codechicken.nei.ItemSorter;
|
||||
import codechicken.nei.bookmark.RecipeChainDetails.BookmarkChainItem;
|
||||
import codechicken.nei.bookmark.RecipeChainDetails.CalculatedType;
|
||||
import codechicken.nei.recipe.Recipe.RecipeId;
|
||||
import codechicken.nei.recipe.StackInfo;
|
||||
|
||||
public class BookmarkGridGenerator {
|
||||
|
||||
protected final BookmarkGrid grid;
|
||||
public List<BookmarksGridSlot> gridMask = null;
|
||||
|
||||
public final Map<Integer, Integer> rowToGroupId = new HashMap<>();
|
||||
public final Map<Integer, RecipeId> itemToRecipe = new HashMap<>();
|
||||
|
||||
public final Map<Integer, Integer> slotToItem = new LinkedHashMap<>();
|
||||
public final Map<Integer, Integer> itemToSlot = new LinkedHashMap<>();
|
||||
|
||||
public final Map<Integer, BookmarkChainItem> caclulatedItems = new HashMap<>();
|
||||
|
||||
public int pageCount = 0;
|
||||
public int maxAbsoluteSlotIndex = 0;
|
||||
|
||||
public BookmarkGridGenerator(BookmarkGrid grid) {
|
||||
this.grid = grid;
|
||||
}
|
||||
|
||||
public int getRowGroupId(int rowIndex) {
|
||||
final int absoluteRowIndex = rowIndex + (this.grid.getPage() - 1) * this.grid.getRows();
|
||||
return rowToGroupId.getOrDefault(absoluteRowIndex, BookmarkGrid.DEFAULT_GROUP_ID);
|
||||
}
|
||||
|
||||
public void generate() {
|
||||
BookmarkItem prevBookmarkItem = null;
|
||||
Map<Integer, String> borderGrid = new HashMap<>();
|
||||
int gridColumns = this.grid.getColumns();
|
||||
int size = this.grid.size();
|
||||
int absoluteSlotIndex = -2;
|
||||
int itemIndex = 0;
|
||||
|
||||
this.pageCount = 0;
|
||||
this.maxAbsoluteSlotIndex = 0;
|
||||
this.gridMask = new ArrayList<>();
|
||||
this.itemToRecipe.clear();
|
||||
this.caclulatedItems.clear();
|
||||
this.slotToItem.clear();
|
||||
this.itemToSlot.clear();
|
||||
this.rowToGroupId.clear();
|
||||
|
||||
if (this.grid.isEmpty() || this.grid.getPerPage() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (itemIndex < size && absoluteSlotIndex != -1) {
|
||||
final BookmarkItem bookmarkItem = this.grid.getBookmarkItem(itemIndex);
|
||||
final BookmarkGroup group = this.grid.getGroup(bookmarkItem.groupId);
|
||||
|
||||
if (group.crafting != null && (!group.crafting.calculatedItems.containsKey(itemIndex)
|
||||
|| bookmarkItem.recipeId != null && !bookmarkItem.recipeId
|
||||
.equals(group.crafting.itemToRecipe.getOrDefault(itemIndex, bookmarkItem.recipeId)))) {
|
||||
itemIndex++;
|
||||
} else if (group.collapsed) {
|
||||
final List<Integer> results = getGroupResults(bookmarkItem.groupId, itemIndex, size);
|
||||
final String groupKey = "group:" + bookmarkItem.groupId;
|
||||
|
||||
for (int index = 0; index < results.size(); index++) {
|
||||
final int collapsedItemIndex = results.get(index);
|
||||
final BookmarkItem item = this.grid.getBookmarkItem(collapsedItemIndex);
|
||||
final int nextSlotIndex = nextSlotIndex(
|
||||
absoluteSlotIndex + 1,
|
||||
prevBookmarkItem,
|
||||
item,
|
||||
BookmarkViewMode.DEFAULT);
|
||||
|
||||
if (nextSlotIndex == -1 || index > 0 && (nextSlotIndex % gridColumns) == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
prevBookmarkItem = item;
|
||||
absoluteSlotIndex = nextSlotIndex;
|
||||
this.slotToItem.put(absoluteSlotIndex, collapsedItemIndex);
|
||||
this.itemToSlot.put(collapsedItemIndex, absoluteSlotIndex);
|
||||
this.caclulatedItems.put(collapsedItemIndex, getItem(group, collapsedItemIndex));
|
||||
borderGrid.put(absoluteSlotIndex, groupKey);
|
||||
}
|
||||
|
||||
while (itemIndex < size && this.grid.getBookmarkItem(itemIndex).groupId == bookmarkItem.groupId) {
|
||||
itemIndex++;
|
||||
}
|
||||
|
||||
} else if (group.crafting != null && group.viewMode == BookmarkViewMode.DEFAULT) {
|
||||
final List<Integer> results = getGroupResults(bookmarkItem.groupId, itemIndex, size);
|
||||
List<Integer> shadows;
|
||||
|
||||
for (int index = 0; index < results.size() && absoluteSlotIndex != -1; index++) {
|
||||
final int collapsedItemIndex = results.get(index);
|
||||
final BookmarkItem item = this.grid.getBookmarkItem(collapsedItemIndex);
|
||||
|
||||
if ((absoluteSlotIndex = nextSlotIndex(
|
||||
absoluteSlotIndex + 1,
|
||||
prevBookmarkItem,
|
||||
item,
|
||||
BookmarkViewMode.DEFAULT)) != -1) {
|
||||
prevBookmarkItem = item;
|
||||
this.slotToItem.put(absoluteSlotIndex, collapsedItemIndex);
|
||||
this.itemToSlot.put(collapsedItemIndex, absoluteSlotIndex);
|
||||
this.caclulatedItems.put(collapsedItemIndex, getItem(group, collapsedItemIndex));
|
||||
|
||||
if (!(shadows = getShadows(group, item.recipeId)).isEmpty()) {
|
||||
borderGrid.put(absoluteSlotIndex, "recipe:" + shadows.get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (itemIndex < size && this.grid.getBookmarkItem(itemIndex).groupId == bookmarkItem.groupId) {
|
||||
itemIndex++;
|
||||
}
|
||||
|
||||
} else if (group.crafting != null && group.crafting.itemToRecipe.values().contains(bookmarkItem.recipeId)) {
|
||||
absoluteSlotIndex = generateCollapsedRecipe(prevBookmarkItem, itemIndex, absoluteSlotIndex, borderGrid);
|
||||
prevBookmarkItem = bookmarkItem;
|
||||
|
||||
while (itemIndex < size && bookmarkItem.equalsRecipe(this.grid.getBookmarkItem(itemIndex))) {
|
||||
itemIndex++;
|
||||
}
|
||||
|
||||
} else if ((absoluteSlotIndex = nextSlotIndex(
|
||||
absoluteSlotIndex + 1,
|
||||
prevBookmarkItem,
|
||||
bookmarkItem,
|
||||
group.viewMode)) != -1) {
|
||||
prevBookmarkItem = bookmarkItem;
|
||||
this.slotToItem.put(absoluteSlotIndex, itemIndex);
|
||||
this.itemToSlot.put(itemIndex, absoluteSlotIndex);
|
||||
this.caclulatedItems.put(itemIndex, getItem(group, itemIndex));
|
||||
itemIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
this.maxAbsoluteSlotIndex = this.slotToItem.keySet().stream().mapToInt(index -> index).max().orElse(-1) + 1;
|
||||
generateGroups();
|
||||
splitGridMask();
|
||||
calculateGroupBorders(borderGrid);
|
||||
}
|
||||
|
||||
private List<Integer> getGroupResults(int groupId, int itemIndex, int size) {
|
||||
final BookmarkGroup group = this.grid.getGroup(groupId);
|
||||
final List<Integer> results = new ArrayList<>();
|
||||
|
||||
while (itemIndex < size) {
|
||||
final BookmarkItem bookmarkItem = this.grid.getBookmarkItem(itemIndex);
|
||||
|
||||
if (bookmarkItem == null || bookmarkItem.groupId != groupId) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!bookmarkItem.isIngredient
|
||||
&& (group.crafting == null || group.crafting.calculatedItems.containsKey(itemIndex))) {
|
||||
results.add(itemIndex);
|
||||
}
|
||||
|
||||
itemIndex++;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private int generateCollapsedRecipe(BookmarkItem prevBookmarkItem, int itemIndex, int absoluteSlotIndex,
|
||||
Map<Integer, String> borderGrid) {
|
||||
final BookmarkItem bookmarkItem = this.grid.getBookmarkItem(itemIndex);
|
||||
final BookmarkGroup group = this.grid.getGroup(bookmarkItem.groupId);
|
||||
final List<Integer> shadows = getShadows(group, bookmarkItem.recipeId);
|
||||
final String borderId = "recipe:" + shadows.get(0);
|
||||
|
||||
shadows.sort(generateSortingComparator(group.crafting.calculatedItems));
|
||||
|
||||
for (int shadowIndex : shadows) {
|
||||
final boolean isIngredient = shadows.get(0) != shadowIndex
|
||||
|| group.crafting.calculatedItems.get(shadowIndex).calculatedType == CalculatedType.INGREDIENT;
|
||||
final BookmarkItem shadowBookmarkItem = BookmarkItem.of(
|
||||
bookmarkItem.groupId,
|
||||
bookmarkItem.itemStack,
|
||||
1,
|
||||
bookmarkItem.recipeId,
|
||||
isIngredient,
|
||||
Collections.emptyMap());
|
||||
|
||||
if ((absoluteSlotIndex = nextSlotIndex(
|
||||
absoluteSlotIndex + 1,
|
||||
prevBookmarkItem,
|
||||
shadowBookmarkItem,
|
||||
group.viewMode)) != -1) {
|
||||
this.slotToItem.put(absoluteSlotIndex, shadowIndex);
|
||||
this.itemToSlot.put(shadowIndex, absoluteSlotIndex);
|
||||
this.caclulatedItems.put(shadowIndex, getItem(group, shadowIndex));
|
||||
this.itemToRecipe.put(shadowIndex, bookmarkItem.recipeId);
|
||||
borderGrid.put(absoluteSlotIndex, borderId);
|
||||
prevBookmarkItem = shadowBookmarkItem;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return absoluteSlotIndex;
|
||||
}
|
||||
|
||||
private List<Integer> getShadows(BookmarkGroup group, RecipeId recipeId) {
|
||||
return group.crafting.itemToRecipe.entrySet().stream().filter(entry -> recipeId.equals(entry.getValue()))
|
||||
.map(entry -> entry.getKey()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private int nextSlotIndex(int index, BookmarkItem previousMeta, BookmarkItem meta, BookmarkViewMode viewMode) {
|
||||
int gridColumns = this.grid.getColumns();
|
||||
int maxIndex = this.grid.getRows() * gridColumns;
|
||||
int j = 0;
|
||||
|
||||
index = Math.max(0, index);
|
||||
|
||||
while (j++ < maxIndex) {
|
||||
boolean isFirstColumn = (index % gridColumns) == 0;
|
||||
|
||||
if (this.grid.isInvalidSlot(index % maxIndex)) {
|
||||
index++;
|
||||
} else if (!isFirstColumn && (previousMeta == null || previousMeta.groupId != meta.groupId)) {
|
||||
// new group must start on a new line
|
||||
index++;
|
||||
} else if (viewMode == BookmarkViewMode.DEFAULT) {
|
||||
return index;
|
||||
} else if (isFirstColumn && (meta.recipeId == null || previousMeta == null
|
||||
|| previousMeta.groupId != meta.groupId
|
||||
|| !meta.recipeId.equals(previousMeta.recipeId)
|
||||
|| index + 1 < maxIndex && this.grid.isInvalidSlot((index + 1) % maxIndex))) {
|
||||
// In first column must be an item without recipe, a recipe result, or an ingredient
|
||||
// if the second column is occupied
|
||||
return index;
|
||||
} else
|
||||
if (!isFirstColumn && meta.recipeId != null && meta.recipeId.equals(previousMeta.recipeId)) {
|
||||
return index;
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private void generateGroups() {
|
||||
int gridColumns = this.grid.getColumns();
|
||||
int previousGroupId = BookmarkGrid.DEFAULT_GROUP_ID;
|
||||
|
||||
for (int absoluteSlotIndex = 0; absoluteSlotIndex < this.maxAbsoluteSlotIndex; absoluteSlotIndex++) {
|
||||
int absoluteRowIndex = absoluteSlotIndex / gridColumns;
|
||||
if (this.rowToGroupId.getOrDefault(absoluteRowIndex, BookmarkGrid.DEFAULT_GROUP_ID)
|
||||
== BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
int groupId = BookmarkGrid.DEFAULT_GROUP_ID;
|
||||
|
||||
if (this.slotToItem.containsKey(absoluteSlotIndex)) {
|
||||
groupId = this.caclulatedItems.get(this.slotToItem.get(absoluteSlotIndex)).getItem().groupId;
|
||||
}
|
||||
|
||||
this.rowToGroupId.put(absoluteRowIndex, groupId);
|
||||
|
||||
if (groupId != BookmarkGrid.DEFAULT_GROUP_ID && previousGroupId == (previousGroupId = groupId)) {
|
||||
while ((--absoluteRowIndex) >= 0
|
||||
&& this.rowToGroupId.get(absoluteRowIndex) == BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
this.rowToGroupId.put(absoluteRowIndex, groupId);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Comparator<Integer> generateSortingComparator(Map<Integer, BookmarkChainItem> calculatedItems) {
|
||||
return Comparator.comparingInt((Integer index) -> calculatedItems.get(index).calculatedType.toInt())
|
||||
.thenComparing(index -> StackInfo.getFluid(calculatedItems.get(index).getItem().itemStack) != null)
|
||||
.thenComparing(
|
||||
(i1, i2) -> ItemSorter.instance.compare(
|
||||
calculatedItems.get(i1).getItem().itemStack,
|
||||
calculatedItems.get(i2).getItem().itemStack));
|
||||
}
|
||||
|
||||
private void splitGridMask() {
|
||||
int gridColumns = this.grid.getColumns();
|
||||
int rows = this.grid.getRows();
|
||||
int perPage = rows * gridColumns;
|
||||
|
||||
this.pageCount = (int) Math.ceil(this.maxAbsoluteSlotIndex / (float) perPage);
|
||||
int page = Math.max(0, Math.min(this.grid.getPage(), this.pageCount) - 1);
|
||||
|
||||
for (int absoluteSlotIndex = page * perPage; absoluteSlotIndex
|
||||
< Math.min(this.maxAbsoluteSlotIndex, (page + 1) * perPage); absoluteSlotIndex++) {
|
||||
if (this.slotToItem.get(absoluteSlotIndex) != null) {
|
||||
this.gridMask
|
||||
.add(getBookmarkGridSlot(absoluteSlotIndex % perPage, this.slotToItem.get(absoluteSlotIndex)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private BookmarkChainItem getItem(BookmarkGroup group, int itemIndex) {
|
||||
if (group.crafting != null) {
|
||||
return group.crafting.calculatedItems.get(itemIndex);
|
||||
} else {
|
||||
BookmarkChainItem item = BookmarkChainItem.of(this.grid.getBookmarkItem(itemIndex));
|
||||
item.setRealAmount(item.getShiftAmount());
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
private BookmarksGridSlot getBookmarkGridSlot(int slotIndex, int itemIndex) {
|
||||
final BookmarkChainItem chainItem = this.caclulatedItems.get(itemIndex);
|
||||
final BookmarkGroup group = this.grid.getGroup(chainItem.getItem().groupId);
|
||||
|
||||
if (group.crafting == null) {
|
||||
return new BookmarksGridSlot(
|
||||
slotIndex,
|
||||
itemIndex,
|
||||
chainItem.getRealAmount(),
|
||||
0,
|
||||
0,
|
||||
chainItem.getItem(),
|
||||
null,
|
||||
group);
|
||||
} else {
|
||||
return new BookmarksGridSlot(
|
||||
slotIndex,
|
||||
itemIndex,
|
||||
chainItem.getRealAmount(),
|
||||
chainItem.getShiftAmount(),
|
||||
chainItem.getCalculatedAmount(),
|
||||
chainItem.getItem(),
|
||||
chainItem.calculatedType,
|
||||
group);
|
||||
}
|
||||
}
|
||||
|
||||
protected void calculateGroupBorders(Map<Integer, String> borderGrid) {
|
||||
int gridColumns = this.grid.getColumns();
|
||||
int page = Math.max(1, Math.min(this.grid.getPage(), this.pageCount)) - 1;
|
||||
int shiftIndex = page * this.grid.getRows() * gridColumns;
|
||||
|
||||
for (BookmarksGridSlot slot : this.gridMask) {
|
||||
if (borderGrid.get(shiftIndex + slot.slotIndex) == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String groupIndex = borderGrid.get(shiftIndex + slot.slotIndex);
|
||||
int column = slot.slotIndex % gridColumns;
|
||||
int row = slot.slotIndex / gridColumns;
|
||||
int prevSlotIndex = shiftIndex + (row - 1) * gridColumns + column;
|
||||
int nextSlotIndex = shiftIndex + (row + 1) * gridColumns + column;
|
||||
BookmarkGroup group = slot.getGroup();
|
||||
|
||||
if (group.crafting == null) {
|
||||
slot.borderColor = BookmarkGrid.GROUP_NONE_COLOR;
|
||||
} else if (group.collapsed) {
|
||||
slot.borderColor = BookmarkGrid.GROUP_CHAIN_COLOR;
|
||||
} else {
|
||||
slot.borderColor = BookmarkGrid.RECIPE_COLOR;
|
||||
}
|
||||
|
||||
slot.borderLeft = column == 0 || !groupIndex.equals(borderGrid.get(shiftIndex + slot.slotIndex - 1));
|
||||
slot.borderRight = column + 1 == gridColumns
|
||||
|| !groupIndex.equals(borderGrid.get(shiftIndex + slot.slotIndex + 1));
|
||||
|
||||
slot.borderTop = !groupIndex.equals(borderGrid.get(prevSlotIndex));
|
||||
slot.borderBottom = !groupIndex.equals(borderGrid.get(nextSlotIndex));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import codechicken.nei.BookmarkPanel.BookmarkViewMode;
|
||||
import codechicken.nei.recipe.Recipe.RecipeId;
|
||||
|
||||
public class BookmarkGroup {
|
||||
|
||||
public RecipeChainDetails crafting = null;
|
||||
public BookmarkViewMode viewMode;
|
||||
public boolean collapsed = false;
|
||||
public Set<RecipeId> collapsedRecipes = new HashSet<>();
|
||||
|
||||
public BookmarkGroup(BookmarkViewMode viewMode) {
|
||||
this.viewMode = viewMode;
|
||||
}
|
||||
|
||||
public BookmarkGroup(BookmarkViewMode viewMode, boolean crafting) {
|
||||
this.viewMode = viewMode;
|
||||
setCraftingMode(crafting);
|
||||
}
|
||||
|
||||
public void toggleViewMode() {
|
||||
if (this.viewMode == BookmarkViewMode.DEFAULT) {
|
||||
this.viewMode = BookmarkViewMode.TODO_LIST;
|
||||
} else {
|
||||
this.viewMode = BookmarkViewMode.DEFAULT;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCraftingMode(boolean crafting) {
|
||||
if ((this.crafting != null) != crafting) {
|
||||
this.crafting = crafting ? new RecipeChainDetails() : null;
|
||||
}
|
||||
}
|
||||
|
||||
public void toggleCraftingMode() {
|
||||
setCraftingMode(this.crafting == null);
|
||||
}
|
||||
|
||||
public void toggleCollapsedState() {
|
||||
this.collapsed = !this.collapsed;
|
||||
}
|
||||
|
||||
public void toggleCollapsedRecipe(RecipeId recipeId) {
|
||||
if (this.collapsedRecipes.contains(recipeId)) {
|
||||
this.collapsedRecipes.remove(recipeId);
|
||||
} else {
|
||||
this.collapsedRecipes.add(recipeId);
|
||||
}
|
||||
}
|
||||
|
||||
public BookmarkGroup copy() {
|
||||
return new BookmarkGroup(this.viewMode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import codechicken.nei.recipe.Recipe;
|
||||
import codechicken.nei.recipe.Recipe.RecipeId;
|
||||
import codechicken.nei.recipe.Recipe.RecipeIngredient;
|
||||
import codechicken.nei.recipe.StackInfo;
|
||||
|
||||
public class BookmarkItem {
|
||||
|
||||
public int groupId;
|
||||
|
||||
public long amount = 0L;
|
||||
public final int fluidCellAmount;
|
||||
public ItemStack itemStack;
|
||||
public Map<String, ItemStack> permutations;
|
||||
|
||||
public long factor;
|
||||
public RecipeId recipeId;
|
||||
public boolean isIngredient = false;
|
||||
|
||||
protected BookmarkItem(int groupId, long amount, int fluidCellAmount, ItemStack itemStack,
|
||||
Map<String, ItemStack> permutations, long factor, RecipeId recipeId, boolean isIngredient) {
|
||||
this.groupId = groupId;
|
||||
|
||||
this.amount = amount;
|
||||
this.fluidCellAmount = fluidCellAmount;
|
||||
this.itemStack = itemStack;
|
||||
this.permutations = permutations;
|
||||
|
||||
this.factor = factor;
|
||||
this.recipeId = recipeId;
|
||||
this.isIngredient = isIngredient;
|
||||
}
|
||||
|
||||
public static BookmarkItem of(int groupId, ItemStack stack, long factor, RecipeId recipeId, boolean isIngredient,
|
||||
Map<String, ItemStack> permutations) {
|
||||
final FluidStack fluidStack = StackInfo.getFluid(stack);
|
||||
int fluidCellAmount = 1;
|
||||
long amount;
|
||||
|
||||
if (fluidStack != null) {
|
||||
amount = fluidStack.amount * (long) Math.max(0, stack.stackSize);
|
||||
fluidCellAmount = Math.max(1, StackInfo.isFluidContainer(stack) ? fluidStack.amount : 1);
|
||||
} else {
|
||||
amount = StackInfo.getAmount(stack);
|
||||
}
|
||||
|
||||
return new BookmarkItem(
|
||||
groupId,
|
||||
amount,
|
||||
fluidCellAmount,
|
||||
stack,
|
||||
permutations,
|
||||
factor * fluidCellAmount,
|
||||
recipeId,
|
||||
isIngredient);
|
||||
}
|
||||
|
||||
public static BookmarkItem of(int groupId, ItemStack stack, long factor, RecipeId recipeId, boolean isIngredient) {
|
||||
return of(groupId, stack, factor, recipeId, isIngredient, generatePermutations(stack, recipeId, isIngredient));
|
||||
}
|
||||
|
||||
public static BookmarkItem of(int groupId, ItemStack stack) {
|
||||
final NBTTagCompound nbTag = StackInfo.itemStackToNBT(stack);
|
||||
final long factor = nbTag.hasKey("gtFluidName") ? Math.min(144, nbTag.getInteger("Count")) : 1;
|
||||
return BookmarkItem
|
||||
.of(groupId, stack, factor, null, false, Collections.singletonMap(getItemGUID(stack), stack));
|
||||
}
|
||||
|
||||
public BookmarkItem copyWithAmount(long amount) {
|
||||
return new BookmarkItem(
|
||||
this.groupId,
|
||||
amount,
|
||||
this.fluidCellAmount,
|
||||
this.itemStack,
|
||||
this.permutations,
|
||||
this.factor,
|
||||
this.recipeId,
|
||||
this.isIngredient);
|
||||
}
|
||||
|
||||
public BookmarkItem copy() {
|
||||
return copyWithAmount(this.amount);
|
||||
}
|
||||
|
||||
public static Map<String, ItemStack> generatePermutations(ItemStack stack, RecipeId recipeId,
|
||||
boolean isIngredient) {
|
||||
|
||||
if (isIngredient && recipeId != null) {
|
||||
return generatePermutations(stack, Recipe.of(recipeId));
|
||||
}
|
||||
|
||||
return Collections.singletonMap(getItemGUID(stack), stack);
|
||||
}
|
||||
|
||||
public static Map<String, ItemStack> generatePermutations(ItemStack stack, Recipe recipe) {
|
||||
|
||||
if (recipe != null) {
|
||||
final RecipeIngredient ingr = recipe.getIngredients().stream()
|
||||
.filter(ingredient -> ingredient.contains(stack)).findAny().orElse(null);
|
||||
|
||||
if (ingr != null) {
|
||||
final Map<String, ItemStack> permutations = new HashMap<>();
|
||||
for (ItemStack ingrStack : ingr.getPermutations()) {
|
||||
permutations.put(getItemGUID(ingrStack), ingrStack);
|
||||
}
|
||||
return permutations;
|
||||
}
|
||||
}
|
||||
|
||||
return Collections.singletonMap(getItemGUID(stack), stack);
|
||||
}
|
||||
|
||||
private static String getItemGUID(ItemStack stack) {
|
||||
final FluidStack fluidStack = StackInfo.getFluid(stack);
|
||||
|
||||
if (fluidStack != null) {
|
||||
return fluidStack.getFluid().getName() + ":" + fluidStack.tag;
|
||||
} else {
|
||||
return StackInfo.getItemStackGUID(stack);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsItems(BookmarkItem item) {
|
||||
return this.permutations.keySet().stream().anyMatch(item.permutations::containsKey);
|
||||
}
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
return getItemStack(this.amount);
|
||||
}
|
||||
|
||||
public ItemStack getItemStack(long amount) {
|
||||
return StackInfo.withAmount(this.itemStack, getStackSize(amount));
|
||||
}
|
||||
|
||||
public long getStackSize() {
|
||||
return getStackSize(this.amount);
|
||||
}
|
||||
|
||||
public long getStackSize(long amount) {
|
||||
return (long) (this.factor > 0 ? Math.ceil(amount / (double) this.fluidCellAmount) : 0);
|
||||
}
|
||||
|
||||
public long getMultiplier() {
|
||||
return getMultiplier(this.amount);
|
||||
}
|
||||
|
||||
public long getMultiplier(long amount) {
|
||||
return (long) (this.factor > 0 ? Math.ceil(amount / (double) this.factor) : 0);
|
||||
}
|
||||
|
||||
public long getFactor() {
|
||||
return this.factor / this.fluidCellAmount;
|
||||
}
|
||||
|
||||
public boolean equalsRecipe(BookmarkItem meta) {
|
||||
return equalsRecipe(meta.recipeId, meta.groupId);
|
||||
}
|
||||
|
||||
public boolean equalsRecipe(RecipeId recipeId, int groupId) {
|
||||
return groupId == this.groupId && recipeId != null && recipeId.equals(this.recipeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.groupId, this.fluidCellAmount, this.isIngredient, this.recipeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
|
||||
if (object instanceof BookmarkItem item) {
|
||||
return this.groupId == item.groupId && this.isIngredient == item.isIngredient
|
||||
&& this.fluidCellAmount == item.fluidCellAmount
|
||||
&& StackInfo.equalItemAndNBT(this.itemStack, item.itemStack, true)
|
||||
&& (this.recipeId == item.recipeId || this.recipeId != null && this.recipeId.equals(item.recipeId));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,366 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
import codechicken.nei.BookmarkPanel.BookmarkViewMode;
|
||||
import codechicken.nei.NEIClientConfig;
|
||||
import codechicken.nei.recipe.Recipe.RecipeId;
|
||||
import codechicken.nei.recipe.StackInfo;
|
||||
import codechicken.nei.util.NBTJson;
|
||||
|
||||
public class BookmarkStorage {
|
||||
|
||||
protected List<BookmarkGrid> namespaces = new ArrayList<>();
|
||||
protected int activeNamespaceIndex = 0;
|
||||
protected File bookmarkFile = null;
|
||||
|
||||
public BookmarkStorage() {
|
||||
this.namespaces.add(new BookmarkGrid());
|
||||
}
|
||||
|
||||
public boolean prevNamespace() {
|
||||
getNamespaceSize();
|
||||
removeEmptyNamespaces();
|
||||
|
||||
if (this.activeNamespaceIndex == 0) {
|
||||
setNamespace(getNamespaceSize() - 1);
|
||||
} else {
|
||||
setNamespace(this.activeNamespaceIndex - 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean nextNamespace() {
|
||||
|
||||
if (removeEmptyNamespaces()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.activeNamespaceIndex == getNamespaceSize() - 1) {
|
||||
setNamespace(0);
|
||||
} else {
|
||||
setNamespace(this.activeNamespaceIndex + 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setNamespace(int namespaceIndex) {
|
||||
this.activeNamespaceIndex = Math.min(namespaceIndex, this.namespaces.size() - 1);
|
||||
BookmarkGrid grid = this.namespaces.get(this.activeNamespaceIndex);
|
||||
|
||||
if (grid.size() == 0 && this.activeNamespaceIndex > 0) {
|
||||
grid.setViewMode(
|
||||
BookmarkGrid.DEFAULT_GROUP_ID,
|
||||
this.namespaces.get(this.activeNamespaceIndex - 1).getViewMode(BookmarkGrid.DEFAULT_GROUP_ID));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int getNamespaceSize() {
|
||||
|
||||
if (this.namespaces.get(this.namespaces.size() - 1).size() > 0) {
|
||||
this.namespaces.add(new BookmarkGrid());
|
||||
} else if (this.activeNamespaceIndex == this.namespaces.size() - 2 && getActiveGrid().size() == 0) {
|
||||
this.namespaces.remove(this.namespaces.size() - 1);
|
||||
}
|
||||
|
||||
return this.namespaces.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.namespaces.size() <= 1 && getActiveGrid().isEmpty();
|
||||
}
|
||||
|
||||
public int getActiveIndex() {
|
||||
return this.activeNamespaceIndex;
|
||||
}
|
||||
|
||||
public BookmarkGrid getActiveGrid() {
|
||||
return this.namespaces.get(this.activeNamespaceIndex);
|
||||
}
|
||||
|
||||
private boolean removeEmptyNamespaces() {
|
||||
|
||||
if (this.activeNamespaceIndex != this.namespaces.size() - 1 && getActiveGrid().size() == 0) {
|
||||
this.namespaces.remove(this.activeNamespaceIndex);
|
||||
setNamespace(this.activeNamespaceIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void load(File bookmarkFile) {
|
||||
|
||||
if (bookmarkFile.equals(this.bookmarkFile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> itemStrings = Collections.emptyList();
|
||||
|
||||
if (bookmarkFile.exists()) {
|
||||
try (FileInputStream reader = new FileInputStream(bookmarkFile)) {
|
||||
NEIClientConfig.logger.info("Loading bookmarks from file {}", bookmarkFile);
|
||||
itemStrings = IOUtils.readLines(reader, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
NEIClientConfig.logger.error("Failed to load bookmarks from file {}", bookmarkFile, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
final JsonParser parser = new JsonParser();
|
||||
final List<BookmarkGrid> namespaces = new ArrayList<>();
|
||||
NBTTagCompound navigation = new NBTTagCompound();
|
||||
namespaces.add(new BookmarkGrid());
|
||||
BookmarkGrid grid = namespaces.get(0);
|
||||
|
||||
if (NEIClientConfig.world.nbt.hasKey("bookmark")) {
|
||||
navigation = NEIClientConfig.world.nbt.getCompoundTag("bookmark");
|
||||
}
|
||||
|
||||
for (String itemStr : itemStrings) {
|
||||
|
||||
try {
|
||||
|
||||
if (itemStr.isEmpty()) {
|
||||
itemStr = "; {}";
|
||||
}
|
||||
|
||||
if (itemStr.startsWith("; ")) {
|
||||
JsonObject settings = parser.parse(itemStr.substring(2)).getAsJsonObject();
|
||||
|
||||
if (grid.size() > 0) {
|
||||
// do not create empty namespaces
|
||||
grid = new BookmarkGrid();
|
||||
namespaces.add(grid);
|
||||
}
|
||||
|
||||
if (navigation.hasKey("namespacePage." + (namespaces.size() - 1))) {
|
||||
grid.setPage(navigation.getInteger("namespacePage." + (namespaces.size() - 1)));;
|
||||
}
|
||||
|
||||
if (settings.get("viewmode") != null) {
|
||||
grid.groups.get(BookmarkGrid.DEFAULT_GROUP_ID).viewMode = BookmarkViewMode
|
||||
.valueOf(settings.get("viewmode").getAsString());
|
||||
} else if (settings.get("groups") instanceof JsonObject jsonObject) {
|
||||
BookmarkGroup group;
|
||||
|
||||
for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
|
||||
if (jsonEntry.getValue() instanceof JsonObject value) {
|
||||
group = new BookmarkGroup(
|
||||
value.has("viewmode")
|
||||
? BookmarkViewMode.valueOf(value.get("viewmode").getAsString())
|
||||
: BookmarkViewMode.DEFAULT,
|
||||
value.has("crafting") && value.get("crafting").getAsBoolean());
|
||||
|
||||
group.collapsed = value.has("collapsed") && value.get("collapsed").getAsBoolean();
|
||||
|
||||
if (value.has("collapsedRecipes")) {
|
||||
for (JsonElement recipeId : value.get("collapsedRecipes").getAsJsonArray()) {
|
||||
if (recipeId instanceof JsonObject item) {
|
||||
group.collapsedRecipes.add(RecipeId.of(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
grid.groups.put(Integer.valueOf(jsonEntry.getKey()), group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!addItemToGrid(grid, parser.parse(itemStr).getAsJsonObject())) {
|
||||
NEIClientConfig.logger.warn(
|
||||
"Failed to load bookmarked ItemStack from json string, the item no longer exists:\n{}",
|
||||
itemStr);
|
||||
}
|
||||
|
||||
} catch (IllegalArgumentException | JsonSyntaxException | IllegalStateException e) {
|
||||
NEIClientConfig.logger.error("Failed to load bookmarked ItemStack from json string:\n{}", itemStr);
|
||||
}
|
||||
}
|
||||
|
||||
prepareOldRecipeFormat(namespaces);
|
||||
|
||||
for (BookmarkGrid gr : namespaces) {
|
||||
gr.onItemsChanged();
|
||||
}
|
||||
|
||||
this.namespaces = namespaces;
|
||||
|
||||
if (navigation.hasKey("namespaceIndex")) {
|
||||
setNamespace(navigation.getInteger("namespaceIndex"));
|
||||
} else {
|
||||
setNamespace(0);
|
||||
}
|
||||
|
||||
this.bookmarkFile = bookmarkFile;
|
||||
}
|
||||
|
||||
private boolean addItemToGrid(BookmarkGrid grid, JsonObject jsonObject) {
|
||||
NBTTagCompound itemStackNBT;
|
||||
|
||||
if (jsonObject.get("item") != null) {
|
||||
itemStackNBT = (NBTTagCompound) NBTJson.toNbt(jsonObject.get("item"));
|
||||
} else { // old format
|
||||
itemStackNBT = (NBTTagCompound) NBTJson.toNbt(jsonObject);
|
||||
}
|
||||
|
||||
ItemStack itemStack = StackInfo.loadFromNBT(itemStackNBT);
|
||||
|
||||
if (itemStack != null) {
|
||||
RecipeId recipeId = null;
|
||||
boolean isFluid = itemStackNBT.hasKey("gtFluidName");
|
||||
int groupId = jsonObject.has("groupId") ? jsonObject.get("groupId").getAsInt()
|
||||
: BookmarkGrid.DEFAULT_GROUP_ID;
|
||||
int factor = jsonObject.has("factor") ? Math.abs(jsonObject.get("factor").getAsInt()) : (isFluid ? 144 : 1);
|
||||
boolean isIngredient = jsonObject.has("ingredient") && jsonObject.get("ingredient").getAsBoolean();
|
||||
|
||||
if (jsonObject.get("recipeId") instanceof JsonObject recipeJson) {
|
||||
recipeId = RecipeId.of(recipeJson);
|
||||
}
|
||||
|
||||
if (!grid.groups.containsKey(groupId)) {
|
||||
groupId = BookmarkGrid.DEFAULT_GROUP_ID;
|
||||
}
|
||||
|
||||
grid.addItem(BookmarkItem.of(groupId, itemStack, factor, recipeId, isIngredient), false);
|
||||
}
|
||||
|
||||
return itemStack != null;
|
||||
}
|
||||
|
||||
private void prepareOldRecipeFormat(List<BookmarkGrid> namespaces) {
|
||||
|
||||
for (BookmarkGrid grid : namespaces) {
|
||||
if (grid.bookmarkItems.stream()
|
||||
.noneMatch(item -> item.recipeId != null && item.recipeId.getResult() == null)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final Map<String, RecipeId> recipes = new HashMap<>();
|
||||
|
||||
for (BookmarkItem item : grid.bookmarkItems) {
|
||||
if (item.recipeId != null && !item.isIngredient && item.recipeId.getResult() == null) {
|
||||
final JsonObject recipeJson = item.recipeId.toJsonObject();
|
||||
final String recipeGUID = item.groupId + ":" + NBTJson.toJson(recipeJson);
|
||||
|
||||
if (!recipes.containsKey(recipeGUID)) {
|
||||
recipeJson.add("result", NBTJson.toJsonObject(StackInfo.itemStackToNBT(item.itemStack)));
|
||||
recipes.put(recipeGUID, item.recipeId = RecipeId.of(recipeJson));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (BookmarkItem item : grid.bookmarkItems) {
|
||||
if (item.recipeId != null && item.recipeId.getResult() == null) {
|
||||
final JsonObject recipeJson = item.recipeId.toJsonObject();
|
||||
final String recipeGUID = item.groupId + ":" + NBTJson.toJson(recipeJson);
|
||||
|
||||
if (recipes.containsKey(recipeGUID)) {
|
||||
item.recipeId = recipes.get(recipeGUID).copy();
|
||||
item.permutations = BookmarkItem
|
||||
.generatePermutations(item.itemStack, item.recipeId, item.isIngredient);
|
||||
} else {
|
||||
item.recipeId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
|
||||
if (this.bookmarkFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> strings = new ArrayList<>();
|
||||
NBTTagCompound navigation = new NBTTagCompound();
|
||||
navigation.setInteger("namespaceIndex", this.activeNamespaceIndex);
|
||||
|
||||
for (int grpIdx = 0; grpIdx < namespaces.size(); grpIdx++) {
|
||||
BookmarkGrid grid = namespaces.get(grpIdx);
|
||||
JsonObject settings = new JsonObject();
|
||||
JsonObject groups = new JsonObject();
|
||||
|
||||
for (int groupId : grid.groups.keySet()) {
|
||||
BookmarkGroup group = grid.groups.get(groupId);
|
||||
JsonObject groupJson = new JsonObject();
|
||||
groupJson.add("viewmode", new JsonPrimitive(group.viewMode.toString()));
|
||||
groupJson.add("crafting", new JsonPrimitive(group.crafting != null));
|
||||
groupJson.add("collapsed", new JsonPrimitive(group.collapsed));
|
||||
|
||||
if (!group.collapsedRecipes.isEmpty()) {
|
||||
JsonArray collapsedRecipes = new JsonArray();
|
||||
for (RecipeId recipeId : group.collapsedRecipes) {
|
||||
collapsedRecipes.add(recipeId.toJsonObject());
|
||||
}
|
||||
groupJson.add("collapsedRecipes", collapsedRecipes);
|
||||
}
|
||||
|
||||
groups.add(String.valueOf(groupId), groupJson);
|
||||
}
|
||||
|
||||
settings.add("groups", groups);
|
||||
strings.add("; " + NBTJson.toJson(settings));
|
||||
|
||||
navigation.setInteger("namespacePage." + grpIdx, grid.getPage() - 1);
|
||||
|
||||
for (BookmarkItem item : grid.bookmarkItems) {
|
||||
try {
|
||||
JsonObject row = new JsonObject();
|
||||
|
||||
row.add("item", NBTJson.toJsonObject(StackInfo.itemStackToNBT(item.getItemStack())));
|
||||
row.add("factor", new JsonPrimitive(item.getFactor()));
|
||||
row.add("ingredient", new JsonPrimitive(item.isIngredient));
|
||||
|
||||
if (item.groupId != BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
row.add("groupId", new JsonPrimitive(item.groupId));
|
||||
}
|
||||
|
||||
if (item.recipeId != null) {
|
||||
row.add("recipeId", item.recipeId.toJsonObject());
|
||||
}
|
||||
|
||||
strings.add(NBTJson.toJson(row));
|
||||
} catch (JsonSyntaxException e) {
|
||||
NEIClientConfig.logger.error("Failed to stringify bookmarked ItemStack to json string");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (FileOutputStream output = new FileOutputStream(bookmarkFile)) {
|
||||
IOUtils.writeLines(strings, "\n", output, StandardCharsets.UTF_8);
|
||||
NEIClientConfig.world.nbt.setTag("bookmark", navigation);
|
||||
} catch (IOException e) {
|
||||
NEIClientConfig.logger.error("Filed to save bookmarks list to file {}", bookmarkFile, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.nei.BookmarkPanel.BookmarkViewMode;
|
||||
import codechicken.nei.ItemPanels;
|
||||
import codechicken.nei.ItemsGrid.ItemsGridSlot;
|
||||
import codechicken.nei.ItemsGrid.MouseContext;
|
||||
import codechicken.nei.NEIClientConfig;
|
||||
import codechicken.nei.NEIClientUtils;
|
||||
import codechicken.nei.NEIClientUtils.Alignment;
|
||||
import codechicken.nei.bookmark.BookmarkGrid.BookmarkMouseContext;
|
||||
import codechicken.nei.bookmark.RecipeChainDetails.CalculatedType;
|
||||
import codechicken.nei.recipe.Recipe.RecipeId;
|
||||
import codechicken.nei.recipe.StackInfo;
|
||||
import codechicken.nei.util.ReadableNumberConverter;
|
||||
|
||||
public class BookmarksGridSlot extends ItemsGridSlot {
|
||||
|
||||
protected enum ShownItemType {
|
||||
REAL,
|
||||
SHIFT,
|
||||
CALCULATED
|
||||
}
|
||||
|
||||
protected static final float LINE_WIDTH = 0.75f;
|
||||
|
||||
protected static final Color INGREDIENTS_COLOR = new Color(0x6645DA75, true);
|
||||
|
||||
protected static final Color RESULTS_COLOR = new Color(0x9966CCFF, true);
|
||||
protected static final Color RESULTS_BG_COLOR = new Color(0x3366CCFF, true);
|
||||
protected static final Color RESULTS_BG_FLUID_COLOR = new Color(0x5566CCFF, true);
|
||||
|
||||
protected static final Color REMAINDER_COLOR = new Color(0x55A033A0, true);
|
||||
protected static final Color REMAINDER_BG_COLOR = new Color(0x33A033A0, true);
|
||||
protected static final Color REMAINDER_BG_FLUID_COLOR = new Color(0x44A033A0, true);
|
||||
|
||||
protected final ItemStack emptyItemStack;
|
||||
protected final long realAmount;
|
||||
protected final long shiftAmount;
|
||||
protected final long calculatedAmount;
|
||||
|
||||
protected final long realMultiplier;
|
||||
protected final long calculatedMultiplier;
|
||||
|
||||
protected final BookmarkGroup group;
|
||||
protected final boolean isFluidDisplay;
|
||||
protected final CalculatedType calculatedType;
|
||||
protected final CalculatedType realType;
|
||||
protected final BookmarkItem bookmarkItem;
|
||||
|
||||
protected final boolean isOutputRecipe;
|
||||
protected final boolean isFirstOutput;
|
||||
protected final int rowIndex;
|
||||
|
||||
public BookmarksGridSlot(int slotIndex, int itemIndex, long realAmount, long shiftAmount, long calculatedAmount,
|
||||
BookmarkItem bookmarkItem, CalculatedType calculatedType, BookmarkGroup group) {
|
||||
super(
|
||||
slotIndex,
|
||||
itemIndex,
|
||||
calculatedType != null ? bookmarkItem.getItemStack(calculatedAmount)
|
||||
: bookmarkItem.getItemStack(realAmount));
|
||||
|
||||
this.emptyItemStack = bookmarkItem.getItemStack(0);
|
||||
this.realAmount = realAmount;
|
||||
this.shiftAmount = shiftAmount;
|
||||
this.calculatedAmount = calculatedAmount;
|
||||
|
||||
this.group = group;
|
||||
this.calculatedType = calculatedType;
|
||||
this.bookmarkItem = bookmarkItem;
|
||||
|
||||
this.isOutputRecipe = group.crafting != null && group.crafting.outputRecipes.contains(bookmarkItem.recipeId);
|
||||
this.isFluidDisplay = StackInfo.itemStackToNBT(bookmarkItem.itemStack).hasKey("gtFluidName");
|
||||
|
||||
this.realMultiplier = bookmarkItem.getMultiplier(realAmount);
|
||||
this.calculatedMultiplier = bookmarkItem.getMultiplier(calculatedAmount);
|
||||
|
||||
if (this.group.crafting != null && this.group.crafting.calculatedItems.containsKey(this.itemIndex)) {
|
||||
this.realType = this.group.crafting.calculatedItems.get(this.itemIndex).calculatedType
|
||||
== CalculatedType.INGREDIENT ? CalculatedType.INGREDIENT : CalculatedType.RESULT;
|
||||
} else {
|
||||
this.realType = this.bookmarkItem.isIngredient
|
||||
|| this.group.crafting != null && this.bookmarkItem.recipeId == null ? CalculatedType.INGREDIENT
|
||||
: CalculatedType.RESULT;
|
||||
}
|
||||
|
||||
final BookmarkGrid grid = ItemPanels.bookmarkPanel.getGrid();
|
||||
final BookmarkItem prevItem = grid.getCalculatedItem(
|
||||
grid.gridGenerator.slotToItem.getOrDefault(grid.gridGenerator.itemToSlot.get(this.itemIndex) - 1, -1));
|
||||
this.isFirstOutput = this.itemIndex >= 0 && !this.bookmarkItem.isIngredient
|
||||
&& ((this.slotIndex % grid.getColumns()) == 0 || group.viewMode == BookmarkViewMode.DEFAULT)
|
||||
&& (prevItem == null || prevItem.isIngredient || !this.bookmarkItem.equalsRecipe(prevItem));
|
||||
this.rowIndex = this.slotIndex / grid.getColumns();
|
||||
}
|
||||
|
||||
public long getMultiplier() {
|
||||
return this.calculatedType != null ? this.calculatedMultiplier : this.realMultiplier;
|
||||
}
|
||||
|
||||
public BookmarkItem getBookmarkItem() {
|
||||
return this.bookmarkItem;
|
||||
}
|
||||
|
||||
public RecipeId getRecipeId() {
|
||||
return this.bookmarkItem.recipeId;
|
||||
}
|
||||
|
||||
public boolean isIngredient() {
|
||||
return this.bookmarkItem.isIngredient;
|
||||
}
|
||||
|
||||
public BookmarkGroup getGroup() {
|
||||
return this.group;
|
||||
}
|
||||
|
||||
public int getGroupId() {
|
||||
return this.bookmarkItem.groupId;
|
||||
}
|
||||
|
||||
protected ShownItemType getShownItemType(BookmarkMouseContext mouseContext) {
|
||||
|
||||
if (mouseContext != null) {
|
||||
if (this.showShiftItem(mouseContext)) {
|
||||
return ShownItemType.SHIFT;
|
||||
} else if (this.showRealItem(mouseContext)) {
|
||||
return ShownItemType.REAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.calculatedType != null) {
|
||||
return ShownItemType.CALCULATED;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected boolean showShiftItem(BookmarkMouseContext mouseContext) {
|
||||
|
||||
if (!mouseContext.shiftKey || this.bookmarkItem.groupId != mouseContext.groupId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.group.crafting != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.bookmarkItem.equalsRecipe(mouseContext.recipeId, mouseContext.groupId);
|
||||
}
|
||||
|
||||
protected boolean showRealItem(BookmarkMouseContext mouseContext) {
|
||||
|
||||
if (!mouseContext.controlKey || this.bookmarkItem.groupId != mouseContext.groupId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.slotIndex == mouseContext.slotIndex || this.group.collapsed) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (mouseContext.recipeId == null && this.group.viewMode == BookmarkViewMode.TODO_LIST
|
||||
&& mouseContext.rowIndex == this.rowIndex) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.bookmarkItem.equalsRecipe(mouseContext.recipeId, mouseContext.groupId)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.group.crafting != null && mouseContext.recipeId != null
|
||||
&& mouseContext.recipeId.equals(this.group.crafting.itemToRecipe.get(this.itemIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawItem(Rectangle4i rect) {
|
||||
super.drawItem(this.emptyItemStack, rect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <M extends MouseContext> void beforeDraw(Rectangle4i rect, M mouseContext) {
|
||||
final ShownItemType shownItemType = getShownItemType((BookmarkMouseContext) mouseContext);
|
||||
boolean showMarker = NEIClientConfig.showRecipeMarkerMode() == 2;
|
||||
CalculatedType calculatedType = null;
|
||||
|
||||
if (shownItemType == ShownItemType.REAL) {
|
||||
calculatedType = this.realType;
|
||||
} else if (shownItemType == ShownItemType.SHIFT) {
|
||||
|
||||
if (this.calculatedType != null) {
|
||||
calculatedType = this.shiftAmount > 0 ? this.calculatedType : null;
|
||||
showMarker = false;
|
||||
} else if (group.crafting == null) {
|
||||
calculatedType = this.realType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!this.bookmarkItem.isIngredient && this.bookmarkItem.recipeId != null
|
||||
&& calculatedType == null
|
||||
&& showMarker
|
||||
&& (mouseContext == null || mouseContext.slotIndex != this.slotIndex)) {
|
||||
drawMerkerBackground(rect);
|
||||
}
|
||||
|
||||
if (calculatedType == null || !drawBackground(rect, calculatedType)) {
|
||||
super.beforeDraw(rect, mouseContext);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean drawBackground(Rectangle4i rect, CalculatedType calculatedType) {
|
||||
|
||||
switch (calculatedType) {
|
||||
case INGREDIENT:
|
||||
NEIClientUtils.drawRect(rect.x, rect.y, rect.w, rect.h, INGREDIENTS_COLOR);
|
||||
break;
|
||||
case RESULT:
|
||||
NEIClientUtils.drawRect(rect.x, rect.y, rect.w, rect.h, RESULTS_COLOR);
|
||||
break;
|
||||
case REMAINDER:
|
||||
NEIClientUtils.drawRect(rect.x, rect.y, rect.w, rect.h, REMAINDER_COLOR);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void drawMerkerBackground(Rectangle4i rect) {
|
||||
Color color = this.isFluidDisplay ? RESULTS_BG_FLUID_COLOR : RESULTS_BG_COLOR;
|
||||
|
||||
if (this.shiftAmount > 0 && this.calculatedType == CalculatedType.REMAINDER) {
|
||||
color = this.isFluidDisplay ? REMAINDER_BG_FLUID_COLOR : REMAINDER_BG_COLOR;
|
||||
}
|
||||
|
||||
NEIClientUtils.drawRect(rect.x, rect.y, rect.w, rect.h, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <M extends MouseContext> void afterDraw(Rectangle4i rect, M mouseContext) {
|
||||
final ShownItemType shownItemType = getShownItemType((BookmarkMouseContext) mouseContext);
|
||||
|
||||
drawStackSize(rect, shownItemType);
|
||||
|
||||
if (this.bookmarkItem.isIngredient || this.bookmarkItem.recipeId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isFirstOutput) {
|
||||
if (shownItemType == ShownItemType.REAL) {
|
||||
drawMarker(
|
||||
rect,
|
||||
"x" + ReadableNumberConverter.INSTANCE.toWideReadableForm(this.realMultiplier),
|
||||
0xFFFFFF);
|
||||
} else if (shownItemType == ShownItemType.SHIFT) {
|
||||
drawMarker(rect, "x" + ReadableNumberConverter.INSTANCE.toWideReadableForm(getMultiplier()), 0xFFFFFF);
|
||||
} else if (this.isOutputRecipe || this.realMultiplier > 1) {
|
||||
drawMarker(
|
||||
rect,
|
||||
"x" + ReadableNumberConverter.INSTANCE.toWideReadableForm(this.realMultiplier),
|
||||
NEIClientConfig.getSetting("inventory.bookmarks.recipeMarkerColor").getHexValue());
|
||||
} else if (NEIClientConfig.showRecipeMarkerMode() == 1) {
|
||||
drawMarker(
|
||||
rect,
|
||||
"R",
|
||||
NEIClientConfig.getSetting("inventory.bookmarks.recipeMarkerColor").getHexValue());
|
||||
}
|
||||
} else if (NEIClientConfig.showRecipeMarkerMode() == 1) {
|
||||
drawMarker(rect, "R", NEIClientConfig.getSetting("inventory.bookmarks.recipeMarkerColor").getHexValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void drawMarker(Rectangle4i rect, String text, int color) {
|
||||
final float panelFactor = (rect.w - 2) / (DEFAULT_SLOT_SIZE - 2);
|
||||
NEIClientUtils.drawNEIOverlayText(
|
||||
text,
|
||||
new Rectangle4i(rect.x + 1, rect.y + 1, rect.w - 2, rect.h - 2),
|
||||
panelFactor,
|
||||
color,
|
||||
true,
|
||||
Alignment.TopLeft);
|
||||
}
|
||||
|
||||
protected void drawStackSize(Rectangle4i rect, ShownItemType shownItemType) {
|
||||
long amount = this.calculatedAmount;
|
||||
|
||||
if (shownItemType == null || shownItemType == ShownItemType.REAL || this.calculatedType == null) {
|
||||
amount = this.realAmount;
|
||||
} else if (shownItemType == ShownItemType.SHIFT) {
|
||||
amount = this.shiftAmount;
|
||||
}
|
||||
|
||||
if (amount > 0 || shownItemType == ShownItemType.REAL && this.bookmarkItem.factor > 0) {
|
||||
final float panelFactor = (rect.w - 2) / (DEFAULT_SLOT_SIZE - 2);
|
||||
final long stackSize = this.bookmarkItem.getStackSize(amount);
|
||||
String amountString = "";
|
||||
|
||||
if (stackSize < 10_000) {
|
||||
amountString = String.valueOf(stackSize);
|
||||
} else {
|
||||
amountString = ReadableNumberConverter.INSTANCE.toWideReadableForm(stackSize);
|
||||
}
|
||||
|
||||
if (this.isFluidDisplay) {
|
||||
amountString += "L";
|
||||
}
|
||||
|
||||
NEIClientUtils.drawNEIOverlayText(
|
||||
amountString,
|
||||
new Rectangle4i(rect.x + 1, rect.y + 1, rect.w - 2, rect.h - 2),
|
||||
panelFactor,
|
||||
0xFFFFFF,
|
||||
true,
|
||||
this.isFluidDisplay ? Alignment.BottomLeft : Alignment.BottomRight);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import codechicken.nei.BookmarkPanel.BookmarkViewMode;
|
||||
import codechicken.nei.recipe.Recipe.RecipeId;
|
||||
|
||||
public class GroupingItem {
|
||||
|
||||
public static class PointEntry {
|
||||
|
||||
public int cursorRowIndex;
|
||||
public int topRowIndex;
|
||||
public int bottomRowIndex;
|
||||
|
||||
public PointEntry(int clickSlotIndex, int topSlotIndex, int bottomSlotIndex) {
|
||||
this.cursorRowIndex = clickSlotIndex;
|
||||
this.topRowIndex = topSlotIndex;
|
||||
this.bottomRowIndex = bottomSlotIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public BookmarkGrid grid;
|
||||
|
||||
public int groupId = -1;
|
||||
public PointEntry startPoint;
|
||||
public PointEntry endPoint;
|
||||
|
||||
public GroupingItem(BookmarkGrid grid, int groupId, int overRowIndex) {
|
||||
this.groupId = groupId;
|
||||
this.grid = grid;
|
||||
setStartPoint(overRowIndex);
|
||||
}
|
||||
|
||||
public void setStartPoint(int overRowIndex) {
|
||||
|
||||
if (this.grid.getRowItemIndex(overRowIndex, true) != Integer.MIN_VALUE) {
|
||||
this.startPoint = new PointEntry(
|
||||
overRowIndex + getRowShift(),
|
||||
getTopRowIndex(overRowIndex),
|
||||
getBottomRowIndex(overRowIndex));
|
||||
} else {
|
||||
this.startPoint = new PointEntry(overRowIndex + getRowShift(), -1, -1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setEndPoint(int overRowIndex) {
|
||||
if (this.grid.getRowItemIndex(overRowIndex, true) != Integer.MIN_VALUE) {
|
||||
this.endPoint = new PointEntry(
|
||||
overRowIndex + getRowShift(),
|
||||
getTopRowIndex(overRowIndex),
|
||||
getBottomRowIndex(overRowIndex));
|
||||
}
|
||||
}
|
||||
|
||||
public int getStartSlotIndex() {
|
||||
return this.startPoint.cursorRowIndex - getRowShift();
|
||||
}
|
||||
|
||||
public int getEndSlotIndex() {
|
||||
return this.endPoint != null ? this.endPoint.cursorRowIndex - getRowShift() : 0;
|
||||
}
|
||||
|
||||
public boolean hasEndRow() {
|
||||
return this.endPoint != null;
|
||||
}
|
||||
|
||||
private int getRowShift() {
|
||||
return (this.grid.getPage() - 1) * this.grid.getRows();
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> prepareGroups(Map<Integer, Integer> groups) {
|
||||
final int topRowIndex = Math.min(this.startPoint.topRowIndex, this.endPoint.topRowIndex);
|
||||
final int bottomRowIndex = Math.max(this.startPoint.bottomRowIndex, this.endPoint.bottomRowIndex);
|
||||
groups = new HashMap<>(groups);
|
||||
|
||||
if (this.groupId != BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
|
||||
if (this.startPoint.cursorRowIndex > this.endPoint.cursorRowIndex) {
|
||||
int rowIndex = this.endPoint.topRowIndex - 1;
|
||||
|
||||
while (groups.containsKey(rowIndex) && groups.get(rowIndex) == this.groupId) {
|
||||
groups.put(rowIndex--, BookmarkGrid.DEFAULT_GROUP_ID);
|
||||
}
|
||||
|
||||
} else if (this.startPoint.cursorRowIndex < this.endPoint.cursorRowIndex) {
|
||||
int rowIndex = this.endPoint.bottomRowIndex + 1;
|
||||
|
||||
while (groups.containsKey(rowIndex) && groups.get(rowIndex) == this.groupId) {
|
||||
groups.put(rowIndex++, BookmarkGrid.DEFAULT_GROUP_ID);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (groups.containsKey(topRowIndex - 1)
|
||||
&& groups.get(topRowIndex - 1).equals(groups.get(bottomRowIndex + 1))) {
|
||||
int groupId = groups.get(bottomRowIndex);
|
||||
int rowIndex = bottomRowIndex + 1;
|
||||
|
||||
while (groups.containsKey(rowIndex) && groups.get(rowIndex) == groupId) {
|
||||
groups.put(rowIndex, -1 * groups.get(rowIndex));
|
||||
rowIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
for (int rowIndex = topRowIndex; rowIndex <= bottomRowIndex && groups.containsKey(rowIndex); rowIndex++) {
|
||||
groups.put(rowIndex, this.groupId);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
protected int getTopRowIndex(int overRowIndex) {
|
||||
final int rowShift = getRowShift();
|
||||
int itemIndex = this.grid.getRowItemIndex(overRowIndex, true);
|
||||
|
||||
if (itemIndex == Integer.MIN_VALUE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
final BookmarkItem item = this.grid.getCalculatedItem(itemIndex);
|
||||
|
||||
if (item.recipeId != null) {
|
||||
final List<Integer> sortedItems = this.grid.getSortedItems();
|
||||
final BookmarkGroup group = this.grid.getGroup(item.groupId);
|
||||
final boolean isCollapsedGroup = group.collapsed
|
||||
|| group.crafting != null && group.viewMode == BookmarkViewMode.DEFAULT;
|
||||
final RecipeId recipeId = group.crafting != null
|
||||
? group.crafting.itemToRecipe.getOrDefault(itemIndex, item.recipeId)
|
||||
: item.recipeId;
|
||||
final Set<RecipeId> recipeRelations = group.crafting != null
|
||||
? group.crafting.recipeRelations.getOrDefault(recipeId, Collections.singleton(recipeId))
|
||||
: Collections.singleton(recipeId);
|
||||
|
||||
for (Integer rItemIndex : sortedItems) {
|
||||
final BookmarkItem rItem = this.grid.getCalculatedItem(rItemIndex);
|
||||
|
||||
if (rItem.groupId == item.groupId && (isCollapsedGroup || recipeRelations.contains(rItem.recipeId))) {
|
||||
return this.grid.getAbsoluteSlotIndex(rItemIndex) / this.grid.getColumns();
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return rowShift + overRowIndex;
|
||||
}
|
||||
|
||||
protected int getBottomRowIndex(int overRowIndex) {
|
||||
final int rowShift = getRowShift();
|
||||
final int itemIndex = this.grid.getRowItemIndex(overRowIndex, false);
|
||||
|
||||
if (itemIndex == Integer.MIN_VALUE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
final BookmarkItem item = this.grid.getCalculatedItem(itemIndex);
|
||||
|
||||
if (item.recipeId != null) {
|
||||
final List<Integer> sortedItems = this.grid.getSortedItems();
|
||||
final BookmarkGroup group = this.grid.getGroup(item.groupId);
|
||||
final boolean isCollapsedGroup = group.collapsed
|
||||
|| group.crafting != null && group.viewMode == BookmarkViewMode.DEFAULT;
|
||||
final RecipeId recipeId = group.crafting != null
|
||||
? group.crafting.itemToRecipe.getOrDefault(itemIndex, item.recipeId)
|
||||
: item.recipeId;
|
||||
final Set<RecipeId> recipeRelations = group.crafting != null
|
||||
? group.crafting.recipeRelations.getOrDefault(recipeId, Collections.singleton(recipeId))
|
||||
: Collections.singleton(recipeId);
|
||||
|
||||
for (int index = sortedItems.size() - 1; index >= 0; index--) {
|
||||
final int rItemIndex = sortedItems.get(index);
|
||||
final BookmarkItem rItem = this.grid.getCalculatedItem(rItemIndex);
|
||||
|
||||
if (rItem.groupId == item.groupId && (isCollapsedGroup || recipeRelations.contains(rItem.recipeId))) {
|
||||
return this.grid.getAbsoluteSlotIndex(rItemIndex) / this.grid.getColumns();
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return rowShift + overRowIndex;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
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.recipe.Recipe.RecipeId;
|
||||
import codechicken.nei.recipe.StackInfo;
|
||||
import codechicken.nei.recipe.chain.RecipeChainMath;
|
||||
|
||||
public class RecipeChainDetails {
|
||||
|
||||
public enum CalculatedType {
|
||||
|
||||
INGREDIENT,
|
||||
RESULT,
|
||||
REMAINDER;
|
||||
|
||||
public int toInt() {
|
||||
switch (this) {
|
||||
case RESULT:
|
||||
return 0;
|
||||
case REMAINDER:
|
||||
return 1;
|
||||
case INGREDIENT:
|
||||
return 2;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BookmarkChainItem {
|
||||
|
||||
private final BookmarkItem item;
|
||||
|
||||
private long realAmount = 0L;
|
||||
private long shiftAmount = 0L;
|
||||
private long calculatedAmount = 0L;
|
||||
|
||||
public CalculatedType calculatedType;
|
||||
|
||||
protected BookmarkChainItem(BookmarkItem item, long shiftItem, long calculatedItem,
|
||||
CalculatedType calculatedType) {
|
||||
this.item = item.copy();
|
||||
this.shiftAmount = shiftItem;
|
||||
this.calculatedAmount = calculatedItem;
|
||||
this.calculatedType = calculatedType;
|
||||
}
|
||||
|
||||
public static BookmarkChainItem of(BookmarkItem item, long shiftItem, long calculatedAmount,
|
||||
CalculatedType calculatedType) {
|
||||
return new BookmarkChainItem(item, shiftItem, calculatedAmount, calculatedType);
|
||||
}
|
||||
|
||||
public static BookmarkChainItem of(BookmarkItem item, long shiftItem, CalculatedType calculatedType) {
|
||||
return new BookmarkChainItem(item, shiftItem, item.amount, calculatedType);
|
||||
}
|
||||
|
||||
public static BookmarkChainItem of(BookmarkItem item) {
|
||||
return new BookmarkChainItem(
|
||||
item,
|
||||
item.amount,
|
||||
item.amount,
|
||||
item.isIngredient ? CalculatedType.INGREDIENT : CalculatedType.RESULT);
|
||||
}
|
||||
|
||||
public ItemStack getItemStack(long amount) {
|
||||
return this.item.getItemStack(amount);
|
||||
}
|
||||
|
||||
public void setRealAmount(long amount) {
|
||||
this.realAmount = amount;
|
||||
}
|
||||
|
||||
public long getRealAmount() {
|
||||
return this.realAmount;
|
||||
}
|
||||
|
||||
public long getShiftAmount() {
|
||||
return this.shiftAmount;
|
||||
}
|
||||
|
||||
public long getCalculatedAmount() {
|
||||
return this.calculatedAmount;
|
||||
}
|
||||
|
||||
public BookmarkItem getItem() {
|
||||
return this.item;
|
||||
}
|
||||
|
||||
public void append(long amount, long calculatedAmount) {
|
||||
this.shiftAmount += amount;
|
||||
this.calculatedAmount += calculatedAmount;
|
||||
this.item.amount = this.calculatedAmount;
|
||||
}
|
||||
}
|
||||
|
||||
public final Map<Integer, BookmarkChainItem> calculatedItems = new HashMap<>();
|
||||
public final Map<Integer, RecipeId> itemToRecipe = new HashMap<>();
|
||||
public final Set<RecipeId> outputRecipes = new HashSet<>();
|
||||
public final Set<RecipeId> recipeInMiddle = new HashSet<>();
|
||||
|
||||
public final Map<RecipeId, Set<RecipeId>> recipeRelations = new HashMap<>();
|
||||
|
||||
public void refresh(Map<Integer, BookmarkItem> chainItems, Set<RecipeId> collapsedRecipes) {
|
||||
final Map<BookmarkItem, Integer> sortingChainItems = new HashMap<>();
|
||||
|
||||
for (Map.Entry<Integer, BookmarkItem> entry : chainItems.entrySet()) {
|
||||
sortingChainItems.put(entry.getValue(), entry.getKey());
|
||||
}
|
||||
|
||||
this.itemToRecipe.clear();
|
||||
this.outputRecipes.clear();
|
||||
this.recipeInMiddle.clear();
|
||||
this.calculatedItems.clear();
|
||||
this.recipeRelations.clear();
|
||||
final RecipeChainMath math = RecipeChainMath.of(new ArrayList<>(chainItems.values()), collapsedRecipes)
|
||||
.refresh();
|
||||
|
||||
this.outputRecipes.addAll(math.outputRecipes.keySet());
|
||||
|
||||
for (BookmarkItem item : math.preferredItems.values()) {
|
||||
if (item != null && item.recipeId != null) {
|
||||
this.recipeInMiddle.add(item.recipeId);
|
||||
}
|
||||
}
|
||||
|
||||
for (BookmarkItem item : math.initialItems) {
|
||||
this.calculatedItems.put(
|
||||
sortingChainItems.get(item),
|
||||
BookmarkChainItem.of(item, math.requiredAmount.getOrDefault(item, 0L), CalculatedType.INGREDIENT));
|
||||
}
|
||||
|
||||
if (collapsedRecipes.isEmpty()) {
|
||||
|
||||
for (BookmarkItem item : math.recipeResults) {
|
||||
generateResult(math, item, sortingChainItems.get(item));
|
||||
}
|
||||
|
||||
for (BookmarkItem item : math.recipeIngredients) {
|
||||
generateIngredient(math, item, sortingChainItems.get(item));
|
||||
}
|
||||
|
||||
} else {
|
||||
final Set<RecipeId> topLevelRecipes = findTopLevelRecipes(math, collapsedRecipes);
|
||||
|
||||
for (RecipeId recipeId : collapsedRecipes) {
|
||||
this.recipeRelations.put(
|
||||
recipeId,
|
||||
getRecipeRelations(math, recipeId, topLevelRecipes, new HashSet<>(Arrays.asList(recipeId))));
|
||||
generateCollapsedRecipe(math, recipeId, topLevelRecipes, sortingChainItems);
|
||||
generateShadowItems(recipeId, chainItems.values(), sortingChainItems);
|
||||
}
|
||||
|
||||
for (BookmarkItem item : math.recipeResults) {
|
||||
if (!collapsedRecipes.contains(item.recipeId) && topLevelRecipes.contains(item.recipeId)) {
|
||||
generateResult(math, item, sortingChainItems.get(item));
|
||||
}
|
||||
}
|
||||
|
||||
for (BookmarkItem item : math.recipeIngredients) {
|
||||
if (!collapsedRecipes.contains(item.recipeId) && topLevelRecipes.contains(item.recipeId)) {
|
||||
generateIngredient(math, item, sortingChainItems.get(item));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, BookmarkItem> entry : chainItems.entrySet()) {
|
||||
final int itemIndex = entry.getKey();
|
||||
if (itemIndex >= 0 && this.calculatedItems.containsKey(itemIndex)) {
|
||||
final BookmarkItem item = entry.getValue();
|
||||
if (this.recipeInMiddle.contains(item.recipeId)) {
|
||||
this.calculatedItems.get(itemIndex).setRealAmount(item.amount);
|
||||
} else {
|
||||
this.calculatedItems.get(itemIndex).setRealAmount(Math.max(item.factor, item.amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Set<RecipeId> getRecipeRelations(RecipeChainMath math, RecipeId recipeId, Set<RecipeId> topLevelRecipes,
|
||||
Set<RecipeId> recipes) {
|
||||
|
||||
for (BookmarkItem item : math.recipeIngredients) {
|
||||
if (item.recipeId.equals(recipeId)) {
|
||||
final BookmarkItem prefItem = math.preferredItems.get(item);
|
||||
if (prefItem != null && !topLevelRecipes.contains(prefItem.recipeId)
|
||||
&& recipes.add(prefItem.recipeId)) {
|
||||
getRecipeRelations(math, prefItem.recipeId, topLevelRecipes, recipes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return recipes;
|
||||
}
|
||||
|
||||
private void generateCollapsedRecipe(RecipeChainMath math, RecipeId recipeId, Set<RecipeId> topLevelRecipes,
|
||||
Map<BookmarkItem, Integer> sortingChainItems) {
|
||||
|
||||
final List<BookmarkChainItem> items = collectItems(
|
||||
math,
|
||||
recipeId,
|
||||
this.recipeRelations.get(recipeId),
|
||||
topLevelRecipes);
|
||||
|
||||
for (BookmarkChainItem value : items) {
|
||||
int itemIndex = sortingChainItems.get(value.getItem());
|
||||
|
||||
if (value.getItem().isIngredient || !recipeId.equals(value.getItem().recipeId)) {
|
||||
itemIndex *= -1;
|
||||
}
|
||||
|
||||
if (!value.getItem().isIngredient && !math.outputRecipes.containsKey(value.getItem().recipeId)) {
|
||||
value.calculatedType = CalculatedType.REMAINDER;
|
||||
}
|
||||
|
||||
this.itemToRecipe.put(itemIndex, recipeId);
|
||||
this.calculatedItems.put(itemIndex, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateShadowItems(RecipeId recipeId, Iterable<BookmarkItem> chainItems,
|
||||
Map<BookmarkItem, Integer> sortingChainItems) {
|
||||
final Set<RecipeId> recipeRelations = this.recipeRelations.get(recipeId);
|
||||
final List<BookmarkItem> subChainItems = new ArrayList<>();
|
||||
|
||||
for (BookmarkItem item : chainItems) {
|
||||
if (recipeRelations.contains(item.recipeId)) {
|
||||
subChainItems.add(item.copyWithAmount(recipeId.equals(item.recipeId) ? item.amount : 0));
|
||||
}
|
||||
}
|
||||
|
||||
final RecipeChainMath math = RecipeChainMath.of(subChainItems, Collections.emptySet()).refresh();
|
||||
final List<BookmarkChainItem> items = collectItems(
|
||||
math,
|
||||
recipeId,
|
||||
this.recipeRelations.get(recipeId),
|
||||
Collections.emptySet());
|
||||
|
||||
for (BookmarkChainItem value : items) {
|
||||
int itemIndex = sortingChainItems.get(value.getItem());
|
||||
|
||||
if (value.getItem().isIngredient || !recipeId.equals(value.getItem().recipeId)) {
|
||||
itemIndex *= -1;
|
||||
}
|
||||
|
||||
if (this.calculatedItems.containsKey(itemIndex)) {
|
||||
this.calculatedItems.get(itemIndex).setRealAmount(value.getShiftAmount());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<BookmarkChainItem> collectItems(RecipeChainMath math, RecipeId recipeId, Set<RecipeId> recipeRelations,
|
||||
Set<RecipeId> topLevelRecipes) {
|
||||
final Map<String, BookmarkChainItem> items = new HashMap<>();
|
||||
final Map<RecipeId, Long> multiplier = new HashMap<>();
|
||||
|
||||
for (RecipeId relRecipeId : recipeRelations) {
|
||||
for (BookmarkItem item : math.recipeResults) {
|
||||
if (item.recipeId.equals(relRecipeId)) {
|
||||
final long amount = item.amount - math.requiredAmount.getOrDefault(item, 0L);
|
||||
|
||||
if (item.recipeId.equals(recipeId)) {
|
||||
multiplier.put(item.recipeId, item.getMultiplier());
|
||||
items.computeIfAbsent(
|
||||
StackInfo.getItemStackGUID(item.itemStack),
|
||||
is -> BookmarkChainItem.of(item, 0, 0, CalculatedType.RESULT))
|
||||
.append(amount, item.amount);
|
||||
} else if (amount > 0) {
|
||||
items.computeIfAbsent(
|
||||
StackInfo.getItemStackGUID(item.itemStack),
|
||||
is -> BookmarkChainItem.of(item, 0, 0, CalculatedType.RESULT)).append(amount, amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (BookmarkItem item : math.recipeIngredients) {
|
||||
if (item.recipeId.equals(relRecipeId)) {
|
||||
final BookmarkItem prefItem = math.preferredItems.get(item);
|
||||
final long amount = math.requiredAmount.containsKey(prefItem) ? 0
|
||||
: math.requiredAmount.getOrDefault(item, item.amount);
|
||||
final long refAmount = prefItem != null && !topLevelRecipes.contains(prefItem.recipeId)
|
||||
? math.requiredAmount.getOrDefault(prefItem, 0L)
|
||||
: 0;
|
||||
|
||||
if (amount != 0 || item.amount > refAmount && math.requiredAmount.containsKey(prefItem)
|
||||
|| recipeId.equals(item.recipeId) && multiplier.get(recipeId) == 0) {
|
||||
items.computeIfAbsent(
|
||||
StackInfo.getItemStackGUID(item.itemStack),
|
||||
is -> BookmarkChainItem.of(item, 0, 0, CalculatedType.INGREDIENT))
|
||||
.append(amount, item.amount - refAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ArrayList<>(items.values());
|
||||
}
|
||||
|
||||
private void generateResult(RecipeChainMath math, BookmarkItem item, int itemIndex) {
|
||||
final long amount = item.amount - math.requiredAmount.getOrDefault(item, 0L);
|
||||
final CalculatedType calculatedType = math.outputRecipes.containsKey(item.recipeId) ? CalculatedType.RESULT
|
||||
: CalculatedType.REMAINDER;
|
||||
|
||||
this.calculatedItems.put(itemIndex, BookmarkChainItem.of(item, amount, calculatedType));
|
||||
}
|
||||
|
||||
private void generateIngredient(RecipeChainMath math, BookmarkItem item, int itemIndex) {
|
||||
final BookmarkItem prefItem = math.preferredItems.get(item);
|
||||
final long amount = math.requiredAmount.containsKey(prefItem) ? 0
|
||||
: math.requiredAmount.getOrDefault(item, item.amount);
|
||||
|
||||
this.calculatedItems.put(itemIndex, BookmarkChainItem.of(item, amount, CalculatedType.INGREDIENT));
|
||||
}
|
||||
|
||||
private Set<RecipeId> findTopLevelRecipes(RecipeChainMath math, Set<RecipeId> collapsedRecipes) {
|
||||
final Set<RecipeId> result = new HashSet<>(math.outputRecipes.keySet());
|
||||
|
||||
for (BookmarkItem item : math.recipeResults) {
|
||||
if (!result.contains(item.recipeId)) {
|
||||
final Set<RecipeId> parents = getRecipeParents(
|
||||
math,
|
||||
item.recipeId,
|
||||
new HashSet<>(),
|
||||
new HashSet<>(Arrays.asList(item.recipeId)));
|
||||
|
||||
if (parents.size() != 1 || parents.stream().noneMatch(collapsedRecipes::contains)) {
|
||||
result.add(item.recipeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Set<RecipeId> getRecipeParents(RecipeChainMath math, RecipeId recipeId, Set<RecipeId> parents,
|
||||
Set<RecipeId> visited) {
|
||||
final Set<RecipeId> recipeIds = math.preferredItems.entrySet().stream()
|
||||
.filter(
|
||||
entry -> entry.getValue() != null && recipeId.equals(entry.getValue().recipeId)
|
||||
&& !visited.contains(entry.getKey().recipeId))
|
||||
.map(entry -> entry.getKey().recipeId).collect(Collectors.toSet());
|
||||
|
||||
visited.addAll(recipeIds);
|
||||
|
||||
if (recipeIds.isEmpty()) {
|
||||
parents.add(recipeId);
|
||||
} else {
|
||||
|
||||
for (RecipeId ingrRecipeId : recipeIds) {
|
||||
if (math.outputRecipes.containsKey(ingrRecipeId)) {
|
||||
parents.add(ingrRecipeId);
|
||||
} else {
|
||||
getRecipeParents(math, ingrRecipeId, parents, visited);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return parents;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.nei.guihook.GuiContainerManager;
|
||||
|
||||
public class SortableGroup {
|
||||
|
||||
public int groupId;
|
||||
public BookmarkGrid grid;
|
||||
|
||||
public int shiftX = 0;
|
||||
public int shiftY = 0;
|
||||
|
||||
public SortableGroup(BookmarkGrid grid, int groupId) {
|
||||
this.grid = grid;
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public void postDraw(int mousex, int mousey) {
|
||||
Point startRect = null;
|
||||
|
||||
GuiContainerManager.drawItems.zLevel += 100;
|
||||
|
||||
for (BookmarksGridSlot slot : this.grid.getMask()) {
|
||||
if (slot.getGroupId() == this.groupId) {
|
||||
final Rectangle4i rect = this.grid.getSlotRect(slot.slotIndex);
|
||||
|
||||
if (startRect == null) {
|
||||
startRect = new Point(rect.x - this.shiftX, rect.y - this.shiftY);
|
||||
}
|
||||
|
||||
GuiContainerManager.drawItem(
|
||||
mousex + rect.x - startRect.x + 1,
|
||||
mousey + rect.y - startRect.y + 1,
|
||||
slot.getItemStack(),
|
||||
true,
|
||||
"");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (startRect != null) {
|
||||
int startRowIndex = -1;
|
||||
int endRowIndex = 0;
|
||||
|
||||
for (int rowIndex = 0; rowIndex < this.grid.getRows(); rowIndex++) {
|
||||
if (this.grid.getRowGroupId(rowIndex) == this.groupId) {
|
||||
|
||||
if (startRowIndex == -1) {
|
||||
startRowIndex = rowIndex;
|
||||
}
|
||||
|
||||
endRowIndex = rowIndex;
|
||||
}
|
||||
}
|
||||
|
||||
GL11.glPushMatrix();
|
||||
GL11.glTranslated(mousex - startRect.x, mousey - startRect.y, 0);
|
||||
this.grid.drawGroup(this.groupId, startRowIndex, endRowIndex);
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
|
||||
GuiContainerManager.drawItems.zLevel -= 100;
|
||||
}
|
||||
|
||||
public List<BookmarkItem> getBookmarkItems() {
|
||||
final List<BookmarkItem> items = new ArrayList<>();
|
||||
|
||||
for (BookmarkItem item : this.grid.bookmarkItems) {
|
||||
if (item.groupId == this.groupId) {
|
||||
items.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
package codechicken.nei.bookmark;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.nei.BookmarkPanel.BookmarkViewMode;
|
||||
import codechicken.nei.ItemsGrid;
|
||||
import codechicken.nei.bookmark.RecipeChainDetails.BookmarkChainItem;
|
||||
import codechicken.nei.guihook.GuiContainerManager;
|
||||
import codechicken.nei.recipe.Recipe.RecipeId;
|
||||
|
||||
public class SortableItem {
|
||||
|
||||
public final BookmarkGrid grid;
|
||||
public final BookmarkItem bookmarkItem;
|
||||
public final boolean moveRecipe;
|
||||
public final boolean crossGroup;
|
||||
public final boolean isCollapsedRecipe;
|
||||
public List<Integer> items = new ArrayList<>();
|
||||
|
||||
public int shiftX = -8;
|
||||
public int shiftY = -8;
|
||||
|
||||
public SortableItem(BookmarkGrid grid, BookmarkItem bookmarkItem) {
|
||||
final BookmarkGroup group = grid.getGroup(bookmarkItem.groupId);
|
||||
|
||||
this.grid = grid;
|
||||
this.bookmarkItem = bookmarkItem.copy();
|
||||
this.moveRecipe = bookmarkItem.recipeId != null && !bookmarkItem.isIngredient
|
||||
&& (group.viewMode == BookmarkViewMode.TODO_LIST || group.crafting != null);
|
||||
this.crossGroup = !group.collapsed && group.viewMode == BookmarkViewMode.TODO_LIST
|
||||
&& !this.bookmarkItem.isIngredient;
|
||||
this.isCollapsedRecipe = group.crafting != null && group.crafting.itemToRecipe.values().stream()
|
||||
.anyMatch(recipeId -> this.bookmarkItem.equalsRecipe(recipeId, this.bookmarkItem.groupId));
|
||||
|
||||
this.update();
|
||||
}
|
||||
|
||||
public void postDraw(int mousex, int mousey) {
|
||||
Point startRect = null;
|
||||
|
||||
GuiContainerManager.drawItems.zLevel += 100;
|
||||
|
||||
for (Integer itemIndex : this.grid.getSortedItems()) {
|
||||
if (this.items.contains(itemIndex)) {
|
||||
final BookmarkItem item = this.grid.getCalculatedItem(itemIndex);
|
||||
final int slotIndex = this.grid.gridGenerator.itemToSlot.get(itemIndex);
|
||||
final Rectangle4i rect = this.grid
|
||||
.getSlotRect(slotIndex % (this.grid.getRows() * this.grid.getColumns()));
|
||||
|
||||
if (startRect == null) {
|
||||
startRect = new Point(rect.x - this.shiftX, rect.y - this.shiftY);
|
||||
}
|
||||
|
||||
GuiContainerManager.drawItem(
|
||||
mousex + rect.x - startRect.x + 1,
|
||||
mousey + rect.y - startRect.y + 1,
|
||||
item.getItemStack(),
|
||||
true,
|
||||
"");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
GuiContainerManager.drawItems.zLevel -= 100;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
final BookmarkGroup group = this.grid.getGroup(this.bookmarkItem.groupId);
|
||||
|
||||
this.items.clear();
|
||||
|
||||
if (group.collapsed) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, BookmarkChainItem> entry : this.grid.gridGenerator.caclulatedItems.entrySet()) {
|
||||
if (containsItem(entry.getValue().getItem())) {
|
||||
this.items.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isCollapsedRecipe && group.crafting != null) {
|
||||
for (Map.Entry<Integer, RecipeId> entry : group.crafting.itemToRecipe.entrySet()) {
|
||||
if (this.bookmarkItem.equalsRecipe(entry.getValue(), this.bookmarkItem.groupId)) {
|
||||
this.items.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.items.isEmpty();
|
||||
}
|
||||
|
||||
public int getItemIndex() {
|
||||
return this.items.isEmpty() ? -1 : this.items.get(0);
|
||||
}
|
||||
|
||||
private boolean canInsert(BookmarkGroup group) {
|
||||
return group.viewMode == BookmarkViewMode.TODO_LIST && !group.collapsed;
|
||||
}
|
||||
|
||||
public void mouseDragged(int mousex, int mousey) {
|
||||
final BookmarkViewMode sortViewMode = this.grid.getViewMode(this.bookmarkItem.groupId);
|
||||
BookmarksGridSlot mouseOverSlot = this.grid.getSlotMouseOver(mousex, mousey);
|
||||
|
||||
if (this.crossGroup) {
|
||||
final Rectangle4i topLeftSlot = this.grid.getSlotRect(0);
|
||||
mouseOverSlot = this.grid.getSlotMouseOver(topLeftSlot.x, mousey);
|
||||
|
||||
if (mouseOverSlot != null) {
|
||||
float ySlot = (float) (mousey - topLeftSlot.y) / ItemsGrid.SLOT_SIZE;
|
||||
int overRowIndex = (int) ySlot;
|
||||
int beforeGroupId = this.grid.getRowGroupId(overRowIndex - 1);
|
||||
int afterGroupId = this.grid.getRowGroupId(overRowIndex + 1);
|
||||
int overGroupId = mouseOverSlot.getGroupId();
|
||||
ySlot -= overRowIndex;
|
||||
|
||||
if (overGroupId == this.bookmarkItem.groupId && !this.items.contains(mouseOverSlot.itemIndex)) {
|
||||
this.grid.moveItem(this, mouseOverSlot.itemIndex);
|
||||
} else if (ySlot <= 0.25) {
|
||||
final BookmarkGroup group = this.grid.getGroup(beforeGroupId);
|
||||
|
||||
if (beforeGroupId != overGroupId && canInsert(group)
|
||||
&& !existsRecipe(this.bookmarkItem.recipeId, beforeGroupId)) {
|
||||
this.grid.moveItem(this, overRowIndex, beforeGroupId, false);
|
||||
}
|
||||
|
||||
} else if (ySlot > 0.25 && ySlot <= 0.5) {
|
||||
|
||||
if (beforeGroupId == overGroupId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (overGroupId != BookmarkGrid.DEFAULT_GROUP_ID
|
||||
&& beforeGroupId != BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
beforeGroupId = BookmarkGrid.DEFAULT_GROUP_ID;
|
||||
}
|
||||
|
||||
if (overGroupId == BookmarkGrid.DEFAULT_GROUP_ID && beforeGroupId != BookmarkGrid.DEFAULT_GROUP_ID
|
||||
&& afterGroupId != BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (canInsert(this.grid.getGroup(beforeGroupId))
|
||||
&& !existsRecipe(this.bookmarkItem.recipeId, beforeGroupId)) {
|
||||
this.grid.moveItem(this, overRowIndex, beforeGroupId, false);
|
||||
}
|
||||
|
||||
} else if (ySlot > 0.5 && ySlot < 0.75) {
|
||||
|
||||
if (afterGroupId == overGroupId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (overGroupId != BookmarkGrid.DEFAULT_GROUP_ID && afterGroupId != BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
afterGroupId = BookmarkGrid.DEFAULT_GROUP_ID;
|
||||
}
|
||||
|
||||
if (overGroupId == BookmarkGrid.DEFAULT_GROUP_ID && beforeGroupId != BookmarkGrid.DEFAULT_GROUP_ID
|
||||
&& afterGroupId != BookmarkGrid.DEFAULT_GROUP_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (canInsert(this.grid.getGroup(afterGroupId))
|
||||
&& !existsRecipe(this.bookmarkItem.recipeId, afterGroupId)) {
|
||||
this.grid.moveItem(this, overRowIndex, afterGroupId, true);
|
||||
}
|
||||
|
||||
} else if (ySlot >= 0.75) {
|
||||
final BookmarkGroup group = this.grid.getGroup(afterGroupId);
|
||||
|
||||
if (afterGroupId != overGroupId && canInsert(group)
|
||||
&& !existsRecipe(this.bookmarkItem.recipeId, afterGroupId)) {
|
||||
this.grid.moveItem(this, overRowIndex, afterGroupId, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
final int overRowIndex = this.grid.getHoveredRowIndex(false);
|
||||
final int overGroupId = this.grid.getRowGroupId(overRowIndex);
|
||||
|
||||
if (overRowIndex != -1 && overGroupId == this.bookmarkItem.groupId) {
|
||||
final int absoluteSlotIndex = ((this.grid.getPage() - 1) * this.grid.getRows() + overRowIndex)
|
||||
* this.grid.getColumns();
|
||||
|
||||
if (absoluteSlotIndex < this.grid.gridGenerator.itemToSlot.getOrDefault(getItemIndex(), -1)) {
|
||||
this.grid.moveItem(this, overRowIndex, overGroupId, true);
|
||||
} else {
|
||||
this.grid.moveItem(this, overRowIndex, overGroupId, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else if (mouseOverSlot != null && !this.items.contains(mouseOverSlot.itemIndex)
|
||||
&& mouseOverSlot.getGroupId() == this.bookmarkItem.groupId
|
||||
&& (sortViewMode == BookmarkViewMode.DEFAULT || sortViewMode == BookmarkViewMode.TODO_LIST
|
||||
&& mouseOverSlot.getBookmarkItem().equalsRecipe(this.bookmarkItem))) {
|
||||
this.grid.moveItem(this, mouseOverSlot.itemIndex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean existsRecipe(RecipeId recipeId, int groupId) {
|
||||
if (recipeId == null) return false;
|
||||
|
||||
for (int itemIndex = 0; itemIndex < this.grid.bookmarkItems.size(); itemIndex++) {
|
||||
if (!this.items.contains(itemIndex)
|
||||
&& this.grid.bookmarkItems.get(itemIndex).equalsRecipe(recipeId, groupId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<BookmarkItem> getBookmarkItems() {
|
||||
final BookmarkGroup group = this.grid.getGroup(this.bookmarkItem.groupId);
|
||||
final List<BookmarkItem> items = new ArrayList<>();
|
||||
final List<RecipeId> recipeRelations = new ArrayList<>();
|
||||
|
||||
if (this.isCollapsedRecipe && group.crafting != null) {
|
||||
recipeRelations.addAll(
|
||||
group.crafting.recipeRelations.getOrDefault(this.bookmarkItem.recipeId, Collections.emptySet()));
|
||||
}
|
||||
|
||||
for (BookmarkItem item : this.grid.bookmarkItems) {
|
||||
if (containsItem(item)
|
||||
|| item.groupId == this.bookmarkItem.groupId && recipeRelations.contains(item.recipeId)) {
|
||||
items.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
public boolean containsItem(BookmarkItem slotItem) {
|
||||
return this.moveRecipe ? this.bookmarkItem.equalsRecipe(slotItem) : this.bookmarkItem.equals(slotItem);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user