|
| 1 | +package com.drtshock.playervaults.converters; |
| 2 | + |
| 3 | +import com.drtshock.playervaults.PlayerVaults; |
| 4 | +import com.drtshock.playervaults.vaultmanagement.VaultManager; |
| 5 | +import org.bukkit.Bukkit; |
| 6 | +import org.bukkit.command.CommandSender; |
| 7 | +import org.bukkit.inventory.Inventory; |
| 8 | + |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.UUID; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.logging.Level; |
| 13 | + |
| 14 | +public class MaltsConverter implements Converter { |
| 15 | + |
| 16 | + private static final String MALTS_API = "dev.jsinco.malts.api.MaltsAPI"; |
| 17 | + |
| 18 | + |
| 19 | + @Override |
| 20 | + public int run(CommandSender initiator) { |
| 21 | + PlayerVaults plugin = PlayerVaults.getInstance(); |
| 22 | + VaultManager vaultManager = VaultManager.getInstance(); |
| 23 | + int convertedCount = 0; |
| 24 | + |
| 25 | + try { |
| 26 | + CompletableFuture<Collection<Object>> allVaults = (CompletableFuture<Collection<Object>>) Class.forName(MALTS_API).getDeclaredMethod("getAllVaults").invoke(null); |
| 27 | + Collection<Object> vaults = allVaults.join(); // Block thread I guess |
| 28 | + |
| 29 | + |
| 30 | + for (Object vault : vaults) { |
| 31 | + try { |
| 32 | + UUID owner = (UUID) vault.getClass().getMethod("getOwner").invoke(vault); |
| 33 | + int id = (int) vault.getClass().getMethod("getId").invoke(vault); |
| 34 | + Inventory inventory = (Inventory) vault.getClass().getMethod("getInventory").invoke(vault); |
| 35 | + |
| 36 | + vaultManager.saveVault(inventory, owner.toString(), id); |
| 37 | + convertedCount++; |
| 38 | + } catch (ReflectiveOperationException e) { |
| 39 | + plugin.getLogger().severe("Failed to convert a vault: " + e.getMessage()); |
| 40 | + } |
| 41 | + } |
| 42 | + initiator.sendMessage("Converted " + convertedCount + " vaults from Malts."); |
| 43 | + |
| 44 | + } catch (ReflectiveOperationException e) { |
| 45 | + initiator.getServer().getLogger().log(Level.SEVERE, "Failed to convert vaults", e); |
| 46 | + return -1; |
| 47 | + } |
| 48 | + return convertedCount; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean canConvert() { |
| 53 | + return Bukkit.getServer().getPluginManager().isPluginEnabled("Malts"); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getName() { |
| 58 | + return "Malts"; |
| 59 | + } |
| 60 | +} |
0 commit comments