diff --git a/build.gradle.kts b/build.gradle.kts index 2f233e0a8..b45f1d74e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { val buildNumber: String? = System.getenv("BUILD_NUMBER") -rootProject.version = if (buildNumber != null) "${libs.versions.minecraft.get()}-$buildNumber" else "3.6.1" +rootProject.version = if (buildNumber != null) "${libs.versions.minecraft.get()}-$buildNumber" else "3.7" val isSnapshot = false diff --git a/libs/ExcellentCrates-5.3.1.jar b/libs/ExcellentCrates-5.3.1.jar new file mode 100644 index 000000000..9d5745a6d Binary files /dev/null and b/libs/ExcellentCrates-5.3.1.jar differ diff --git a/libs/nightcore-2.6.3.jar b/libs/nightcore-2.6.3.jar new file mode 100644 index 000000000..fb4eff1a3 Binary files /dev/null and b/libs/nightcore-2.6.3.jar differ diff --git a/run/eula.txt b/run/eula.txt deleted file mode 100644 index 1dd01a0f9..000000000 --- a/run/eula.txt +++ /dev/null @@ -1,3 +0,0 @@ -#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://aka.ms/MinecraftEULA). -#Mon Oct 16 20:19:20 EDT 2023 -eula=true diff --git a/src/main/java/com/badbones69/crazycrates/api/enums/Messages.java b/src/main/java/com/badbones69/crazycrates/api/enums/Messages.java index 63ae58418..b20cd78b4 100644 --- a/src/main/java/com/badbones69/crazycrates/api/enums/Messages.java +++ b/src/main/java/com/badbones69/crazycrates/api/enums/Messages.java @@ -87,7 +87,8 @@ public enum Messages { transfer_not_enough_keys(CommandKeys.transfer_not_enough_keys), error_migrating(CommandKeys.error_migrating), migration_not_available(CommandKeys.migration_not_available), - successfully_migrated(CommandKeys.successfully_migrated), + migration_plugin_not_enabled(CommandKeys.migration_plugin_not_enabled), + successfully_migrated(CommandKeys.successfully_migrated, true), transfer_sent_keys(CommandKeys.transfer_sent_keys), transfer_received_keys(CommandKeys.transfer_received_keys), no_virtual_keys(CommandKeys.no_virtual_keys), diff --git a/src/main/java/com/badbones69/crazycrates/api/utils/MiscUtils.java b/src/main/java/com/badbones69/crazycrates/api/utils/MiscUtils.java index a83a0f137..f7d960238 100644 --- a/src/main/java/com/badbones69/crazycrates/api/utils/MiscUtils.java +++ b/src/main/java/com/badbones69/crazycrates/api/utils/MiscUtils.java @@ -270,4 +270,8 @@ public static void registerPermissions() { public static boolean isLogging() { return ConfigManager.getConfig().getProperty(ConfigKeys.verbose_logging); } + + public static boolean isExcellentCratesEnabled() { + return plugin.getServer().getPluginManager().isPluginEnabled("ExcellentCrates"); + } } \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/commands/CommandManager.java b/src/main/java/com/badbones69/crazycrates/commands/CommandManager.java index c86d1e964..7e72aba62 100644 --- a/src/main/java/com/badbones69/crazycrates/commands/CommandManager.java +++ b/src/main/java/com/badbones69/crazycrates/commands/CommandManager.java @@ -4,6 +4,7 @@ import com.badbones69.crazycrates.api.objects.other.CrateLocation; import com.badbones69.crazycrates.commands.crates.types.admin.crates.CommandAddItem; import com.badbones69.crazycrates.commands.crates.types.admin.crates.CommandMigrate; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums.MigrationType; import com.badbones69.crazycrates.commands.relations.ArgumentRelations; import com.badbones69.crazycrates.commands.crates.types.player.CommandHelp; import com.badbones69.crazycrates.commands.crates.types.admin.CommandAdmin; @@ -91,6 +92,18 @@ public static void load() { return numbers; }); + commandManager.registerSuggestion(SuggestionKey.of("migrators"), (sender, context) -> { + final List migrators = new ArrayList<>(); + + for (MigrationType value : MigrationType.values()) { + final String name = value.getName(); + + migrators.add(name); + } + + return migrators; + }); + commandManager.registerArgument(PlayerBuilder.class, (sender, context) -> new PlayerBuilder(context)); List.of( diff --git a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/CommandMigrate.java b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/CommandMigrate.java index 10223612f..be52e721f 100644 --- a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/CommandMigrate.java +++ b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/CommandMigrate.java @@ -1,231 +1,45 @@ package com.badbones69.crazycrates.commands.crates.types.admin.crates; import com.badbones69.crazycrates.api.enums.Messages; -import com.badbones69.crazycrates.api.utils.ItemUtils; +import com.badbones69.crazycrates.api.utils.MiscUtils; import com.badbones69.crazycrates.commands.crates.types.BaseCommand; -import com.ryderbelserion.vital.core.util.FileUtil; -import com.ryderbelserion.vital.paper.files.config.CustomFile; -import com.ryderbelserion.vital.paper.util.ItemUtil; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums.MigrationType; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types.MojangMappedMigratorMultiple; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types.MojangMappedMigratorSingle; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types.deprecation.DeprecatedCrateMigrator; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types.plugins.ExcellentCratesMigrator; import dev.triumphteam.cmd.bukkit.annotation.Permission; import dev.triumphteam.cmd.core.annotations.ArgName; import dev.triumphteam.cmd.core.annotations.Command; import dev.triumphteam.cmd.core.annotations.Optional; import dev.triumphteam.cmd.core.annotations.Suggestion; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.permissions.PermissionDefault; -import org.jetbrains.annotations.Nullable; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; public class CommandMigrate extends BaseCommand { - public enum MigrationType { - MOJANG_MAPPED_SINGLE("MojangMappedSingle"), - MOJANG_MAPPED_ALL("MojangMappedAll"), - - //MIGRATE_OLD_COMMANDS("MigrateOldCommands"), - - CRATES_DEPRECATED_ALL("CratesDeprecated"), - - EXCELLENT_CRATES("ExcellentCrates"), - - SPECIALIZED_CRATES("SpecializedCrates"); - - private final String name; - - MigrationType(String name) { - this.name = name; - } - - public final String getName() { - return this.name; - } - } - @Command("migrate") @Permission(value = "crazycrates.migrate", def = PermissionDefault.OP) - public void migrate(final CommandSender sender, @ArgName("migration_type") final MigrationType type, @ArgName("crate") @Optional @Suggestion("crates") final String crateName) { - switch (type) { - case MOJANG_MAPPED_ALL -> this.plugin.getFileManager().getCustomFiles().forEach(customFile -> migrate(sender, customFile, "", type)); + public void migrate(final CommandSender sender, @ArgName("migration_type") @Suggestion("migrators") final String name, @ArgName("crate") @Optional @Suggestion("crates") final String crateName) { + final MigrationType type = MigrationType.valueOf(name.toUpperCase()); - case MOJANG_MAPPED_SINGLE -> { - if (crateName == null || crateName.isEmpty() || crateName.isBlank()) { - Messages.cannot_be_empty.sendMessage(sender, "{value}", "crate name"); + switch (type) { + case MOJANG_MAPPED_ALL -> new MojangMappedMigratorMultiple(sender, MigrationType.MOJANG_MAPPED_ALL).run(); + case MOJANG_MAPPED_SINGLE -> new MojangMappedMigratorSingle(sender, MigrationType.MOJANG_MAPPED_SINGLE, crateName).run(); - return; - } + case CRATES_DEPRECATED_ALL -> new DeprecatedCrateMigrator(sender, MigrationType.CRATES_DEPRECATED_ALL).run(); - final CustomFile file = this.fileManager.getCustomFile(crateName); + case SPECIALIZED_CRATES -> sender.sendRichMessage(Messages.migration_not_available.getMessage(sender)); - if (file == null) { - Messages.error_migrating.sendMessage(sender, new HashMap<>() {{ - put("{file}", crateName); - put("{type}", type.getName()); - put("{reason}", "File was not loaded properly."); - }}); + case EXCELLENT_CRATES -> { + if (!MiscUtils.isExcellentCratesEnabled()) { + sender.sendRichMessage(Messages.migration_plugin_not_enabled.getMessage(sender, "{name}", type.getName())); return; } - migrate(sender, file, crateName, type); - - Messages.successfully_migrated.sendMessage(sender, new HashMap<>() {{ - put("{file}", crateName); - put("{type}", type.getName()); - }}); + new ExcellentCratesMigrator(sender).run(); } - - case CRATES_DEPRECATED_ALL -> { - this.plugin.getFileManager().getCustomFiles().forEach(file -> { - final YamlConfiguration configuration = file.getConfiguration(); - - final ConfigurationSection prizes = configuration.getConfigurationSection("Crate.Prizes"); - - if (prizes != null) { - prizes.getKeys(false).forEach(value -> { - if (configuration.contains("Crate.Prizes." + value + ".Lore")) { - configuration.set("Crate.Prizes." + value + ".DisplayLore", configuration.getStringList("Crate.Prizes." + value + ".Lore")); - - configuration.set("Crate.Prizes." + value + ".Lore", null); - } - - if (configuration.contains("Crate.Prizes." + value + ".Patterns")) { - configuration.set("Crate.Prizes." + value + ".DisplayPatterns", configuration.getStringList("Crate.Prizes." + value + ".Patterns")); - - configuration.set("Crate.Prizes." + value + ".Patterns", null); - } - }); - - file.save(); - } - }); - - Messages.successfully_migrated.sendMessage(sender, new HashMap<>() {{ - put("{file}", crateName); - put("{type}", type.getName()); - }}); - - this.plugin.getFileManager().init(); - } - - /*case MIGRATE_OLD_COMMANDS -> { - final File oldDirectory = new File(this.plugin.getDataFolder(), "old"); - - final List files = FileUtil.getFiles(oldDirectory, ".yml"); - - files.forEach(key -> { - final @Nullable CustomFile customFile = this.plugin.getFileManager().getCustomFile(key); - - if (customFile != null) { - final File file = new File(oldDirectory, key + ".yml"); - - final YamlConfiguration root = YamlConfiguration.loadConfiguration(file); - - final ConfigurationSection prizes = root.getConfigurationSection("Crate.Prizes"); - - if (prizes != null) { - final YamlConfiguration configuration = customFile.getConfiguration(); - - prizes.getKeys(false).forEach(value -> { - List commands = prizes.getStringList(value + ".Commands"); - - if (!commands.isEmpty()) { - configuration.set("Crate.Prizes." + value + ".Commands", commands); - } - }); - - customFile.save(); - } - } - }); - }*/ - - case SPECIALIZED_CRATES, EXCELLENT_CRATES -> Messages.migration_not_available.sendMessage(sender); } } - - private void migrate(final CommandSender sender, final CustomFile file, final String crateName, final MigrationType type) { - final YamlConfiguration configuration = file.getConfiguration(); - - final ConfigurationSection crate = configuration.getConfigurationSection("Crate"); - - if (crate == null) { - Messages.error_migrating.sendMessage(sender, new HashMap<>() {{ - put("{file}", crateName.isEmpty() ? file.getStrippedName() : crateName); - put("{type}", type.getName()); - put("{reason}", "File could not be found in our data, likely invalid yml file that didn't load properly."); - }}); - - return; - } - - set(crate, "Item", crate.getString("Item", "diamond").toLowerCase()); - set(crate, "Preview.Glass.Item", crate.getString("Preview.Glass.Item", "gray_stained_glass_pane").toLowerCase()); - set(crate, "PhysicalKey.Item", crate.getString("PhysicalKey.Item", "lime_dye").toLowerCase()); - - final ConfigurationSection prizes = crate.getConfigurationSection("Prizes"); - - if (prizes != null) { - prizes.getKeys(false).forEach(key -> { - final ConfigurationSection prize = prizes.getConfigurationSection(key); - - if (prize == null) return; - - if (prize.contains("DisplayItem")) { - set(prize, "DisplayItem", prize.getString("DisplayItem", "red_terracotta").toLowerCase()); - } - - if (prize.contains("DisplayTrim")) { - set(prize, "DisplayTrim.Material", prize.getString("DisplayTrim.Material", "quartz").toLowerCase()); - set(prize, "DisplayTrim.Pattern", prize.getString("DisplayTrim.Pattern", "sentry").toLowerCase()); - } - - if (prize.contains("Editor-Items")) { - final List items = prize.getList("Editor-Items"); - - if (items != null) { - items.forEach(item -> { - final org.bukkit.inventory.ItemStack itemStack = (org.bukkit.inventory.ItemStack) item; - - final String asBase64 = ItemUtil.toBase64(itemStack); - - if (prize.contains("Items")) { - final List list = prize.getStringList("Items"); - - list.add("Data: " + asBase64); - - prize.set("Items", list); - } else { - prize.set("Items", new ArrayList<>() {{ - add("Data: " + asBase64); - }}); - } - }); - - prize.set("Editor-Items", null); - } - } - - if (prize.contains("DisplayEnchantments")) { - List enchants = new ArrayList<>() {{ - prize.getStringList("DisplayEnchantments").forEach(enchant -> add(ItemUtils.getEnchant(enchant))); - }}; - - set(prize, "DisplayEnchantments", enchants); - } - }); - } - - file.save(); - file.reload(); - } - - private void set(ConfigurationSection section, String path, T value) { - section.set(path, value); - } } \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/ICrateMigrator.java b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/ICrateMigrator.java new file mode 100644 index 000000000..99f026b26 --- /dev/null +++ b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/ICrateMigrator.java @@ -0,0 +1,146 @@ +package com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator; + +import com.badbones69.crazycrates.CrazyCrates; +import com.badbones69.crazycrates.api.enums.Messages; +import com.badbones69.crazycrates.api.utils.ItemUtils; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums.MigrationType; +import com.badbones69.crazycrates.tasks.crates.CrateManager; +import com.ryderbelserion.vital.paper.files.config.CustomFile; +import com.ryderbelserion.vital.paper.files.config.FileManager; +import com.ryderbelserion.vital.paper.util.ItemUtil; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public abstract class ICrateMigrator { + + protected final CrazyCrates plugin = JavaPlugin.getPlugin(CrazyCrates.class); + + protected final CrateManager crateManager = this.plugin.getCrateManager(); + + protected final FileManager fileManager = this.plugin.getFileManager(); + + protected final CommandSender sender; + + protected final MigrationType type; + + public ICrateMigrator(final CommandSender sender, MigrationType type) { + this.sender = sender; + this.type = type; + } + + protected String crateName; + + public ICrateMigrator(final CommandSender sender, final MigrationType type, final String crateName) { + this(sender, type); + + this.crateName = crateName; + } + + public abstract void run(); + + public abstract void set(ConfigurationSection section, String path, T value); + + public abstract File getCratesDirectory(); + + public void sendMessage(List files, final int failed, final int success) { + Messages.successfully_migrated.sendMessage(this.sender, new HashMap<>() {{ + if (files.size() > 1) { + final StringBuilder sb = new StringBuilder(); + + files.forEach(sb::append); + + put("{files}", sb.toString()); + } else { + put("{files}", files.getFirst()); + } + + put("{succeeded_amount}", String.valueOf(success)); + put("{failed_amount}", String.valueOf(failed)); + put("{type}", type.getName()); + }}); + } + + public void migrate(final CustomFile customFile, final String crateName) { + final YamlConfiguration configuration = customFile.getConfiguration(); + + final ConfigurationSection crate = configuration.getConfigurationSection("Crate"); + + if (crate == null) { + Messages.error_migrating.sendMessage(sender, new HashMap<>() {{ + put("{file}", crateName.isEmpty() ? customFile.getStrippedName() : crateName); + put("{type}", type.getName()); + put("{reason}", "File could not be found in our data, likely invalid yml file that didn't load properly."); + }}); + + return; + } + + set(crate, "Item", crate.getString("Item", "diamond").toLowerCase()); + set(crate, "Preview.Glass.Item", crate.getString("Preview.Glass.Item", "gray_stained_glass_pane").toLowerCase()); + set(crate, "PhysicalKey.Item", crate.getString("PhysicalKey.Item", "lime_dye").toLowerCase()); + + final ConfigurationSection prizes = crate.getConfigurationSection("Prizes"); + + if (prizes != null) { + prizes.getKeys(false).forEach(key -> { + final ConfigurationSection prize = prizes.getConfigurationSection(key); + + if (prize == null) return; + + if (prize.contains("DisplayItem")) { + set(prize, "DisplayItem", prize.getString("DisplayItem", "red_terracotta").toLowerCase()); + } + + if (prize.contains("DisplayTrim")) { + set(prize, "DisplayTrim.Material", prize.getString("DisplayTrim.Material", "quartz").toLowerCase()); + set(prize, "DisplayTrim.Pattern", prize.getString("DisplayTrim.Pattern", "sentry").toLowerCase()); + } + + if (prize.contains("Editor-Items")) { + final List items = prize.getList("Editor-Items"); + + if (items != null) { + items.forEach(item -> { + final org.bukkit.inventory.ItemStack itemStack = (org.bukkit.inventory.ItemStack) item; + + final String asBase64 = ItemUtil.toBase64(itemStack); + + if (prize.contains("Items")) { + final List list = prize.getStringList("Items"); + + list.add("Data: " + asBase64); + + prize.set("Items", list); + } else { + prize.set("Items", new ArrayList<>() {{ + add("Data: " + asBase64); + }}); + } + }); + + prize.set("Editor-Items", null); + } + } + + if (prize.contains("DisplayEnchantments")) { + List enchants = new ArrayList<>() {{ + prize.getStringList("DisplayEnchantments").forEach(enchant -> add(ItemUtils.getEnchant(enchant))); + }}; + + set(prize, "DisplayEnchantments", enchants); + } + }); + } + + customFile.save(); + customFile.reload(); + + //todo() add crate to plugin + } +} \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/enums/MigrationType.java b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/enums/MigrationType.java new file mode 100644 index 000000000..0dd080996 --- /dev/null +++ b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/enums/MigrationType.java @@ -0,0 +1,24 @@ +package com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums; + +public enum MigrationType { + MOJANG_MAPPED_SINGLE("MojangMappedSingle"), + MOJANG_MAPPED_ALL("MojangMappedAll"), + + //MIGRATE_OLD_COMMANDS("MigrateOldCommands"), + + CRATES_DEPRECATED_ALL("CratesDeprecated"), + + EXCELLENT_CRATES("ExcellentCrates"), + + SPECIALIZED_CRATES("SpecializedCrates"); + + private final String name; + + MigrationType(String name) { + this.name = name; + } + + public final String getName() { + return this.name; + } +} \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/MojangMappedMigratorMultiple.java b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/MojangMappedMigratorMultiple.java new file mode 100644 index 000000000..1e6efa502 --- /dev/null +++ b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/MojangMappedMigratorMultiple.java @@ -0,0 +1,58 @@ +package com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types; + +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.ICrateMigrator; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums.MigrationType; +import com.ryderbelserion.vital.paper.files.config.CustomFile; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class MojangMappedMigratorMultiple extends ICrateMigrator { + + public MojangMappedMigratorMultiple(final CommandSender sender, final MigrationType type) { + super(sender, type); + } + + @Override + public void run() { + final Set customFiles = this.plugin.getFileManager().getCustomFiles(); + + final List failed = new ArrayList<>(); + final List success = new ArrayList<>(); + + customFiles.forEach(customFile -> { + try { + migrate(customFile, ""); + + success.add("" + customFile.getStrippedName()); + } catch (Exception exception) { + failed.add("" + customFile.getStrippedName()); + } + }); + + // reload crates + this.crateManager.loadHolograms(); + this.crateManager.loadCrates(); + + final int failedCrates = failed.size(); + final int convertedCrates = success.size(); + + sendMessage(new ArrayList<>(failedCrates + convertedCrates) {{ + addAll(failed); + addAll(success); + }}, failedCrates, convertedCrates); + } + + @Override + public void set(ConfigurationSection section, String path, T value) { + section.set(path, value); + } + + @Override + public final File getCratesDirectory() { + return new File(this.plugin.getDataFolder(), "crates"); + } +} \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/MojangMappedMigratorSingle.java b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/MojangMappedMigratorSingle.java new file mode 100644 index 000000000..c30888d5d --- /dev/null +++ b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/MojangMappedMigratorSingle.java @@ -0,0 +1,74 @@ +package com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types; + +import com.badbones69.crazycrates.api.enums.Messages; +import com.badbones69.crazycrates.api.objects.Crate; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.ICrateMigrator; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums.MigrationType; +import com.ryderbelserion.vital.paper.files.config.CustomFile; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class MojangMappedMigratorSingle extends ICrateMigrator { + + public MojangMappedMigratorSingle(final CommandSender sender, final MigrationType type, final String crateName) { + super(sender, type, crateName); + } + + @Override + public void run() { + if (this.crateName == null || this.crateName.isEmpty() || this.crateName.isBlank()) { + Messages.cannot_be_empty.sendMessage(this.sender, "{value}", "crate name"); + + return; + } + + final CustomFile customFile = this.plugin.getFileManager().getCustomFile(this.crateName); + + if (customFile == null) { + Messages.error_migrating.sendMessage(this.sender, new HashMap<>() {{ + put("{file}", crateName); + put("{type}", type.getName()); + put("{reason}", "File was not loaded properly."); + }}); + + return; + } + + final List failed = new ArrayList<>(); + final List success = new ArrayList<>(); + + try { + migrate(customFile, this.crateName); + + success.add("" + this.crateName); + } catch (Exception exception) { + failed.add("" + this.crateName); + } + + final Crate crate = this.crateManager.getCrateFromName(this.crateName); + + this.crateManager.reloadCrate(crate); + + final int failedCrates = failed.size(); + final int convertedCrates = success.size(); + + sendMessage(new ArrayList<>(failedCrates + convertedCrates) {{ + addAll(failed); + addAll(success); + }}, failedCrates, convertedCrates); + } + + @Override + public void set(ConfigurationSection section, String path, T value) { + section.set(path, value); + } + + @Override + public final File getCratesDirectory() { + return new File(this.plugin.getDataFolder(), "crates"); + } +} \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/deprecation/DeprecatedCrateMigrator.java b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/deprecation/DeprecatedCrateMigrator.java new file mode 100644 index 000000000..fa6dc3dd4 --- /dev/null +++ b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/deprecation/DeprecatedCrateMigrator.java @@ -0,0 +1,77 @@ +package com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types.deprecation; + +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.ICrateMigrator; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums.MigrationType; +import com.ryderbelserion.vital.paper.files.config.CustomFile; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class DeprecatedCrateMigrator extends ICrateMigrator { + + public DeprecatedCrateMigrator(final CommandSender sender, final MigrationType type) { + super(sender, type); + } + + @Override + public void run() { + final Set customFiles = this.plugin.getFileManager().getCustomFiles(); + + final List failed = new ArrayList<>(); + final List success = new ArrayList<>(); + + customFiles.forEach(customFile -> { + try { + final YamlConfiguration configuration = customFile.getConfiguration(); + + final ConfigurationSection prizes = configuration.getConfigurationSection("Crate.Prizes"); + + if (prizes != null) { + prizes.getKeys(false).forEach(value -> { + if (configuration.contains("Crate.Prizes." + value + ".Lore")) { + configuration.set("Crate.Prizes." + value + ".DisplayLore", configuration.getStringList("Crate.Prizes." + value + ".Lore")); + + configuration.set("Crate.Prizes." + value + ".Lore", null); + } + + if (configuration.contains("Crate.Prizes." + value + ".Patterns")) { + configuration.set("Crate.Prizes." + value + ".DisplayPatterns", configuration.getStringList("Crate.Prizes." + value + ".Patterns")); + + configuration.set("Crate.Prizes." + value + ".Patterns", null); + } + }); + + customFile.save(); + } + + success.add("" + customFile.getStrippedName()); + } catch (Exception exception) { + failed.add("" + customFile.getStrippedName()); + } + }); + + final int convertedCrates = failed.size(); + final int failedCrates = success.size(); + + sendMessage(new ArrayList<>(failedCrates + convertedCrates) {{ + addAll(failed); + addAll(success); + }}, failedCrates, convertedCrates); + + this.fileManager.init(); + } + + @Override + public void set(ConfigurationSection section, String path, T value) { + section.set(path, value); + } + + @Override + public final File getCratesDirectory() { + return new File(this.plugin.getDataFolder(), "crates"); + } +} \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/plugins/ExcellentCratesMigrator.java b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/plugins/ExcellentCratesMigrator.java new file mode 100644 index 000000000..3603b292c --- /dev/null +++ b/src/main/java/com/badbones69/crazycrates/commands/crates/types/admin/crates/migrator/types/plugins/ExcellentCratesMigrator.java @@ -0,0 +1,268 @@ +package com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.types.plugins; + +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.ICrateMigrator; +import com.badbones69.crazycrates.commands.crates.types.admin.crates.migrator.enums.MigrationType; +import com.ryderbelserion.vital.paper.files.config.CustomFile; +import com.ryderbelserion.vital.paper.util.ItemUtil; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import su.nightexpress.excellentcrates.CratesAPI; +import su.nightexpress.excellentcrates.crate.impl.Crate; +import su.nightexpress.excellentcrates.key.CrateKey; +import su.nightexpress.nightcore.config.FileConfig; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class ExcellentCratesMigrator extends ICrateMigrator { + + public ExcellentCratesMigrator(final CommandSender sender) { + super(sender, MigrationType.EXCELLENT_CRATES); + } + + @Override + public void run() { + final File directory = getCratesDirectory(); + + if (!directory.exists()) { + directory.mkdirs(); + } + + final File crateDirectory = CratesAPI.PLUGIN.getDataFolder(); + + final List failed = new ArrayList<>(); + final List success = new ArrayList<>(); + + for (final Crate crate : CratesAPI.getCrateManager().getCrates()) { + final String crateName = crate.getFile().getName(); + + final File crateFile = new File(directory, crateName); + + if (crateFile.exists()) { + this.plugin.getComponentLogger().warn("Crate {} already exists in {}.", crateName, directory.getName()); + + failed.add("" + crateName); + + return; + } + + try { + crateFile.createNewFile(); + } catch (IOException exception) { + this.plugin.getComponentLogger().warn("Failed to create crate file {} in {}.", crateName, directory.getName(), exception); + + failed.add("" + crateName); + } + + final CustomFile customFile = new CustomFile(directory).apply(crateName); + + final YamlConfiguration configuration = customFile.getConfiguration(); + + set(configuration, "Crate.CrateType", "CSGO"); + + final ConfigurationSection root = configuration.getConfigurationSection("Crate"); + + if (root == null) return; + + final FileConfig crateConfig = crate.getConfig(); + + set(root, "Glowing", false); + + final String name = crate.getPreviewConfig(); + + final File file = new File(new File(crateDirectory, "menu"), name == null ? "default.yml" : name + ".yml"); + + if (file.exists()) { + final YamlConfiguration menuFile = YamlConfiguration.loadConfiguration(file); + + final String previewName = menuFile.getString("Crate.Name", "<#9af7ff>%crate%").replace("%crate_name%", "%crate%").replace("%crate%", crateName.replace(".yml", "")); + + final List previewLore = new ArrayList<>(); + + menuFile.getStringList("Crate.Lore").forEach(line -> previewLore.add(line.replaceAll(" 0); + set(root, "Slot", slot); + + break; + } + } + } + } + + set(root, "Preview.Toggle", true); + set(root, "Preview.ChestLines", 6); + set(root, "Preview.Glass.Toggle", true); + set(root, "Preview.Glass.Name", " "); + set(root, "Preview.Glass.Item", "gray_stained_glass_pane"); + set(root, "Preview.Glass.Custom-Model-Data", -1); + + set(root, "StartingKeys", 0); + set(root, "RequiredKeys", 0); + set(root, "Max-Mass-Open", 10); + + set(root, "OpeningBroadCast", false); + set(root, "BroadCast", "%prefix%%player% is opening a %crate%.".replace("%crate%", crate.getName())); + + set(root, "opening-command.toggle", false); + set(root, "opening-command.commands", List.of("put your command here.")); + + set(root, "sound.cycle-sound.toggle", false); + set(root, "sound.cycle-sound.value", "block.note_block_xylophone"); + set(root, "sound.cycle-sound.volume", 1.0); + set(root, "sound.cycle-sound.pitch", 1.0); + + set(root, "sound.click-sound.toggle", false); + set(root, "sound.click-sound.value", "block.note_block_xylophone"); + set(root, "sound.click-sound.volume", 1.0); + set(root, "sound.click-sound.pitch", 1.0); + + set(root, "sound.stop-sound.toggle", false); + set(root, "sound.stop-sound.value", "block.note_block_xylophone"); + set(root, "sound.stop-sound.volume", 1.0); + set(root, "sound.stop-sound.pitch", 1.0); + + set(root, "Prize-Message", List.of("You have won %reward% from %crate%.")); + + final ItemStack crateItem = crate.getItem(); + + set(root, "Item", crateConfig.getString("Item.Material", "player_head").toLowerCase()); + + final String itemName = crateConfig.getString("Item.Name", "%crate%").replace("%crate%", crateName); + + set(root, "Preview-Name", itemName + " Preview"); + + if (crateItem.hasItemMeta()) { + final ItemMeta itemMeta = crateItem.getItemMeta(); + + set(root, "Custom-Model-Data", itemMeta.hasCustomModelData() ? itemMeta.getCustomModelData() : -1); + } + + set(root, "Settings.Knockback", crate.isPushbackEnabled()); + + set(root, "Hologram.Toggle", crate.isHologramEnabled()); + set(root, "Hologram.Height", crate.getHologramYOffset()); + set(root, "Hologram.Range", 8); + set(root, "Hologram.Update-Interval", -1); + set(root, "Hologram.Color", "transparent"); + + List hologramText = new ArrayList<>(); + crate.getHologramText().forEach(line -> { + final String filtered = line.replace( + "%excellentcrates_keys_" + crateName.replace(".yml", "") + "%", + "%crazycrates_" + crateName.replace(".yml", "") + "%" + ).replace( + "%crate%", + crate.getName() + ); + + hologramText.add(filtered); + }); + + set(root, "Hologram.Message", hologramText); + + final Optional value = crate.getKeys().stream().findFirst(); + + if (value.isPresent()) { + final CrateKey key = value.get(); + + final FileConfig config = key.getConfig(); + + final ItemStack itemStack = key.getItem(); + + set(root, "PhysicalKey.Data", ItemUtil.toBase64(itemStack)); + + set(root, "PhysicalKey.Name", key.getName()); + set(root, "PhysicalKey.Item", itemStack.getType().getKey().getKey()); + set(root, "PhysicalKey.Lore", List.of()); + + set(root, "PhysicalKey.Glowing", config.contains("Item.Enchants")); + } + + crate.getRewards().forEach(reward -> { + // Get the id i.e. '1': + final String id = reward.getId(); + + set(root, "Prizes." + id + ".DisplayData", ItemUtil.toBase64(reward.getPreview())); + + set(root, "Prizes." + id + ".Commands", reward.getCommands()); + + set(root, "Prizes." + id + ".Settings.Broadcast.Toggle", reward.isBroadcast()); + set(root, "Prizes." + id + ".Settings.Broadcast.Messages", List.of()); + set(root, "Prizes." + id + ".Settings.Broadcast.Permission", "your_permission"); + + set(root, "Prizes." + id + ".BlackListed-Permissions", reward.getIgnoredForPermissions()); + + set(root, "Prizes." + id + ".Chance", (int) reward.getWeight()); + + final ConfigurationSection section = root.getConfigurationSection("Prizes"); + + if (section == null) return; + + final ConfigurationSection prizes = section.getConfigurationSection(id); + + if (prizes == null) return; + + reward.getItems().forEach(itemStack -> { + final String base64 = ItemUtil.toBase64(itemStack); + + if (prizes.contains("Items")) { + final List items = prizes.getStringList("Items"); + + items.add("Data: " + base64); + + set(prizes, "Items", items); + } else { + set(prizes, "Items", new ArrayList<>() {{ + add("Data: " + base64); + }}); + } + }); + }); + + customFile.save(); + + this.fileManager.addCustomFile(customFile); + + success.add("" + crateName); + } + + // reload crates + this.crateManager.loadHolograms(); + this.crateManager.loadCrates(); + + final int failedCrates = failed.size(); + final int convertedCrates = success.size(); + + sendMessage(new ArrayList<>(failedCrates + convertedCrates) {{ + addAll(failed); + addAll(success); + }}, failedCrates, convertedCrates); + } + + @Override + public void set(final ConfigurationSection section, final String path, final T value) { + section.set(path, value); + } + + @Override + public final File getCratesDirectory() { + return new File(this.plugin.getDataFolder(), "crates"); + } +} \ No newline at end of file diff --git a/src/main/java/com/badbones69/crazycrates/config/impl/messages/CommandKeys.java b/src/main/java/com/badbones69/crazycrates/config/impl/messages/CommandKeys.java index d16ce0380..2e6207c61 100644 --- a/src/main/java/com/badbones69/crazycrates/config/impl/messages/CommandKeys.java +++ b/src/main/java/com/badbones69/crazycrates/config/impl/messages/CommandKeys.java @@ -52,10 +52,28 @@ public void registerComments(CommentsConfiguration conf) { @Comment("A list of available placeholders: {file}, {type}, {reason}") public static final Property error_migrating = newProperty("command.migrate.error", "{prefix}We could not migrate {file} using {type} migration for {reason}."); - public static final Property migration_not_available = newProperty("command.migrate.not-available", "{prefix}This migration type is not available."); + @Comment("A list of available placeholders: {prefix}") + public static final Property migration_not_available = newProperty("command.migrate.not-available", "{prefix}This migration type is not available."); - @Comment("A list of available placeholders: {file}, {type}") - public static final Property successfully_migrated = newProperty("command.migrate.success", "{prefix}Successfully migrated {file} using {type} migration."); + public static final Property migration_plugin_not_enabled = newProperty("command.migrate.plugin-not-available", "{prefix}The plugin {name} is not enabled. Cannot use as migration!"); + + @Comment({ + "A list of available placeholders: {type}, {files}", + "", + "{files} will output multiple crates if migrating from another plugin" + }) + public static final Property> successfully_migrated = newListProperty("command.migrate.success", List.of( + "━━━━━━━━━━━━━━━━━━━ Migration Stats ━━━━━━━━━━━━━━━━━━━", + "» Successful Conversions: ", + " ⤷ {succeeded_amount}", + "» Failed Conversions: ", + " ⤷ {failed_amount}", + "", + "Type: {type}", + "Files: {files}", + "", + "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + )); public static final Property reloaded_plugin = newProperty("command.reload.completed", "{prefix}You have reloaded the Config and Data Files."); diff --git a/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java b/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java index 545b64c6e..4d9d43bcb 100644 --- a/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java +++ b/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java @@ -1335,11 +1335,12 @@ private ItemBuilder getKey(@NotNull final FileConfiguration file) { final String name = file.getString("Crate.PhysicalKey.Name", ""); final int customModelData = file.getInt("Crate.PhysicalKey.Custom-Model-Data", -1); final List lore = file.getStringList("Crate.PhysicalKey.Lore"); - final String id = file.getString("Crate.PhysicalKey.Item", "tripwire_hook"); final boolean glowing = file.getBoolean("Crate.PhysicalKey.Glowing", true); final boolean hideFlags = file.getBoolean("Crate.PhysicalKey.HideItemFlags", false); - return new ItemBuilder().withType(id.toLowerCase()).setDisplayName(name).setDisplayLore(lore).setGlowing(glowing).setHidingItemFlags(hideFlags).setCustomModelData(customModelData); + final ItemBuilder itemBuilder = file.contains("Crate.PhysicalKey.Data") ? new ItemBuilder().fromBase64(file.getString("Crate.PhysicalKey.Data")) : new ItemBuilder().withType(file.getString("Crate.PhysicalKey.Item", "tripwire_hook")); + + return itemBuilder.setDisplayName(name).setDisplayLore(lore).setGlowing(glowing).setHidingItemFlags(hideFlags).setCustomModelData(customModelData); } // Cleans the data file. diff --git a/src/main/resources/paper-plugin.yml b/src/main/resources/paper-plugin.yml index 1a7b950f1..0ee9e9366 100644 --- a/src/main/resources/paper-plugin.yml +++ b/src/main/resources/paper-plugin.yml @@ -31,6 +31,12 @@ dependencies: required: false join-classpath: true HeadDatabase: + load: BEFORE + required: false + join-classpath: true + + # Other crate plugins to migrate for people + ExcellentCrates: load: BEFORE required: false join-classpath: true \ No newline at end of file