Skip to content

Commit

Permalink
clean up/attempt to idiot proof the migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Sep 8, 2024
1 parent 7bdcaed commit 8f5b00d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 38 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

### Fixed:
- CSGO Crate animation was delayed by 1 tick, for some reason.
- Fixed spacing in migrate command usage.

### Changes:
- Improved /crazycrates migrate internally
- ExcellentCrates Migrator has changed significantly, report any bugs you might find. It will convert legacy color codes to MiniMessage
- ExcellentCrates Migrator has changed significantly, report any bugs you might find. It will convert legacy color codes to MiniMessage.
- Properly warn the player/sender if the inputted migration type is not valid.
- Removed sections of code related to giving a prize if `Editor-Items`, `Commands` or `Items` were all not found.
- This would use the `DisplayItem`, `DisplayName`, `DisplayLore`, `DisplayEnchantments` and `DisplayAmount` as the prize.
- Updated the /crazycrates additem command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ public void sendRichMessage(final CommandSender sender) {

public void migrate() {
if (this.isList) {
this.messages.setProperty(this.properties, MiscUtils.fromComponent(MiscUtils.toComponent(this.messages.getProperty(this.properties))));
this.messages.setProperty(this.properties, MiscUtils.convert(this.messages.getProperty(this.properties), true));

return;
}

this.messages.setProperty(this.property, MiscUtils.fromComponent(MiscUtils.toComponent(this.messages.getProperty(this.property))));
this.messages.setProperty(this.property, MiscUtils.convert(this.messages.getProperty(this.property), true));
}

private @NotNull String parse(@NotNull final CommandSender sender, @NotNull final Map<String, String> placeholders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,18 @@ public static void spawnFirework(@NotNull final Location location, @Nullable fin
* @return {@link List<Component>}
*/
public static @NotNull List<String> fromComponent(@NotNull final List<Component> lore) {
return fromComponent(lore, false);
}

/**
* Converts a lore to minimessage strings
*
* @param lore {@link List<Component>}
* @return {@link List<Component>}
*/
public static @NotNull List<String> fromComponent(@NotNull final List<Component> lore, final boolean isMessage) {
return new ArrayList<>(lore.size()) {{
lore.forEach(line -> add(fromComponent(line)));
lore.forEach(line -> add(fromComponent(line, isMessage)));
}};
}

Expand All @@ -122,7 +132,68 @@ public static void spawnFirework(@NotNull final Location location, @Nullable fin
* @return {@link String}
*/
public static @NotNull String fromComponent(@NotNull final Component component) {
return MiniMessage.miniMessage().serialize(component);
return fromComponent(component, false);
}

/**
* Converts a {@link Component} to {@link String}.
*
* @param component {@link Component}
* @param isMessage true or false
* @return {@link String}
*/
public static @NotNull String fromComponent(@NotNull final Component component, final boolean isMessage) {
final String value = MiniMessage.miniMessage().serialize(component);

if (isMessage) {
return value.replace("\\<", "<");
}

return value;
}

/**
* Converts a {@link String} to a {@link Component} then a {@link String}.
*
* @param component {@link String}
* @return {@link String}
*/
public static @NotNull String convert(@NotNull final String component) {
return convert(component, false);
}

/**
* Converts a {@link List<String>} to a {@link List<Component>} then a {@link List<String>}.
*
* @param components {@link List<String>}
* @return {@link List<String>}
*/
public static @NotNull List<String> convert(@NotNull final List<String> components) {
return convert(components, false);
}

/**
* Converts a {@link List<String>} to a {@link List<Component>} then a {@link List<String>}.
*
* @param components {@link List<String>}
* @param isMessage true or false
* @return {@link List<String>}
*/
public static @NotNull List<String> convert(@NotNull final List<String> components, final boolean isMessage) {
return new ArrayList<>(components.size()) {{
components.forEach(line -> add(convert(line, isMessage)));
}};
}

/**
* Converts a {@link String} to a {@link Component} then a {@link String}.
*
* @param component {@link String}
* @param isMessage true or false
* @return {@link String}
*/
public static @NotNull String convert(@NotNull final String component, final boolean isMessage) {
return fromComponent(toComponent(component), isMessage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

Expand All @@ -27,27 +26,40 @@ public void run() {
final List<String> failed = new ArrayList<>();
final List<String> success = new ArrayList<>();

this.config.setProperty(ConfigKeys.command_prefix, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.command_prefix))));
this.config.setProperty(ConfigKeys.inventory_name, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.inventory_name))));
try {
this.config.setProperty(ConfigKeys.command_prefix, MiscUtils.convert(this.config.getProperty(ConfigKeys.command_prefix), true));
this.config.setProperty(ConfigKeys.inventory_name, MiscUtils.convert(this.config.getProperty(ConfigKeys.inventory_name), true));

this.config.setProperty(ConfigKeys.menu_button_name, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.menu_button_name))));
this.config.setProperty(ConfigKeys.menu_button_lore, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.menu_button_lore))));
this.config.setProperty(ConfigKeys.menu_button_name, MiscUtils.convert(this.config.getProperty(ConfigKeys.menu_button_name), true));
this.config.setProperty(ConfigKeys.menu_button_lore, MiscUtils.convert(this.config.getProperty(ConfigKeys.menu_button_lore), true));

this.config.setProperty(ConfigKeys.next_button_name, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.next_button_name))));
this.config.setProperty(ConfigKeys.next_button_lore, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.next_button_lore))));
this.config.setProperty(ConfigKeys.next_button_name, MiscUtils.convert(this.config.getProperty(ConfigKeys.next_button_name), true));
this.config.setProperty(ConfigKeys.next_button_lore, MiscUtils.convert(this.config.getProperty(ConfigKeys.next_button_lore), true));

this.config.setProperty(ConfigKeys.back_button_name, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.back_button_name))));
this.config.setProperty(ConfigKeys.back_button_lore, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.back_button_lore))));
this.config.setProperty(ConfigKeys.back_button_name, MiscUtils.convert(this.config.getProperty(ConfigKeys.back_button_name), true));
this.config.setProperty(ConfigKeys.back_button_lore, MiscUtils.convert(this.config.getProperty(ConfigKeys.back_button_lore), true));

this.config.setProperty(ConfigKeys.filler_name, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.filler_name))));
this.config.setProperty(ConfigKeys.filler_lore, MiscUtils.fromComponent(MiscUtils.toComponent(this.config.getProperty(ConfigKeys.filler_lore))));
this.config.setProperty(ConfigKeys.filler_name, MiscUtils.convert(this.config.getProperty(ConfigKeys.filler_name), true));
this.config.setProperty(ConfigKeys.filler_lore, MiscUtils.convert(this.config.getProperty(ConfigKeys.filler_lore), true));

for (Messages message : Messages.values()) {
message.migrate();
success.add("<green>⤷ config.yml");

this.config.reload();
} catch (Exception exception) {
failed.add("<red>⤷ config.yml");
}

this.config.reload();
this.messages.reload();
try {
for (Messages message : Messages.values()) {
message.migrate();
}

success.add("<green>⤷ messages.yml");

this.messages.reload();
} catch (Exception exception) {
failed.add("<red>⤷ messages.yml");
}

customFiles.forEach(customFile -> {
try {
Expand All @@ -62,55 +74,55 @@ public void run() {
boolean isSave = false;

if (section.contains("CrateName")) {
set(section, "Name", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("CrateName", " "))));
set(section, "Name", MiscUtils.convert(section.getString("CrateName", " "), true));
set(section, "CrateName", null);

isSave = true;
} else if (section.contains("Name")) {
set(section, "Name", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("Name", " "))));
set(section, "Name", MiscUtils.convert(section.getString("Name", " "), true));

isSave = true;
}

if (section.contains("Preview-Name")) {
set(section, "Preview.Name", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("Preview.Name", " "))));
set(section, "Preview.Name", MiscUtils.convert(section.getString("Preview.Name", " "), true));
set(section, "Preview-Name", null);

isSave = true;
} else if (section.contains("Preview.Name")) {
set(section, "Preview.Name", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("Preview.Name", " "))));
set(section, "Preview.Name", MiscUtils.convert(section.getString("Preview.Name", " "), true));

isSave = true;
}

if (section.contains("Preview.Glass.Name")) {
set(section, "Preview.Glass.Name", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("Preview.Glass.Name", " "))));
set(section, "Preview.Glass.Name", MiscUtils.convert(section.getString("Preview.Glass.Name", " "), true));

isSave = true;
}

if (section.contains("tier-preview.glass.name")) {
set(section, "tier-preview.glass.name", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("tier-preview.glass.name", " "))));
set(section, "tier-preview.glass.name", MiscUtils.convert(section.getString("tier-preview.glass.name", " "), true));

isSave = true;
}

if (section.contains("BroadCast")) {
set(section, "BroadCast", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("BroadCast", " "))));
set(section, "BroadCast", MiscUtils.convert(section.getString("BroadCast", " "), true));

isSave = true;
}

if (section.contains("Lore")) {
set(section, "Lore", MiscUtils.fromComponent(MiscUtils.toComponent(section.getStringList("Lore"))));
set(section, "Lore", MiscUtils.convert(section.getStringList("Lore"), true));

isSave = true;
}

if (section.contains("PhysicalKey")) {
set(section, "PhysicalKey.Name", MiscUtils.fromComponent(MiscUtils.toComponent(section.getString("PhysicalKey.Name", " "))));
set(section, "PhysicalKey.Name", MiscUtils.convert(section.getString("PhysicalKey.Name", " "), true));

set(section, "PhysicalKey.Lore", MiscUtils.fromComponent(MiscUtils.toComponent(section.getStringList("PhysicalKey.Lore"))));
set(section, "PhysicalKey.Lore", MiscUtils.convert(section.getStringList("PhysicalKey.Lore"), true));

isSave = true;
}
Expand All @@ -124,13 +136,13 @@ public void run() {
if (tierSection == null) continue;

if (tierSection.contains("Name")) {
set(tierSection, "Name", MiscUtils.fromComponent(MiscUtils.toComponent(tierSection.getString("Name", " "))));
set(tierSection, "Name", MiscUtils.convert(tierSection.getString("Name", " "), true));

isSave = true;
}

if (tierSection.contains("Lore")) {
set(tierSection, "Lore", MiscUtils.fromComponent(MiscUtils.toComponent(tierSection.getStringList("Name"))));
set(tierSection, "Lore", MiscUtils.convert(tierSection.getStringList("Name"), true));

isSave = true;
}
Expand All @@ -146,36 +158,36 @@ public void run() {
if (prizeSection == null) continue;

if (prizeSection.contains("Lore")) {
set(prizeSection, "DisplayLore", MiscUtils.fromComponent(MiscUtils.toComponent(prizeSection.getStringList("Lore"))));
set(prizeSection, "DisplayLore", MiscUtils.convert(prizeSection.getStringList("Lore"), true));
set(prizeSection, "Lore", null);

isSave = true;
} else if (prizeSection.contains("DisplayLore")) {
set(prizeSection, "DisplayLore", MiscUtils.fromComponent(MiscUtils.toComponent(prizeSection.getStringList("DisplayLore"))));
set(prizeSection, "DisplayLore", MiscUtils.convert(prizeSection.getStringList("DisplayLore"), true));

isSave = true;
}

if (prizeSection.contains("Messages")) {
set(prizeSection, "Messages", MiscUtils.fromComponent(MiscUtils.toComponent(prizeSection.getStringList("Messages"))));
set(prizeSection, "Messages", MiscUtils.convert(prizeSection.getStringList("Messages"), true));

isSave = true;
}

if (prizeSection.contains("Items")) {
set(prizeSection, "Items", MiscUtils.fromComponent(MiscUtils.toComponent(prizeSection.getStringList("Items"))));
set(prizeSection, "Items", MiscUtils.convert(prizeSection.getStringList("Items"), true));

isSave = true;
}

if (prizeSection.contains("Alternative-Prize.Messages")) {
set(prizeSection, "Alternative-Prize.Messages", MiscUtils.fromComponent(MiscUtils.toComponent(prizeSection.getStringList("Alternative-Prize.Messages"))));
set(prizeSection, "Alternative-Prize.Messages", MiscUtils.convert(prizeSection.getStringList("Alternative-Prize.Messages"), true));

isSave = true;
}

if (prizeSection.contains("Alternative-Prize.Items")) {
set(prizeSection, "Alternative-Prize.Items", MiscUtils.fromComponent(MiscUtils.toComponent(prizeSection.getStringList("Alternative-Prize.Items"))));
set(prizeSection, "Alternative-Prize.Items", MiscUtils.convert(prizeSection.getStringList("Alternative-Prize.Items"), true));

isSave = true;
}
Expand Down

0 comments on commit 8f5b00d

Please sign in to comment.