Skip to content

Commit

Permalink
Add proper description generation to the bundles.
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Jan 8, 2022
1 parent 5073e07 commit 5f42f11
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,17 @@ else if (button == CommonButtons.SEARCH)
*/
private enum CommonButtons
{
/**
* Next common buttons.
*/
NEXT,
/**
* Previous common buttons.
*/
PREVIOUS,
/**
* Search common buttons.
*/
SEARCH
}

Expand Down
109 changes: 107 additions & 2 deletions src/main/java/world/bentobox/biomes/panels/CommonPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.biomes.BiomesAddon;
import world.bentobox.biomes.database.objects.BiomesBundleObject;
import world.bentobox.biomes.database.objects.BiomesObject;
import world.bentobox.biomes.managers.BiomesAddonManager;
import world.bentobox.biomes.utils.Constants;
Expand Down Expand Up @@ -108,7 +112,7 @@ protected CommonPanel(@NotNull CommonPanel parentPanel)
*/
protected String generateBiomesDescription(BiomesObject biome, @Nullable User target)
{
final String reference = Constants.DESCRIPTIONS + "biomes.";
final String reference = Constants.DESCRIPTIONS + "biome.";

// Get description from custom translations
String description = this.user.getTranslationOrNothing(
Expand Down Expand Up @@ -147,7 +151,7 @@ protected String generateBiomesDescription(BiomesObject biome, @Nullable User ta

String permissions = this.generatePermissions(biome, target);

String returnString = this.user.getTranslationOrNothing(reference + "biome.lore",
String returnString = this.user.getTranslationOrNothing(reference + "lore",
"[description]", description,
"[biome]", originalBiome,
"[price]", price,
Expand Down Expand Up @@ -221,6 +225,64 @@ else if (!missingPermissions.isEmpty())
}


/**
* Admin should see simplified view. It is not necessary to view all unnecessary things.
*
* @param bundle Bundle which description must be generated.
* @return List of strings that describes bundle.
*/
protected String generateBundleDescription(BiomesBundleObject bundle)
{
final String reference = Constants.DESCRIPTIONS + "bundle.";

StringBuilder descriptionBuilder = new StringBuilder();
bundle.getDescription().forEach(line ->
descriptionBuilder.append(Util.translateColorCodes(line)).append("\n"));

String permission;

if (bundle != BiomesBundleObject.dummyBundle)
{
permission = this.user.getTranslation(reference + "permission",
Constants.PARAMETER_ID, bundle.getUniqueId(),
Constants.PARAMETER_GAMEMODE, Utils.getGameMode(this.world).toLowerCase());
}
else
{
permission = "";
}

StringBuilder biomesBuilder = new StringBuilder();

// Add missing permissions
if (!bundle.getBiomeObjects().isEmpty())
{
biomesBuilder.append(this.user.getTranslation(reference + "title"));

bundle.getBiomeObjects().stream().
map(this.manager::getBiomeByID).
filter(Objects::nonNull).
forEach(biome -> biomesBuilder.append("\n").
append(this.user.getTranslation(reference + "value",
Constants.PARAMETER_BIOME, biome.getFriendlyName())));
}
else
{
biomesBuilder.append(this.user.getTranslation(reference + "no-biomes"));
}


String returnString = this.user.getTranslationOrNothing(reference + "lore",
"[description]", descriptionBuilder.toString(),
"[biomes]", biomesBuilder.toString(),
"[permission]", permission);

// Remove empty lines and returns as a list.

return returnString.replaceAll("(?m)^[ \\t]*\\r?\\n", "");
}


/**
* This method reopens given panel.
* @param panel Panel that must be reopened.
Expand All @@ -231,6 +293,49 @@ public static void reopen(CommonPanel panel)
}


/**
* This method finds and try to execute given sub command with given arguments.
* @param subCommand Sub Command that need to be called.
* @param arguments List of arguments for current command.
*/
protected void callCommand(String subCommand, List<String> arguments)
{
CompositeCommand command = this.addon.getPlugin().getCommandsManager().getCommand(this.topLabel);

if (command == null)
{
// TODO: Throw error that top command not found.
return;
}

Optional<CompositeCommand> commandOptional =
command.getSubCommand(this.addon.getSettings().getPlayerCommand().split(" ")[0]);

if (commandOptional.isEmpty())
{
// TODO: Throw error that biomes command not found.
return;
}

commandOptional = commandOptional.get().getSubCommand(subCommand);

if (commandOptional.isEmpty())
{
// TODO: Throw error that biomes sub-command not found.
return;
}

command = commandOptional.get();

if (command.canExecute(this.user, subCommand, arguments))
{
command.execute(this.user, subCommand, arguments);
}

this.user.closeInventory();
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand Down

0 comments on commit 5f42f11

Please sign in to comment.