Skip to content

Commit

Permalink
Implement WebManager that will download Challenges Libraries from Git…
Browse files Browse the repository at this point in the history
…Hub.

Implement GUI for selecting and downloading Challenges Libraries.
  • Loading branch information
BONNe committed Sep 2, 2019
1 parent 29a7714 commit f611727
Show file tree
Hide file tree
Showing 8 changed files with 790 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/world/bentobox/challenges/ChallengesAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import world.bentobox.challenges.handlers.*;
import world.bentobox.challenges.listeners.ResetListener;
import world.bentobox.challenges.listeners.SaveListener;
import world.bentobox.challenges.web.WebManager;
import world.bentobox.level.Level;


Expand All @@ -39,6 +40,11 @@ public class ChallengesAddon extends Addon {

private ChallengesImportManager importManager;

/**
* This class manages web content loading.
*/
private WebManager webManager;

private Settings settings;

private boolean hooked;
Expand Down Expand Up @@ -138,6 +144,9 @@ public void onEnable() {
// Challenge import setup
this.importManager = new ChallengesImportManager(this);

// Web content loading
this.webManager = new WebManager(this);

List<GameModeAddon> hookedGameModes = new ArrayList<>();

this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
Expand Down Expand Up @@ -315,6 +324,14 @@ public ChallengesImportManager getImportManager() {
}


/**
* @return the webManager
*/
public WebManager getWebManager() {
return this.webManager;
}


/**
* @return the challenge settings.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,77 @@ public boolean loadDefaultChallenges(User user, World world)
}


/**
* This method loads downloaded challenges into memory.
* @param user User who calls downloaded challenge loading
* @param world Target world.
* @param downloadString String that need to be loaded via DefaultDataHolder.
* @return <code>true</code> if everything was successful, otherwise <code>false</code>.
*/
public boolean loadDownloadedChallenges(User user, World world, String downloadString)
{
ChallengesManager manager = this.addon.getChallengesManager();

// If exist any challenge or level that is bound to current world, then do not load default challenges.
if (manager.hasAnyChallengeData(world.getName()))
{
if (user.isPlayer())
{
user.sendMessage("challenges.errors.exist-challenges-or-levels");
}
else
{
this.addon.logWarning("challenges.errors.exist-challenges-or-levels");
}

return false;
}

try
{
// This prefix will be used to all challenges. That is a unique way how to separate challenged for
// each game mode.
String uniqueIDPrefix = Utils.getGameMode(world) + "_";
DefaultDataHolder downloadedChallenges = new DefaultJSONHandler(this.addon).loadWebObject(downloadString);

// All new challenges should get correct ID. So we need to map it to loaded challenges.
downloadedChallenges.getChallengeList().forEach(challenge -> {
// Set correct challenge ID
challenge.setUniqueId(uniqueIDPrefix + challenge.getUniqueId());
// Set up correct level ID if it is necessary
if (!challenge.getLevel().isEmpty())
{
challenge.setLevel(uniqueIDPrefix + challenge.getLevel());
}
// Load challenge in memory
manager.loadChallenge(challenge, false, user, user == null);
});

downloadedChallenges.getLevelList().forEach(challengeLevel -> {
// Set correct level ID
challengeLevel.setUniqueId(uniqueIDPrefix + challengeLevel.getUniqueId());
// Set correct world name
challengeLevel.setWorld(Util.getWorld(world).getName());
// Reset names for all challenges.
challengeLevel.setChallenges(challengeLevel.getChallenges().stream().
map(challenge -> uniqueIDPrefix + challenge).
collect(Collectors.toSet()));
// Load level in memory
manager.loadLevel(challengeLevel, false, user, user == null);
});
}
catch (Exception e)
{
e.printStackTrace();
return false;
}

this.addon.getChallengesManager().save();

return true;
}


// ---------------------------------------------------------------------
// Section: Default generation
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -320,6 +391,16 @@ DefaultDataHolder loadObject()
}


/**
* This method creates and adds to list all objects from default.json file.
* @return List of all objects from default.json that is with T instance.
*/
DefaultDataHolder loadWebObject(String downloadedObject)
{
return this.gson.fromJson(downloadedObject, DefaultDataHolder.class);
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand Down
37 changes: 36 additions & 1 deletion src/main/java/world/bentobox/challenges/panel/CommonGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public abstract class CommonGUI
/**
* This variable stores parent gui.
*/
private CommonGUI parentGUI;
protected CommonGUI parentGUI;

/**
* Variable stores Challenges addon.
Expand Down Expand Up @@ -136,6 +136,7 @@ protected enum CommonButtons

protected static final String COMPLETE = "complete";

protected static final String DOWNLOAD = "download";

// ---------------------------------------------------------------------
// Section: Constructors
Expand Down Expand Up @@ -205,6 +206,40 @@ public CommonGUI(ChallengesAddon addon,
}


/**
* Default constructor that inits panels with minimal requirements.
* @param parentGUI Parent panel for current panel.
*/
public CommonGUI(CommonGUI parentGUI)
{
this.addon = parentGUI.addon;
this.world = parentGUI.world;
this.user = parentGUI.user;

this.topLabel = parentGUI.topLabel;
this.permissionPrefix = parentGUI.permissionPrefix;

this.parentGUI = parentGUI;

this.pageIndex = 0;

this.returnButton = new PanelItemBuilder().
name(this.user.getTranslation("challenges.gui.buttons.return")).
icon(Material.OAK_DOOR).
clickHandler((panel, user1, clickType, i) -> {

if (this.parentGUI == null)
{
this.user.closeInventory();
return true;
}

this.parentGUI.build();
return true;
}).build();
}


// ---------------------------------------------------------------------
// Section: Common methods
// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ private enum Button
EDIT_SETTINGS,
DEFAULT_IMPORT_CHALLENGES,
DEFAULT_EXPORT_CHALLENGES,
COMPLETE_WIPE
COMPLETE_WIPE,
LIBRARY
}


Expand Down Expand Up @@ -122,6 +123,8 @@ public void build()

// Import Challenges
panelBuilder.item(15, this.createButton(Button.DEFAULT_IMPORT_CHALLENGES));
panelBuilder.item(24, this.createButton(Button.LIBRARY));

// Not added as I do not think admins should use it. It still will be able via command.
// panelBuilder.item(33, this.createButton(Button.DEFAULT_EXPORT_CHALLENGES));

Expand Down Expand Up @@ -439,6 +442,21 @@ private PanelItem createButton(Button button)

break;
}
case LIBRARY:
{
permissionSuffix = DOWNLOAD;

name = this.user.getTranslation("challenges.gui.buttons.admin.library");
description = this.user.getTranslation("challenges.gui.descriptions.admin.library");
icon = new ItemStack(Material.COBWEB);
clickHandler = (panel, user, clickType, slot) -> {
ListLibraryGUI.open(this);
return true;
};
glow = false;

break;
}
default:
// This should never happen.
return null;
Expand Down

0 comments on commit f611727

Please sign in to comment.