Skip to content

Commit

Permalink
use InventorySize
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 16, 2024
1 parent a47d3da commit 4e84971
Show file tree
Hide file tree
Showing 26 changed files with 270 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@
*/
public class BukkitGUIDisplay extends InventoryGUIDisplay<BukkitGUIHolder> implements InventoryHolder {
private Inventory inventory;
private InventorySize inventorySize;

/**
* Create a new display
Expand All @@ -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
Expand All @@ -42,8 +45,8 @@ protected void clearInventory() {
}

@Override
protected int getInventorySize() {
return inventory != null ? inventory.getSize() : 0;
protected InventorySize getInventorySize() {
return inventorySize;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,4 +117,36 @@ public static Function<BukkitGUIDisplay, Inventory> 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();
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<Integer, DisplayButton> map = new HashMap<>();
for (Mask mask : masks) {
Optional<Map<Integer, Button>> buttons = mask.generateButtons(uuid, size);
Optional<Map<Integer, Button>> 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,7 +23,7 @@ public interface Mask extends Initializable {
static Mask empty(String name) {
return new Mask() {
@Override
public Optional<Map<@NotNull Integer, @NotNull Button>> generateButtons(@NotNull UUID uuid, int size) {
public Optional<Map<@NotNull Integer, @NotNull Button>> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize) {
return Optional.empty();
}

Expand All @@ -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<Map<@NotNull Integer, @NotNull Button>> generateButtons(@NotNull UUID uuid, int size);
Optional<Map<@NotNull Integer, @NotNull Button>> generateButtons(@NotNull UUID uuid, @NotNull InventorySize inventorySize);

/**
* Get the name of the mask
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,7 +24,7 @@ public interface MaskSlot {
*/
@NotNull
static MaskSlot of(@NotNull List<@NotNull Integer> slots) {
return uuid -> slots;
return (uuid, size) -> slots;
}

/**
Expand All @@ -35,7 +37,7 @@ static MaskSlot of(@NotNull List<@NotNull Integer> slots) {
@NotNull
static MaskSlot of(@NotNull Integer... slots) {
List<Integer> slotList = Arrays.asList(slots);
return uuid -> slotList;
return (uuid, size) -> slotList;
}

/**
Expand All @@ -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<InventorySize, List<Integer>> 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<Integer> getSlots(UUID uuid);
List<Integer> getSlots(UUID uuid, InventorySize size);
}
Loading

0 comments on commit 4e84971

Please sign in to comment.