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> <target>1.7</target>
</configuration> </configuration>
</plugin> </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> </plugins>
</build> </build>
@@ -45,6 +71,10 @@
<id>vault-repo</id> <id>vault-repo</id>
<url>http://nexus.theyeticave.net/content/repositories/pub_releases/</url> <url>http://nexus.theyeticave.net/content/repositories/pub_releases/</url>
</repository> </repository>
<repository>
<id>turt2live-repo</id>
<url>http://repo.turt2live.com</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@@ -58,5 +88,10 @@
<artifactId>Vault</artifactId> <artifactId>Vault</artifactId>
<version>1.2.32</version> <version>1.2.32</version>
</dependency> </dependency>
<dependency>
<groupId>com.turt2live</groupId>
<artifactId>UUID-Library</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -1,25 +1,43 @@
package com.drtshock.playervaults.commands; package com.drtshock.playervaults.commands;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.converters.BackpackConverter; import com.drtshock.playervaults.converters.BackpackConverter;
import com.drtshock.playervaults.converters.Converter; import com.drtshock.playervaults.converters.Converter;
import com.drtshock.playervaults.util.Lang; 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.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.ArrayList; import java.util.*;
import java.util.List;
public class ConvertCommand implements CommandExecutor { public class ConvertCommand implements CommandExecutor {
private List<Converter> converters = new ArrayList<>(); private List<Converter> converters = new ArrayList<>();
private ServiceProvider uuidProvider;
public ConvertCommand() { public ConvertCommand() {
converters.add(new BackpackConverter()); 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 @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")) { if (!sender.hasPermission("playervaults.convert")) {
sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS); sender.sendMessage(Lang.TITLE.toString() + Lang.NO_PERMS);
} else { } else {
@@ -27,7 +45,7 @@ public class ConvertCommand implements CommandExecutor {
sender.sendMessage(Lang.TITLE + "/pvconvert <all | plugin name>"); sender.sendMessage(Lang.TITLE + "/pvconvert <all | plugin name>");
} else { } else {
String name = args[0]; String name = args[0];
List<Converter> applicableConverters = new ArrayList<>(); final List<Converter> applicableConverters = new ArrayList<>();
if (name.equalsIgnoreCase("all")) { if (name.equalsIgnoreCase("all")) {
applicableConverters.addAll(converters); applicableConverters.addAll(converters);
} else { } else {
@@ -41,13 +59,22 @@ public class ConvertCommand implements CommandExecutor {
if (applicableConverters.size() <= 0) { if (applicableConverters.size() <= 0) {
sender.sendMessage(Lang.TITLE.toString() + Lang.CONVERT_PLUGIN_NOT_FOUND); sender.sendMessage(Lang.TITLE.toString() + Lang.CONVERT_PLUGIN_NOT_FOUND);
} else { } else {
int converted = 0; // Fork into background
for (Converter converter : applicableConverters) { sender.sendMessage(Lang.TITLE + Lang.CONVERT_BACKGROUND.toString());
if (converter.canConvert()) { PlayerVaults.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(PlayerVaults.getInstance(), new Runnable() {
converted += converter.doConvert(); @Override
public void run() {
int converted = 0;
VaultOperations.setLocked(true);
for (Converter converter : applicableConverters) {
if (converter.canConvert()) {
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
sender.sendMessage(Lang.TITLE + Lang.CONVERT_COMPLETE.toString().replace("%converted", converted + ""));
} }
} }
} }
@@ -12,6 +12,11 @@ import org.bukkit.entity.Player;
public class DeleteCommand implements CommandExecutor { public class DeleteCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { 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) { switch (args.length) {
case 1: case 1:
if (sender instanceof Player) { if (sender instanceof Player) {
@@ -17,6 +17,11 @@ import org.bukkit.entity.Player;
public class VaultCommand implements CommandExecutor { public class VaultCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { 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) { if (sender instanceof Player) {
Player player = (Player) sender; Player player = (Player) sender;
if (PlayerVaults.getInstance().getInVault().containsKey(player.getName())) { 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.PlayerVaults;
import com.drtshock.playervaults.vaultmanagement.UUIDVaultManager; 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.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@@ -21,7 +23,9 @@ import java.util.UUID;
public class BackpackConverter implements Converter { public class BackpackConverter implements Converter {
@Override @Override
public int doConvert() { public int run(CommandSender initiator, ServiceProvider uuidProvider) {
if(uuidProvider == null)throw new IllegalArgumentException();
PlayerVaults plugin = PlayerVaults.getInstance(); PlayerVaults plugin = PlayerVaults.getInstance();
File destination = new File(plugin.getDataFolder().getParentFile(), "Backpack" + File.separator + "backpacks"); File destination = new File(plugin.getDataFolder().getParentFile(), "Backpack" + File.separator + "backpacks");
if (!destination.exists()) return -1; if (!destination.exists()) return -1;
@@ -32,7 +36,7 @@ public class BackpackConverter implements Converter {
int vaultNum = 1; int vaultNum = 1;
for (File file : worldDirs != null ? worldDirs : new File[0]) { for (File file : worldDirs != null ? worldDirs : new File[0]) {
if (file.isDirectory()) { if (file.isDirectory()) {
converted += convert(file, vaultNum); converted += convert(file, vaultNum, uuidProvider);
vaultNum++; vaultNum++;
} }
} }
@@ -40,18 +44,19 @@ public class BackpackConverter implements Converter {
return converted; return converted;
} }
private int convert(File worldFolder, int intoVaultNum) { private int convert(File worldFolder, int intoVaultNum, ServiceProvider uuidProvider) {
PlayerVaults plugin = PlayerVaults.getInstance(); PlayerVaults plugin = PlayerVaults.getInstance();
UUIDVaultManager vaults = UUIDVaultManager.getInstance(); UUIDVaultManager vaults = UUIDVaultManager.getInstance();
int converted = 0; int converted = 0;
long lastUpdate = 0;
File[] files = worldFolder.listFiles(); File[] files = worldFolder.listFiles();
for (File file : files != null ? files : new File[0]) { for (File file : files != null ? files : new File[0]) {
if (file.isFile() && file.getName().toLowerCase().endsWith(".yml")) { if (file.isFile() && file.getName().toLowerCase().endsWith(".yml")) {
OfflinePlayer player = plugin.getServer().getOfflinePlayer(file.getName().substring(0, file.getName().lastIndexOf('.'))); PlayerRecord player = uuidProvider.doLookup(file.getName().substring(0,file.getName().lastIndexOf('.')));
if (player == null || player.getUniqueId() == null) { if (player == null || player.getUuid() == null) {
plugin.getLogger().warning("Unable to convert Backpack for player: " + (player != null ? player.getName() : file.getName())); plugin.getLogger().warning("Unable to convert Backpack for player: " + (player != null ? player.getName() : file.getName()));
} else { } else {
UUID uuid = player.getUniqueId(); UUID uuid = player.getUuid();
FileConfiguration yaml = YamlConfiguration.loadConfiguration(file); FileConfiguration yaml = YamlConfiguration.loadConfiguration(file);
ConfigurationSection section = yaml.getConfigurationSection("backpack"); ConfigurationSection section = yaml.getConfigurationSection("backpack");
if (section.getKeys(false).size() <= 0) continue; // No slots 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()); plugin.getLogger().severe("Error converting Backpack: " + file.getName());
e.printStackTrace(); 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; 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 * Represents a simple converter for converting another plugin's content
* to PlayerVaults. * to PlayerVaults.
@@ -11,10 +14,13 @@ public interface Converter {
/** /**
* Converts the other plugin's data. * 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 * @return the number of vaults converted. Returns 0 on none converted
* or -1 if no vaults were 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. * 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"), 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_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 path;
private String def; private String def;
@@ -16,17 +16,53 @@
*/ */
package com.drtshock.playervaults.vaultmanagement; package com.drtshock.playervaults.vaultmanagement;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.util.Lang; import com.drtshock.playervaults.util.Lang;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
public class VaultOperations { 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. * Check whether or not the player has permission to open the requested vault.
* *
@@ -70,11 +106,12 @@ public class VaultOperations {
* Open a player's own vault. * Open a player's own vault.
* *
* @param player The player to open to. * @param player The player to open to.
* @param arg The vault number to open. * @param arg The vault number to open.
* *
* @return Whether or not the player was allowed to open it. * @return Whether or not the player was allowed to open it.
*/ */
public static boolean openOwnVault(Player player, String arg) { public static boolean openOwnVault(Player player, String arg) {
if (isLocked()) return false;
int number; int number;
try { try {
number = Integer.parseInt(arg); number = Integer.parseInt(arg);
@@ -107,11 +144,12 @@ public class VaultOperations {
* *
* @param player The player to open to. * @param player The player to open to.
* @param holder The user to whom the requested vault belongs. * @param holder The user to whom the requested vault belongs.
* @param arg The vault number to open. * @param arg The vault number to open.
* *
* @return Whether or not the player was allowed to open it. * @return Whether or not the player was allowed to open it.
*/ */
public static boolean openOtherVault(Player player, Player holder, String arg) { public static boolean openOtherVault(Player player, Player holder, String arg) {
if (isLocked()) return false;
if (player.hasPermission("playervaults.admin")) { if (player.hasPermission("playervaults.admin")) {
int number = 0; int number = 0;
try { try {
@@ -140,9 +178,10 @@ public class VaultOperations {
* Delete a player's own vault. * Delete a player's own vault.
* *
* @param player The player to delete. * @param player The player to delete.
* @param arg The vault number to delete. * @param arg The vault number to delete.
*/ */
public static void deleteOwnVault(Player player, String arg) { public static void deleteOwnVault(Player player, String arg) {
if (isLocked()) return;
if (arg.matches("^[0-9]{1,2}$")) { if (arg.matches("^[0-9]{1,2}$")) {
int number = 0; int number = 0;
try { try {
@@ -172,9 +211,10 @@ public class VaultOperations {
* *
* @param sender The sender executing the deletion. * @param sender The sender executing the deletion.
* @param holder The user to whom the deleted vault belongs. * @param holder The user to whom the deleted vault belongs.
* @param arg The vault number to delete. * @param arg The vault number to delete.
*/ */
public static void deleteOtherVault(CommandSender sender, Player holder, String arg) { public static void deleteOtherVault(CommandSender sender, Player holder, String arg) {
if (isLocked()) return;
if (sender.hasPermission("playervaults.delete")) { if (sender.hasPermission("playervaults.delete")) {
if (arg.matches("^[0-9]{1,2}$")) { if (arg.matches("^[0-9]{1,2}$")) {
int number = 0; 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" no-player-found: "&cCannot find player &a%p"
plugin-not-found: "&cNo converter found for that plugin" plugin-not-found: "&cNo converter found for that plugin"
conversion-complete: "&aConverted %converted players to PlayerVaults" 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!"