3939import java .util .Map ;
4040import java .util .Objects ;
4141import java .util .function .Consumer ;
42+ import java .util .function .Function ;
4243import java .util .function .Predicate ;
4344import java .util .stream .IntStream ;
4445
4748 * The project is on <a href="https://github.com/MrMicky-FR/FastInv">GitHub</a>.
4849 *
4950 * @author MrMicky
50- * @version 3.0.3
51+ * @version 3.0.4
5152 */
5253public class FastInv implements InventoryHolder {
5354
@@ -66,7 +67,7 @@ public class FastInv implements InventoryHolder {
6667 * @param size The size of the inventory.
6768 */
6869 public FastInv (int size ) {
69- this (size , InventoryType . CHEST . getDefaultTitle ( ));
70+ this (owner -> Bukkit . createInventory ( owner , size ));
7071 }
7172
7273 /**
@@ -76,7 +77,7 @@ public FastInv(int size) {
7677 * @param title The title (name) of the inventory.
7778 */
7879 public FastInv (int size , String title ) {
79- this (size , InventoryType . CHEST , title );
80+ this (owner -> Bukkit . createInventory ( owner , size , title ) );
8081 }
8182
8283 /**
@@ -85,7 +86,7 @@ public FastInv(int size, String title) {
8586 * @param type The type of the inventory.
8687 */
8788 public FastInv (InventoryType type ) {
88- this (Objects . requireNonNull ( type , " type" ), type . getDefaultTitle ( ));
89+ this (owner -> Bukkit . createInventory ( owner , type ));
8990 }
9091
9192 /**
@@ -95,19 +96,18 @@ public FastInv(InventoryType type) {
9596 * @param title The title of the inventory.
9697 */
9798 public FastInv (InventoryType type , String title ) {
98- this (0 , Objects . requireNonNull ( type , " type" ) , title );
99+ this (owner -> Bukkit . createInventory ( owner , type , title ) );
99100 }
100101
101- private FastInv (int size , InventoryType type , String title ) {
102- if (type == InventoryType .CHEST && size > 0 ) {
103- this .inventory = Bukkit .createInventory (this , size , title );
104- } else {
105- this .inventory = Bukkit .createInventory (this , type , title );
106- }
102+ public FastInv (Function <InventoryHolder , Inventory > inventoryFunction ) {
103+ Objects .requireNonNull (inventoryFunction , "inventoryFunction" );
104+ Inventory inv = inventoryFunction .apply (this );
107105
108- if (this . inventory .getHolder () != this ) {
109- throw new IllegalStateException ("Inventory holder is not FastInv, found: " + this . inventory .getHolder ());
106+ if (inv .getHolder () != this ) {
107+ throw new IllegalStateException ("Inventory holder is not FastInv, found: " + inv .getHolder ());
110108 }
109+
110+ this .inventory = inv ;
111111 }
112112
113113 protected void onOpen (InventoryOpenEvent event ) {
@@ -132,7 +132,7 @@ public void addItem(ItemStack item) {
132132 * Add an {@link ItemStack} to the inventory on the first empty slot with a click handler.
133133 *
134134 * @param item The item to add.
135- * @param handler The the click handler for the item.
135+ * @param handler The click handler for the item.
136136 */
137137 public void addItem (ItemStack item , Consumer <InventoryClickEvent > handler ) {
138138 int slot = this .inventory .firstEmpty ();
@@ -290,7 +290,8 @@ public void open(Player player) {
290290 */
291291 public int [] getBorders () {
292292 int size = this .inventory .getSize ();
293- return IntStream .range (0 , size ).filter (i -> size < 27 || i < 9 || i % 9 == 0 || (i - 8 ) % 9 == 0 || i > size - 9 ).toArray ();
293+ return IntStream .range (0 , size ).filter (i -> size < 27 || i < 9
294+ || i % 9 == 0 || (i - 8 ) % 9 == 0 || i > size - 9 ).toArray ();
294295 }
295296
296297 /**
@@ -300,7 +301,9 @@ public int[] getBorders() {
300301 */
301302 public int [] getCorners () {
302303 int size = this .inventory .getSize ();
303- return IntStream .range (0 , size ).filter (i -> i < 2 || (i > 6 && i < 10 ) || i == 17 || i == size - 18 || (i > size - 11 && i < size - 7 ) || i > size - 3 ).toArray ();
304+ return IntStream .range (0 , size ).filter (i -> i < 2 || (i > 6 && i < 10 )
305+ || i == 17 || i == size - 18
306+ || (i > size - 11 && i < size - 7 ) || i > size - 3 ).toArray ();
304307 }
305308
306309 /**
0 commit comments