Skip to content

Commit

Permalink
Overhaul the migrate command, and add new migrators (#747)
Browse files Browse the repository at this point in the history
* start working on a migrator for ExcellentCrates

* Create an items section out of DisplayData

DisplayData should be solely for the preview only, if I see there a prize that contains DisplayData but has no value in Items, I will put it in Items

* update additem command to reflect last commit change

* add nightcore as a compile only lib

* add excellentcrates as a soft dependency

* add message stating plugin not enabled when migrating

* add a friendly name to the migration types

* send message if plugin not enabled

* create crate file if it doesn't exist

* update migrator impl

* update the migrator for excellent crates

* add more detailed import message

* made the migrator look fancy

now that the migrator works, we abstract and make it look fancy

* bump to 3.7

---------

Signed-off-by: Ryder Belserion <[email protected]>
  • Loading branch information
ryderbelserion authored Aug 13, 2024
1 parent 2a8539c commit 6a9b1d6
Show file tree
Hide file tree
Showing 17 changed files with 714 additions and 213 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Binary file added libs/ExcellentCrates-5.3.1.jar
Binary file not shown.
Binary file added libs/nightcore-2.6.3.jar
Binary file not shown.
3 changes: 0 additions & 3 deletions run/eula.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,6 +92,18 @@ public static void load() {
return numbers;
});

commandManager.registerSuggestion(SuggestionKey.of("migrators"), (sender, context) -> {
final List<String> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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<String> enchants = new ArrayList<>() {{
prize.getStringList("DisplayEnchantments").forEach(enchant -> add(ItemUtils.getEnchant(enchant)));
}};

set(prize, "DisplayEnchantments", enchants);
}
});
}

file.save();
file.reload();
}

private <T> void set(ConfigurationSection section, String path, T value) {
section.set(path, value);
}
}
Loading

0 comments on commit 6a9b1d6

Please sign in to comment.