diff --git a/src/main/java/world/bentobox/challenges/ChallengesImportManager.java b/src/main/java/world/bentobox/challenges/ChallengesImportManager.java index 220090c..d850352 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesImportManager.java +++ b/src/main/java/world/bentobox/challenges/ChallengesImportManager.java @@ -9,208 +9,33 @@ import java.nio.file.Paths; import java.util.*; import java.util.stream.Collectors; -import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.EntityType; -import org.bukkit.inventory.ItemStack; import world.bentobox.bentobox.database.json.BentoboxTypeAdapterFactory; import world.bentobox.bentobox.database.objects.DataObject; -import world.bentobox.bentobox.util.ItemParser; import world.bentobox.challenges.database.object.ChallengeLevel; import world.bentobox.challenges.database.object.Challenge; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.util.Util; -import world.bentobox.challenges.utils.GuiUtils; import world.bentobox.challenges.utils.Utils; /** * Imports challenges - * @author tastybento + * @author BONNe1704 * */ public class ChallengesImportManager { - - private ChallengesAddon addon; - private YamlConfiguration chal; - /** * Import challenges from challenges.yml * @param challengesAddon */ - public ChallengesImportManager(ChallengesAddon challengesAddon) { + public ChallengesImportManager(ChallengesAddon challengesAddon) + { this.addon = challengesAddon; } - /** - * Import challenges - * @param user - user - * @param world - world to import into - * @param overwrite - true if previous ones should be overwritten - * @return true if successful - */ - public boolean importChallenges(User user, World world, boolean overwrite) { - File challengeFile = new File(addon.getDataFolder(), "challenges.yml"); - if (!challengeFile.exists()) { - user.sendMessage("challenges.errors.import-no-file"); - return false; - } - chal = new YamlConfiguration(); - try { - chal.load(challengeFile); - } catch (IOException | InvalidConfigurationException e) { - user.sendMessage("challenges.errors.no-load","[message]", e.getMessage()); - return false; - } - makeLevels(user, world, overwrite); - makeChallenges(user, world, overwrite); - addon.getChallengesManager().save(); - return true; - } - - private void makeLevels(User user, World world, boolean overwrite) { - // Parse the levels - String levels = chal.getString("challenges.levels", ""); - if (!levels.isEmpty()) { - user.sendMessage("challenges.messages.import-levels"); - String[] lvs = levels.split(" "); - int order = 0; - for (String level : lvs) { - ChallengeLevel challengeLevel = new ChallengeLevel(); - challengeLevel.setFriendlyName(level); - challengeLevel.setUniqueId(Utils.getGameMode(world) + "_" + level); - challengeLevel.setOrder(order++); - challengeLevel.setWorld(Util.getWorld(world).getName()); - challengeLevel.setWaiverAmount(chal.getInt("challenges.waiveramount")); - // Check if there is a level reward - ConfigurationSection unlock = chal.getConfigurationSection("challenges.levelUnlock." + level); - if (unlock != null) { - challengeLevel.setUnlockMessage(unlock.getString("message", "")); - challengeLevel.setRewardText(unlock.getString("rewardDesc","")); - challengeLevel.setRewardItems(parseItems(unlock.getString("itemReward",""))); - challengeLevel.setRewardMoney(unlock.getInt("moneyReward")); - challengeLevel.setRewardExperience(unlock.getInt("expReward")); - challengeLevel.setRewardCommands(unlock.getStringList("commands")); - } - addon.getChallengesManager().loadLevel(challengeLevel, overwrite, user, false); - } - } else { - user.sendMessage("challenges.messages.no-levels"); - } - } - - /** - * Imports challenges - * @param overwrite - */ - private void makeChallenges(User user, World world, boolean overwrite) { - int size = 0; - // Parse the challenge file - ConfigurationSection chals = chal.getConfigurationSection("challenges.challengeList"); - user.sendMessage("challenges.messages.import-challenges"); - - for (String challenge : chals.getKeys(false)) { - Challenge newChallenge = new Challenge(); - newChallenge.setUniqueId(Utils.getGameMode(world) + "_" + challenge); - newChallenge.setDeployed(true); - ConfigurationSection details = chals.getConfigurationSection(challenge); - newChallenge.setFriendlyName(details.getString("friendlyname", challenge)); - newChallenge.setDescription(GuiUtils.stringSplit( - details.getString("description", ""), - this.addon.getChallengesSettings().getLoreLineLength())); - newChallenge.setIcon(ItemParser.parse(details.getString("icon", "") + ":1")); - - if (details.getString("type", "").equalsIgnoreCase("level")) - { - // Fix for older version config - newChallenge.setChallengeType(Challenge.ChallengeType.OTHER); - } - else - { - newChallenge.setChallengeType(Challenge.ChallengeType.valueOf(details.getString("type","INVENTORY").toUpperCase())); - } - - newChallenge.setTakeItems(details.getBoolean("takeItems",true)); - newChallenge.setRewardText(details.getString("rewardText", "")); - newChallenge.setRewardCommands(details.getStringList("rewardcommands")); - newChallenge.setRewardMoney(details.getInt("moneyReward",0)); - newChallenge.setRewardExperience(details.getInt("expReward")); - newChallenge.setRepeatable(details.getBoolean("repeatable")); - newChallenge.setRepeatRewardText(details.getString("repeatRewardText","")); - newChallenge.setRepeatMoneyReward(details.getInt("repearMoneyReward")); - newChallenge.setRepeatExperienceReward(details.getInt("repeatExpReward")); - newChallenge.setRepeatRewardCommands(details.getStringList("repeatrewardcommands")); - newChallenge.setMaxTimes(details.getInt("maxtimes")); - // TODO reset allowed - newChallenge.setRequiredMoney(details.getInt("requiredMoney")); - newChallenge.setRequiredExperience(details.getInt("requiredExp")); - String reqItems = details.getString("requiredItems",""); - if (newChallenge.getChallengeType().equals(Challenge.ChallengeType.INVENTORY)) { - newChallenge.setRequiredItems(parseItems(reqItems)); - } else if (newChallenge.getChallengeType().equals(Challenge.ChallengeType.OTHER)) { - newChallenge.setRequiredIslandLevel(Long.parseLong(reqItems)); - } else if (newChallenge.getChallengeType().equals(Challenge.ChallengeType.ISLAND)) { - parseEntities(newChallenge, reqItems); - } - newChallenge.setRewardItems(parseItems(details.getString("itemReward", ""))); - newChallenge.setRepeatItemReward(parseItems(details.getString("repeatItemReward", ""))); - // Save - if (!details.getString("level", "").isEmpty()) - { - this.addon.getChallengesManager().addChallengeToLevel(newChallenge, - addon.getChallengesManager().getLevel( - Utils.getGameMode(world) + "_" + details.getString("level", ""))); - } - - if (addon.getChallengesManager().loadChallenge(newChallenge, overwrite, user, false)) { - size++; - } - } - - user.sendMessage("challenges.messages.import-number", "[number]", String.valueOf(size)); - } - - /** - * Run through entity types and materials and try to match to the string given - * @param challenge - challenge to be adjusted - * @param string - string from YAML file - */ - private void parseEntities(Challenge challenge, String string) { - Map req = new EnumMap<>(EntityType.class); - Map blocks = new EnumMap<>(Material.class); - if (!string.isEmpty()) { - for (String s : string.split(" ")) { - String[] part = s.split(":"); - try { - Arrays.asList(EntityType.values()).stream().filter(t -> t.name().equalsIgnoreCase(part[0])).forEach(t -> req.put(t, Integer.valueOf(part[1]))); - Arrays.asList(Material.values()).stream().filter(t -> t.name().equalsIgnoreCase(part[0])).forEach(t -> blocks.put(t, Integer.valueOf(part[1]))); - } catch (Exception e) { - addon.getLogger().severe("Cannot parse '" + s + "'. Skipping..."); - } - } - } - challenge.setRequiredEntities(req); - challenge.setRequiredBlocks(blocks); - } - - private List parseItems(String reqList) { - List result = new ArrayList<>(); - if (!reqList.isEmpty()) { - for (String s : reqList.split(" ")) { - ItemStack item = ItemParser.parse(s); - if (item != null) { - result.add(item); - } - } - } - return result; - } - // --------------------------------------------------------------------- // Section: Default Challenge Loader @@ -301,9 +126,9 @@ public class ChallengesImportManager } - // --------------------------------------------------------------------- - // Section: Default generation - // --------------------------------------------------------------------- +// --------------------------------------------------------------------- +// Section: Default generation +// --------------------------------------------------------------------- /** @@ -424,9 +249,9 @@ public class ChallengesImportManager } - // --------------------------------------------------------------------- - // Section: Private classes for default challegnes - // --------------------------------------------------------------------- +// --------------------------------------------------------------------- +// Section: Private classes for default challenges +// --------------------------------------------------------------------- /** @@ -633,4 +458,12 @@ public class ChallengesImportManager @Expose private String version; } + + +// --------------------------------------------------------------------- +// Section: Variables +// --------------------------------------------------------------------- + + + private ChallengesAddon addon; } \ No newline at end of file diff --git a/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java b/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java index aa86351..1377c9a 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/Challenges.java @@ -33,8 +33,7 @@ public class Challenges extends CompositeCommand // This method reloads challenges addon new ReloadChallenges(getAddon(), this); - // Import ASkyBlock Challenges - new ImportCommand(getAddon(), this); + // Defaults processing command new DefaultsCommand(this.getAddon(), this); diff --git a/src/main/java/world/bentobox/challenges/commands/admin/ImportCommand.java b/src/main/java/world/bentobox/challenges/commands/admin/ImportCommand.java deleted file mode 100644 index 882a70a..0000000 --- a/src/main/java/world/bentobox/challenges/commands/admin/ImportCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -package world.bentobox.challenges.commands.admin; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import world.bentobox.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 ImportCommand extends CompositeCommand { - - /** - * Import challenges - * @param addon - * @param cmd - */ - public ImportCommand(Addon addon, CompositeCommand cmd) { - super(addon, cmd, "import"); - } - - @Override - public boolean execute(User user, String label, List args) { - return ((ChallengesAddon)getAddon()).getImportManager().importChallenges(user, getWorld(), !args.isEmpty() && args.get(0).equalsIgnoreCase("overwrite")); - } - - @Override - public void setup() { - this.setPermission("challenges.admin"); - this.setParametersHelp("challenges.commands.admin.import.parameters"); - this.setDescription("challenges.commands.admin.import.description"); - } - - @Override - public Optional> tabComplete(User user, String alias, List args) { - String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; - return Optional.of(Util.tabLimit(Arrays.asList("overwrite"), lastArg)); - } - -} diff --git a/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java index 0971efb..58bec9a 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java @@ -15,7 +15,6 @@ 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; -import world.bentobox.bentobox.util.Util; import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.panel.CommonGUI; import world.bentobox.challenges.panel.util.ConfirmationGUI; @@ -61,7 +60,6 @@ public class AdminGUI extends CommonGUI EDIT_LEVEL, DELETE_CHALLENGE, DELETE_LEVEL, - IMPORT_CHALLENGES, EDIT_SETTINGS, DEFAULT_IMPORT_CHALLENGES, DEFAULT_EXPORT_CHALLENGES, @@ -123,8 +121,7 @@ public class AdminGUI extends CommonGUI // Import Challenges - panelBuilder.item(15, this.createButton(Button.IMPORT_CHALLENGES)); - panelBuilder.item(24, this.createButton(Button.DEFAULT_IMPORT_CHALLENGES)); + panelBuilder.item(15, this.createButton(Button.DEFAULT_IMPORT_CHALLENGES)); // 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)); @@ -355,31 +352,6 @@ public class AdminGUI extends CommonGUI break; } - case IMPORT_CHALLENGES: - { - permissionSuffix = IMPORT; - - name = this.user.getTranslation("challenges.gui.buttons.admin.import"); - description = this.user.getTranslation("challenges.gui.descriptions.admin.import"); - icon = new ItemStack(Material.HOPPER); - clickHandler = (panel, user, clickType, slot) -> { - if (clickType.isRightClick()) - { - this.overwriteMode = !this.overwriteMode; - this.build(); - } - else - { - // Run import command. - this.user.performCommand(this.topLabel + " " + CHALLENGES + " " + IMPORT + - (this.overwriteMode ? " overwrite" : "")); - } - return true; - }; - glow = this.overwriteMode; - - break; - } case DEFAULT_IMPORT_CHALLENGES: { permissionSuffix = DEFAULT;