This commit is contained in:
NichtStudioCode 2023-09-30 14:34:58 +02:00
parent 130ed90aaa
commit 205b566959
2 changed files with 83 additions and 14 deletions

@ -12,6 +12,7 @@ import xyz.xenondevs.invui.animation.Animation;
import xyz.xenondevs.invui.gui.structure.Marker;
import xyz.xenondevs.invui.gui.structure.Structure;
import xyz.xenondevs.invui.inventory.Inventory;
import xyz.xenondevs.invui.inventory.ReferencingInventory;
import xyz.xenondevs.invui.inventory.event.ItemPreUpdateEvent;
import xyz.xenondevs.invui.inventory.event.PlayerUpdateReason;
import xyz.xenondevs.invui.inventory.event.UpdateReason;
@ -219,7 +220,8 @@ public abstract class AbstractGui implements Gui, GuiParent {
leftOverAmount = ((AbstractGui) otherGui).putIntoFirstInventory(updateReason, clicked, inventory);
} else {
leftOverAmount = InventoryUtils.addItemCorrectly(event.getWhoClicked().getInventory(), inventory.getItem(slot));
Inventory playerInventory = ReferencingInventory.fromReversedPlayerStorageContents(player.getInventory());
leftOverAmount = playerInventory.addItem(null, inventory.getItem(slot));
}
clicked.setAmount(leftOverAmount);
@ -330,7 +332,7 @@ public abstract class AbstractGui implements Gui, GuiParent {
LinkedHashSet<Inventory> inventories = getAllInventories(ignored);
int originalAmount = itemStack.getAmount();
if (inventories.size() > 0) {
if (!inventories.isEmpty()) {
for (Inventory inventory : inventories) {
int amountLeft = inventory.addItem(updateReason, itemStack);
if (originalAmount != amountLeft)

@ -2,6 +2,7 @@ package xyz.xenondevs.invui.inventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.invui.gui.Gui;
@ -27,12 +28,12 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor
private static final int MAX_STACK_SIZE = 64;
private final Inventory inventory;
private final Function<Inventory, ItemStack[]> itemsGetter;
private final BiFunction<Inventory, Integer, ItemStack> itemGetter;
private final TriConsumer<Inventory, Integer, ItemStack> itemSetter;
private final int size;
private final int[] maxStackSizes;
protected final @NotNull Inventory inventory;
protected final @NotNull Function<@NotNull Inventory, @Nullable ItemStack @NotNull []> itemsGetter;
protected final @NotNull BiFunction<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemGetter;
protected final @NotNull TriConsumer<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemSetter;
protected final int size;
protected final int @NotNull [] maxStackSizes;
/**
* Constructs a new {@link ReferencingInventory}.
@ -43,10 +44,10 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor
* @param itemSetter A {@link TriConsumer} which copies and then sets the {@link ItemStack} on the specified slot.
*/
public ReferencingInventory(
Inventory inventory,
Function<Inventory, ItemStack[]> itemsGetter,
BiFunction<Inventory, Integer, ItemStack> itemGetter,
TriConsumer<Inventory, Integer, ItemStack> itemSetter
@NotNull Inventory inventory,
@NotNull Function<@NotNull Inventory, @Nullable ItemStack @NotNull []> itemsGetter,
@NotNull BiFunction<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemGetter,
@NotNull TriConsumer<@NotNull Inventory, @NotNull Integer, @Nullable ItemStack> itemSetter
) {
this.inventory = inventory;
this.itemsGetter = itemsGetter;
@ -63,7 +64,7 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor
* @param inventory The {@link Inventory} to reference.
* @return The new {@link ReferencingInventory}.
*/
public static ReferencingInventory fromStorageContents(Inventory inventory) {
public static @NotNull ReferencingInventory fromStorageContents(@NotNull Inventory inventory) {
return new ReferencingInventory(inventory, Inventory::getStorageContents, Inventory::getItem, Inventory::setItem);
}
@ -73,10 +74,22 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor
* @param inventory The {@link Inventory} to reference.
* @return The new {@link ReferencingInventory}.
*/
public static ReferencingInventory fromContents(Inventory inventory) {
public static @NotNull ReferencingInventory fromContents(@NotNull Inventory inventory) {
return new ReferencingInventory(inventory, Inventory::getContents, Inventory::getItem, Inventory::setItem);
}
/**
* Creates a new {@link ReferencingInventory} with a reversed view of the {@link PlayerInventory PlayerInventory's}
* {@link Inventory#getStorageContents() storage contents}, where the last hotbar slot is the first slot and
* the top left slot is the last slot.
*
* @param inventory The {@link PlayerInventory} to reference.
* @return The new {@link ReferencingInventory}.
*/
public static @NotNull ReferencingInventory fromReversedPlayerStorageContents(@NotNull PlayerInventory inventory) {
return new ReversedPlayerContents(inventory);
}
@Override
public int getSize() {
return size;
@ -122,4 +135,58 @@ public class ReferencingInventory extends xyz.xenondevs.invui.inventory.Inventor
itemSetter.accept(inventory, slot, itemStack);
}
private static class ReversedPlayerContents extends ReferencingInventory {
public ReversedPlayerContents(PlayerInventory inventory) {
super(inventory, Inventory::getStorageContents, Inventory::getItem, Inventory::setItem);
}
private int convertSlot(int invUiSlot) {
if (invUiSlot < 9) return 8 - invUiSlot;
else return 44 - invUiSlot;
}
@Override
public @Nullable ItemStack getItem(int slot) {
return super.getItem(convertSlot(slot));
}
@Override
public @Nullable ItemStack getUnsafeItem(int slot) {
return super.getUnsafeItem(convertSlot(slot));
}
@Override
public @Nullable ItemStack @NotNull [] getUnsafeItems() {
return getItems();
}
@Override
public @Nullable ItemStack @NotNull [] getItems() {
ItemStack[] items = itemsGetter.apply(inventory);
ItemStack[] reorderedItems = new ItemStack[items.length];
for (int i = 0; i < 9; i++) {
reorderedItems[8 - i] = items[i];
}
for (int i = 9; i < 36; i++) {
reorderedItems[44 - i] = items[i];
}
return reorderedItems;
}
@Override
protected void setCloneBackingItem(int slot, @Nullable ItemStack itemStack) {
super.setCloneBackingItem(convertSlot(slot), itemStack);
}
@Override
protected void setDirectBackingItem(int slot, @Nullable ItemStack itemStack) {
super.setDirectBackingItem(convertSlot(slot), itemStack);
}
}
}