Rework ChallengesManager.

Implement all methods that were only placeholders.
Remove all unnecessary old methods.
Implement new methods in all classes.

User and Admin command now opens new GUI.
This commit is contained in:
BONNe1704 2019-01-23 17:30:30 +02:00
parent 1bc6bb2b76
commit 590b3f114c
21 changed files with 981 additions and 680 deletions

View File

@ -97,7 +97,7 @@ public class ChallengesImportManager
challengeLevel.setRewardExperience(unlock.getInt("expReward")); challengeLevel.setRewardExperience(unlock.getInt("expReward"));
challengeLevel.setRewardCommands(unlock.getStringList("commands")); challengeLevel.setRewardCommands(unlock.getStringList("commands"));
} }
addon.getChallengesManager().storeLevel(challengeLevel); addon.getChallengesManager().loadLevel(challengeLevel, overwrite, user, false);
} }
} else { } else {
user.sendMessage("challenges.admin.import.no-levels"); user.sendMessage("challenges.admin.import.no-levels");
@ -118,7 +118,7 @@ public class ChallengesImportManager
newChallenge.setDeployed(true); newChallenge.setDeployed(true);
ConfigurationSection details = chals.getConfigurationSection(challenge); ConfigurationSection details = chals.getConfigurationSection(challenge);
newChallenge.setFriendlyName(details.getString("friendlyname", challenge)); newChallenge.setFriendlyName(details.getString("friendlyname", challenge));
newChallenge.setDescription(addon.getChallengesManager().stringSplit(details.getString("description", ""))); newChallenge.setDescription(GuiUtils.stringSplit(details.getString("description", "")));
newChallenge.setIcon(ItemParser.parse(details.getString("icon") + ":1")); newChallenge.setIcon(ItemParser.parse(details.getString("icon") + ":1"));
newChallenge.setChallengeType(Challenge.ChallengeType.valueOf(details.getString("type","INVENTORY").toUpperCase())); newChallenge.setChallengeType(Challenge.ChallengeType.valueOf(details.getString("type","INVENTORY").toUpperCase()));
newChallenge.setTakeItems(details.getBoolean("takeItems",true)); newChallenge.setTakeItems(details.getBoolean("takeItems",true));
@ -149,7 +149,7 @@ public class ChallengesImportManager
this.addon.getChallengesManager().addChallengeToLevel(newChallenge, this.addon.getChallengesManager().addChallengeToLevel(newChallenge,
addon.getChallengesManager().getLevel(Util.getWorld(world).getName() + "_" + details.getString("level"))); addon.getChallengesManager().getLevel(Util.getWorld(world).getName() + "_" + details.getString("level")));
if (addon.getChallengesManager().storeChallenge(newChallenge, overwrite, user, false)) { if (addon.getChallengesManager().loadChallenge(newChallenge, overwrite, user, false)) {
size++; size++;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -3,10 +3,10 @@ package world.bentobox.challenges.commands;
import java.util.List; import java.util.List;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.panel.ChallengesPanels2;
import world.bentobox.challenges.panel.ChallengesPanels2.Mode;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.user.ChallengesGUI;
public class ChallengesCommand extends CompositeCommand { public class ChallengesCommand extends CompositeCommand {
public static final String CHALLENGE_COMMAND = "challenges"; public static final String CHALLENGE_COMMAND = "challenges";
@ -19,7 +19,11 @@ public class ChallengesCommand extends CompositeCommand {
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
// Open up the challenges GUI // Open up the challenges GUI
if (user.isPlayer()) { if (user.isPlayer()) {
new ChallengesPanels2((ChallengesAddon) getAddon(), user, user, args.isEmpty() ? "" : args.get(0), getWorld(), getPermissionPrefix(), getTopLabel(), Mode.PLAYER); new ChallengesGUI((ChallengesAddon) this.getAddon(),
this.getWorld(),
user,
this.getTopLabel(),
this.getPermissionPrefix()).build();
return true; return true;
} }
// Show help // Show help

View File

@ -3,10 +3,10 @@ package world.bentobox.challenges.commands.admin;
import java.util.List; import java.util.List;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.panel.ChallengesPanels2;
import world.bentobox.challenges.panel.ChallengesPanels2.Mode;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.admin.AdminGUI;
public class Challenges extends CompositeCommand { public class Challenges extends CompositeCommand {
@ -25,7 +25,7 @@ public class Challenges extends CompositeCommand {
this.setDescription("challenges.admin.description"); this.setDescription("challenges.admin.description");
// Register sub commands // Register sub commands
new ImportCommand(getAddon(), this); new ImportCommand(getAddon(), this);
new CompleteChallenge(getAddon(), this); // new CompleteChallenge(getAddon(), this);
new ReloadChallenges(getAddon(), this); new ReloadChallenges(getAddon(), this);
new ResetChallenge(getAddon(), this); new ResetChallenge(getAddon(), this);
//new ShowChallenges(getAddon(), this); //new ShowChallenges(getAddon(), this);
@ -37,7 +37,12 @@ public class Challenges extends CompositeCommand {
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
// Open up the admin challenges GUI // Open up the admin challenges GUI
if (user.isPlayer()) { if (user.isPlayer()) {
new ChallengesPanels2((ChallengesAddon) getAddon(), user, user, args.isEmpty() ? "" : args.get(0), getWorld(), getPermissionPrefix(), getTopLabel(), Mode.ADMIN); new AdminGUI((ChallengesAddon) this.getAddon(),
this.getWorld(),
user,
this.getTopLabel(),
this.getPermissionPrefix()).build();
return true; return true;
} }
return false; return false;

View File

@ -51,13 +51,13 @@ public class CompleteChallenge extends CompositeCommand {
return false; return false;
} }
// Check for valid challenge name // Check for valid challenge name
if (!manager.isChallenge(getWorld(), args.get(1))) { if (!manager.containsChallenge(args.get(1))) {
user.sendMessage("challenges.admin.complete.unknown-challenge"); user.sendMessage("challenges.admin.complete.unknown-challenge");
return false; return false;
} }
// Complete challenge // Complete challenge
User target = User.getInstance(targetUUID); User target = User.getInstance(targetUUID);
manager.setChallengeComplete(target, args.get(1), getWorld()); manager.setChallengeComplete(target, this.manager.getChallenge(args.get(1)));
user.sendMessage("general.success"); user.sendMessage("general.success");
return true; return true;
} }
@ -70,7 +70,7 @@ public class CompleteChallenge extends CompositeCommand {
return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg)); return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg));
} else if (args.size() == 4) { } else if (args.size() == 4) {
// Challenges in this world // Challenges in this world
return Optional.of(Util.tabLimit(manager.getAllChallengesList(getWorld()), lastArg)); return Optional.of(Util.tabLimit(manager.getAllChallengesNames(getWorld()), lastArg));
} }
return Optional.empty(); return Optional.empty();
} }

View File

@ -56,13 +56,13 @@ public class ResetChallenge extends CompositeCommand {
return false; return false;
} }
// Check for valid challenge name // Check for valid challenge name
if (!manager.isChallenge(getWorld(), args.get(1))) { if (!manager.containsChallenge(args.get(1))) {
user.sendMessage("challenges.admin.complete.unknown-challenge"); user.sendMessage("challenges.admin.complete.unknown-challenge");
return false; return false;
} }
// Complete challenge // Complete challenge
User target = User.getInstance(targetUUID); User target = User.getInstance(targetUUID);
manager.setResetChallenge(target, args.get(1), getWorld()); manager.resetChallenge(target, manager.getChallenge(args.get(1)));
user.sendMessage("general.success"); user.sendMessage("general.success");
return true; return true;
} }
@ -75,7 +75,7 @@ public class ResetChallenge extends CompositeCommand {
return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg)); return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg));
} else if (args.size() == 4) { } else if (args.size() == 4) {
// Challenges in this world // Challenges in this world
return Optional.of(Util.tabLimit(manager.getAllChallengesList(getWorld()), lastArg)); return Optional.of(Util.tabLimit(manager.getAllChallengesNames(getWorld()), lastArg));
} }
return Optional.empty(); return Optional.empty();
} }

View File

@ -28,7 +28,7 @@ public class ShowChallenges extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
((ChallengesAddon)getAddon()).getChallengesManager().getAllChallengesList().forEach(user::sendRawMessage); ((ChallengesAddon)getAddon()).getChallengesManager().getAllChallengesNames(this.getWorld()).forEach(user::sendRawMessage);
return true; return true;
} }

View File

@ -77,7 +77,7 @@ public class SurroundChallengeBuilder {
} }
public boolean build() { public boolean build() {
return addon.getChallengesManager().createSurroundingChallenge(this); return false; //addon.getChallengesManager().createSurroundingChallenge(this);
} }

View File

@ -7,6 +7,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason; import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
@ -27,7 +28,7 @@ public class ResetListener implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onIslandReset(IslandEvent e) { public void onIslandReset(IslandEvent e) {
if (e.getReason().equals(Reason.CREATED) || (addon.getChallengesSettings().isResetChallenges() && e.getReason().equals(Reason.RESETTED))) { if (e.getReason().equals(Reason.CREATED) || (addon.getChallengesSettings().isResetChallenges() && e.getReason().equals(Reason.RESETTED))) {
addon.getChallengesManager().resetAllChallenges(e.getOwner(), e.getLocation().getWorld()); addon.getChallengesManager().resetAllChallenges(User.getInstance(e.getOwner()), e.getLocation().getWorld());
} }
} }
} }

View File

@ -23,7 +23,7 @@ public class SaveListener implements Listener
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onWorldSave(WorldSaveEvent e) public void onWorldSave(WorldSaveEvent e)
{ {
if (!this.addon.getChallengesManager().getAllChallengesList(e.getWorld()).isEmpty()) if (!this.addon.getChallengesManager().getAllChallenges(e.getWorld()).isEmpty())
{ {
this.addon.getChallengesManager().save(); this.addon.getChallengesManager().save();
} }

View File

@ -11,6 +11,8 @@ import org.bukkit.inventory.ItemStack;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.ChallengesManager; import world.bentobox.challenges.ChallengesManager;
import world.bentobox.challenges.database.object.ChallengeLevel;
import world.bentobox.challenges.utils.GuiUtils;
import world.bentobox.challenges.utils.LevelStatus; import world.bentobox.challenges.utils.LevelStatus;
import world.bentobox.challenges.commands.ChallengesCommand; import world.bentobox.challenges.commands.ChallengesCommand;
import world.bentobox.challenges.database.object.Challenge; import world.bentobox.challenges.database.object.Challenge;
@ -30,7 +32,7 @@ public class ChallengesPanels {
private ChallengesAddon addon; private ChallengesAddon addon;
private ChallengesManager manager; private ChallengesManager manager;
private User user; private User user;
private String level; private ChallengeLevel level;
private World world; private World world;
private String permPrefix; private String permPrefix;
private String label; private String label;
@ -43,17 +45,17 @@ public class ChallengesPanels {
this.permPrefix = permPrefix; this.permPrefix = permPrefix;
this.label = label; this.label = label;
if (manager.getChallengeList().isEmpty()) { if (manager.getAllChallenges(world).isEmpty()) {
addon.getLogger().severe("There are no challenges set up!"); addon.getLogger().severe("There are no challenges set up!");
user.sendMessage("general.errors.general"); user.sendMessage("general.errors.general");
return; return;
} }
if (level.isEmpty()) { if (level.isEmpty()) {
level = manager.getChallengeList().keySet().iterator().next().getUniqueId(); level = manager.getLevels(world).iterator().next().getUniqueId();
} }
this.level = level; this.level = this.manager.getLevel(level);
// Check if level is valid // Check if level is valid
if (!manager.isLevelUnlocked(user, level, world)) { if (!manager.isLevelUnlocked(user, world, this.level)) {
return; return;
} }
PanelBuilder panelBuilder = new PanelBuilder() PanelBuilder panelBuilder = new PanelBuilder()
@ -69,7 +71,7 @@ public class ChallengesPanels {
} }
private void addChallengeItems(PanelBuilder panelBuilder) { private void addChallengeItems(PanelBuilder panelBuilder) {
Set<Challenge> levelChallenges = manager.getChallenges(level, world); List<Challenge> levelChallenges = manager.getLevelChallenges(level);
// Only show a control panel for the level requested. // Only show a control panel for the level requested.
for (Challenge challenge : levelChallenges) { for (Challenge challenge : levelChallenges) {
createItem(panelBuilder, challenge); createItem(panelBuilder, challenge);
@ -77,7 +79,7 @@ public class ChallengesPanels {
} }
private void addFreeChallanges(PanelBuilder panelBuilder) { private void addFreeChallanges(PanelBuilder panelBuilder) {
manager.getChallenges(ChallengesManager.FREE, world).forEach(challenge -> createItem(panelBuilder, challenge)); manager.getFreeChallenges(world).forEach(challenge -> createItem(panelBuilder, challenge));
} }
@ -88,7 +90,7 @@ public class ChallengesPanels {
*/ */
private void createItem(PanelBuilder panelBuilder, Challenge challenge) { private void createItem(PanelBuilder panelBuilder, Challenge challenge) {
// Check completion // Check completion
boolean completed = manager.isChallengeComplete(user, challenge.getUniqueId(), world); boolean completed = manager.isChallengeComplete(user, challenge);
// If challenge is removed after completion, remove it // If challenge is removed after completion, remove it
if (completed && challenge.isRemoveWhenCompleted()) { if (completed && challenge.isRemoveWhenCompleted()) {
return; return;
@ -130,7 +132,7 @@ public class ChallengesPanels {
PanelItem item = new PanelItemBuilder() PanelItem item = new PanelItemBuilder()
.icon(new ItemStack(Material.ENCHANTED_BOOK)) .icon(new ItemStack(Material.ENCHANTED_BOOK))
.name(name) .name(name)
.description(manager.stringSplit(user.getTranslation("challenges.navigation","[level]",name))) .description(GuiUtils.stringSplit(user.getTranslation("challenges.navigation","[level]",name)))
.clickHandler((p, u, c, s) -> { .clickHandler((p, u, c, s) -> {
u.closeInventory(); u.closeInventory();
u.performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + status.getLevel().getUniqueId()); u.performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + status.getLevel().getUniqueId());
@ -144,7 +146,7 @@ public class ChallengesPanels {
PanelItem item = new PanelItemBuilder() PanelItem item = new PanelItemBuilder()
.icon(new ItemStack(Material.BOOK)) .icon(new ItemStack(Material.BOOK))
.name(name) .name(name)
.description(manager.stringSplit(user.getTranslation("challenges.to-complete", "[challengesToDo]",String.valueOf(previousStatus != null ? previousStatus.getNumberOfChallengesStillToDo() : ""), "[thisLevel]", previousLevelName))) .description(GuiUtils.stringSplit(user.getTranslation("challenges.to-complete", "[challengesToDo]",String.valueOf(previousStatus != null ? previousStatus.getNumberOfChallengesStillToDo() : ""), "[thisLevel]", previousLevelName)))
.build(); .build();
panelBuilder.item(item); panelBuilder.item(item);
} }
@ -167,9 +169,9 @@ public class ChallengesPanels {
} }
// Check if completed or not // Check if completed or not
boolean complete = addon.getChallengesManager().isChallengeComplete(user, challenge.getUniqueId(), world); boolean complete = addon.getChallengesManager().isChallengeComplete(user, challenge);
int maxTimes = challenge.getMaxTimes(); int maxTimes = challenge.getMaxTimes();
long doneTimes = addon.getChallengesManager().checkChallengeTimes(user, challenge, world); long doneTimes = addon.getChallengesManager().getChallengeTimes(user, challenge);
if (complete) { if (complete) {
result.add(user.getTranslation("challenges.complete")); result.add(user.getTranslation("challenges.complete"));
} }
@ -234,6 +236,6 @@ public class ChallengesPanels {
} }
private Collection<? extends String> splitTrans(User user, String string, String...strings) { private Collection<? extends String> splitTrans(User user, String string, String...strings) {
return addon.getChallengesManager().stringSplit(user.getTranslation(string, strings)); return GuiUtils.stringSplit(user.getTranslation(string, strings));
} }
} }

View File

@ -10,6 +10,8 @@ import org.bukkit.inventory.ItemStack;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.ChallengesManager; import world.bentobox.challenges.ChallengesManager;
import world.bentobox.challenges.database.object.ChallengeLevel;
import world.bentobox.challenges.utils.GuiUtils;
import world.bentobox.challenges.utils.LevelStatus; import world.bentobox.challenges.utils.LevelStatus;
import world.bentobox.challenges.commands.ChallengesCommand; import world.bentobox.challenges.commands.ChallengesCommand;
import world.bentobox.challenges.database.object.Challenge; import world.bentobox.challenges.database.object.Challenge;
@ -35,7 +37,7 @@ public class ChallengesPanels2 {
private ChallengesAddon addon; private ChallengesAddon addon;
private ChallengesManager manager; private ChallengesManager manager;
private User requester; private User requester;
private String level; private ChallengeLevel level;
private World world; private World world;
private String permPrefix; private String permPrefix;
private String label; private String label;
@ -52,18 +54,18 @@ public class ChallengesPanels2 {
this.label = label; this.label = label;
this.mode = mode; this.mode = mode;
if (manager.getChallengeList().isEmpty()) { if (manager.getAllChallenges(world).isEmpty()) {
addon.getLogger().severe("There are no challenges set up!"); addon.getLogger().severe("There are no challenges set up!");
requester.sendMessage("general.errors.general"); requester.sendMessage("general.errors.general");
return; return;
} }
if (level.isEmpty()) { if (level.isEmpty()) {
// TODO: open the farthest challenge panel // TODO: open the farthest challenge panel
level = manager.getChallengeList().keySet().iterator().next().getUniqueId(); level = manager.getLevels(world).iterator().next().getUniqueId();
} }
this.level = level; this.level = manager.getLevel(level);
// Check if level is valid // Check if level is valid
if (mode.equals(Mode.PLAYER) && !manager.isLevelUnlocked(requester, level, world)) { if (mode.equals(Mode.PLAYER) && !manager.isLevelUnlocked(requester, world, this.level)) {
return; return;
} }
PanelBuilder panelBuilder = new PanelBuilder(); PanelBuilder panelBuilder = new PanelBuilder();
@ -93,13 +95,13 @@ public class ChallengesPanels2 {
private void addChallengeItems(PanelBuilder panelBuilder) { private void addChallengeItems(PanelBuilder panelBuilder) {
// Only show a control panel for the level requested. // Only show a control panel for the level requested.
for (Challenge challenge : manager.getChallenges(level, world)) { for (Challenge challenge : manager.getLevelChallenges(level)) {
createItem(panelBuilder, challenge); createItem(panelBuilder, challenge);
} }
} }
private void addFreeChallanges(PanelBuilder panelBuilder) { private void addFreeChallanges(PanelBuilder panelBuilder) {
manager.getChallenges(ChallengesManager.FREE, world).forEach(challenge -> createItem(panelBuilder, challenge)); manager.getFreeChallenges(world).forEach(challenge -> createItem(panelBuilder, challenge));
} }
@ -116,10 +118,10 @@ public class ChallengesPanels2 {
glow = challenge.isDeployed(); glow = challenge.isDeployed();
break; break;
case EDIT: case EDIT:
glow = manager.isChallengeComplete(requester, challenge.getUniqueId(), world); glow = manager.isChallengeComplete(requester, challenge);
break; break;
case PLAYER: case PLAYER:
glow = manager.isChallengeComplete(requester, challenge.getUniqueId(), world); glow = manager.isChallengeComplete(requester, challenge);
break; break;
default: default:
break; break;
@ -169,7 +171,7 @@ public class ChallengesPanels2 {
// Add navigation to other levels // Add navigation to other levels
for (LevelStatus status: manager.getChallengeLevelStatus(requester, world)) { for (LevelStatus status: manager.getChallengeLevelStatus(requester, world)) {
if (status.getLevel().getUniqueId().equalsIgnoreCase(level)) { if (status.getLevel().getUniqueId().equalsIgnoreCase(level.getUniqueId())) {
// Skip if this is the current level // Skip if this is the current level
previousStatus = status; previousStatus = status;
continue; continue;
@ -182,7 +184,7 @@ public class ChallengesPanels2 {
PanelItem item = new PanelItemBuilder() PanelItem item = new PanelItemBuilder()
.icon(new ItemStack(Material.ENCHANTED_BOOK)) .icon(new ItemStack(Material.ENCHANTED_BOOK))
.name(name) .name(name)
.description(manager.stringSplit(requester.getTranslation("challenges.navigation","[level]",name))) .description(GuiUtils.stringSplit(requester.getTranslation("challenges.navigation","[level]",name)))
.clickHandler((p, u, c, s) -> { .clickHandler((p, u, c, s) -> {
u.closeInventory(); u.closeInventory();
u.performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + status.getLevel().getUniqueId()); u.performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + status.getLevel().getUniqueId());
@ -196,7 +198,7 @@ public class ChallengesPanels2 {
PanelItem item = new PanelItemBuilder() PanelItem item = new PanelItemBuilder()
.icon(new ItemStack(Material.BOOK)) .icon(new ItemStack(Material.BOOK))
.name(name) .name(name)
.description(manager.stringSplit(requester.getTranslation("challenges.to-complete", "[challengesToDo]",String.valueOf(previousStatus != null ? previousStatus.getNumberOfChallengesStillToDo() : ""), "[thisLevel]", previousLevelName))) .description(GuiUtils.stringSplit(requester.getTranslation("challenges.to-complete", "[challengesToDo]",String.valueOf(previousStatus != null ? previousStatus.getNumberOfChallengesStillToDo() : ""), "[thisLevel]", previousLevelName)))
.build(); .build();
panelBuilder.item(item); panelBuilder.item(item);
} }
@ -230,9 +232,9 @@ public class ChallengesPanels2 {
result.addAll(addRewards(challenge, true, true)); result.addAll(addRewards(challenge, true, true));
} else { } else {
// Check if completed or not // Check if completed or not
boolean complete = addon.getChallengesManager().isChallengeComplete(requester, challenge.getUniqueId(), world); boolean complete = addon.getChallengesManager().isChallengeComplete(requester, challenge);
int maxTimes = challenge.getMaxTimes(); int maxTimes = challenge.getMaxTimes();
long doneTimes = addon.getChallengesManager().checkChallengeTimes(requester, challenge, world); long doneTimes = addon.getChallengesManager().getChallengeTimes(requester, challenge);
if (complete) { if (complete) {
result.add(requester.getTranslation("challenges.complete")); result.add(requester.getTranslation("challenges.complete"));
} }
@ -307,6 +309,6 @@ public class ChallengesPanels2 {
} }
private Collection<? extends String> splitTrans(User user, String string, String...strings) { private Collection<? extends String> splitTrans(User user, String string, String...strings) {
return addon.getChallengesManager().stringSplit(user.getTranslation(string, strings)); return GuiUtils.stringSplit(user.getTranslation(string, strings));
} }
} }

View File

@ -29,7 +29,7 @@ public class CreateChallengeListener implements PanelListener {
@Override @Override
public void onInventoryClose(InventoryCloseEvent event) { public void onInventoryClose(InventoryCloseEvent event) {
addon.getChallengesManager().createInvChallenge(user, event.getInventory()); addon.getChallengesManager().createChallenge("uniqueID");
} }
@Override @Override

View File

@ -124,7 +124,7 @@ public class TryToComplete {
user.sendMessage("challenges.you-repeated", "[challenge]", challenge.getFriendlyName()); user.sendMessage("challenges.you-repeated", "[challenge]", challenge.getFriendlyName());
} }
// Mark as complete // Mark as complete
manager.setChallengeComplete(user, challenge.getUniqueId(), world); manager.setChallengeComplete(user, challenge);
user.closeInventory(); user.closeInventory();
user.getPlayer().performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + challenge.getLevel()); user.getPlayer().performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + challenge.getLevel());
return result; return result;
@ -189,7 +189,7 @@ public class TryToComplete {
user.sendMessage("challenges.you-repeated", "[challenge]", challenge.getFriendlyName()); user.sendMessage("challenges.you-repeated", "[challenge]", challenge.getFriendlyName());
} }
// Mark as complete // Mark as complete
manager.setChallengeComplete(user, challenge.getUniqueId(), world); manager.setChallengeComplete(user, challenge);
user.closeInventory(); user.closeInventory();
user.getPlayer().performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + challenge.getLevel()); user.getPlayer().performCommand(label + " " + ChallengesCommand.CHALLENGE_COMMAND + " " + challenge.getLevel());
} }
@ -204,17 +204,17 @@ public class TryToComplete {
return new ChallengeResult(); return new ChallengeResult();
} }
// Check if user has the // Check if user has the
if (!challenge.getLevel().equals(ChallengesManager.FREE) && !manager.isLevelUnlocked(user, challenge.getLevel(), world)) { if (!challenge.getLevel().equals(ChallengesManager.FREE) && !manager.isLevelUnlocked(user, world, manager.getLevel(challenge.getLevel()))) {
user.sendMessage("challenges.errors.challenge-level-not-available"); user.sendMessage("challenges.errors.challenge-level-not-available");
return new ChallengeResult(); return new ChallengeResult();
} }
// Check max times // Check max times
if (challenge.isRepeatable() && challenge.getMaxTimes() > 0 && manager.checkChallengeTimes(user, challenge, world) >= challenge.getMaxTimes()) { if (challenge.isRepeatable() && challenge.getMaxTimes() > 0 && manager.getChallengeTimes(user, challenge) >= challenge.getMaxTimes()) {
user.sendMessage("challenges.not-repeatable"); user.sendMessage("challenges.not-repeatable");
return new ChallengeResult(); return new ChallengeResult();
} }
// Check repeatability // Check repeatability
if (manager.isChallengeComplete(user, challenge.getUniqueId(), world) if (manager.isChallengeComplete(user, challenge)
&& (!challenge.isRepeatable() || challenge.getChallengeType().equals(ChallengeType.OTHER) && (!challenge.isRepeatable() || challenge.getChallengeType().equals(ChallengeType.OTHER)
|| challenge.getChallengeType().equals(ChallengeType.ISLAND))) { || challenge.getChallengeType().equals(ChallengeType.ISLAND))) {
user.sendMessage("challenges.not-repeatable"); user.sendMessage("challenges.not-repeatable");
@ -288,7 +288,7 @@ public class TryToComplete {
this.removeMoney(); this.removeMoney();
// Return the result // Return the result
return new ChallengeResult().setMeetsRequirements().setRepeat(manager.isChallengeComplete(user, challenge.getUniqueId(), world)); return new ChallengeResult().setMeetsRequirements().setRepeat(manager.isChallengeComplete(user, challenge));
} }

View File

@ -11,6 +11,7 @@ import net.wesjd.anvilgui.AnvilGUI;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.panel.CommonGUI; import world.bentobox.challenges.panel.CommonGUI;
import world.bentobox.challenges.utils.GuiUtils; import world.bentobox.challenges.utils.GuiUtils;
@ -198,8 +199,9 @@ public class AdminGUI extends CommonGUI
this.user.getPlayer(), this.user.getPlayer(),
"unique_id", "unique_id",
(player, reply) -> { (player, reply) -> {
String newName = Util.getWorld(this.world).getName() + "_" + reply;
if (this.addon.getChallengesManager().validateChallengeUniqueID(this.world, reply)) if (this.addon.getChallengesManager().containsChallenge(newName))
{ {
new EditChallengeGUI(this.addon, new EditChallengeGUI(this.addon,
this.world, this.world,
@ -212,7 +214,6 @@ public class AdminGUI extends CommonGUI
else else
{ {
this.user.sendMessage("challenges.errors.unique-id", "[id]", reply); this.user.sendMessage("challenges.errors.unique-id", "[id]", reply);
this.build();
} }
return reply; return reply;
@ -234,8 +235,9 @@ public class AdminGUI extends CommonGUI
this.user.getPlayer(), this.user.getPlayer(),
"unique_id", "unique_id",
(player, reply) -> { (player, reply) -> {
String newName = Util.getWorld(this.world).getName() + "_" + reply;
if (this.addon.getChallengesManager().validateLevelUniqueID(this.world, reply)) if (this.addon.getChallengesManager().containsLevel(newName))
{ {
new EditLevelGUI(this.addon, new EditLevelGUI(this.addon,
this.world, this.world,
@ -248,7 +250,6 @@ public class AdminGUI extends CommonGUI
else else
{ {
this.user.sendMessage("challenges.errors.unique-id", "[id]", reply); this.user.sendMessage("challenges.errors.unique-id", "[id]", reply);
this.build();
} }
return reply; return reply;

View File

@ -145,7 +145,7 @@ public class EditLevelGUI extends CommonGUI
*/ */
private void buildChallengesPanel(PanelBuilder panelBuilder) private void buildChallengesPanel(PanelBuilder panelBuilder)
{ {
List<Challenge> challengeList = this.addon.getChallengesManager().getChallenges(this.challengeLevel); List<Challenge> challengeList = this.addon.getChallengesManager().getLevelChallenges(this.challengeLevel);
final int MAX_ELEMENTS = 21; final int MAX_ELEMENTS = 21;
@ -538,8 +538,8 @@ public class EditLevelGUI extends CommonGUI
ChallengesManager manager = this.addon.getChallengesManager(); ChallengesManager manager = this.addon.getChallengesManager();
// Get all challenge that is not in current challenge. // Get all challenge that is not in current challenge.
List<Challenge> challengeList = manager.getChallengesList(); List<Challenge> challengeList = manager.getAllChallenges(this.world);
challengeList.removeAll(manager.getChallenges(this.challengeLevel)); challengeList.removeAll(manager.getLevelChallenges(this.challengeLevel));
new SelectChallengeGUI(this.user, challengeList, (status, value) -> { new SelectChallengeGUI(this.user, challengeList, (status, value) -> {
if (status) if (status)
@ -563,10 +563,10 @@ public class EditLevelGUI extends CommonGUI
clickHandler = (panel, user, clickType, slot) -> { clickHandler = (panel, user, clickType, slot) -> {
ChallengesManager manager = this.addon.getChallengesManager(); ChallengesManager manager = this.addon.getChallengesManager();
new SelectChallengeGUI(this.user, manager.getChallenges(this.challengeLevel), (status, value) -> { new SelectChallengeGUI(this.user, manager.getLevelChallenges(this.challengeLevel), (status, value) -> {
if (status) if (status)
{ {
manager.unlinkChallenge(this.challengeLevel, value); manager.removeChallengeFromLevel(value, this.challengeLevel);
} }
this.build(); this.build();

View File

@ -82,7 +82,7 @@ public class ListChallengesGUI extends CommonGUI
GuiUtils.fillBorder(panelBuilder); GuiUtils.fillBorder(panelBuilder);
} }
List<Challenge> challengeList = this.addon.getChallengesManager().getChallengesList(); List<Challenge> challengeList = this.addon.getChallengesManager().getAllChallenges(this.world);
final int MAX_ELEMENTS = 21; final int MAX_ELEMENTS = 21;

View File

@ -82,7 +82,7 @@ public class ListLevelsGUI extends CommonGUI
GuiUtils.fillBorder(panelBuilder); GuiUtils.fillBorder(panelBuilder);
} }
List<ChallengeLevel> levelList = this.addon.getChallengesManager().getChallengeLevelList(); List<ChallengeLevel> levelList = this.addon.getChallengesManager().getLevels(this.world);
final int MAX_ELEMENTS = 21; final int MAX_ELEMENTS = 21;

View File

@ -178,20 +178,20 @@ public class ListUsersGUI extends CommonGUI
switch (this.operationMode) switch (this.operationMode)
{ {
case COMPLETE: case COMPLETE:
new SelectChallengeGUI(this.user, manager.getChallengesList(), (status, value) -> { new SelectChallengeGUI(this.user, manager.getAllChallenges(this.world), (status, value) -> {
if (status) if (status)
{ {
manager.completeChallenge(player.getUniqueId(), value); manager.setChallengeComplete(User.getInstance(player), value);
} }
this.build(); this.build();
}); });
break; break;
case RESET: case RESET:
new SelectChallengeGUI(this.user, manager.getChallengesList(), (status, value) -> { new SelectChallengeGUI(this.user, manager.getAllChallenges(this.world), (status, value) -> {
if (status) if (status)
{ {
manager.resetChallenge(player.getUniqueId(), value); manager.resetChallenge(User.getInstance(player), value);
} }
this.build(); this.build();

View File

@ -123,7 +123,7 @@ public class ChallengesGUI extends CommonGUI
*/ */
private void addFreeChallenges(PanelBuilder panelBuilder) private void addFreeChallenges(PanelBuilder panelBuilder)
{ {
List<Challenge> freeChallenges = this.challengesManager.getFreeChallenges(this.user, this.world); List<Challenge> freeChallenges = this.challengesManager.getFreeChallenges(this.world);
final int freeChallengesCount = freeChallenges.size(); final int freeChallengesCount = freeChallenges.size();
if (freeChallengesCount > 18) if (freeChallengesCount > 18)
@ -185,7 +185,7 @@ public class ChallengesGUI extends CommonGUI
{ {
if (this.lastSelectedLevel != null) if (this.lastSelectedLevel != null)
{ {
List<Challenge> challenges = this.challengesManager.getChallenges(this.lastSelectedLevel.getLevel()); List<Challenge> challenges = this.challengesManager.getLevelChallenges(this.lastSelectedLevel.getLevel());
final int challengesCount = challenges.size(); final int challengesCount = challenges.size();
if (challengesCount > 18) if (challengesCount > 18)
@ -346,7 +346,7 @@ public class ChallengesGUI extends CommonGUI
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
result.add(this.user.getTranslation("challenges.level", result.add(this.user.getTranslation("challenges.level",
"[level]", this.challengesManager.getChallengesLevel(challenge))); "[level]", this.challengesManager.getLevel(challenge).getFriendlyName()));
boolean completed = this.challengesManager.isChallengeComplete(this.user, challenge); boolean completed = this.challengesManager.isChallengeComplete(this.user, challenge);
@ -358,7 +358,7 @@ public class ChallengesGUI extends CommonGUI
if (challenge.isRepeatable()) if (challenge.isRepeatable())
{ {
int maxTimes = challenge.getMaxTimes(); int maxTimes = challenge.getMaxTimes();
long doneTimes = this.challengesManager.checkChallengeTimes(this.user, challenge); long doneTimes = this.challengesManager.getChallengeTimes(this.user, challenge);
if (maxTimes > 0) if (maxTimes > 0)
{ {

View File

@ -1,11 +1,16 @@
package world.bentobox.challenges.utils; package world.bentobox.challenges.utils;
import org.apache.commons.lang.WordUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
@ -360,4 +365,24 @@ public class GuiUtils
return new BorderBlock(itemStack); return new BorderBlock(itemStack);
} }
} }
/**
* Simple splitter
*
* @param string - string to be split
* @return list of split strings
*/
public static List<String> stringSplit(String string)
{
string = ChatColor.translateAlternateColorCodes('&', string);
// Check length of lines
List<String> result = new ArrayList<>();
Arrays.asList(string.split("\\|")).
forEach(line -> result.addAll(
Arrays.asList(WordUtils.wrap(line, 25).split("\\n"))));
return result;
}
} }