Allow async conversion and use a UUID service where possible

This commit is contained in:
turt2live
2014-08-31 18:45:58 -06:00
parent 7eb269fc8e
commit 57272da0cc
9 changed files with 157 additions and 25 deletions
+35
View File
@@ -33,6 +33,32 @@
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.turt2live:UUID-Library</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.turt2live.uuid</pattern>
<shadedPattern>com.drtshock.playervaults.lib.uuid</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
@@ -45,6 +71,10 @@
<id>vault-repo</id>
<url>http://nexus.theyeticave.net/content/repositories/pub_releases/</url>
</repository>
<repository>
<id>turt2live-repo</id>
<url>http://repo.turt2live.com</url>
</repository>
</repositories>
<dependencies>
@@ -58,5 +88,10 @@
<artifactId>Vault</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>com.turt2live</groupId>
<artifactId>UUID-Library</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
@@ -1,25 +1,43 @@
package com.drtshock.playervaults.commands;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.converters.BackpackConverter;
import com.drtshock.playervaults.converters.Converter;
import com.drtshock.playervaults.util.Lang;
import com.drtshock.playervaults.vaultmanagement.VaultOperations;
import com.turt2live.uuid.CachingServiceProvider;
import com.turt2live.uuid.ServiceProvider;
import com.turt2live.uuid.turt2live.v2.ApiV2Service;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class ConvertCommand implements CommandExecutor {
private List<Converter> converters = new ArrayList<>();
private ServiceProvider uuidProvider;
public ConvertCommand() {
converters.add(new BackpackConverter());
CachingServiceProvider cachedUuidProvider = new CachingServiceProvider(new ApiV2Service());
Map<UUID, String> seed = new HashMap<>();
for (OfflinePlayer player : PlayerVaults.getInstance().getServer().getOfflinePlayers()) {
if (player.hasPlayedBefore()) {
seed.put(player.getUniqueId(), player.getName());
}
}
cachedUuidProvider.seedLoad(seed, 6 * 60 * 60); // 6 hour cache time
uuidProvider = cachedUuidProvider;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
if (!sender.hasPermission("playervaults.convert")) {
sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
} else {
@@ -27,7 +45,7 @@ public class ConvertCommand implements CommandExecutor {
sender.sendMessage(Lang.TITLE + "/pvconvert <all | plugin name>");
} else {
String name = args[0];
List<Converter> applicableConverters = new ArrayList<>();
final List<Converter> applicableConverters = new ArrayList<>();
if (name.equalsIgnoreCase("all")) {
applicableConverters.addAll(converters);
} else {
@@ -41,14 +59,23 @@ public class ConvertCommand implements CommandExecutor {
if (applicableConverters.size() <= 0) {
sender.sendMessage(Lang.TITLE.toString() + Lang.CONVERT_PLUGIN_NOT_FOUND);
} else {
// Fork into background
sender.sendMessage(Lang.TITLE + Lang.CONVERT_BACKGROUND.toString());
PlayerVaults.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(PlayerVaults.getInstance(), new Runnable() {
@Override
public void run() {
int converted = 0;
VaultOperations.setLocked(true);
for (Converter converter : applicableConverters) {
if (converter.canConvert()) {
converted += converter.doConvert();
converted += converter.run(sender, uuidProvider);
}
}
VaultOperations.setLocked(false);
sender.sendMessage(Lang.TITLE + Lang.CONVERT_COMPLETE.toString().replace("%converted", converted + ""));
}
}, 5); // This comment is to annoy evilmidget38
}
}
}
@@ -12,6 +12,11 @@ import org.bukkit.entity.Player;
public class DeleteCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(VaultOperations.isLocked()){
sender.sendMessage(Lang.TITLE+Lang.LOCKED.toString());
return true;
}
switch (args.length) {
case 1:
if (sender instanceof Player) {
@@ -17,6 +17,11 @@ import org.bukkit.entity.Player;
public class VaultCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(VaultOperations.isLocked()){
sender.sendMessage(Lang.TITLE+Lang.LOCKED.toString());
return true;
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (PlayerVaults.getInstance().getInVault().containsKey(player.getName())) {
@@ -2,7 +2,9 @@ package com.drtshock.playervaults.converters;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.vaultmanagement.UUIDVaultManager;
import org.bukkit.OfflinePlayer;
import com.turt2live.uuid.PlayerRecord;
import com.turt2live.uuid.ServiceProvider;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -21,7 +23,9 @@ import java.util.UUID;
public class BackpackConverter implements Converter {
@Override
public int doConvert() {
public int run(CommandSender initiator, ServiceProvider uuidProvider) {
if(uuidProvider == null)throw new IllegalArgumentException();
PlayerVaults plugin = PlayerVaults.getInstance();
File destination = new File(plugin.getDataFolder().getParentFile(), "Backpack" + File.separator + "backpacks");
if (!destination.exists()) return -1;
@@ -32,7 +36,7 @@ public class BackpackConverter implements Converter {
int vaultNum = 1;
for (File file : worldDirs != null ? worldDirs : new File[0]) {
if (file.isDirectory()) {
converted += convert(file, vaultNum);
converted += convert(file, vaultNum, uuidProvider);
vaultNum++;
}
}
@@ -40,18 +44,19 @@ public class BackpackConverter implements Converter {
return converted;
}
private int convert(File worldFolder, int intoVaultNum) {
private int convert(File worldFolder, int intoVaultNum, ServiceProvider uuidProvider) {
PlayerVaults plugin = PlayerVaults.getInstance();
UUIDVaultManager vaults = UUIDVaultManager.getInstance();
int converted = 0;
long lastUpdate = 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) {
PlayerRecord player = uuidProvider.doLookup(file.getName().substring(0,file.getName().lastIndexOf('.')));
if (player == null || player.getUuid() == null) {
plugin.getLogger().warning("Unable to convert Backpack for player: " + (player != null ? player.getName() : file.getName()));
} else {
UUID uuid = player.getUniqueId();
UUID uuid = player.getUuid();
FileConfiguration yaml = YamlConfiguration.loadConfiguration(file);
ConfigurationSection section = yaml.getConfigurationSection("backpack");
if (section.getKeys(false).size() <= 0) continue; // No slots
@@ -73,6 +78,11 @@ public class BackpackConverter implements Converter {
plugin.getLogger().severe("Error converting Backpack: " + file.getName());
e.printStackTrace();
}
if (System.currentTimeMillis() - lastUpdate >= 1500) {
plugin.getLogger().info(converted + " backpacks have been converted in " + worldFolder.getAbsolutePath());
lastUpdate = System.currentTimeMillis();
}
}
}
}
@@ -1,5 +1,8 @@
package com.drtshock.playervaults.converters;
import com.turt2live.uuid.ServiceProvider;
import org.bukkit.command.CommandSender;
/**
* Represents a simple converter for converting another plugin's content
* to PlayerVaults.
@@ -11,10 +14,13 @@ public interface Converter {
/**
* Converts the other plugin's data.
*
* @param initiator the initiator of the conversion. May be null
* @param uuidProvider the UUID provider to use, cannot be null
*
* @return the number of vaults converted. Returns 0 on none converted
* or -1 if no vaults were converted.
*/
public int doConvert();
public int run(CommandSender initiator, ServiceProvider uuidProvider);
/**
* Determines if this converter is applicable for converting to PlayerVaults.
@@ -47,7 +47,9 @@ public enum Lang {
OPEN_WITH_SIGN("open-with-sign", "&fOpening vault &a%v &fof &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");
CONVERT_COMPLETE("conversion-complete", "&aConverted %converted players to PlayerVaults"),
CONVERT_BACKGROUND("conversion-background", "&fConversion has been forked to the background. See console for updates."),
LOCKED("vaults-locked", "&cVaults are currently locked while conversion occurs. Please try again in a moment!");
private String path;
private String def;
@@ -16,17 +16,53 @@
*/
package com.drtshock.playervaults.vaultmanagement;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.util.Lang;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
public class VaultOperations {
private static AtomicBoolean LOCKED = new AtomicBoolean(false);
/**
* Gets whether or not player vaults are locked
*
* @return true if locked, false otherwise
*/
public static boolean isLocked() {
return LOCKED.get();
}
/**
* Sets whether or not player vaults are locked. If set to true, this
* will kick anyone who is currently using their vaults out.
*
* @param locked true for locked, false otherwise
*/
public static void setLocked(boolean locked) {
LOCKED.set(locked);
if (locked) {
for (Player player : PlayerVaults.getInstance().getServer().getOnlinePlayers()) {
if (player.getOpenInventory() != null) {
InventoryView view = player.getOpenInventory();
if (view.getTopInventory().getHolder() instanceof VaultHolder) {
player.closeInventory();
player.sendMessage(Lang.TITLE + Lang.LOCKED.toString());
}
}
}
}
}
/**
* Check whether or not the player has permission to open the requested vault.
*
@@ -75,6 +111,7 @@ public class VaultOperations {
* @return Whether or not the player was allowed to open it.
*/
public static boolean openOwnVault(Player player, String arg) {
if (isLocked()) return false;
int number;
try {
number = Integer.parseInt(arg);
@@ -112,6 +149,7 @@ public class VaultOperations {
* @return Whether or not the player was allowed to open it.
*/
public static boolean openOtherVault(Player player, Player holder, String arg) {
if (isLocked()) return false;
if (player.hasPermission("playervaults.admin")) {
int number = 0;
try {
@@ -143,6 +181,7 @@ public class VaultOperations {
* @param arg The vault number to delete.
*/
public static void deleteOwnVault(Player player, String arg) {
if (isLocked()) return;
if (arg.matches("^[0-9]{1,2}$")) {
int number = 0;
try {
@@ -175,6 +214,7 @@ public class VaultOperations {
* @param arg The vault number to delete.
*/
public static void deleteOtherVault(CommandSender sender, Player holder, String arg) {
if (isLocked()) return;
if (sender.hasPermission("playervaults.delete")) {
if (arg.matches("^[0-9]{1,2}$")) {
int number = 0;
+2
View File
@@ -24,3 +24,5 @@ existing-vaults: "&f%p has vaults: &a%v"
no-player-found: "&cCannot find player &a%p"
plugin-not-found: "&cNo converter found for that plugin"
conversion-complete: "&aConverted %converted players to PlayerVaults"
conversion-background: "&fConversion has been forked to the background. See console for updates."
vaults-locked: "&cVaults are currently locked while conversion occurs. Please try again in a moment!"