Allow async conversion and use a UUID service where possible
This commit is contained in:
@@ -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,13 +59,22 @@ public class ConvertCommand implements CommandExecutor {
|
||||
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();
|
||||
// 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.run(sender, uuidProvider);
|
||||
}
|
||||
}
|
||||
VaultOperations.setLocked(false);
|
||||
sender.sendMessage(Lang.TITLE + Lang.CONVERT_COMPLETE.toString().replace("%converted", converted + ""));
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright (C) 2013 drtshock
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
@@ -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.
|
||||
*
|
||||
@@ -70,11 +106,12 @@ public class VaultOperations {
|
||||
* Open a player's own vault.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
public static boolean openOwnVault(Player player, String arg) {
|
||||
if (isLocked()) return false;
|
||||
int number;
|
||||
try {
|
||||
number = Integer.parseInt(arg);
|
||||
@@ -107,11 +144,12 @@ public class VaultOperations {
|
||||
*
|
||||
* @param player The player to open to.
|
||||
* @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.
|
||||
*/
|
||||
public static boolean openOtherVault(Player player, Player holder, String arg) {
|
||||
if (isLocked()) return false;
|
||||
if (player.hasPermission("playervaults.admin")) {
|
||||
int number = 0;
|
||||
try {
|
||||
@@ -140,9 +178,10 @@ public class VaultOperations {
|
||||
* Delete a player's own vault.
|
||||
*
|
||||
* @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) {
|
||||
if (isLocked()) return;
|
||||
if (arg.matches("^[0-9]{1,2}$")) {
|
||||
int number = 0;
|
||||
try {
|
||||
@@ -172,9 +211,10 @@ public class VaultOperations {
|
||||
*
|
||||
* @param sender The sender executing the deletion.
|
||||
* @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) {
|
||||
if (isLocked()) return;
|
||||
if (sender.hasPermission("playervaults.delete")) {
|
||||
if (arg.matches("^[0-9]{1,2}$")) {
|
||||
int number = 0;
|
||||
|
||||
Reference in New Issue
Block a user