mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-11-24 11:36:53 +01:00
Create SelectChallengeGUI that allows to choose one challenge from input challenge list and return it into GUI.
This commit is contained in:
parent
9415452e33
commit
6125eb5d34
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user