@@ -40,120 +40,51 @@ public class CreatorTutorialsMenu extends Gui
4040 */
4141 public CreatorTutorialsMenu (TeachingTutorials plugin , User user , Tutorial [] tutorials )
4242 {
43- //Sets up the menu with the icons already in place
44- super (getGUI (tutorials ));
43+ super (calculateNumRows (tutorials .length )*9 , inventoryName );
4544 this .plugin = plugin ;
4645 this .user = user ;
4746 this .tutorials = tutorials ;
4847
49- //Adds the click actions
50- setActions ();
48+ //Adds the items
49+ addItems ();
5150 }
5251
5352 /**
54- * Creates an inventory design for the current status of a creator's tutorials
55- * @param allTutorials A list of tutorials to include in the menu
56- * @return An inventory filled with icons, informing the user of the current state of their tutorials, including which
57- * ones are in use
53+ * Populates the menu with icons and actions
5854 */
59- private static Inventory getGUI ( Tutorial [] allTutorials )
55+ private void addItems ( )
6056 {
61- //Declare variables
62- int i ;
63- int iTutorials ;
64- int iDiv ;
65- int iMod ;
66- int iRows ;
67-
68- Inventory inventory ;
69-
70- //Works out how many rows in the inventory are needed
71- iTutorials = allTutorials .length ;
72- iDiv = iTutorials /9 ;
73- iMod = iTutorials %9 ;
74-
75- if (iMod != 0 || iDiv == 0 )
76- {
77- iDiv = iDiv + 1 ;
78- }
79-
80- //Enables an empty row and then a row for the back button
81- iRows = iDiv +2 ;
82-
83- //------------------------------
84-
85- //Create inventories
86- inventory = Bukkit .createInventory (null , iRows * 9 );
87- inventory .clear ();
88-
89- Inventory toReturn = Bukkit .createInventory (null , iRows * 9 , inventoryName );
90-
91- //Inv slot 0 = the first one
92-
93- //Indicates that the creator has no tutorials if they don't own any
94- if (allTutorials .length == 0 )
57+ //Indicates that the creator has no tutorials
58+ if (tutorials .length == 0 )
59+ setItem (5 -1 , Utils .createItem (Material .BARRIER , 1 , TutorialGUIUtils .optionTitle ("You have no tutorials" )));
60+
61+ //Adds the tutorial icons
62+ int iNumTutorials = tutorials .length ;
63+ if (iNumTutorials > 45 )
64+ iNumTutorials = 45 ;
65+ for (int i = 0 ; i < iNumTutorials ; i ++)
9566 {
96- ItemStack noTutorials = Utils .createItem (Material .BARRIER , 1 , TutorialGUIUtils .optionTitle ("You have no tutorials" ));
97- inventory .setItem (5 -1 , noTutorials );
67+ int finalI = i ;
68+ setItem (i , Utils .createItem (Material .WRITTEN_BOOK , 1 ,
69+ TutorialGUIUtils .optionTitle (tutorials [i ].getTutorialName ()),
70+ TutorialGUIUtils .optionLore ("Click to manage Tutorial" )),
71+ new guiAction () {
72+ @ Override
73+ public void rightClick (User u ) {
74+ leftClick (u );
75+ }
76+
77+ @ Override
78+ public void leftClick (User u ) {
79+ u .mainGui = new TutorialManageMenu (plugin , user , tutorials [finalI ], CreatorTutorialsMenu .this );
80+ u .mainGui .open (u );
81+ }
82+ });
9883 }
9984
10085 //Adds back button
10186 ItemStack back = Utils .createItem (Material .SPRUCE_DOOR , 1 , TutorialGUIUtils .backButton ("Back to creator menu" ));
102- inventory .setItem ((iRows * 9 )-1 , back );
103-
104- //Creates the menu options
105- for (i = 0 ; i < allTutorials .length ; i ++)
106- {
107- ItemStack tutorial ;
108- //Sets tutorial name bold for tutorials in use
109- if (allTutorials [i ].isInUse ())
110- tutorial = Utils .createItem (Material .WRITTEN_BOOK , 1 ,
111- TutorialGUIUtils .optionTitle (allTutorials [i ].getTutorialName ()).decoration (TextDecoration .BOLD , true ),
112- TutorialGUIUtils .optionLore ("In Use - Left click to remove from use" ),
113- TutorialGUIUtils .optionLore ("Right click to add a new location" ));
114- else
115- tutorial = Utils .createItem (Material .BOOK , 1 ,
116- TutorialGUIUtils .optionTitle (allTutorials [i ].getTutorialName ()),
117- TutorialGUIUtils .optionLore ("Not in Use - Left click to set in use" ),
118- TutorialGUIUtils .optionLore ("Right click to add a new location" ));
119- inventory .setItem (i , tutorial );
120- }
121-
122- toReturn .setContents (inventory .getContents ());
123- return toReturn ;
124- }
125-
126- /**
127- * Sets the click actions for each slot of the menu
128- */
129- private void setActions ()
130- {
131- //Declare variables
132- int i ;
133- int iTutorials ;
134- int iDiv ;
135- int iMod ;
136- int iRows ;
137-
138- //Works out how many rows in the inventory are needed
139- iTutorials = tutorials .length ;
140- iDiv = iTutorials /9 ;
141- iMod = iTutorials %9 ;
142-
143- if (iMod != 0 || iDiv == 0 )
144- {
145- iDiv = iDiv + 1 ;
146- }
147-
148- //Enables an empty row and then a row for the back button
149- iRows = iDiv +2 ;
150-
151- //------------------------------
152-
153- //Inv slot 0 = the first one
154-
155- //Adds back button
156- setAction ((iRows * 9 ) - 1 , new guiAction () {
87+ setItem ((calculateNumRows (tutorials .length ) * 9 ) - 1 , back , new guiAction () {
15788 @ Override
15889 public void rightClick (User u ) {
15990 leftClick (u );
@@ -166,75 +97,76 @@ public void leftClick(User u) {
16697 u .mainGui .open (u );
16798 }
16899 });
100+ }
169101
170- //Creates the actions for clicking on the tutorials
171- for (i = 0 ; i < tutorials .length ; i ++)
172- {
173- int iSlot = i ;
174-
175- setAction (i , new guiAction () {
176- @ Override
177- public void rightClick (User u ) {
178- rightClicked (u , iSlot );
179- }
180-
181- @ Override
182- public void leftClick (User u ) {
183- leftClicked (u , iSlot );
184- }
185- });
186- }
102+ /**
103+ * Clears items from the GUI, recreates the items and then opens the menu
104+ */
105+ @ Override
106+ public void refresh () {
107+ //Clears items from the gui
108+ this .clearGui ();
109+
110+ //Adds the items back
111+ this .addItems ();
112+
113+ //Opens the gui
114+ this .open (user );
187115 }
188116
189- //Left click is for toggling whether a tutorial is in use or not
190- private void leftClicked (User u , int iSlot )
117+ /**
118+ * Refreshes the name on the tutorial icons
119+ */
120+ public void refreshTutorialIcons ()
191121 {
192- if ( tutorials [ iSlot ]. toggleInUse ( plugin ))
193- {
194- this . refresh ();
195- }
196- else
122+ //Adds the tutorial icons
123+ int iNumTutorials = tutorials . length ;
124+ if ( iNumTutorials > 45 )
125+ iNumTutorials = 45 ;
126+ for ( int i = 0 ; i < iNumTutorials ; i ++)
197127 {
198- u .player .sendMessage (Display .errorText ("There are no locations for this tutorial" ));
128+ setItem (i , Utils .createItem (Material .WRITTEN_BOOK , 1 ,
129+ TutorialGUIUtils .optionTitle (tutorials [i ].getTutorialName ()),
130+ TutorialGUIUtils .optionLore ("Click to manage Tutorial" )));
199131 }
200132 }
201133
202- //Right click is for creating a new location
203- private void rightClicked (User u , int iSlot )
134+ /**
135+ * Calculates the number of rows needed to form an inventory for a given number of tutorials
136+ * @param iNumTutorials The number of tutorials
137+ * @return The number of rows needed to form an inventory
138+ */
139+ public static int calculateNumRows (int iNumTutorials )
204140 {
205- //Only starts the new location process if the creator is idle
206- if (user .getCurrentMode ().equals (Mode .Idle ))
207- {
208- delete ();
209- u .mainGui = null ;
141+ int iDiv ;
142+ int iMod ;
210143
211- //Creates a NewLocation object
212- NewLocation newLocation = new NewLocation (user , tutorials [iSlot ], plugin );
144+ //Works out how many rows in the inventory are needed
145+ iDiv = iNumTutorials /9 ;
146+ iMod = iNumTutorials %9 ;
213147
214- //Launches them into the new location adding process
215- newLocation .launchNewLocationAdding ();
216- }
217- else
148+ if (iMod != 0 || iDiv == 0 )
218149 {
219- user . player . sendMessage ( Display . errorText ( "Complete or pause your current tutorial or location creation first" )) ;
150+ iDiv = iDiv + 1 ;
220151 }
221- }
222152
223- /**
224- * Clears items from the GUI, recreates the items and then opens the menu
225- */
226- @ Override
227- public void refresh () {
228- //Clears items from the gui
229- this .clearGui ();
153+ //Add row for the back button
154+ int iRows = iDiv +1 ;
230155
231- //Adds icons to the gui
232- this .getInventory ().setContents (getGUI (tutorials ).getContents ());
156+ //Add an extra for pleasure
157+ if (iRows < 6 )
158+ iRows ++;
233159
234- //Adds click actions to the gui
235- this .setActions ();
160+ else if (iRows == 6 )
161+ {
162+ // Do nothing
163+ }
164+ else
165+ {
166+ iRows = 6 ;
167+ }
236168
237- //Opens the gui
238- this . open ( user ) ;
169+ //Enables an empty row and then a row for the back button
170+ return iRows ;
239171 }
240172}
0 commit comments