Add converting framework as well as the converter for Backpack

Adds command /pvconvert <all | plugin name>
Adds permission playervaults.convert
Adds Converter interface for future converters
Adds BackpackConverter - A converter for Backpack
(http://dev.bukkit.org/bukkit-plugins/backpack/)
This commit is contained in:
turt2live
2014-08-31 17:06:10 -06:00
parent 9741c80014
commit 338d2cb889
7 changed files with 200 additions and 5 deletions
@@ -77,6 +77,7 @@ public class PlayerVaults extends JavaPlugin {
getCommand("pvdel").setExecutor(new DeleteCommand()); getCommand("pvdel").setExecutor(new DeleteCommand());
getCommand("pvsign").setExecutor(new SignCommand()); getCommand("pvsign").setExecutor(new SignCommand());
getCommand("workbench").setExecutor(new WorkbenchCommand()); getCommand("workbench").setExecutor(new WorkbenchCommand());
getCommand("pvconvert").setExecutor(new ConvertCommand());
useVault = setupEconomy(); useVault = setupEconomy();
startMetrics(); startMetrics();
@@ -0,0 +1,57 @@
package com.drtshock.playervaults.commands;
import com.drtshock.playervaults.converters.BackpackConverter;
import com.drtshock.playervaults.converters.Converter;
import com.drtshock.playervaults.util.Lang;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.List;
public class ConvertCommand implements CommandExecutor {
private List<Converter> converters = new ArrayList<>();
public ConvertCommand() {
converters.add(new BackpackConverter());
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!sender.hasPermission("playervaults.convert")) {
sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
} else {
if (args.length == 0) {
sender.sendMessage(Lang.TITLE + "/pvconvert <all | plugin name>");
} else {
String name = args[0];
List<Converter> applicableConverters = new ArrayList<>();
if (name.equalsIgnoreCase("all")) {
applicableConverters.addAll(converters);
} else {
for (Converter converter : converters) {
if (converter.getName().equalsIgnoreCase(name)) {
applicableConverters.add(converter);
}
}
}
if (applicableConverters.size() <= 0) {
sender.sendMessage(Lang.TITLE.toString() + Lang.CONVERT_PLUGIN_NOT_FOUND);
} else {
int converted = 0;
for (Converter converter : applicableConverters) {
if (converter.canConvert()) {
converted += converter.doConvert();
}
}
sender.sendMessage(Lang.TITLE + Lang.CONVERT_COMPLETE.toString().replace("%converted", converted + ""));
}
}
}
return true;
}
}
@@ -0,0 +1,95 @@
package com.drtshock.playervaults.converters;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.vaultmanagement.UUIDVaultManager;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
* Simple converter for Backpack (http://dev.bukkit.org/bukkit-plugins/backpack/)
*
* @author turt2live
*/
public class BackpackConverter implements Converter {
@Override
public int doConvert() {
PlayerVaults plugin = PlayerVaults.getInstance();
File destination = new File(plugin.getDataFolder().getParentFile(), "Backpack" + File.separator + "backpacks");
if (!destination.exists()) return -1;
int converted = 0;
File[] worldDirs = destination.listFiles();
int vaultNum = 1;
for (File file : worldDirs != null ? worldDirs : new File[0]) {
if (file.isDirectory()) {
converted += convert(file, vaultNum);
vaultNum++;
}
}
return converted;
}
private int convert(File worldFolder, int intoVaultNum) {
PlayerVaults plugin = PlayerVaults.getInstance();
UUIDVaultManager vaults = UUIDVaultManager.getInstance();
int converted = 0;
File[] files = worldFolder.listFiles();
for (File file : files != null ? files : new File[0]) {
if (file.isFile() && file.getName().toLowerCase().endsWith(".yml")) {
OfflinePlayer player = plugin.getServer().getOfflinePlayer(file.getName().substring(0, file.getName().lastIndexOf('.')));
if (player == null || player.getUniqueId() == null) {
plugin.getLogger().warning("Unable to convert Backpack for player: " + (player != null ? player.getName() : file.getName()));
} else {
UUID uuid = player.getUniqueId();
FileConfiguration yaml = YamlConfiguration.loadConfiguration(file);
ConfigurationSection section = yaml.getConfigurationSection("backpack");
if (section.getKeys(false).size() <= 0) continue; // No slots
Inventory vault = vaults.getVault(uuid, intoVaultNum);
if (vault == null) vault = plugin.getServer().createInventory(null, section.getKeys(false).size());
for (String key : section.getKeys(false)) {
ConfigurationSection slotSection = section.getConfigurationSection(key);
ItemStack item = slotSection.getItemStack("ItemStack");
if (item == null) continue;
// Overwrite
vault.setItem(Integer.parseInt(key.split(" ")[1]), item);
}
try {
vaults.saveVault(vault, uuid, intoVaultNum);
converted++;
} catch (IOException e) {
plugin.getLogger().severe("Error converting Backpack: " + file.getName());
e.printStackTrace();
}
}
}
}
return converted;
}
@Override
public boolean canConvert() {
PlayerVaults plugin = PlayerVaults.getInstance();
File expectedFolder = new File(plugin.getDataFolder().getParentFile(), "Backpack");
if (!expectedFolder.exists()) return false;
File backpackDir = new File(expectedFolder, "backpacks");
return backpackDir.exists();
}
@Override
public String getName() {
return "Backpack";
}
}
@@ -0,0 +1,34 @@
package com.drtshock.playervaults.converters;
/**
* Represents a simple converter for converting another plugin's content
* to PlayerVaults.
*
* @author turt2live
*/
public interface Converter {
/**
* Converts the other plugin's data.
*
* @return the number of vaults converted. Returns 0 on none converted
* or -1 if no vaults were converted.
*/
public int doConvert();
/**
* Determines if this converter is applicable for converting to PlayerVaults.
* This may check for the existance of a plugin, plugin folder, or otherwise.
*
* @return true if this converter can convert, false otherwise
*/
public boolean canConvert();
/**
* Gets the name of this converter
*
* @return the converter name
*/
public String getName();
}
@@ -45,7 +45,9 @@ public enum Lang {
EXISTING_VAULTS("existing-vaults", "&f%p has vaults: &a%v"), EXISTING_VAULTS("existing-vaults", "&f%p has vaults: &a%v"),
VAULT_TITLE("vault-title", "&4Vault #%number"), VAULT_TITLE("vault-title", "&4Vault #%number"),
OPEN_WITH_SIGN("open-with-sign", "&fOpening vault &a%v &fof &a%p"), OPEN_WITH_SIGN("open-with-sign", "&fOpening vault &a%v &fof &a%p"),
NO_PLAYER_FOUND("no-player-found", "&cCannot find player &a%p"); NO_PLAYER_FOUND("no-player-found", "&cCannot find player &a%p"),
CONVERT_PLUGIN_NOT_FOUND("plugin-not-found", "&cNo converter found for that plugin"),
CONVERT_COMPLETE("conversion-complete", "&aConverted %converted players to PlayerVaults");
private String path; private String path;
private String def; private String def;
+3
View File
@@ -3,6 +3,7 @@
# %v is where the vault number will get inserted. # %v is where the vault number will get inserted.
# %price is the price. # %price is the price.
# %number is the vault number # %number is the vault number
# %converted is the number of vaults converted (where applicable)
# Made with love :) # Made with love :)
title-name: "&4[&fPlayerVaults&4]:" title-name: "&4[&fPlayerVaults&4]:"
open-vault: "&fOpening vault &a%v" open-vault: "&fOpening vault &a%v"
@@ -21,3 +22,5 @@ cost-to-open: "&fYou were charged &c%price &ffor opening that vault."
vault-number: "&4Vault #%number" vault-number: "&4Vault #%number"
existing-vaults: "&f%p has vaults: &a%v" existing-vaults: "&f%p has vaults: &a%v"
no-player-found: "&cCannot find player &a%p" no-player-found: "&cCannot find player &a%p"
plugin-not-found: "&cNo converter found for that plugin"
conversion-complete: "&aConverted %converted players to PlayerVaults"
+3
View File
@@ -17,3 +17,6 @@ commands:
workbench: workbench:
description: Opens a workbench. description: Opens a workbench.
aliases: [wb] aliases: [wb]
pvconvert:
description: Convert other plugins to PlayerVaults
aliases: [vaultconvert]