Add PagedInventoriesGuiImpl

This commit is contained in:
NichtStudioCode 2023-07-19 09:24:04 +02:00
parent 673e9e5fe2
commit 215f088e3f
4 changed files with 134 additions and 2 deletions

@ -4,6 +4,7 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.gui.structure.Structure;
import xyz.xenondevs.invui.inventory.Inventory;
import xyz.xenondevs.invui.item.Item; import xyz.xenondevs.invui.item.Item;
import java.util.List; import java.util.List;
@ -107,6 +108,51 @@ public interface PagedGui<C> extends Gui {
return new PagedNestedGuiImpl(guis, structure); return new PagedNestedGuiImpl(guis, structure);
} }
/**
* Creates a new {@link Builder Gui Builder} for a {@link PagedGui} that uses {@link Inventory Inventories} as content.
*
* @return The new {@link Builder Gui Builder}.
*/
static @NotNull Builder<@NotNull Inventory> inventories() {
return new PagedInventoriesGuiImpl.Builder();
}
/**
* Creates a new {@link PagedGui} after configuring a {@link Builder Gui Builder} using the given {@link Consumer}.
*
* @param consumer The {@link Consumer} to configure the {@link Builder Gui Builder}.
* @return The created {@link PagedGui}.
*/
static @NotNull PagedGui<@NotNull Inventory> inventories(@NotNull Consumer<@NotNull Builder<@NotNull Inventory>> consumer) {
Builder<Inventory> builder = inventories();
consumer.accept(builder);
return builder.build();
}
/**
* Creates a new {@link PagedGui}.
*
* @param width The width of the {@link PagedGui}.
* @param height The height of the {@link PagedGui}.
* @param inventories The {@link Inventory Inventories} to use as pages.
* @param contentListSlots The slots where content should be displayed.
* @return The created {@link PagedGui}.
*/
static @NotNull PagedGui<@NotNull Inventory> ofInventories(int width, int height, @NotNull List<@NotNull Inventory> inventories, int... contentListSlots) {
return new PagedInventoriesGuiImpl(width, height, inventories, contentListSlots);
}
/**
* Creates a new {@link PagedGui}.
*
* @param structure The {@link Structure} to use.
* @param inventories The {@link Inventory Inventories} to use as pages.
* @return The created {@link PagedGui}.
*/
static @NotNull PagedGui<@NotNull Inventory> ofInventories(@NotNull Structure structure, @NotNull List<@NotNull Inventory> inventories) {
return new PagedInventoriesGuiImpl(inventories, structure);
}
/** /**
* Gets the amount of pages this {@link PagedGui} has. * Gets the amount of pages this {@link PagedGui} has.
* *

@ -0,0 +1,84 @@
package xyz.xenondevs.invui.gui;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.invui.gui.structure.Structure;
import xyz.xenondevs.invui.inventory.Inventory;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* An {@link AbstractPagedGui} where every page is its own {@link Inventory}.
*
* @see PagedItemsGuiImpl
* @see PagedNestedGuiImpl
*/
final class PagedInventoriesGuiImpl extends AbstractPagedGui<Inventory> {
private List<Inventory> inventories;
/**
* Creates a new {@link PagedInventoriesGuiImpl}.
*
* @param width The width of this Gui.
* @param height The height of this Gui.
* @param inventories The {@link Inventory Inventories} to use as pages.
* @param contentListSlots The slots where content should be displayed.
*/
public PagedInventoriesGuiImpl(int width, int height, @Nullable List<@NotNull Inventory> inventories, int... contentListSlots) {
super(width, height, false, contentListSlots);
setContent(inventories);
}
/**
* Creates a new {@link PagedInventoriesGuiImpl}.
*
* @param inventories The {@link Inventory Inventories} to use as pages.
* @param structure The {@link Structure} to use.
*/
public PagedInventoriesGuiImpl(@Nullable List<@NotNull Inventory> inventories, @NotNull Structure structure) {
super(structure.getWidth(), structure.getHeight(), false, structure);
setContent(inventories);
}
@Override
public int getPageAmount() {
return inventories.size();
}
@Override
public void setContent(@Nullable List<@NotNull Inventory> inventories) {
this.inventories = inventories == null ? new ArrayList<>() : inventories;
update();
}
@Override
protected List<SlotElement> getPageElements(int page) {
if (inventories.size() <= page) return new ArrayList<>();
Inventory inventory = inventories.get(page);
int size = inventory.getSize();
return IntStream.range(0, size)
.mapToObj(i -> new SlotElement.InventorySlotElement(inventory, i))
.collect(Collectors.toList());
}
public static final class Builder extends AbstractBuilder<Inventory> {
@Override
public @NotNull PagedGui<Inventory> build() {
if (structure == null)
throw new IllegalStateException("Structure is not defined.");
var gui = new PagedInventoriesGuiImpl(content, structure);
applyModifiers(gui);
return gui;
}
}
}

@ -11,9 +11,10 @@ import java.util.function.BiConsumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* A {@link AbstractPagedGui} that is filled with {@link Item Items}. * An {@link AbstractPagedGui} that is filled with {@link Item Items}.
* *
* @see PagedNestedGuiImpl * @see PagedNestedGuiImpl
* @see PagedInventoriesGuiImpl
*/ */
final class PagedItemsGuiImpl extends AbstractPagedGui<Item> { final class PagedItemsGuiImpl extends AbstractPagedGui<Item> {

@ -10,9 +10,10 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
/** /**
* A {@link AbstractPagedGui} where every page is its own {@link Gui}. * An {@link AbstractPagedGui} where every page is its own {@link Gui}.
* *
* @see PagedItemsGuiImpl * @see PagedItemsGuiImpl
* @see PagedInventoriesGuiImpl
*/ */
final class PagedNestedGuiImpl extends AbstractPagedGui<Gui> { final class PagedNestedGuiImpl extends AbstractPagedGui<Gui> {