-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create SelectChallengeGUI that allows to choose one challenge from in…
…put challenge list and return it into GUI.
- Loading branch information
Showing
1 changed file
with
138 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package world.bentobox.challenges.panel.util; | ||
|
||
|
||
import org.bukkit.Material; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
|
||
import world.bentobox.bentobox.api.panels.PanelItem; | ||
import world.bentobox.bentobox.api.panels.builders.PanelBuilder; | ||
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; | ||
import world.bentobox.bentobox.api.user.User; | ||
import world.bentobox.challenges.database.object.Challenges; | ||
|
||
|
||
/** | ||
* This class creates new GUI that allows to select single challenge, which is returned via consumer. | ||
*/ | ||
public class SelectChallengeGUI | ||
{ | ||
public SelectChallengeGUI(User user, List<Challenges> challengesList, BiConsumer<Boolean, Challenges> consumer) | ||
{ | ||
this.consumer = consumer; | ||
this.user = user; | ||
this.challengesList = challengesList; | ||
|
||
this.build(0); | ||
} | ||
|
||
|
||
/** | ||
* This method builds panel that allows to select single challenge from input challenges. | ||
*/ | ||
private void build(int pageIndex) | ||
{ | ||
PanelBuilder panelBuilder = new PanelBuilder().user(this.user).name(this.user.getTranslation("challenges.gui.choose-challenge-title")); | ||
|
||
// Maximal elements in page. | ||
final int MAX_ELEMENTS = 36; | ||
|
||
final int correctPage; | ||
|
||
if (pageIndex < 0) | ||
{ | ||
correctPage = this.challengesList.size() / MAX_ELEMENTS; | ||
} | ||
else if (pageIndex > (this.challengesList.size() / MAX_ELEMENTS)) | ||
{ | ||
correctPage = 0; | ||
} | ||
else | ||
{ | ||
correctPage = pageIndex; | ||
} | ||
|
||
// Navigation buttons | ||
|
||
panelBuilder.item(3, | ||
new PanelItemBuilder(). | ||
icon(Material.SIGN). | ||
name(this.user.getTranslation("challenges.gui.buttons.previous")). | ||
clickHandler( (panel, user1, clickType, slot) -> { | ||
this.build(correctPage - 1); | ||
return true; | ||
}).build()); | ||
|
||
panelBuilder.item(4, | ||
new PanelItemBuilder(). | ||
icon(Material.OAK_DOOR). | ||
name(this.user.getTranslation("challenges.gui.buttons.return")). | ||
clickHandler( (panel, user1, clickType, slot) -> { | ||
this.consumer.accept(false, null); | ||
return true; | ||
}).build()); | ||
|
||
panelBuilder.item(5, | ||
new PanelItemBuilder(). | ||
icon(Material.SIGN). | ||
name(this.user.getTranslation("challenges.gui.buttons.next")). | ||
clickHandler( (panel, user1, clickType, slot) -> { | ||
this.build(correctPage + 1); | ||
return true; | ||
}).build()); | ||
|
||
int challengesIndex = MAX_ELEMENTS * correctPage; | ||
|
||
// I want first row to be only for navigation and return button. | ||
int index = 9; | ||
|
||
while (challengesIndex < ((correctPage + 1) * MAX_ELEMENTS) && | ||
challengesIndex < this.challengesList.size()) | ||
{ | ||
panelBuilder.item(index++, this.createChallengeButton(this.challengesList.get(challengesIndex++))); | ||
} | ||
|
||
panelBuilder.build(); | ||
|
||
panelBuilder.build(); | ||
} | ||
|
||
|
||
/** | ||
* This method builds PanelItem for given challenge. | ||
* @param challenge Challenge which PanelItem must be created. | ||
* @return new PanelItem for given Challenge. | ||
*/ | ||
private PanelItem createChallengeButton(Challenges challenge) | ||
{ | ||
return new PanelItemBuilder(). | ||
name(challenge.getFriendlyName()). | ||
description(challenge.getDescription()). | ||
icon(challenge.getIcon()). | ||
clickHandler((panel, user1, clickType, slot) -> { | ||
this.consumer.accept(true, challenge); | ||
return true; | ||
}).build(); | ||
} | ||
|
||
|
||
// --------------------------------------------------------------------- | ||
// Section: Variables | ||
// --------------------------------------------------------------------- | ||
|
||
|
||
/** | ||
* This variable stores consumer. | ||
*/ | ||
private BiConsumer<Boolean, Challenges> consumer; | ||
|
||
/** | ||
* User who runs GUI. | ||
*/ | ||
private User user; | ||
|
||
/** | ||
* Current value. | ||
*/ | ||
private List<Challenges> challengesList; | ||
} |