WIP moved to admin challenges command

Adding a management GUI
This commit is contained in:
tastybento 2018-08-12 03:04:30 +09:00
parent 371dafa32a
commit c22fd34fbe
10 changed files with 413 additions and 20 deletions

View File

@ -26,7 +26,8 @@ challenges:
name: "Challenge Name"
name-has-completed: "[name] has completed the [challenge] challenge!"
navigation: "Click to see [level] challenges!"
not-repeatable: "This Challenge is not repeatable!"
repeatable: "This challenge can be repeated [maxtimes] times"
not-repeatable: "This challenge is not repeatable!"
repeat-rewards: "&6Repeat reward(s)"
rewards: "&FReward(s)"
to-complete: "Complete [challengesToDo] more [thisLevel] challenges to unlock this level!"
@ -87,4 +88,5 @@ challenges:
error:
no-such-challenge: "&cNo such challenge name"
description: "sets the challenge icon to inhand item"
parameters: "[challenge name]"
parameters: "[challenge name]"
gui-title: "&aChallenges Admin"

View File

@ -50,7 +50,7 @@
<dependency>
<groupId>world.bentobox</groupId>
<artifactId>bentobox</artifactId>
<version>FC-0.9</version>
<version>FC-0.92</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -3,7 +3,7 @@ package bentobox.addon.challenges;
import org.bukkit.Bukkit;
import bentobox.addon.challenges.commands.ChallengesCommand;
import bentobox.addon.challenges.commands.admin.ChallengesAdminImportCommand;
import bentobox.addon.challenges.commands.admin.Challenges;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -40,7 +40,8 @@ public class ChallengesAddon extends Addon {
if (acidIslandCmd != null) {
new ChallengesCommand(this, acidIslandCmd);
CompositeCommand acidCmd = getPlugin().getCommandsManager().getCommand("acid");
new ChallengesAdminImportCommand(this, acidCmd);
new Challenges(this, acidCmd);
}
});
this.getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> {
@ -49,7 +50,7 @@ public class ChallengesAddon extends Addon {
if (bsbIslandCmd != null) {
new ChallengesCommand(this, bsbIslandCmd);
CompositeCommand bsbAdminCmd = getPlugin().getCommandsManager().getCommand("bsbadmin");
new ChallengesAdminImportCommand(this, bsbAdminCmd);
new Challenges(this, bsbAdminCmd);
}
});
});

View File

@ -182,7 +182,6 @@ public class ChallengesManager {
/**
* Get the list of all challenge unique names.
* Used for checking admin commands and tab complete
* @return List of challenge names
*/
public List<String> getAllChallengesList() {

View File

@ -3,24 +3,22 @@ package bentobox.addon.challenges.commands;
import java.util.List;
import bentobox.addon.challenges.ChallengesAddon;
import bentobox.addon.challenges.panel.ChallengesPanels;
import bentobox.addon.challenges.panel.ChallengesPanels2;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
public class ChallengesCommand extends CompositeCommand {
public static final String CHALLENGE_COMMAND = "challenges";
private ChallengesAddon addon;
public ChallengesCommand(ChallengesAddon addon, CompositeCommand cmd) {
super(cmd, CHALLENGE_COMMAND);
this.addon = addon;
super(addon, cmd, CHALLENGE_COMMAND);
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Open up the challenges GUI
if (user.isPlayer()) {
new ChallengesPanels(addon, user, args.isEmpty() ? "" : args.get(0), getWorld(), getPermissionPrefix(), getTopLabel());
new ChallengesPanels2((ChallengesAddon) getAddon(), user, args.isEmpty() ? "" : args.get(0), getWorld(), getPermissionPrefix(), getTopLabel(), false);
return true;
}
return false;

View File

@ -0,0 +1,41 @@
package bentobox.addon.challenges.commands.admin;
import java.util.List;
import bentobox.addon.challenges.ChallengesAddon;
import bentobox.addon.challenges.panel.ChallengesPanels2;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
public class Challenges extends CompositeCommand {
/**
* Admin command for challenges
* @param parent
*/
public Challenges(ChallengesAddon addon, CompositeCommand parent) {
super(addon, parent, "challenges");
}
@Override
public void setup() {
this.setPermission("admin.challenges");
this.setParametersHelp("challeneges.admin.parameters");
this.setDescription("challenges.admin.description");
// Register sub commands
new ImportCommand(getAddon(), this);
new ShowChallenges(getAddon(), this);
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Open up the admin challenges GUI
if (user.isPlayer()) {
new ChallengesPanels2((ChallengesAddon) getAddon(), user, args.isEmpty() ? "" : args.get(0), getWorld(), getPermissionPrefix(), getTopLabel(), true);
return true;
}
return false;
}
}

View File

@ -4,34 +4,34 @@ import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.bukkit.Bukkit;
import bentobox.addon.challenges.ChallengesAddon;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
public class ChallengesAdminImportCommand extends CompositeCommand {
private ChallengesAddon addon;
public class ImportCommand extends CompositeCommand {
/**
* Import challenges
* @param addon
* @param cmd
*/
public ChallengesAdminImportCommand(ChallengesAddon addon, CompositeCommand cmd) {
super(cmd, "cimport");
this.addon = addon;
public ImportCommand(Addon addon, CompositeCommand cmd) {
super(addon, cmd, "cimport");
}
@Override
public boolean execute(User user, String label, List<String> args) {
return addon.getImportManager().importChallenges(user, getWorld(), !args.isEmpty() && args.get(0).equalsIgnoreCase("overwrite"));
return ((ChallengesAddon)getAddon()).getImportManager().importChallenges(user, getWorld(), !args.isEmpty() && args.get(0).equalsIgnoreCase("overwrite"));
}
@Override
public void setup() {
this.setPermission("challenges.admin");
this.setParameters("challenges.admin.import.parameters");
this.setParametersHelp("challenges.admin.import.parameters");
this.setDescription("challenges.admin.import.description");
}

View File

@ -0,0 +1,35 @@
package bentobox.addon.challenges.commands.admin;
import java.util.List;
import bentobox.addon.challenges.ChallengesAddon;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
public class ShowChallenges extends CompositeCommand {
/**
* Admin command to show challenges and manage them
* @param parent
*/
public ShowChallenges(Addon addon, CompositeCommand parent) {
super(addon, parent, "show");
}
@Override
public void setup() {
this.setPermission("admin.challenges");
this.setParametersHelp("challaneges.admin.show.parameters");
this.setDescription("challenges.admin.show.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
((ChallengesAddon)getAddon()).getChallengesManager().getAllChallengesList().forEach(user::sendRawMessage);
return true;
}
}

View File

@ -0,0 +1,49 @@
package bentobox.addon.challenges.panel;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import bentobox.addon.challenges.ChallengesAddon;
import bentobox.addon.challenges.ChallengesManager;
import bentobox.addon.challenges.database.object.Challenges;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
public class AdminGUI {
private ChallengesAddon addon;
private User player;
private Challenges challenge;
private World world;
private String permPrefix;
private String label;
/**
* Shows the admin panel for the challenge
* @param addon
* @param player
* @param challenge
* @param world
* @param permPrefix
* @param label
*/
public AdminGUI(ChallengesAddon addon, User player, Challenges challenge, World world,
String permPrefix, String label) {
super();
this.addon = addon;
this.player = player;
this.challenge = challenge;
this.world = world;
this.permPrefix = permPrefix;
this.label = label;
new PanelBuilder().size(27).user(player).name(player.getTranslation("challenges.admin.gui-title"))
.item(new PanelItemBuilder().icon(challenge.getIcon()).name("Icon").build())
.item(9, new PanelItemBuilder().icon(new ItemStack(Material.WHITE_BANNER)).name("Description").description(challenge.getDescription()).build())
.item(18, new PanelItemBuilder().icon(new ItemStack(Material.GREEN_STAINED_GLASS_PANE)).name("Active").build()).build();
}
}

View File

@ -0,0 +1,268 @@
package bentobox.addon.challenges.panel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import bentobox.addon.challenges.ChallengesAddon;
import bentobox.addon.challenges.ChallengesManager;
import bentobox.addon.challenges.LevelStatus;
import bentobox.addon.challenges.commands.ChallengesCommand;
import bentobox.addon.challenges.database.object.Challenges;
import bentobox.addon.challenges.database.object.Challenges.ChallengeType;
import world.bentobox.bentobox.api.panels.Panel;
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;
public class ChallengesPanels2 {
private ChallengesAddon addon;
private ChallengesManager manager;
private User user;
private String level;
private World world;
private String permPrefix;
private String label;
private boolean admin;
public ChallengesPanels2(ChallengesAddon addon, User user, String level, World world, String permPrefix, String label, boolean admin) {
this.addon = addon;
this.manager = addon.getChallengesManager();
this.user = user;
this.world = world;
this.permPrefix = permPrefix;
this.label = label;
this.admin = admin;
if (manager.getChallengeList().isEmpty()) {
addon.getLogger().severe("There are no challenges set up!");
user.sendMessage("general.errors.general");
return;
}
if (level.isEmpty()) {
level = manager.getChallengeList().keySet().iterator().next().getUniqueId();
}
this.level = level;
// Check if level is valid
if (!admin && !manager.isLevelUnlocked(user, level, world)) {
return;
}
PanelBuilder panelBuilder = new PanelBuilder()
.name(admin ? user.getTranslation("challenges.admin.gui-title") : user.getTranslation("challenges.gui-title"));
addChallengeItems(panelBuilder);
addNavigation(panelBuilder);
addFreeChallanges(panelBuilder);
// Create the panel
Panel panel = panelBuilder.build();
panel.open(user);
}
private void addChallengeItems(PanelBuilder panelBuilder) {
Set<Challenges> levelChallenges = manager.getChallenges(level, world);
// Only show a control panel for the level requested.
for (Challenges challenge : levelChallenges) {
createItem(panelBuilder, challenge);
}
}
private void addFreeChallanges(PanelBuilder panelBuilder) {
manager.getChallenges(ChallengesManager.FREE, world).forEach(challenge -> createItem(panelBuilder, challenge));
}
/**
* Creates a panel item for challenge if appropriate and adds it to panelBuilder
* @param panelBuilder
* @param challenge
* @param user
*/
private void createItem(PanelBuilder panelBuilder, Challenges challenge) {
// For admin, glow means activated. For user, glow means done
boolean glow = admin ? challenge.isDeployed() : manager.isChallengeComplete(user, challenge.getUniqueId(), world);
// If not admin and challenge is removed after completion, remove it
if (!admin && glow && challenge.isRemoveWhenCompleted()) {
return;
}
PanelItemBuilder itemBuilder = new PanelItemBuilder()
.icon(challenge.getIcon())
.name(challenge.getFriendlyName().isEmpty() ? challenge.getUniqueId() : challenge.getFriendlyName())
.description(challengeDescription(challenge))
.glow(glow);
if (admin) {
// Admin click
itemBuilder.clickHandler((panel, player, c, s) -> {
if (!challenge.getChallengeType().equals(ChallengeType.ICON)) {
new AdminGUI(addon, player, challenge, world, permPrefix, label);
}
return true;
});
} else {
// Player click
itemBuilder.clickHandler((panel, player, c, s) -> {
if (!challenge.getChallengeType().equals(ChallengeType.ICON)) {
new TryToComplete(addon, player, manager, challenge, world, permPrefix, label);
}
return true;
});
}
// If the challenge has a specific slot allocated, use it
if (challenge.getSlot() >= 0) {
panelBuilder.item(challenge.getSlot(),itemBuilder.build());
} else {
panelBuilder.item(itemBuilder.build());
}
}
private void addNavigation(PanelBuilder panelBuilder) {
// Add navigation to other levels
for (LevelStatus status: manager.getChallengeLevelStatus(user, world)) {
if (status.getLevel().getUniqueId().equals(level)) {
// Skip if this is the current level
continue;
}
// Create a nice name for the level
String name = status.getLevel().getFriendlyName().isEmpty() ? status.getLevel().getUniqueId() : status.getLevel().getFriendlyName();
if (admin || status.isUnlocked()) {
// Clicking on this icon will open up this level's challenges
PanelItem item = new PanelItemBuilder()
.icon(new ItemStack(Material.ENCHANTED_BOOK))
.name(name)
.description(manager.stringSplit(user.getTranslation("challenges.navigation","[level]",name)))
.clickHandler((p, u, c, s) -> {
u.closeInventory();
u.performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + status.getLevel().getUniqueId());
return true;
})
.build();
panelBuilder.item(item);
} else {
// Clicking on this icon will do nothing because the challenge is not unlocked yet
String previousLevelName = status.getPreviousLevel().getFriendlyName().isEmpty() ? status.getPreviousLevel().getUniqueId() : status.getPreviousLevel().getFriendlyName();
PanelItem item = new PanelItemBuilder()
.icon(new ItemStack(Material.BOOK))
.name(name)
.description(manager.stringSplit(user.getTranslation("challenges.to-complete", "[challengesToDo]",String.valueOf(status.getNumberOfChallengesStillToDo()), "[thisLevel]", previousLevelName)))
.build();
panelBuilder.item(item);
}
}
}
/**
* Creates the challenge description for the "item" in the inventory
*
* @param challenge
* @param player
* @return List of strings splitting challenge string into 25 chars long
*/
private List<String> challengeDescription(Challenges challenge) {
List<String> result = new ArrayList<String>();
String level = challenge.getLevel();
if (!level.isEmpty()) {
result.addAll(splitTrans(user, "challenges.level", "[level]", level));
}
if (admin) {
if ((!challenge.getChallengeType().equals(ChallengeType.INVENTORY) || !challenge.isRepeatable())) {
result.addAll(splitTrans(user, "challenges.not-repeatable"));
} else {
result.addAll(splitTrans(user, "challenges.repeatable", "[maxtimes]", String.valueOf(challenge.getMaxTimes())));
}
if (challenge.getChallengeType().equals(ChallengeType.INVENTORY) && challenge.isTakeItems()) {
result.addAll(splitTrans(user, "challenges.item-take-warning"));
}
result.addAll(addRewards(challenge, true, true));
} else {
// Check if completed or not
boolean complete = addon.getChallengesManager().isChallengeComplete(user, challenge.getUniqueId(), world);
int maxTimes = challenge.getMaxTimes();
long doneTimes = addon.getChallengesManager().checkChallengeTimes(user, challenge, world);
if (complete) {
result.add(user.getTranslation("challenges.complete"));
}
if (challenge.isRepeatable()) {
if (maxTimes == 0) {
// Check if the player has maxed out the challenge
if (doneTimes < maxTimes) {
result.addAll(splitTrans(user, "challenges.completed-times","[donetimes]", String.valueOf(doneTimes),"[maxtimes]", String.valueOf(maxTimes)));
} else {
result.addAll(splitTrans(user, "challenges.maxed-reached","[donetimes]", String.valueOf(doneTimes),"[maxtimes]", String.valueOf(maxTimes)));
}
}
}
if (!complete || (complete && challenge.isRepeatable())) {
result.addAll(challenge.getDescription());
if (challenge.getChallengeType().equals(ChallengeType.INVENTORY)) {
if (challenge.isTakeItems()) {
result.addAll(splitTrans(user, "challenges.item-take-warning"));
}
} else if (challenge.getChallengeType().equals(ChallengeType.ISLAND)) {
result.addAll(splitTrans(user, "challenges.items-closeby"));
}
}
if (complete && (!challenge.getChallengeType().equals(ChallengeType.INVENTORY) || !challenge.isRepeatable())) {
result.addAll(splitTrans(user, "challenges.not-repeatable"));
result.replaceAll(x -> x.replace("[label]", label));
return result;
}
result.addAll(addRewards(challenge, complete, false));
}
// Final placeholder change for [label]
result.replaceAll(x -> x.replace("[label]", label));
return result;
}
private List<String> addRewards(Challenges challenge, boolean complete, boolean admin) {
List<String> result = new ArrayList<>();
double moneyReward = 0;
int expReward = 0;
String rewardText = "";
if (admin || !complete) {
// First time
moneyReward = challenge.getMoneyReward();
rewardText = challenge.getRewardText();
expReward = challenge.getExpReward();
if (!rewardText.isEmpty()) {
result.addAll(splitTrans(user, "challenges.first-time-rewards"));
}
}
if (admin || complete){
// Repeat challenge
moneyReward = challenge.getRepeatMoneyReward();
rewardText = challenge.getRepeatRewardText();
expReward = challenge.getRepeatExpReward();
if (!rewardText.isEmpty()) {
result.addAll(splitTrans(user, "challenges.repeat-rewards"));
}
}
if (!rewardText.isEmpty()) {
result.addAll(splitTrans(user,rewardText));
}
if (expReward > 0) {
result.addAll(splitTrans(user,"challenges.exp-reward", "[reward]", String.valueOf(expReward)));
}
if (addon.getPlugin().getSettings().isUseEconomy() && moneyReward > 0) {
result.addAll(splitTrans(user,"challenges.money-reward", "[reward]", String.valueOf(moneyReward)));
}
return result;
}
private Collection<? extends String> splitTrans(User user, String string, String...strings) {
return addon.getChallengesManager().stringSplit(user.getTranslation(string, strings));
}
}