mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-05 11:39:57 -04:00
Check stack size in FabricItemGroupEntries (#2705)
* Check stack size in FabricItemGroupEntries * Add stack to message
This commit is contained in:
parent
d9ac3c39e0
commit
b7096da8ef
1 changed files with 32 additions and 0 deletions
|
@ -87,6 +87,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
@Override
|
||||
public void add(ItemStack stack, ItemGroup.StackVisibility visibility) {
|
||||
if (isEnabled(stack)) {
|
||||
checkStack(stack);
|
||||
|
||||
switch (visibility) {
|
||||
case PARENT_AND_SEARCH_TABS -> {
|
||||
this.displayStacks.add(stack);
|
||||
|
@ -114,6 +116,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
*/
|
||||
public void prepend(ItemStack stack, ItemGroup.StackVisibility visibility) {
|
||||
if (isEnabled(stack)) {
|
||||
checkStack(stack);
|
||||
|
||||
switch (visibility) {
|
||||
case PARENT_AND_SEARCH_TABS -> {
|
||||
this.displayStacks.add(0, stack);
|
||||
|
@ -395,6 +399,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
* Adds the {@link ItemStack} before the first match, if no matches the {@link ItemStack} is appended to the end of the {@link ItemGroup}.
|
||||
*/
|
||||
private static void addBefore(Predicate<ItemStack> predicate, Collection<ItemStack> newStacks, List<ItemStack> addTo) {
|
||||
checkStacks(newStacks);
|
||||
|
||||
for (int i = 0; i < addTo.size(); i++) {
|
||||
if (predicate.test(addTo.get(i))) {
|
||||
addTo.subList(i, i).addAll(newStacks);
|
||||
|
@ -407,6 +413,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
}
|
||||
|
||||
private static void addAfter(Predicate<ItemStack> predicate, Collection<ItemStack> newStacks, List<ItemStack> addTo) {
|
||||
checkStacks(newStacks);
|
||||
|
||||
// Iterate in reverse to add after the last match
|
||||
for (int i = addTo.size() - 1; i >= 0; i--) {
|
||||
if (predicate.test(addTo.get(i))) {
|
||||
|
@ -420,6 +428,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
}
|
||||
|
||||
private static void addBefore(ItemStack anchor, Collection<ItemStack> newStacks, List<ItemStack> addTo) {
|
||||
checkStacks(newStacks);
|
||||
|
||||
for (int i = 0; i < addTo.size(); i++) {
|
||||
if (ItemStack.canCombine(anchor, addTo.get(i))) {
|
||||
addTo.subList(i, i).addAll(newStacks);
|
||||
|
@ -432,6 +442,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
}
|
||||
|
||||
private static void addAfter(ItemStack anchor, Collection<ItemStack> newStacks, List<ItemStack> addTo) {
|
||||
checkStacks(newStacks);
|
||||
|
||||
// Iterate in reverse to add after the last match
|
||||
for (int i = addTo.size() - 1; i >= 0; i--) {
|
||||
if (ItemStack.canCombine(anchor, addTo.get(i))) {
|
||||
|
@ -445,6 +457,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
}
|
||||
|
||||
private static void addBefore(ItemConvertible anchor, Collection<ItemStack> newStacks, List<ItemStack> addTo) {
|
||||
checkStacks(newStacks);
|
||||
|
||||
Item anchorItem = anchor.asItem();
|
||||
|
||||
for (int i = 0; i < addTo.size(); i++) {
|
||||
|
@ -459,6 +473,8 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
}
|
||||
|
||||
private static void addAfter(ItemConvertible anchor, Collection<ItemStack> newStacks, List<ItemStack> addTo) {
|
||||
checkStacks(newStacks);
|
||||
|
||||
Item anchorItem = anchor.asItem();
|
||||
|
||||
// Iterate in reverse to add after the last match
|
||||
|
@ -472,4 +488,20 @@ public class FabricItemGroupEntries implements ItemGroup.Entries {
|
|||
// Anchor not found, add to end
|
||||
addTo.addAll(newStacks);
|
||||
}
|
||||
|
||||
private static void checkStacks(Collection<ItemStack> stacks) {
|
||||
for (ItemStack stack : stacks) {
|
||||
checkStack(stack);
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkStack(ItemStack stack) {
|
||||
if (stack.isEmpty()) {
|
||||
throw new IllegalArgumentException("Cannot add empty stack");
|
||||
}
|
||||
|
||||
if (stack.getCount() != 1) {
|
||||
throw new IllegalArgumentException("Stack size must be exactly 1 for stack: " + stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue