chore: rebase fixes on inventory branch

This commit is contained in:
mworzala 2024-04-19 13:58:39 -04:00
parent 93e47d1b09
commit 9a7cd7170d
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
2 changed files with 19 additions and 23 deletions

View File

@ -1,7 +1,6 @@
package net.minestom.server.inventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.StackingRule;
import org.jetbrains.annotations.NotNull;
import java.util.function.BiPredicate;
@ -43,26 +42,25 @@ public interface TransactionOperator extends UnaryOperator<TransactionOperator.E
return (entry) -> {
final ItemStack left = entry.left();
final ItemStack right = entry.right();
final StackingRule rule = StackingRule.get();
// Quick exit if the right is air (nothing can be transferred anyway)
// If the left is air then we know it can be transferred, but it can also be transferred if they're stackable
// and left isn't full, even if left isn't air.
if (right.isAir() || (!left.isAir() && !(rule.canBeStacked(left, right) && rule.getAmount(left) < rule.getMaxSize(left)))) {
if (right.isAir() || (!left.isAir() && !(left.isSimilar(right) && left.amount() < left.maxStackSize()))) {
return null;
}
int leftAmount = left.isAir() ? 0 : rule.getAmount(left);
int rightAmount = rule.getAmount(right);
int leftAmount = left.isAir() ? 0 : left.amount();
int rightAmount = right.amount();
int addedAmount = Math.min(rightAmount, count);
if (!left.isAir()) {
addedAmount = Math.min(addedAmount, rule.getMaxSize(left) - leftAmount);
addedAmount = Math.min(addedAmount, left.maxStackSize() - leftAmount);
}
if (addedAmount == 0) return null;
return new Entry(rule.apply(left.isAir() ? right : left, leftAmount + addedAmount), rule.apply(right, rightAmount - addedAmount));
return new Entry((left.isAir() ? right : left).withAmount(leftAmount + addedAmount), right.withAmount(rightAmount - addedAmount));
};
}
@ -73,24 +71,23 @@ public interface TransactionOperator extends UnaryOperator<TransactionOperator.E
TransactionOperator STACK_LEFT = (entry) -> {
final ItemStack left = entry.left();
final ItemStack right = entry.right();
final StackingRule rule = StackingRule.get();
// Quick exit if the right is air (nothing can be transferred anyway)
// If the left is air then we know it can be transferred, but it can also be transferred if they're stackable
// and left isn't full, even if left isn't air.
if (right.isAir() || (!left.isAir() && !(rule.canBeStacked(left, right) && rule.getAmount(left) < rule.getMaxSize(left)))) {
if (right.isAir() || (!left.isAir() && !(left.isSimilar(right) && left.amount() < left.maxStackSize()))) {
return null;
}
int leftAmount = left.isAir() ? 0 : rule.getAmount(left);
int rightAmount = rule.getAmount(right);
int leftAmount = left.isAir() ? 0 : left.amount();
int rightAmount = right.amount();
int addedAmount = rightAmount;
if (!left.isAir()) {
addedAmount = Math.min(rightAmount, rule.getMaxSize(left) - leftAmount);
addedAmount = Math.min(rightAmount, left.maxStackSize() - leftAmount);
}
return new Entry(rule.apply(left.isAir() ? right : left, leftAmount + addedAmount), rule.apply(right, rightAmount - addedAmount));
return new Entry((left.isAir() ? right : left).withAmount(leftAmount + addedAmount), right.withAmount(rightAmount - addedAmount));
};
/**
@ -105,14 +102,13 @@ public interface TransactionOperator extends UnaryOperator<TransactionOperator.E
TransactionOperator TAKE = (entry) -> {
final ItemStack left = entry.left();
final ItemStack right = entry.right();
final StackingRule rule = StackingRule.get();
if (right.isAir() || !rule.canBeStacked(left, right)) {
if (right.isAir() || !left.isSimilar(right)) {
return null;
}
final int leftAmount = rule.getAmount(left);
final int rightAmount = rule.getAmount(right);
final int leftAmount = left.amount();
final int rightAmount = right.amount();
final int subtracted = Math.min(leftAmount, rightAmount);
return new Entry(rule.apply(left, leftAmount - subtracted), rule.apply(right, rightAmount - subtracted));
return new Entry(left.withAmount(leftAmount - subtracted), right.withAmount(rightAmount - subtracted));
};
default Entry apply(@NotNull ItemStack left, @NotNull ItemStack right) {

View File

@ -49,11 +49,11 @@ public final class ClickProcessors {
if (cursor.isAir() && clickedItem.isAir()) return List.of(); // Both are air, no changes
if (cursor.isAir()) { // Take half (rounded up) of the clicked item
int newAmount = (int) Math.ceil(RULE.getAmount(clickedItem) / 2d);
int newAmount = (int) Math.ceil(clickedItem.amount() / 2d);
final TransactionOperator.Entry cursorSlot = TransactionOperator.stackLeftN(newAmount).apply(cursor, clickedItem);
if (cursorSlot == null) return List.of();
return List.of(new Container(slot, cursorSlot.right()), new Cursor(cursorSlot.left()));
} else if (clickedItem.isAir() || RULE.canBeStacked(clickedItem, cursor)) { // Can add, transfer one over
} else if (clickedItem.isAir() || clickedItem.isSimilar(cursor)) { // Can add, transfer one over
final TransactionOperator.Entry slotCursor = TransactionOperator.stackLeftN(1).apply(clickedItem, cursor);
if (slotCursor == null) return List.of();
return List.of(new Container(slot, slotCursor.left()), new Cursor(slotCursor.right()));
@ -65,7 +65,7 @@ public final class ClickProcessors {
public static @NotNull List<Click.Change> middleClick(int slot, @NotNull Click.Getter getter) {
final ItemStack item = getter.get(slot);
if (!getter.cursor().isAir() || item.isAir()) return List.of();
return List.of(new Cursor(RULE.apply(item, RULE.getMaxSize(item))));
return List.of(new Cursor(item.withAmount(item.maxStackSize())));
}
public static @NotNull List<Click.Change> shiftClick(int slot, @NotNull List<Integer> slots, @NotNull Click.Getter getter) {
@ -90,9 +90,9 @@ public final class ClickProcessors {
if (cursor.isAir()) return List.of();
final TransactionType unstacked = TransactionType.general(TransactionOperator.filter(TransactionOperator.STACK_RIGHT,
(left, right) -> RULE.getAmount(left) < RULE.getMaxSize(left)), slots);
(left, right) -> left.amount() < left.maxStackSize()), slots);
final TransactionType stacked = TransactionType.general(TransactionOperator.filter(TransactionOperator.STACK_RIGHT,
(left, right) -> RULE.getAmount(left) == RULE.getMaxSize(left)), slots);
(left, right) -> left.amount() == left.maxStackSize()), slots);
final TransactionType.Entry result = TransactionType.join(unstacked, stacked).apply(cursor, getter::get);
List<Click.Change> changes = new ArrayList<>();