Challenges/src/main/java/world/bentobox/challenges/panel/admin/ListChallengesGUI.java

208 lines
6.5 KiB
Java
Raw Normal View History

package world.bentobox.challenges.panel.admin;
Bug fix release (#228) * Fix issue when users could not select non-block items as icons for challenges and levels. (#190) * English update (#193) * Organized imports * Minor code cleanup * Updated English locale file. * Translate zh-CN.yml via GitLocalize (#188) * Make default translation looking a bit nicer (#192) * Make default translation looking a bit nicer * Updating a few friendly names and rewording some phrases * Add generic .gitignore * Fix novice level Update `chiseledmaker` name in `novice` level. * Adapt literal style (#197) Improve translations and process as a YAML string. * Test coverage (#199) * Test coverage for Challenges Command * Added CompleteChallengeCommand test class * Added Utils test class * Added ChallengesGUI test class * Fix code smells from sonarcloud analysis * Added .gitignore * Added Travis CI config file * WIP ChallengesManager Test class * Added ChallengesManager test class * Removed debug * Removed code smells. * Added ChallengesAddon test class. * Added onDisbale test * Added new TryToComplete test class - WIP Covers inventory challenges. * Added Island Challenge entity tests to TryToComplete test class * Fix a bug with challenge deletion. If challenge has been left in a level, then system did not remove challenge from it and was kept as ghost challenge, preventing from completing level. * Fixes tests * Updated travis.yml * All strings to spanish (#200) * Translate es.yml via GitLocalize * Translate es.yml via GitLocalize * Translate es.yml via GitLocalize * Fix LevelListRequestHandler. This handler did not return list of strings but list of challenge levels, that is incorrect. Not it should work correctly. * Create ro.yml * Create id.yml * Remove blanks files now that GitLocalize is fixed. * Initial Russian translation (#207) * Translate ru.yml via GitLocalize Co-authored-by: @mt-gitlocalize @IPeredero @LoveBiscuit * Changed build character from # to b * Add German translation (#210) * Translate de.yml via GitLocalize * Translate de.yml via GitLocalize * Translate de.yml via GitLocalize Co-authored-by: xXjojojXx <36734820+xXjojojXx@users.noreply.github.com> Co-authored-by: FunnysBanana <51290016+FunnysBanana@users.noreply.github.com> Co-authored-by: mt-gitlocalize <mt@gitlocalize.com> * Czech translation. Credit @Polda18 * Added a uniqueId sanitization when creating challenges/levels This will help fixing issues with spaces, hyphens and accents in non-English languages. * Fixes bug with checking entities in nether and end (#219) https://github.com/BentoBoxWorld/Challenges/issues/218 Adds test case to check for compliance. * Add 7 new placeholders for Challenges Addon. - `[gamemode]_challenge_total_completion_count` returns number of sum of challenge completions for user. - `[gamemode]_challenge_completed_count` returns number of completed challenges (at least once) for user. - `[gamemode]_challenge_uncompleted_count` returns number of uncompleted challenges for user. - `[gamemode]_challenge_completed_level_count` returns number of completed levels for user. - `[gamemode]_challenge_uncompleted_level_count` returns number of uncompleted levels for user. - `[gamemode]_challenge_unlocked_level_count` returns number of unlocked levels for user. - `[gamemode]_challenge_locked_level_count` returns number of locked levels for user. Fixes #224 * Add 2 new placeholders: - `[gamemode]_challenge_latest_level_name` returns latest unlocked challenge level name - `[gamemode]_challenge_latest_level_id` returns latest unlocked challenge level id Fixes #226 * Fix broken tests due to placeholder additions. https://github.com/BentoBoxWorld/Challenges/commit/b5ecffb725d78c154f469d7e2868574ca7ecff30 https://github.com/BentoBoxWorld/Challenges/commit/2958ca8b6c98f9c8d389aa93c4a2846f578fff43 * Added default perms for aoneblock * Downgrade to 0.8.1 version * Add option to quit from conversation by writing "cancel" in chat. Move sanitizeInput to a GuiUtil class. * Change latest version to 0.8.1
2020-04-22 00:19:46 +02:00
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
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.ChallengesAddon;
import world.bentobox.challenges.database.object.Challenge;
import world.bentobox.challenges.panel.CommonGUI;
import world.bentobox.challenges.panel.util.ConfirmationGUI;
import world.bentobox.challenges.utils.GuiUtils;
/**
* This class contains all necessary elements to create GUI that lists all challenges.
* It allows to edit them or remove, depending on given input mode.
*/
public class ListChallengesGUI extends CommonGUI
{
// ---------------------------------------------------------------------
// Section: Constructor
// ---------------------------------------------------------------------
/**
* @param addon Addon where panel operates.
* @param world World from which panel was created.
* @param user User who created panel.
* @param topLabel Command top label which creates panel (f.e. island or ai)
* @param permissionPrefix Command permission prefix (f.e. bskyblock.)
* @param mode - mode that indicate what should do icon clicking.
*/
public ListChallengesGUI(ChallengesAddon addon,
World world,
User user,
Mode mode,
String topLabel,
String permissionPrefix)
{
this(addon, world, user, mode, topLabel, permissionPrefix, null);
}
/**
* @param addon Addon where panel operates.
* @param world World from which panel was created.
* @param user User who created panel.
* @param topLabel Command top label which creates panel (f.e. island or ai)
* @param permissionPrefix Command permission prefix (f.e. bskyblock.)
* @param mode - mode that indicate what should do icon clicking.
*/
public ListChallengesGUI(ChallengesAddon addon,
World world,
User user,
Mode mode,
String topLabel,
String permissionPrefix,
CommonGUI parentGUI)
{
super(addon, world, user, topLabel, permissionPrefix, parentGUI);
this.currentMode = mode;
}
// ---------------------------------------------------------------------
// Section: Methods
// ---------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public void build()
{
PanelBuilder panelBuilder = new PanelBuilder().user(this.user).name(
this.user.getTranslation("challenges.gui.title.admin.choose-challenge-title"));
if (this.currentMode.equals(Mode.DELETE))
{
GuiUtils.fillBorder(panelBuilder, Material.RED_STAINED_GLASS_PANE);
}
else
{
GuiUtils.fillBorder(panelBuilder);
}
List<Challenge> challengeList = this.addon.getChallengesManager().getAllChallenges(this.world);
final int MAX_ELEMENTS = 21;
if (this.pageIndex < 0)
{
this.pageIndex = challengeList.size() / MAX_ELEMENTS;
}
else if (this.pageIndex > (challengeList.size() / MAX_ELEMENTS))
{
this.pageIndex = 0;
}
int challengeIndex = MAX_ELEMENTS * this.pageIndex;
// I want first row to be only for navigation and return button.
int index = 10;
while (challengeIndex < ((this.pageIndex + 1) * MAX_ELEMENTS) &&
challengeIndex < challengeList.size() &&
index < 36)
{
if (!panelBuilder.slotOccupied(index))
{
panelBuilder.item(index, this.createChallengeIcon(challengeList.get(challengeIndex++)));
}
index++;
}
// Navigation buttons only if necessary
if (challengeList.size() > MAX_ELEMENTS)
{
panelBuilder.item(18, this.getButton(CommonButtons.PREVIOUS));
panelBuilder.item(26, this.getButton(CommonButtons.NEXT));
}
panelBuilder.item(44, this.returnButton);
panelBuilder.build();
}
/**
* This method creates button for given challenge.
* @param challenge Challenge which button must be created.
* @return Challenge button.
*/
private PanelItem createChallengeIcon(Challenge challenge)
{
PanelItemBuilder itemBuilder = new PanelItemBuilder().
name(ChatColor.translateAlternateColorCodes('&', challenge.getFriendlyName())).
description(GuiUtils.stringSplit(this.generateChallengeDescription(challenge, this.user.getPlayer()),
this.addon.getChallengesSettings().getLoreLineLength())).
icon(challenge.getIcon()).
glow(challenge.isDeployed());
if (this.currentMode.equals(Mode.EDIT))
{
itemBuilder.clickHandler((panel, user1, clickType, i) -> {
new EditChallengeGUI(this.addon,
this.world,
this.user,
challenge,
this.topLabel,
this.permissionPrefix,
this).build();
return true;
});
}
else if (this.currentMode.equals(Mode.DELETE))
{
itemBuilder.clickHandler((panel, user1, clickType, i) -> {
new ConfirmationGUI(this.user, value -> {
if (value)
{
this.addon.getChallengesManager().deleteChallenge(challenge);
}
this.build();
});
return true;
});
}
return itemBuilder.build();
}
// ---------------------------------------------------------------------
// Section: Enums
// ---------------------------------------------------------------------
/**
* Mode in which gui icons should processed.
*/
public enum Mode
{
EDIT,
DELETE
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* Current mode in which icons will act.
*/
private Mode currentMode;
}