Skip to content

Commit

Permalink
Add functional LibraryPanel.
Browse files Browse the repository at this point in the history
- Add ability to load template files via LibraryPanel
- Add ability to load database export via LibraryPanel
- Add ability to load web library files via LibraryPanel

Create WebLibrary that checks files in BentoBoxWorld/weblib/mcg repository.
  • Loading branch information
BONNe committed Oct 7, 2020
1 parent 10d0b68 commit ec6ab99
Show file tree
Hide file tree
Showing 8 changed files with 1,203 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import world.bentobox.magiccobblestonegenerator.managers.StoneGeneratorImportManager;
import world.bentobox.magiccobblestonegenerator.managers.StoneGeneratorManager;
import world.bentobox.magiccobblestonegenerator.tasks.MagicGenerator;
import world.bentobox.upgrades.UpgradesAddon;
import world.bentobox.magiccobblestonegenerator.web.WebManager;


/**
Expand Down Expand Up @@ -69,6 +69,9 @@ public void onEnable()
this.stoneGeneratorManager = new StoneGeneratorManager(this);
this.stoneGeneratorManager.load();

// Init new Web Manager.
this.webManager = new WebManager(this);

this.getPlugin().getAddonsManager().getGameModeAddons().stream().
filter(gameMode -> !settings.getDisabledGameModes().contains(gameMode.getDescription().getName())).
forEach(this::hookIntoGameMode);
Expand Down Expand Up @@ -446,6 +449,17 @@ public VaultHook getVaultHook()
}


/**
* Gets web manager.
*
* @return the web manager
*/
public WebManager getWebManager()
{
return webManager;
}


/**
* This method allows to access static addon instance.
* @return Addon instance.
Expand Down Expand Up @@ -480,6 +494,11 @@ public static StoneGeneratorAddon getInstance()
*/
private StoneGeneratorImportManager stoneGeneratorImportManager;

/**
* Variable holds web manager object.
*/
private WebManager webManager;

/**
* Variable holds MagicGenerator object.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,37 @@ public boolean importFile(@Nullable User user, World world)
return false;
}

return this.importFile(user, world, this.generatorFile.getName());
}


/**
* This method imports generator tiers from template
*
* @param user - user
* @param world - world to import into
* @param file - file that must be imported
* @return true if successful
*/
public boolean importFile(@Nullable User user, World world, String file)
{
File generatorFile = new File(this.addon.getDataFolder(), file.endsWith(".yml") ? file : file + ".yml");

if (!generatorFile.exists())
{
if (user != null)
{
user.sendMessage(Constants.ERRORS + "no-file");
}

return false;
}

YamlConfiguration config = new YamlConfiguration();

try
{
config.load(this.generatorFile);
config.load(generatorFile);
}
catch (IOException | InvalidConfigurationException e)
{
Expand Down Expand Up @@ -425,6 +451,7 @@ public boolean generateDatabaseFile(User user, World world, String fileName)
exportedGeneratorData.setGeneratorTiers(generatorTierList);
exportedGeneratorData.setGeneratorBundles(levelList);
exportedGeneratorData.setVersion(this.addon.getDescription().getVersion());
exportedGeneratorData.setAuthor(user.getName());

try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(defaultFile), StandardCharsets.UTF_8))) {
Expand Down Expand Up @@ -521,6 +548,45 @@ public boolean importDatabaseFile(User user, World world, String fileName)
}


/**
* This method saves and imports given string as generators.
* @param user User who called method.
* @param world World which will be targeted.
* @param stoneGeneratorLibrary String that contains all data for generators.
*/
public void processDownloadedFile(User user, World world, String stoneGeneratorLibrary)
{
DefaultDataHolder downloadedGenerators =
new DefaultJSONHandler(this.addon).loadWebObject(stoneGeneratorLibrary);

File downloadFile = new File(this.addon.getDataFolder(), downloadedGenerators.getUniqueId() + ".json");
int i = 1;

while (downloadFile.exists())
{
downloadFile = new File(this.addon.getDataFolder(),
downloadedGenerators.getUniqueId() + "-" + i++ + ".json");
}

try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(downloadFile), StandardCharsets.UTF_8))) {
writer.write(Objects.requireNonNull(
new DefaultJSONHandler(this.addon).toJsonString(downloadedGenerators)));
}
catch (Exception e)
{
if (user.isPlayer())
{
user.sendMessage(Constants.ERRORS + "file-error");
}

this.addon.logError("Could not save json file: " + e.getMessage());
}

this.importDatabaseFile(user, world, downloadFile.getName());
}


// ---------------------------------------------------------------------
// Section: Class instances
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -645,6 +711,7 @@ private static final class DefaultDataHolder implements DataObject
this.generatorTiers = Collections.emptyList();
this.generatorBundles = Collections.emptyList();
this.version = "";
this.author = null;
}


Expand Down Expand Up @@ -729,6 +796,28 @@ public void setUniqueId(String uniqueId)
}


/**
* Gets author.
*
* @return the author
*/
public String getAuthor()
{
return author;
}


/**
* Sets author.
*
* @param author the author
*/
public void setAuthor(String author)
{
this.author = author;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand All @@ -752,6 +841,15 @@ public void setUniqueId(String uniqueId)
@Expose
private String version;

/**
* Holds an author for export file.
*/
@Expose
private String author;

/**
* The Unique id.
*/
@Expose
private String uniqueId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private PanelItem createButton(Action button)
case IMPORT_TEMPLATE:
{
clickHandler = (panel, user1, clickType, slot) -> {
// LibraryPanel.open(this, LibraryPanel.Library.TEMPLATE);
LibraryPanel.open(this, LibraryPanel.Library.TEMPLATE);
return true;
};
material = Material.BOOKSHELF;
Expand All @@ -168,7 +168,7 @@ private PanelItem createButton(Action button)
case WEB_LIBRARY:
{
clickHandler = (panel, user1, clickType, slot) -> {
// LibraryPanel.open(this, LibraryPanel.Library.WEB);
LibraryPanel.open(this, LibraryPanel.Library.WEB);
return true;
};
material = Material.COBWEB;
Expand Down Expand Up @@ -211,9 +211,7 @@ private PanelItem createButton(Action button)
case IMPORT_TO_DATABASE:
{
clickHandler = (panel, user1, clickType, slot) -> {
this.user.sendMessage(Constants.MESSAGE + "generator-data-removed",
Constants.GAMEMODE, Utils.getGameMode(this.world));
// LibraryPanel.open(this, LibraryPanel.Library.DATABASE);
LibraryPanel.open(this, LibraryPanel.Library.DATABASE);
return true;
};
material = Material.BOOKSHELF;
Expand Down

0 comments on commit ec6ab99

Please sign in to comment.