Skip to content

Commit

Permalink
fixed the migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Jul 19, 2024
1 parent 7e3ab09 commit 6703491
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 90 deletions.
22 changes: 5 additions & 17 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
### Fixed:
- Re-worked how display names are handled for items/previews, This format now works properly and stacks with vanilla items.
```yml
'1':
# The item to display/give in the gui.
DisplayItem: "diamond"
# The amount to display/give
DisplayAmount: 3
# The max range i.e. 25/100 = 15% chance to win.
MaxRange: 100
# The chance to win i.e. 25%
Chance: 25
```
- Applied a bandaid to quadcrates
- Use correct crate name for {crate} when a crate location already exists in `/crazycrates set <crate>`
- Fixed npe with /keys view, player name wasn't supplied, so it freaked out.
- Fixed npe with placeholder parsing in messages.
- Issue with /crazycrates migrator.
- Multiple null checks in commands.
- Issue with invalid argument output not outputting the proper syntax.

### Changes:
- Optimize display reward above quad/quick crate
- Updated migrator command format.
- Updated invalid argument output to just describe it better.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public static ItemBuilder convertString(String itemString, String section) {
String value = optionString.replace(option + ":", "").replace(option, "");

switch (option.toLowerCase()) {
case "item" -> itemBuilder.withType(value);
case "item" -> itemBuilder.withType(value.toLowerCase());
case "data" -> itemBuilder = itemBuilder.fromBase64(value);
case "name" -> itemBuilder.setDisplayName(value);
case "amount" -> {
Expand All @@ -328,10 +328,10 @@ public static ItemBuilder convertString(String itemString, String section) {
case "trim-pattern" -> itemBuilder.applyTrimPattern(value);
case "trim-material" -> itemBuilder.applyTrimMaterial(value);
default -> {
if (getEnchantment(option) != null) {
if (getEnchantment(option.toLowerCase()) != null) {
final Optional<Number> amount = StringUtil.tryParseInt(value);

itemBuilder.addEnchantment(option, amount.map(Number::intValue).orElse(1), true);
itemBuilder.addEnchantment(option.toLowerCase(), amount.map(Number::intValue).orElse(1), true);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import com.badbones69.crazycrates.api.utils.ItemUtils;
import com.badbones69.crazycrates.commands.crates.types.BaseCommand;
import com.ryderbelserion.vital.paper.files.config.CustomFile;
import com.ryderbelserion.vital.paper.util.ItemUtil;
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;
Expand All @@ -19,110 +22,118 @@ public class CommandMigrate extends BaseCommand {

public enum MigrationType {
MOJANG_MAPPED,
SPECIALIZED_CRATES
SPECIALIZED_CRATES,
ALL
}

@Command("migrate")
@Permission(value = "crazycrates.migrate", def = PermissionDefault.OP)
public void migrate(final CommandSender sender, @Suggestion("crates") final String crateName, final MigrationType type) {
if (crateName.isEmpty() || crateName.isBlank()) {
sender.sendRichMessage(Messages.cannot_be_empty.getMessage(sender, "{value}", "crate name"));

return;
}

final CustomFile file = this.fileManager.getCustomFile(crateName);

if (file == null) {
sender.sendRichMessage(Messages.error_migrating.getMessage(sender, new HashMap<>() {{
put("{file}", crateName);
put("{type}", String.valueOf(type));
put("{reason}", "File was not loaded properly.");
}}));

return;
}

public void migrate(final CommandSender sender, @ArgName("migration_type") final MigrationType type, @ArgName("crate") @Optional @Suggestion("crates") final String crateName) {
switch (type) {
case ALL -> this.plugin.getFileManager().getCustomFiles().forEach(customFile -> migrate(sender, customFile, "", type));

case MOJANG_MAPPED -> {
final YamlConfiguration configuration = file.getConfiguration();
if (crateName == null || crateName.isEmpty() || crateName.isBlank()) {
sender.sendRichMessage(Messages.cannot_be_empty.getMessage(sender, "{value}", "crate name"));

return;
}

final ConfigurationSection crate = configuration.getConfigurationSection("Crate");
final CustomFile file = this.fileManager.getCustomFile(crateName);

if (crate == null) {
if (file == null) {
sender.sendRichMessage(Messages.error_migrating.getMessage(sender, new HashMap<>() {{
put("{file}", crateName);
put("{type}", String.valueOf(type));
put("{reason}", "File could not be found in our data, likely invalid yml file that didn't load properly.");
put("{reason}", "File was not loaded 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());
migrate(sender, file, crateName, type);
}

final ConfigurationSection prizes = configuration.getConfigurationSection("Prizes");
case SPECIALIZED_CRATES -> sender.sendRichMessage(Messages.migration_not_available.getMessage(sender));
}
}

if (prizes != null) {
prizes.getKeys(false).forEach(key -> {
final ConfigurationSection prize = prizes.getConfigurationSection(key);
private void migrate(final CommandSender sender, final CustomFile file, final String crateName, final MigrationType type) {
final YamlConfiguration configuration = file.getConfiguration();

if (prize == null) return;
final ConfigurationSection crate = configuration.getConfigurationSection("Crate");

if (prize.contains("DisplayItem")) {
set(prize, "DisplayItem", prize.getString("DisplayItem", "red_terracotta").toLowerCase());
}
if (crate == null) {
sender.sendRichMessage(Messages.error_migrating.getMessage(sender, new HashMap<>() {{
put("{file}", crateName.isEmpty() ? file.getStrippedName() : crateName);
put("{type}", String.valueOf(type));
put("{reason}", "File could not be found in our data, likely invalid yml file that didn't load properly.");
}}));

if (prize.contains("DisplayTrim")) {
set(prize, "DisplayTrim.Material", prize.getString("DisplayTrim.Material", "quartz").toLowerCase());
set(prize, "DisplayTrim.Pattern", prize.getString("DisplayTrim.Pattern", "sentry").toLowerCase());
}
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());

if (prize.contains("DisplayEnchantments")) {
List<String> enchants = new ArrayList<>() {{
prize.getStringList("DisplayEnchantments").forEach(enchant -> add(ItemUtils.getEnchant(enchant)));
}};
final ConfigurationSection prizes = crate.getConfigurationSection("Prizes");

set(prize, "DisplayEnchantments", enchants);
}
if (prizes != null) {
prizes.getKeys(false).forEach(key -> {
final ConfigurationSection prize = prizes.getConfigurationSection(key);

if (prize.contains("Items")) {
set(prize, "Items", getItems(prize.getStringList("Items")));
}
if (prize == null) return;

if (prize.contains("Alternative-Prize.Items")) {
set(prize, "Items", getItems(prize.getStringList("Alternative-Prize.Items")));
}
});
if (prize.contains("DisplayItem")) {
set(prize, "DisplayItem", prize.getString("DisplayItem", "red_terracotta").toLowerCase());
}

file.save();
}
if (prize.contains("DisplayTrim")) {
set(prize, "DisplayTrim.Material", prize.getString("DisplayTrim.Material", "quartz").toLowerCase());
set(prize, "DisplayTrim.Pattern", prize.getString("DisplayTrim.Pattern", "sentry").toLowerCase());
}

case SPECIALIZED_CRATES -> sender.sendRichMessage(Messages.migration_not_available.getMessage(sender));
}
}
if (prize.contains("Editor-Items")) {
final List<?> items = prize.getList("Editor-Items");

private <T> void set(ConfigurationSection section, String path, T value) {
section.set(path, value);
}
if (items != null) {
items.forEach(item -> {
final org.bukkit.inventory.ItemStack itemStack = (org.bukkit.inventory.ItemStack) item;

private List<String> getItems(List<String> items) {
return new ArrayList<>() {{
items.forEach(line -> {
for (String option : line.split(", ")) {
String key = option.split(":")[0];
String value = option.replace(option + ":", "").replace(option, "");
final String asBase64 = ItemUtil.toBase64(itemStack);

switch (key.toLowerCase()) {
case "item", "trim-material", "trim-pattern" -> option = option.replace(value, value.toLowerCase());
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)));
}};

add(ItemUtils.getEnchant(option));
set(prize, "DisplayEnchantments", enchants);
}
});
}};
}

file.save();
}

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

0 comments on commit 6703491

Please sign in to comment.