@@ -43,6 +43,11 @@ public interface Inventory {
4343 */
4444 public InventoryProvider getInventoryProvider ();
4545
46+ /**
47+ * @return The number of rows
48+ */
49+ public int getRows ();
50+
4651 /**
4752 * Set an item at specific pos.<br />
4853 *
@@ -56,9 +61,9 @@ public interface Inventory {
5661 public default void set (int col , int row , ClickableItem item ) {
5762 if (col < 1 || col > 9 )
5863 throw new IllegalArgumentException ("col must be between 1 and 9" );
59- if (row < 1 || row > 6 )
60- throw new IllegalArgumentException ("row must be between 1 and the maximum number of rows" );
61- set ((row - 1 ) * 9 + ( col - 1 ), item );
64+ if (row < 1 || row > getRows () )
65+ throw new IllegalArgumentException ("row must be between 1 and " + getRows () );
66+ set (locToPos (row , col ), item );
6267 }
6368
6469 /**
@@ -102,9 +107,9 @@ public default void rectangle(int row, int col, int width, int height, Clickable
102107 // 10 - col because width starts with 1 and not 0
103108 if (width < 1 || width > 10 - col )
104109 throw new IllegalArgumentException ("The width must be between 1 and " + (10 - col ));
105- if (height < 1 || height > 7 - col )
106- throw new IllegalArgumentException ("The height must be between 1 and " + (7 - col ));
107- rectangle ((row - 1 ) * 9 + ( col - 1 ), width , height , item );
110+ if (height < 1 || height > getRows () + 1 - col )
111+ throw new IllegalArgumentException ("The height must be between 1 and " + (getRows () + 1 - col ));
112+ rectangle (locToPos (row , col ), width , height , item );
108113 }
109114
110115 /**
@@ -114,13 +119,58 @@ public default void rectangle(int row, int col, int width, int height, Clickable
114119 * The position of the item. Must be between 0 and the maximum number
115120 * of case (9 * number of rows - 1)
116121 * @param width
122+ * The width. Must be between 1 and 9 and stay inside the inventory
123+ * @param height
124+ * The height. Must be between 1 and the maximum number of rows and
125+ * stay inside the inventory
126+ * @param item
127+ * The item
128+ */
129+ public void rectangle (int pos , int width , int height , ClickableItem item );
130+
131+ /**
132+ * Create a rectangle of items and fill the rectangle
133+ *
134+ * @param row
135+ * The row. Must be between 1 and the maximum number of rows
136+ * @param col
137+ * The col. Must be between 1 and 9
138+ * @param width
117139 * The width. Must be between 1 and 9
118140 * @param height
119141 * The height. Must be between 1 and the maximum number of rows
120142 * @param item
121143 * The item
122144 */
123- public void rectangle (int pos , int width , int height , ClickableItem item );
145+ public default void fillRectangle (int row , int col , int width , int height , ClickableItem item ) {
146+ if (col < 1 || col > 9 )
147+ throw new IllegalArgumentException ("col must be between 1 and 9, but is " + col );
148+ if (row < 1 || row > 6 )
149+ throw new IllegalArgumentException ("row must be between 1 and the maximum number of rows, but is " + row );
150+ // 10 - col because width starts with 1 and not 0
151+ if (width < 1 || width > 10 - col )
152+ throw new IllegalArgumentException ("The width must be between 1 and " + (10 - col ) + ", but is " + width );
153+ if (height < 1 || height > getRows () + 1 - col )
154+ throw new IllegalArgumentException (
155+ "The height must be between 1 and " + (getRows () + 1 - col ) + ", but is " + height );
156+ fillRectangle (locToPos (row , col ), width , height , item );
157+ }
158+
159+ /**
160+ * Create a rectangle of items and fill the rectangle
161+ *
162+ * @param pos
163+ * The position of the item. Must be between 0 and the maximum number
164+ * of case (9 * number of rows - 1)
165+ * @param width
166+ * The width. Must be between 1 and 9 and stay inside the inventory
167+ * @param height
168+ * The height. Must be between 1 and the maximum number of rows and
169+ * stay inside the inventory
170+ * @param item
171+ * The item
172+ */
173+ public void fillRectangle (int pos , int width , int height , ClickableItem item );
124174
125175 /**
126176 * Save a variable in the Inventory. If the key already exists, the old value is
@@ -141,4 +191,28 @@ public default void rectangle(int row, int col, int width, int height, Clickable
141191 * exist, null is returned
142192 */
143193 public Object get (String key );
194+
195+ /**
196+ * Transform a single position to two location
197+ *
198+ * @param pos
199+ * The position
200+ * @return an array of two integer: The row and the column
201+ */
202+ public default int [] posToLoc (int pos ) {
203+ return new int [] { (pos / 9 ) + 1 , (pos % 9 ) + 1 };
204+ }
205+
206+ /**
207+ * Transform two location to a single position
208+ *
209+ * @param row
210+ * The row
211+ * @param col
212+ * The col
213+ * @return The position
214+ */
215+ public default int locToPos (int row , int col ) {
216+ return (row - 1 ) * 9 + (col - 1 );
217+ }
144218}
0 commit comments