diff --git a/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIDisplay.java b/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIDisplay.java index e9725911ba..1545f99f3b 100644 --- a/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIDisplay.java +++ b/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIDisplay.java @@ -2,6 +2,7 @@ import me.hsgamer.hscore.bukkit.gui.object.BukkitItem; import me.hsgamer.hscore.minecraft.gui.InventoryGUIDisplay; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import me.hsgamer.hscore.minecraft.gui.object.Item; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -18,6 +19,7 @@ */ public class BukkitGUIDisplay extends InventoryGUIDisplay implements InventoryHolder { private Inventory inventory; + private InventorySize inventorySize; /** * Create a new display @@ -32,6 +34,7 @@ public BukkitGUIDisplay(UUID uuid, BukkitGUIHolder holder) { @Override protected void initInventory() { this.inventory = holder.getInventoryFunction().apply(this); + this.inventorySize = BukkitGUIUtils.getInventorySize(inventory); } @Override @@ -42,8 +45,8 @@ protected void clearInventory() { } @Override - protected int getInventorySize() { - return inventory != null ? inventory.getSize() : 0; + protected InventorySize getInventorySize() { + return inventorySize; } @Override diff --git a/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIUtils.java b/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIUtils.java index 03e7ced6cf..1f84cdf710 100644 --- a/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIUtils.java +++ b/bukkit/bukkit-gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUIUtils.java @@ -2,6 +2,7 @@ import me.hsgamer.hscore.bukkit.gui.event.BukkitClickEvent; import me.hsgamer.hscore.bukkit.gui.event.BukkitDragEvent; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import org.bukkit.Bukkit; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryDragEvent; @@ -116,4 +117,36 @@ public static Function getInventoryFunctionFromTitl : Bukkit.createInventory(display, type, title); }; } + + /** + * Get the inventory size from the inventory + * + * @param inventory the inventory + * + * @return the inventory size + */ + public static InventorySize getInventorySize(Inventory inventory) { + return new InventorySize() { + @Override + public int getSize() { + return inventory.getSize(); + } + + @Override + public int getSlotPerRow() { + switch (inventory.getType()) { + case CHEST: + case ENDER_CHEST: + case SHULKER_BOX: + return 9; + case DISPENSER: + case DROPPER: + case HOPPER: + return 3; + default: + return getSize(); + } + } + }; + } } diff --git a/minecraft/minecraft-gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java b/minecraft/minecraft-gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java index 619a90812f..4768c31173 100644 --- a/minecraft/minecraft-gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java +++ b/minecraft/minecraft-gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java @@ -4,6 +4,7 @@ import me.hsgamer.hscore.minecraft.gui.button.ButtonMap; import me.hsgamer.hscore.minecraft.gui.button.DisplayButton; import me.hsgamer.hscore.minecraft.gui.mask.Mask; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import org.jetbrains.annotations.NotNull; import java.util.*; @@ -73,13 +74,13 @@ public void stop() { } @Override - public @NotNull Map<@NotNull Integer, @NotNull DisplayButton> getButtons(@NotNull UUID uuid, int size) { + public @NotNull Map<@NotNull Integer, @NotNull DisplayButton> getButtons(@NotNull UUID uuid, InventorySize inventorySize) { Map map = new HashMap<>(); for (Mask mask : masks) { - Optional> buttons = mask.generateButtons(uuid, size); + Optional> buttons = mask.generateButtons(uuid, inventorySize); if (!buttons.isPresent()) continue; buttons.get().forEach((slot, button) -> { - if (slot < 0 || slot >= size) { + if (slot < 0 || slot >= inventorySize.getSize()) { return; } diff --git a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/Mask.java b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/Mask.java index ec08b0e98c..42ecdcf3de 100644 --- a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/Mask.java +++ b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/Mask.java @@ -1,6 +1,7 @@ package me.hsgamer.hscore.minecraft.gui.mask; import me.hsgamer.hscore.minecraft.gui.button.Button; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import me.hsgamer.hscore.ui.property.Initializable; import org.jetbrains.annotations.NotNull; @@ -22,7 +23,7 @@ public interface Mask extends Initializable { static Mask empty(String name) { return new Mask() { @Override - public Optional> generateButtons(@NotNull UUID uuid, int size) { + public Optional> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize) { return Optional.empty(); } @@ -36,12 +37,12 @@ static Mask empty(String name) { /** * Generate the buttons for the unique id * - * @param uuid the unique id - * @param size the size of the inventory + * @param uuid the unique id + * @param inventorySize the size of the inventory * * @return the map contains the slots and the buttons */ - Optional> generateButtons(@NotNull UUID uuid, int size); + Optional> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize); /** * Get the name of the mask diff --git a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskSlot.java b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskSlot.java index d202683632..4d59f3ab16 100644 --- a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskSlot.java +++ b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskSlot.java @@ -1,10 +1,12 @@ package me.hsgamer.hscore.minecraft.gui.mask; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.List; import java.util.UUID; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -22,7 +24,7 @@ public interface MaskSlot { */ @NotNull static MaskSlot of(@NotNull List<@NotNull Integer> slots) { - return uuid -> slots; + return (uuid, size) -> slots; } /** @@ -35,7 +37,7 @@ static MaskSlot of(@NotNull List<@NotNull Integer> slots) { @NotNull static MaskSlot of(@NotNull Integer... slots) { List slotList = Arrays.asList(slots); - return uuid -> slotList; + return (uuid, size) -> slotList; } /** @@ -46,17 +48,30 @@ static MaskSlot of(@NotNull Integer... slots) { * @return the mask slot */ @NotNull - static MaskSlot of(IntStream slotStream) { + static MaskSlot of(@NotNull IntStream slotStream) { return of(slotStream.boxed().collect(Collectors.toList())); } + /** + * Create a mask slot from the slot function + * + * @param slotFunction the slot function + * + * @return the mask slot + */ + @NotNull + static MaskSlot of(@NotNull Function> slotFunction) { + return (uuid, size) -> slotFunction.apply(size); + } + /** * Get the slots * * @param uuid the unique id + * @param size the size of the inventory * * @return the slots */ @NotNull - List getSlots(UUID uuid); + List getSlots(UUID uuid, InventorySize size); } diff --git a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskUtils.java b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskUtils.java index 3138e31f55..b2e416301f 100644 --- a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskUtils.java +++ b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/MaskUtils.java @@ -1,10 +1,12 @@ package me.hsgamer.hscore.minecraft.gui.mask; import me.hsgamer.hscore.minecraft.gui.object.InventoryPosition; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import org.jetbrains.annotations.NotNull; -import java.util.Collections; +import java.util.function.Function; import java.util.stream.IntStream; +import java.util.stream.Stream; /** * The utility class for {@link Mask} @@ -16,199 +18,66 @@ private MaskUtils() { } /** - * Get the raw slot from the position + * Generate the stream of positions in the area between two positions * - * @param x the x position - * @param y the y position - * @param slotPerRow the number of slots for each row - * - * @return the raw slot - */ - public static int toSlot(int x, int y, int slotPerRow) { - return x + y * slotPerRow; - } - - /** - * Get the raw slot from the position, assuming the number of slots for each row is 9 - * - * @param x the x position - * @param y the y position - * - * @return the raw slot - */ - public static int toSlot(int x, int y) { - return toSlot(x, y, 9); - } - - /** - * Convert the slot to the position - * - * @param slot the slot - * @param slotPerRow the number of slots for each row - * - * @return the position - */ - @NotNull - public static InventoryPosition toPosition(int slot, int slotPerRow) { - int x = slot % slotPerRow; - int y = slot / slotPerRow; - return InventoryPosition.of(x, y); - } - - /** - * Convert the slot to the position, assuming the number of slots for each row is 9 - * - * @param slot the slot + * @param position1 the first position + * @param position2 the second position * - * @return the position + * @return the stream of positions */ @NotNull - public static InventoryPosition toPosition(int slot) { - return toPosition(slot, 9); - } - - /** - * Get the raw slot from the position - * - * @param position the pair value for the position - * @param slotPerRow the number of slots for each row - * - * @return the raw slot - */ - public static int toSlot(@NotNull InventoryPosition position, int slotPerRow) { - return toSlot(position.getX(), position.getY(), slotPerRow); - } - - /** - * Get the raw slot from the position, assuming the number of slots for each row is 9 - * - * @param position the pair value for the position - * - * @return the raw slot - */ - public static int toSlot(@NotNull InventoryPosition position) { - return toSlot(position, 9); - } - - /** - * Generate the stream of slots in the area between two positions - * - * @param x1 the x of the first position - * @param y1 the y of the first position - * @param x2 the x of the second position - * @param y2 the y of the second position - * @param slotPerRow the number of slots for each row - * - * @return the stream of slots - */ - public static IntStream generateAreaSlots(int x1, int y1, int x2, int y2, int slotPerRow) { - InventoryPosition max = InventoryPosition.maxPosition(x1, y1, x2, y2); - InventoryPosition min = InventoryPosition.minPosition(x1, y1, x2, y2); - return IntStream.rangeClosed(min.getY(), max.getY()).flatMap(y -> IntStream.rangeClosed(toSlot(min.getX(), y, slotPerRow), toSlot(max.getX(), y, slotPerRow))); - } - - /** - * Generate the stream of slots in the area between two positions, assuming the number of slots for each row is 9 - * - * @param x1 the x of the first position - * @param y1 the y of the first position - * @param x2 the x of the second position - * @param y2 the y of the second position - * - * @return the stream of slots - */ - public static IntStream generateAreaSlots(int x1, int y1, int x2, int y2) { - return generateAreaSlots(x1, y1, x2, y2, 9); + public static Stream generateAreaPositions(@NotNull InventoryPosition position1, @NotNull InventoryPosition position2) { + InventoryPosition max = InventoryPosition.maxPosition(position1, position2); + InventoryPosition min = InventoryPosition.minPosition(position1, position2); + return IntStream.rangeClosed(min.getY(), max.getY()) + .mapToObj(y -> IntStream.rangeClosed(min.getX(), max.getX()).mapToObj(x -> InventoryPosition.of(x, y))) + .flatMap(Function.identity()); } /** * Generate the stream of slots in the area between two positions * - * @param position1 the first position - * @param position2 the second position - * @param slotPerRow the number of slots for each row - * - * @return the stream of slots - */ - @NotNull - public static IntStream generateAreaSlots(@NotNull InventoryPosition position1, @NotNull InventoryPosition position2, int slotPerRow) { - return generateAreaSlots(position1.getX(), position1.getY(), position2.getX(), position2.getY(), slotPerRow); - } - - /** - * Generate the stream of slots in the area between two positions, assuming the number of slots for each row is 9 - * * @param position1 the first position * @param position2 the second position + * @param size the size of the inventory * * @return the stream of slots */ @NotNull - public static IntStream generateAreaSlots(@NotNull InventoryPosition position1, @NotNull InventoryPosition position2) { - return generateAreaSlots(position1, position2, 9); - } - - /** - * Get the stream of slots drawing the outline of the area between 2 positions - * - * @param x1 the x of the first position - * @param y1 the y of the first position - * @param x2 the x of the second position - * @param y2 the y of the second position - * @param slotPerRow the number of slots for each row - * - * @return the stream of slots - */ - @NotNull - public static IntStream generateOutlineSlots(int x1, int y1, int x2, int y2, int slotPerRow) { - InventoryPosition max = InventoryPosition.maxPosition(x1, y1, x2, y2); - InventoryPosition min = InventoryPosition.minPosition(x1, y1, x2, y2); - IntStream top = IntStream.rangeClosed(toSlot(min.getX(), min.getY(), slotPerRow), toSlot(max.getX(), min.getY(), slotPerRow)); - IntStream right = IntStream.rangeClosed(min.getY(), max.getY()).map(y -> toSlot(max.getX(), y, slotPerRow)); - IntStream bottom = IntStream.rangeClosed(toSlot(min.getX(), max.getY(), slotPerRow), toSlot(max.getX(), max.getY(), slotPerRow)).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue); - IntStream left = IntStream.rangeClosed(min.getY(), max.getY()).map(y -> toSlot(min.getX(), y, slotPerRow)).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue); - return IntStream.concat(top, IntStream.concat(right, IntStream.concat(bottom, left))).distinct(); + public static IntStream generateAreaSlots(@NotNull InventoryPosition position1, @NotNull InventoryPosition position2, @NotNull InventorySize size) { + return size.toSlots(generateAreaPositions(position1, position2)); } /** - * Get the stream of slots drawing the outline of the area between 2 positions, assuming the number of slots for each row is 9 + * Get the stream of positions drawing the outline of the area between 2 positions * - * @param x1 the x of the first position - * @param y1 the y of the first position - * @param x2 the x of the second position - * @param y2 the y of the second position + * @param position1 the first position + * @param position2 the second position * - * @return the stream of slots + * @return the stream of positions */ @NotNull - public static IntStream generateOutlineSlots(int x1, int y1, int x2, int y2) { - return generateOutlineSlots(x1, y1, x2, y2, 9); + public static Stream generateOutlineSlots(@NotNull InventoryPosition position1, @NotNull InventoryPosition position2) { + InventoryPosition max = InventoryPosition.maxPosition(position1, position2); + InventoryPosition min = InventoryPosition.minPosition(position1, position2); + Stream top = IntStream.rangeClosed(min.getX(), max.getX()).mapToObj(x -> InventoryPosition.of(x, min.getY())); + Stream right = IntStream.rangeClosed(min.getY(), max.getY()).mapToObj(y -> InventoryPosition.of(max.getX(), y)); + Stream bottom = IntStream.rangeClosed(min.getX(), max.getX()).mapToObj(x -> InventoryPosition.of(x, max.getY())); + Stream left = IntStream.rangeClosed(min.getY(), max.getY()).mapToObj(y -> InventoryPosition.of(min.getX(), y)); + return Stream.concat(Stream.concat(top, right), Stream.concat(bottom, left)).distinct(); } /** * Get the stream of slots drawing the outline of the area between 2 positions * - * @param position1 the first position - * @param position2 the second position - * @param slotPerRow the number of slots for each row - * - * @return the stream of slots - */ - @NotNull - public static IntStream generateOutlineSlots(InventoryPosition position1, InventoryPosition position2, int slotPerRow) { - return generateOutlineSlots(position1.getX(), position1.getY(), position2.getX(), position2.getY(), slotPerRow); - } - - /** - * Get the stream of slots drawing the outline of the area between 2 positions, assuming the number of slots for each row is 9 - * * @param position1 the first position * @param position2 the second position + * @param size the size of the inventory * * @return the stream of slots */ @NotNull - public static IntStream generateOutlineSlots(InventoryPosition position1, InventoryPosition position2) { - return generateOutlineSlots(position1, position2, 9); + public static IntStream generateOutlineSlots(@NotNull InventoryPosition position1, @NotNull InventoryPosition position2, @NotNull InventorySize size) { + return size.toSlots(generateOutlineSlots(position1, position2)); } } diff --git a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/AnimatedMask.java b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/AnimatedMask.java index 41cb3c1586..dcb141a17a 100644 --- a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/AnimatedMask.java +++ b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/AnimatedMask.java @@ -4,6 +4,7 @@ import me.hsgamer.hscore.minecraft.gui.button.Button; import me.hsgamer.hscore.minecraft.gui.mask.BaseMask; import me.hsgamer.hscore.minecraft.gui.mask.Mask; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import me.hsgamer.hscore.ui.property.IdentifiedUpdatable; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -98,9 +99,9 @@ private int getCurrentIndex(@NotNull UUID uuid) { } @Override - public Optional> generateButtons(@NotNull UUID uuid, int size) { + public Optional> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize) { update(uuid); - return masks.get(getCurrentIndex(uuid)).generateButtons(uuid, size); + return masks.get(getCurrentIndex(uuid)).generateButtons(uuid, inventorySize); } @Override diff --git a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonMapMask.java b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonMapMask.java index a35d223806..390aeaa0c9 100644 --- a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonMapMask.java +++ b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonMapMask.java @@ -2,6 +2,7 @@ import me.hsgamer.hscore.minecraft.gui.button.Button; import me.hsgamer.hscore.minecraft.gui.mask.BaseMask; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -76,7 +77,7 @@ public Map getButtonMap() { } @Override - public Optional> generateButtons(@NotNull UUID uuid, int size) { + public Optional> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize) { return Optional.of(buttonMap); } diff --git a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonPaginatedMask.java b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonPaginatedMask.java index 005659ee18..5250f0f0fb 100644 --- a/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonPaginatedMask.java +++ b/minecraft/minecraft-gui-mask/src/main/java/me/hsgamer/hscore/minecraft/gui/mask/impl/ButtonPaginatedMask.java @@ -2,6 +2,7 @@ import me.hsgamer.hscore.minecraft.gui.button.Button; import me.hsgamer.hscore.minecraft.gui.mask.MaskSlot; +import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import org.jetbrains.annotations.NotNull; import java.util.*; @@ -44,8 +45,8 @@ public MaskSlot getMaskSlot() { public abstract List<@NotNull Button> getButtons(@NotNull UUID uuid); @Override - protected Optional> generateButtons(@NotNull UUID uuid, int size, int pageNumber) { - List slots = this.maskSlot.getSlots(uuid); + protected Optional> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize, int pageNumber) { + List slots = this.maskSlot.getSlots(uuid, inventorySize); List