diff --git a/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java b/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java index 8e1d8e4d..7eb64e5e 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java +++ b/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java @@ -4,11 +4,11 @@ import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.event.MVConfigReloadEvent; import com.onarandombox.MultiverseCore.event.MVVersionEvent; +import com.onarandombox.multiverseinventories.dataimport.DataImporter; import com.onarandombox.multiverseinventories.profile.GlobalProfile; import com.onarandombox.multiverseinventories.profile.PlayerProfile; import com.onarandombox.multiverseinventories.profile.container.ProfileContainer; import com.onarandombox.multiverseinventories.share.Sharables; -import me.drayshak.WorldInventories.WorldInventories; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -32,7 +32,7 @@ import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.world.WorldUnloadEvent; import org.bukkit.inventory.InventoryHolder; -import uk.co.tggl.pluckerpluck.multiinv.MultiInv; +import org.bukkit.plugin.Plugin; import java.io.File; import java.io.IOException; @@ -84,14 +84,11 @@ public void configReload(MVConfigReloadEvent event) { */ @EventHandler public void pluginEnable(PluginEnableEvent event) { - try { - if (event.getPlugin() instanceof MultiInv) { - this.inventories.getImportManager().hookMultiInv((MultiInv) event.getPlugin()); - } else if (event.getPlugin() instanceof WorldInventories) { - this.inventories.getImportManager().hookWorldInventories((WorldInventories) event.getPlugin()); - } - } catch (NoClassDefFoundError ignore) { + DataImporter dataImporter = this.inventories.getImportManager().getImporter(event.getPlugin()); + if (dataImporter == null) { + return; } + dataImporter.enable(event.getPlugin()); } /** @@ -101,14 +98,11 @@ public void pluginEnable(PluginEnableEvent event) { */ @EventHandler public void pluginDisable(PluginDisableEvent event) { - try { - if (event.getPlugin() instanceof MultiInv) { - this.inventories.getImportManager().unHookMultiInv(); - } else if (event.getPlugin() instanceof WorldInventories) { - this.inventories.getImportManager().unHookWorldInventories(); - } - } catch (NoClassDefFoundError ignore) { + DataImporter dataImporter = this.inventories.getImportManager().getImporter(event.getPlugin()); + if (dataImporter == null) { + return; } + dataImporter.disable(); } @EventHandler(priority = EventPriority.MONITOR) diff --git a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java index bf2949ef..e4ccf004 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java +++ b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java @@ -5,11 +5,6 @@ import com.onarandombox.MultiverseCore.api.MVPlugin; import com.onarandombox.MultiverseCore.commands.HelpCommand; import com.onarandombox.commandhandler.CommandHandler; -import com.onarandombox.multiverseinventories.profile.ProfileDataSource; -import com.onarandombox.multiverseinventories.profile.WorldGroupManager; -import com.onarandombox.multiverseinventories.profile.container.ContainerType; -import com.onarandombox.multiverseinventories.profile.container.ProfileContainerStore; -import com.onarandombox.multiverseinventories.share.Sharables; import com.onarandombox.multiverseinventories.command.AddSharesCommand; import com.onarandombox.multiverseinventories.command.AddWorldCommand; import com.onarandombox.multiverseinventories.command.CreateGroupCommand; @@ -24,22 +19,25 @@ import com.onarandombox.multiverseinventories.command.RemoveWorldCommand; import com.onarandombox.multiverseinventories.command.SpawnCommand; import com.onarandombox.multiverseinventories.command.ToggleCommand; +import com.onarandombox.multiverseinventories.dataimport.DataImportManager; +import com.onarandombox.multiverseinventories.dataimport.multiinv.MultiInvImporter; +import com.onarandombox.multiverseinventories.dataimport.worldinventories.WorldInventoriesImporter; import com.onarandombox.multiverseinventories.locale.Message; import com.onarandombox.multiverseinventories.locale.Messager; import com.onarandombox.multiverseinventories.locale.Messaging; -import com.onarandombox.multiverseinventories.migration.ImportManager; +import com.onarandombox.multiverseinventories.profile.ProfileDataSource; +import com.onarandombox.multiverseinventories.profile.WorldGroupManager; +import com.onarandombox.multiverseinventories.profile.container.ContainerType; +import com.onarandombox.multiverseinventories.profile.container.ProfileContainerStore; +import com.onarandombox.multiverseinventories.share.Sharables; import com.onarandombox.multiverseinventories.util.Perm; -import me.drayshak.WorldInventories.WorldInventories; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; -import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; -import uk.co.tggl.pluckerpluck.multiinv.MultiInv; import java.io.File; import java.io.IOException; @@ -67,7 +65,7 @@ public static MultiverseInventories getPlugin() { private WorldGroupManager worldGroupManager = null; private ProfileContainerStore worldProfileContainerStore = null; private ProfileContainerStore groupProfileContainerStore = null; - private ImportManager importManager = new ImportManager(this); + private DataImportManager importManager = new DataImportManager(); private CommandHandler commandHandler = null; private MultiverseCore core = null; @@ -174,8 +172,9 @@ public void onEnable() { // Register Commands this.registerCommands(); - // Hook plugins that can be imported from - this.hookImportables(); + // Register and hook plugins that can be imported from + this.importManager.register(new WorldInventoriesImporter(this)); + this.importManager.register(new MultiInvImporter(this)); Sharables.init(this); @@ -208,22 +207,10 @@ private void registerCommands() { } } - private void hookImportables() { - final PluginManager pm = Bukkit.getPluginManager(); - Plugin plugin = pm.getPlugin("MultiInv"); - if (plugin != null) { - this.getImportManager().hookMultiInv((MultiInv) plugin); - } - plugin = pm.getPlugin("WorldInventories"); - if (plugin != null) { - this.getImportManager().hookWorldInventories((WorldInventories) plugin); - } - } - /** * @return A class used for managing importing data from other similar plugins. */ - public ImportManager getImportManager() { + public DataImportManager getImportManager() { return this.importManager; } @@ -311,6 +298,7 @@ public String getVersionInfo() { + "[Multiverse-Inventories] Default Ungrouped Worlds: " + this.getMVIConfig().isDefaultingUngroupedWorlds() + '\n' + "[Multiverse-Inventories] Save and Load on Log In and Out: " + this.getMVIConfig().usingLoggingSaveLoad() + '\n' + "[Multiverse-Inventories] Using GameMode Profiles: " + this.getMVIConfig().isUsingGameModeProfiles() + '\n' + + "[Multiverse-Inventories] Enabled importers: " + this.getImportManager().getEnabledImporterNames() + '\n' + "[Multiverse-Inventories] === Shares ===" + '\n' + "[Multiverse-Inventories] Optionals for Ungrouped Worlds: " + this.getMVIConfig().usingOptionalsForUngrouped() + '\n' + "[Multiverse-Inventories] Enabled Optionals: " + this.getMVIConfig().getOptionalShares() + '\n' diff --git a/src/main/java/com/onarandombox/multiverseinventories/command/ImportCommand.java b/src/main/java/com/onarandombox/multiverseinventories/command/ImportCommand.java index 1831351b..d7d40e7b 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/command/ImportCommand.java +++ b/src/main/java/com/onarandombox/multiverseinventories/command/ImportCommand.java @@ -2,12 +2,13 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.multiverseinventories.MultiverseInventories; +import com.onarandombox.multiverseinventories.dataimport.DataImporter; import com.onarandombox.multiverseinventories.locale.Message; -import com.onarandombox.multiverseinventories.migration.DataImporter; -import com.onarandombox.multiverseinventories.migration.MigrationException; +import com.onarandombox.multiverseinventories.dataimport.DataImportException; import com.onarandombox.multiverseinventories.util.Perm; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; import java.util.List; @@ -29,27 +30,23 @@ public ImportCommand(MultiverseInventories plugin) { @Override public void runCommand(CommandSender sender, List args) { - DataImporter importer = null; - if (args.get(0).equalsIgnoreCase("MultiInv")) { - importer = this.plugin.getImportManager().getMultiInvImporter(); - } else if (args.get(0).equalsIgnoreCase("WorldInventories")) { - importer = this.plugin.getImportManager().getWorldInventoriesImporter(); - } else { - this.messager.bad(Message.ERROR_PLUGIN_NOT_ENABLED, - sender, args.get(0)); + DataImporter dataImporter = this.plugin.getImportManager().getImporter(args.get(0)); + if (dataImporter == null) { + this.messager.bad(Message.ERROR_UNSUPPORTED_IMPORT, sender, args.get(0)); return; } - if (importer == null) { - this.messager.bad(Message.ERROR_PLUGIN_NOT_ENABLED, - sender, args.get(0)); - } else { - try { - importer.importData(); - } catch (MigrationException e) { - Logging.severe(e.getMessage()); - Logging.severe("Cause: " + e.getCauseException().getMessage()); - } + if (!dataImporter.isEnabled()) { + this.messager.bad(Message.ERROR_PLUGIN_NOT_ENABLED, sender, dataImporter.getPluginName()); + return; } + + this.messager.normal(Message.IMPORT_ATTEMPT, sender, dataImporter.getPluginName()); + if (!dataImporter.importData()) { + this.messager.bad(Message.IMPORT_FAILED, sender, dataImporter.getPluginName()); + return; + } + + this.messager.normal(Message.IMPORT_SUCCESSFUL, sender, dataImporter.getPluginName()); } } diff --git a/src/main/java/com/onarandombox/multiverseinventories/dataimport/AbstractDataImporter.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/AbstractDataImporter.java new file mode 100644 index 00000000..4dfbca34 --- /dev/null +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/AbstractDataImporter.java @@ -0,0 +1,123 @@ +package com.onarandombox.multiverseinventories.dataimport; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.multiverseinventories.MultiverseInventories; +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +/** + * Abstract implementation of {@link DataImporter} without actual import logic. + */ +public abstract class AbstractDataImporter implements DataImporter { + + protected final MultiverseInventories plugin; + protected T importer = null; + + public AbstractDataImporter(MultiverseInventories plugin) { + this.plugin = plugin; + } + + /** + * Logic that does the actual importing data. + * + * @throws DataImportException Errors occurred that caused import to fail. + */ + protected abstract void doDataImport() throws DataImportException; + + /** + * {@inheritDoc} + */ + @Override + public boolean importData(boolean disableOnSuccess) { + if (!isEnabled()) { + Logging.severe("Data importer %s not enabled. No data is imported.", this.getPluginName()); + return false; + } + + try { + doDataImport(); + } catch (DataImportException e) { + Logging.severe(e.getMessage()); + Logging.severe("Cause: %s", e.getCauseException().getMessage()); + e.printStackTrace(); + return false; + } + + Logging.info("Successfully imported data from %s!", this.getPluginName()); + if (disableOnSuccess) { + Logging.info("Disabling %s...", this.getPluginName()); + Bukkit.getPluginManager().disablePlugin(this.importer); + } + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean importData() { + return importData(true); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean enable(Plugin importerPlugin) { + if (isEnabled()) { + return false; + } + if (!importerPlugin.getClass().equals(this.getPluginClass())) { + Logging.warning("Plugin '%s' is not data importer for '%s'.", + plugin.getClass().getName(), getPluginName()); + return false; + } + try { + this.importer = (T) importerPlugin; + } catch (ClassCastException | NoClassDefFoundError e) { + Logging.warning("Error while enabling data importer for '%s'.", getPluginName()); + return false; + } + Logging.info("Successfully enabled data importer for '%s'.", getPluginName()); + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean enable() { + Plugin importerPlugin = Bukkit.getPluginManager().getPlugin(this.getPluginName()); + if (importerPlugin == null) { + Logging.finer("Unable to get plugin '%s' for import hook.", this.getPluginName()); + return false; + } + return enable(importerPlugin); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean disable() { + this.importer = null; + Logging.info("Successfully disabled data importer for '%s'.", getPluginName()); + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isEnabled() { + return importer != null; + } + + /** + * {@inheritDoc} + */ + @Override + public T getPlugin() { + return importer; + } +} diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/MigrationException.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImportException.java similarity index 69% rename from src/main/java/com/onarandombox/multiverseinventories/migration/MigrationException.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImportException.java index c169b378..d966a434 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/MigrationException.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImportException.java @@ -1,13 +1,13 @@ -package com.onarandombox.multiverseinventories.migration; +package com.onarandombox.multiverseinventories.dataimport; /** * Exception thrown when migration doesn't go well. */ -public class MigrationException extends Exception { +public class DataImportException extends Exception { private Exception causeException = null; - public MigrationException(String message) { + public DataImportException(String message) { super(message); } @@ -17,7 +17,7 @@ public MigrationException(String message) { * @param exception The cause exception. * @return This exception for easy chainability. */ - public MigrationException setCauseException(Exception exception) { + public DataImportException setCauseException(Exception exception) { this.causeException = exception; return this; } diff --git a/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImportManager.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImportManager.java new file mode 100644 index 00000000..4474e7e9 --- /dev/null +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImportManager.java @@ -0,0 +1,74 @@ +package com.onarandombox.multiverseinventories.dataimport; + +import org.bukkit.plugin.Plugin; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Manager class for importing data from other inventory plugins or similar, e.g. PerWorldInventory. + */ +public class DataImportManager { + + final private Map> dataImporters; + + public DataImportManager() { + this.dataImporters = new HashMap<>(); + } + + /** + * Register a Data Importer and optionally try to enable to it as well. + * + * @param dataImporter The Data Importer to register. + * @param tryEnable Whether to try and {@link DataImporter#enable(Plugin)} the Data Importer. + */ + public void register(DataImporter dataImporter, boolean tryEnable) { + this.dataImporters.put(dataImporter.getPluginName().toLowerCase(), dataImporter); + if (tryEnable) { + dataImporter.enable(); + } + } + + /** + * Register a Data Importer and try to enable to it as well. + * + * @param dataImporter The Data Importer to register. + */ + public void register(DataImporter dataImporter) { + this.register(dataImporter, true); + } + + /** + * Gets a {@link DataImporter} based on an importable plugin name. + * + * @param pluginName The plugin name you want to import data from. + * @return The {@link DataImporter} if Data Importer present for that plugin, else null. + */ + public DataImporter getImporter(String pluginName) { + return this.dataImporters.get(pluginName.toLowerCase()); + } + + /** + * Gets a {@link DataImporter} based on an importable {@link Plugin}. + * + * @param plugin The plugin you want to import data from. + * @return The {@link DataImporter} if Data Importer present for that plugin, else null. + */ + public DataImporter getImporter(Plugin plugin) { + return getImporter(plugin.getName()); + } + + /** + * Gets all the Data Importer names that are enabled. + * + * @return A collection of Data Importer names that are enabled. + */ + public Collection getEnabledImporterNames() { + return this.dataImporters.values().stream() + .filter(DataImporter::isEnabled) + .map(DataImporter::getPluginName) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImporter.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImporter.java new file mode 100644 index 00000000..18c4a351 --- /dev/null +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/DataImporter.java @@ -0,0 +1,71 @@ +package com.onarandombox.multiverseinventories.dataimport; + +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Interface for data migration importers. + */ +public interface DataImporter { + + /** + * Imports the data from another plugin and optionally disable it after successful import. + * + * @param disableOnSuccess Whether to disable the importer plugin after a successful import. + * @return True if data import is successful, else false. + */ + boolean importData(boolean disableOnSuccess); + + /** + * Imports the data from another plugin and disabled it upon success so Multiverse inventories + * can work without conflicts. + * + * @return True if data import is successful, else false. + */ + boolean importData(); + + /** + * Hooks plugin for importing it's data. Needs plugin class of {@link #getPluginClass()}. + * + * @param plugin The target plugin instance to hook. + * @return True if successfully enabled, else false. + */ + boolean enable(Plugin plugin); + + /** + * Hooks plugin for importing it's data. Needs plugin class of {@link #getPluginClass()}. + * + * @return True if successfully enabled, else false. + */ + boolean enable(); + + /** + * Unhook plugin from this Data Importer. + * + * @return True if successfully disabled, else false. + */ + boolean disable(); + + /** + * Checks if this Data Importer has been {@link #enable(Plugin)} successfully. + * + * @return True if is enabled, else false. + */ + boolean isEnabled(); + + /** + * @return The plugin associated with this Data Importer, null if not enabled. + */ + @Nullable T getPlugin(); + + /** + * @return The plugin name associated with this Data Importer. + */ + @NotNull String getPluginName(); + + /** + * @return The plugin class associated with this Data Importer. + */ + @NotNull Class getPluginClass(); +} diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryConverter.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryConverter.java similarity index 92% rename from src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryConverter.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryConverter.java index 33f1d5e2..db162f74 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryConverter.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryConverter.java @@ -1,4 +1,4 @@ -package com.onarandombox.multiverseinventories.migration.multiinv; +package com.onarandombox.multiverseinventories.dataimport.multiinv; import com.onarandombox.multiverseinventories.util.MinecraftTools; import org.bukkit.inventory.ItemStack; diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryInterface.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryInterface.java similarity index 84% rename from src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryInterface.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryInterface.java index 04f46b18..63d10b17 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryInterface.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryInterface.java @@ -1,4 +1,4 @@ -package com.onarandombox.multiverseinventories.migration.multiinv; +package com.onarandombox.multiverseinventories.dataimport.multiinv; import org.bukkit.inventory.ItemStack; diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryOldWrapper.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryOldWrapper.java similarity index 90% rename from src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryOldWrapper.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryOldWrapper.java index d624a566..773e51d6 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryOldWrapper.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryOldWrapper.java @@ -1,4 +1,4 @@ -package com.onarandombox.multiverseinventories.migration.multiinv; +package com.onarandombox.multiverseinventories.dataimport.multiinv; import org.bukkit.inventory.ItemStack; import uk.co.tggl.pluckerpluck.multiinv.inventory.MIInventoryOld; diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryWrapper.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryWrapper.java similarity index 90% rename from src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryWrapper.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryWrapper.java index 5572b3b3..9627c0e3 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIInventoryWrapper.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIInventoryWrapper.java @@ -1,4 +1,4 @@ -package com.onarandombox.multiverseinventories.migration.multiinv; +package com.onarandombox.multiverseinventories.dataimport.multiinv; import org.bukkit.inventory.ItemStack; import uk.co.tggl.pluckerpluck.multiinv.inventory.MIInventory; diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIPlayerFileLoader.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIPlayerFileLoader.java similarity index 94% rename from src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIPlayerFileLoader.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIPlayerFileLoader.java index ba0e6e02..f0bd7d61 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MIPlayerFileLoader.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MIPlayerFileLoader.java @@ -1,114 +1,114 @@ -package com.onarandombox.multiverseinventories.migration.multiinv; - -import com.onarandombox.multiverseinventories.PlayerStats; -import org.bukkit.OfflinePlayer; -import org.bukkit.configuration.file.YamlConfiguration; -import uk.co.tggl.pluckerpluck.multiinv.MultiInv; - -import java.io.File; - -/** - * A replacement for MultiInv's MIPlayerFile class so that it may accept an OfflinePlayer instead of Player. - */ -public class MIPlayerFileLoader { - - private YamlConfiguration playerFile; - private File file; - - public MIPlayerFileLoader(MultiInv plugin, OfflinePlayer player, String group) { - // Find and load configuration file for the player - File worldsFolder = new File(plugin.getDataFolder(), "Groups"); - file = new File(worldsFolder, group + File.separator + player.getName() + ".yml"); - - playerFile = new YamlConfiguration(); - } - - /** - * Loads the player file into memory. - * - * @return True if there was a file to load and it loaded successfully. - */ - public boolean load() { - if (file.exists()) { - try { - playerFile.load(file); - return true; - } catch (Exception ignore) { } - } - return false; - } - - /** - * Load particular inventory for specified player from specified group. - * - * @param inventoryName The gamemode for the inventory to load. - * @return An interface for retrieve the inventory/armor contents. - */ - public MIInventoryInterface getInventory(String inventoryName) { - // Get stored string from configuration file - MIInventoryInterface inventory; - String inventoryString = playerFile.getString(inventoryName, null); - // Check for old inventory save - if (inventoryString == null || inventoryString.contains(";-;")) { - inventory = new MIInventoryOldWrapper(inventoryString); - } else { - inventory = new MIInventoryWrapper(inventoryString); - } - return inventory; - } - - /** - * @return The player's health. - */ - public double getHealth() { - double health = playerFile.getDouble("health", PlayerStats.HEALTH); - if (health <= 0 || health > PlayerStats.HEALTH) { - health = PlayerStats.HEALTH; - } - return health; - } - - /** - * @return The player's hunger. - */ - public int getHunger() { - int hunger = playerFile.getInt("hunger", PlayerStats.FOOD_LEVEL); - if (hunger <= 0 || hunger > PlayerStats.FOOD_LEVEL) { - hunger = PlayerStats.FOOD_LEVEL; - } - return hunger; - } - - /** - * @return The player's saturation. - */ - public float getSaturation() { - double saturationDouble = playerFile.getDouble("saturation", 0); - float saturation = (float) saturationDouble; - return saturation; - } - - /** - * @return The player's total exp. - */ - public int getTotalExperience() { - return playerFile.getInt("experience", 0); - } - - /** - * @return The player's level. - */ - public int getLevel() { - return playerFile.getInt("level", 0); - } - - /** - * @return The player's exp. - */ - public float getExperience() { - double expDouble = playerFile.getDouble("exp", 0); - float exp = (float) expDouble; - return exp; - } -} - +package com.onarandombox.multiverseinventories.dataimport.multiinv; + +import com.onarandombox.multiverseinventories.PlayerStats; +import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.file.YamlConfiguration; +import uk.co.tggl.pluckerpluck.multiinv.MultiInv; + +import java.io.File; + +/** + * A replacement for MultiInv's MIPlayerFile class so that it may accept an OfflinePlayer instead of Player. + */ +public class MIPlayerFileLoader { + + private YamlConfiguration playerFile; + private File file; + + public MIPlayerFileLoader(MultiInv plugin, OfflinePlayer player, String group) { + // Find and load configuration file for the player + File worldsFolder = new File(plugin.getDataFolder(), "Groups"); + file = new File(worldsFolder, group + File.separator + player.getName() + ".yml"); + + playerFile = new YamlConfiguration(); + } + + /** + * Loads the player file into memory. + * + * @return True if there was a file to load and it loaded successfully. + */ + public boolean load() { + if (file.exists()) { + try { + playerFile.load(file); + return true; + } catch (Exception ignore) { } + } + return false; + } + + /** + * Load particular inventory for specified player from specified group. + * + * @param inventoryName The gamemode for the inventory to load. + * @return An interface for retrieve the inventory/armor contents. + */ + public MIInventoryInterface getInventory(String inventoryName) { + // Get stored string from configuration file + MIInventoryInterface inventory; + String inventoryString = playerFile.getString(inventoryName, null); + // Check for old inventory save + if (inventoryString == null || inventoryString.contains(";-;")) { + inventory = new MIInventoryOldWrapper(inventoryString); + } else { + inventory = new MIInventoryWrapper(inventoryString); + } + return inventory; + } + + /** + * @return The player's health. + */ + public double getHealth() { + double health = playerFile.getDouble("health", PlayerStats.HEALTH); + if (health <= 0 || health > PlayerStats.HEALTH) { + health = PlayerStats.HEALTH; + } + return health; + } + + /** + * @return The player's hunger. + */ + public int getHunger() { + int hunger = playerFile.getInt("hunger", PlayerStats.FOOD_LEVEL); + if (hunger <= 0 || hunger > PlayerStats.FOOD_LEVEL) { + hunger = PlayerStats.FOOD_LEVEL; + } + return hunger; + } + + /** + * @return The player's saturation. + */ + public float getSaturation() { + double saturationDouble = playerFile.getDouble("saturation", 0); + float saturation = (float) saturationDouble; + return saturation; + } + + /** + * @return The player's total exp. + */ + public int getTotalExperience() { + return playerFile.getInt("experience", 0); + } + + /** + * @return The player's level. + */ + public int getLevel() { + return playerFile.getInt("level", 0); + } + + /** + * @return The player's exp. + */ + public float getExperience() { + double expDouble = playerFile.getDouble("exp", 0); + float exp = (float) expDouble; + return exp; + } +} + diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MultiInvImporter.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MultiInvImporter.java similarity index 63% rename from src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MultiInvImporter.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MultiInvImporter.java index c0f3ae8c..ad7bddd7 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/MultiInvImporter.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/MultiInvImporter.java @@ -1,167 +1,153 @@ -package com.onarandombox.multiverseinventories.migration.multiinv; - -import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.multiverseinventories.MultiverseInventories; -import com.onarandombox.multiverseinventories.WorldGroup; -import com.onarandombox.multiverseinventories.profile.ProfileTypes; -import com.onarandombox.multiverseinventories.profile.container.ContainerType; -import com.onarandombox.multiverseinventories.profile.PlayerProfile; -import com.onarandombox.multiverseinventories.share.Sharables; -import com.onarandombox.multiverseinventories.migration.DataImporter; -import com.onarandombox.multiverseinventories.migration.MigrationException; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; -import org.bukkit.plugin.Plugin; -import uk.co.tggl.pluckerpluck.multiinv.MIYamlFiles; -import uk.co.tggl.pluckerpluck.multiinv.MultiInv; - -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; - -/** - * A class to help with importing data from MultiInv. - */ -public class MultiInvImporter implements DataImporter { - - private MultiInv miPlugin; - private MultiverseInventories inventories; - - public MultiInvImporter(MultiverseInventories inventories, MultiInv miPlugin) { - this.inventories = inventories; - this.miPlugin = miPlugin; - } - - /** - * @return The MultiInv plugin hooked to the importer. - */ - public MultiInv getMIPlugin() { - return this.miPlugin; - } - - /** - * {@inheritDoc} - */ - @Override - public Plugin getPlugin() { - return this.getMIPlugin(); - } - - /** - * Imports the data from MultiInv. - * - * @throws MigrationException If there was any MAJOR issue loading the data. - */ - @Override - public void importData() throws MigrationException { - HashMap miGroupMap = this.getGroupMap(); - if (miGroupMap == null) { - throw new MigrationException("There is no data to import from MultiInv!"); - } - if (!miGroupMap.isEmpty()) { - WorldGroup defaultWorldGroup = this.inventories.getGroupManager().getDefaultGroup(); - if (defaultWorldGroup != null) { - this.inventories.getGroupManager().removeGroup(defaultWorldGroup); - Logging.info("Removed automatically created world group in favor of imported groups."); - } - } - for (Map.Entry groupEntry : miGroupMap.entrySet()) { - WorldGroup worldGroup = this.inventories.getGroupManager().getGroup(groupEntry.getValue()); - if (worldGroup == null) { - worldGroup = this.inventories.getGroupManager().newEmptyGroup(groupEntry.getValue()); - worldGroup.getShares().mergeShares(Sharables.allOf()); - Logging.info("Importing group: " + groupEntry.getValue()); - this.inventories.getGroupManager().updateGroup(worldGroup); - } - worldGroup.addWorld(groupEntry.getValue()); - } - this.inventories.getMVIConfig().save(); - - for (OfflinePlayer player : Bukkit.getServer().getOfflinePlayers()) { - Logging.info("Processing MultiInv data for player: " + player.getName()); - for (Map.Entry entry : miGroupMap.entrySet()) { - String worldName = entry.getKey(); - String groupName = entry.getValue(); - MIPlayerFileLoader playerFileLoader = - new MIPlayerFileLoader(this.getMIPlugin(), player, groupName); - if (!playerFileLoader.load()) { - continue; - } - Logging.info("Processing MultiInv data for player: " + player.getName() - + " for group: " + groupName); - mergeData(player, playerFileLoader, groupName, ContainerType.GROUP); - } - for (World world : Bukkit.getWorlds()) { - String worldName = world.getName(); - MIPlayerFileLoader playerFileLoader = - new MIPlayerFileLoader(this.getMIPlugin(), player, worldName); - if (!playerFileLoader.load()) { - continue; - } - Logging.info("Processing MultiInv data for player: " + player.getName() - + " for world only: " + worldName); - mergeData(player, playerFileLoader, worldName, ContainerType.WORLD); - } - } - - Logging.info("Import from MultiInv finished. Disabling MultiInv."); - Bukkit.getPluginManager().disablePlugin(this.getMIPlugin()); - } - - private void mergeData(OfflinePlayer player, MIPlayerFileLoader playerFileLoader, - String dataName, ContainerType type) { - PlayerProfile playerProfile; - if (type.equals(ContainerType.GROUP)) { - WorldGroup group = this.inventories.getGroupManager() - .getGroup(dataName); - if (group == null) { - Logging.warning("Could not import player data for group: " + dataName); - return; - } - playerProfile = group.getGroupProfileContainer().getPlayerData(ProfileTypes.SURVIVAL, player); - } else { - playerProfile = this.inventories.getWorldProfileContainerStore() - .getContainer(dataName).getPlayerData(ProfileTypes.SURVIVAL, player); - } - MIInventoryInterface inventoryInterface = - playerFileLoader.getInventory(GameMode.SURVIVAL.toString()); - playerProfile.set(Sharables.INVENTORY, inventoryInterface.getInventoryContents()); - playerProfile.set(Sharables.ARMOR, inventoryInterface.getArmorContents()); - playerProfile.set(Sharables.HEALTH, playerFileLoader.getHealth()); - playerProfile.set(Sharables.SATURATION, playerFileLoader.getSaturation()); - playerProfile.set(Sharables.EXPERIENCE, playerFileLoader.getExperience()); - playerProfile.set(Sharables.TOTAL_EXPERIENCE, playerFileLoader.getTotalExperience()); - playerProfile.set(Sharables.LEVEL, playerFileLoader.getLevel()); - playerProfile.set(Sharables.FOOD_LEVEL, playerFileLoader.getHunger()); - this.inventories.getData().updatePlayerData(playerProfile); - } - - /** - * @return The group mapping from MultiInv, where worldName -> groupName. - * @throws MigrationException If there was any issues getting the data through reflection. - */ - private HashMap getGroupMap() throws MigrationException { - Field field; - try { - field = MIYamlFiles.class.getDeclaredField("groups"); - } catch (NoSuchFieldException nsfe) { - throw new MigrationException("The running version of MultiInv is " - + "incompatible with the import feature.").setCauseException(nsfe); - } - field.setAccessible(true); - HashMap miGroupMap = null; - try { - miGroupMap = (HashMap) field.get(null); - } catch (IllegalAccessException iae) { - throw new MigrationException("The running version of MultiInv is " - + "incompatible with the import feature.").setCauseException(iae); - } catch (ClassCastException cce) { - throw new MigrationException("The running version of MultiInv is " - + "incompatible with the import feature.").setCauseException(cce); - } - return miGroupMap; - } -} - +package com.onarandombox.multiverseinventories.dataimport.multiinv; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.multiverseinventories.MultiverseInventories; +import com.onarandombox.multiverseinventories.WorldGroup; +import com.onarandombox.multiverseinventories.dataimport.AbstractDataImporter; +import com.onarandombox.multiverseinventories.dataimport.DataImportException; +import com.onarandombox.multiverseinventories.profile.PlayerProfile; +import com.onarandombox.multiverseinventories.profile.ProfileTypes; +import com.onarandombox.multiverseinventories.profile.container.ContainerType; +import com.onarandombox.multiverseinventories.share.Sharables; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; +import org.jetbrains.annotations.NotNull; +import uk.co.tggl.pluckerpluck.multiinv.MIYamlFiles; +import uk.co.tggl.pluckerpluck.multiinv.MultiInv; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +public class MultiInvImporter extends AbstractDataImporter { + + public MultiInvImporter(MultiverseInventories plugin) { + super(plugin); + } + + /** + * {@inheritDoc} + */ + @Override + protected void doDataImport() throws DataImportException { + HashMap miGroupMap = this.getGroupMap(); + if (miGroupMap == null) { + throw new DataImportException("There is no data to import from MultiInv!"); + } + if (!miGroupMap.isEmpty()) { + WorldGroup defaultWorldGroup = this.plugin.getGroupManager().getDefaultGroup(); + if (defaultWorldGroup != null) { + this.plugin.getGroupManager().removeGroup(defaultWorldGroup); + Logging.info("Removed automatically created world group in favor of imported groups."); + } + } + for (Map.Entry groupEntry : miGroupMap.entrySet()) { + WorldGroup worldGroup = this.plugin.getGroupManager().getGroup(groupEntry.getValue()); + if (worldGroup == null) { + worldGroup = this.plugin.getGroupManager().newEmptyGroup(groupEntry.getValue()); + worldGroup.getShares().mergeShares(Sharables.allOf()); + Logging.info("Importing group: " + groupEntry.getValue()); + this.plugin.getGroupManager().updateGroup(worldGroup); + } + worldGroup.addWorld(groupEntry.getValue()); + } + this.plugin.getMVIConfig().save(); + + for (OfflinePlayer player : Bukkit.getServer().getOfflinePlayers()) { + Logging.info("Processing MultiInv data for player: " + player.getName()); + for (Map.Entry entry : miGroupMap.entrySet()) { + String worldName = entry.getKey(); + String groupName = entry.getValue(); + MIPlayerFileLoader playerFileLoader = new MIPlayerFileLoader(this.importer, player, groupName); + if (!playerFileLoader.load()) { + continue; + } + Logging.info("Processing MultiInv data for player: " + player.getName() + + " for group: " + groupName); + mergeData(player, playerFileLoader, groupName, ContainerType.GROUP); + } + for (World world : Bukkit.getWorlds()) { + String worldName = world.getName(); + MIPlayerFileLoader playerFileLoader = new MIPlayerFileLoader(this.importer, player, worldName); + if (!playerFileLoader.load()) { + continue; + } + Logging.info("Processing MultiInv data for player: " + player.getName() + + " for world only: " + worldName); + mergeData(player, playerFileLoader, worldName, ContainerType.WORLD); + } + } + } + + private void mergeData(OfflinePlayer player, MIPlayerFileLoader playerFileLoader, + String dataName, ContainerType type) { + PlayerProfile playerProfile; + if (type.equals(ContainerType.GROUP)) { + WorldGroup group = this.plugin.getGroupManager() + .getGroup(dataName); + if (group == null) { + Logging.warning("Could not import player data for group: " + dataName); + return; + } + playerProfile = group.getGroupProfileContainer().getPlayerData(ProfileTypes.SURVIVAL, player); + } else { + playerProfile = this.plugin.getWorldProfileContainerStore() + .getContainer(dataName).getPlayerData(ProfileTypes.SURVIVAL, player); + } + MIInventoryInterface inventoryInterface = + playerFileLoader.getInventory(GameMode.SURVIVAL.toString()); + playerProfile.set(Sharables.INVENTORY, inventoryInterface.getInventoryContents()); + playerProfile.set(Sharables.ARMOR, inventoryInterface.getArmorContents()); + playerProfile.set(Sharables.HEALTH, playerFileLoader.getHealth()); + playerProfile.set(Sharables.SATURATION, playerFileLoader.getSaturation()); + playerProfile.set(Sharables.EXPERIENCE, playerFileLoader.getExperience()); + playerProfile.set(Sharables.TOTAL_EXPERIENCE, playerFileLoader.getTotalExperience()); + playerProfile.set(Sharables.LEVEL, playerFileLoader.getLevel()); + playerProfile.set(Sharables.FOOD_LEVEL, playerFileLoader.getHunger()); + this.plugin.getData().updatePlayerData(playerProfile); + } + + /** + * @return The group mapping from MultiInv, where worldName -> groupName. + * @throws DataImportException If there was any issues getting the data through reflection. + */ + private HashMap getGroupMap() throws DataImportException { + Field field; + try { + field = MIYamlFiles.class.getDeclaredField("groups"); + } catch (NoSuchFieldException nsfe) { + throw new DataImportException("The running version of MultiInv is " + + "incompatible with the import feature.").setCauseException(nsfe); + } + field.setAccessible(true); + HashMap miGroupMap = null; + try { + miGroupMap = (HashMap) field.get(null); + } catch (IllegalAccessException iae) { + throw new DataImportException("The running version of MultiInv is " + + "incompatible with the import feature.").setCauseException(iae); + } catch (ClassCastException cce) { + throw new DataImportException("The running version of MultiInv is " + + "incompatible with the import feature.").setCauseException(cce); + } + return miGroupMap; + } + + /** + * {@inheritDoc} + */ + @Override + public @NotNull String getPluginName() { + return "MultiInv"; + } + + /** + * {@inheritDoc} + */ + @Override + public @NotNull Class getPluginClass() { + return MultiInv.class; + } +} diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/package-info.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/package-info.java similarity index 54% rename from src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/package-info.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/package-info.java index 62a128b2..8853e329 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/multiinv/package-info.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/multiinv/package-info.java @@ -1,5 +1,4 @@ /** * This package contains MultiInv classes to handle importing their data. */ -package com.onarandombox.multiverseinventories.migration.multiinv; - +package com.onarandombox.multiverseinventories.dataimport.multiinv; diff --git a/src/main/java/com/onarandombox/multiverseinventories/dataimport/package-info.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/package-info.java new file mode 100644 index 00000000..ffedae36 --- /dev/null +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/package-info.java @@ -0,0 +1,5 @@ +/** + * This package contains importer classes to help with importing player stats/inventory data from + * other similar plugins into Multiverse Inventories. + */ +package com.onarandombox.multiverseinventories.dataimport; diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/worldinventories/WorldInventoriesImporter.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/worldinventories/WorldInventoriesImporter.java similarity index 75% rename from src/main/java/com/onarandombox/multiverseinventories/migration/worldinventories/WorldInventoriesImporter.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/worldinventories/WorldInventoriesImporter.java index 7ff07ffd..404351ea 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/worldinventories/WorldInventoriesImporter.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/worldinventories/WorldInventoriesImporter.java @@ -1,274 +1,262 @@ -package com.onarandombox.multiverseinventories.migration.worldinventories; - -import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.multiverseinventories.MultiverseInventories; -import com.onarandombox.multiverseinventories.WorldGroup; -import com.onarandombox.multiverseinventories.profile.ProfileTypes; -import com.onarandombox.multiverseinventories.profile.PlayerProfile; -import com.onarandombox.multiverseinventories.profile.container.ProfileContainer; -import com.onarandombox.multiverseinventories.share.Sharables; -import com.onarandombox.multiverseinventories.migration.DataImporter; -import com.onarandombox.multiverseinventories.migration.MigrationException; -import me.drayshak.WorldInventories.Group; -import me.drayshak.WorldInventories.WIPlayerInventory; -import me.drayshak.WorldInventories.WIPlayerStats; -import me.drayshak.WorldInventories.WorldInventories; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; -import org.bukkit.plugin.Plugin; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -/** - * Handles the importing of data from WorldInventories. - */ -public class WorldInventoriesImporter implements DataImporter { - - private WorldInventories wiPlugin; - private MultiverseInventories inventories; - - public WorldInventoriesImporter(MultiverseInventories inventories, WorldInventories wiPlugin) { - this.inventories = inventories; - this.wiPlugin = wiPlugin; - } - - /** - * @return The WorldInventories plugin hooked to the importer. - */ - public WorldInventories getWIPlugin() { - return this.wiPlugin; - } - - /** - * {@inheritDoc} - */ - @Override - public Plugin getPlugin() { - return this.getWIPlugin(); - } - - /** - * Imports the data from WorldInventories into MultiverseInventories. - * - * @throws MigrationException If there was any MAJOR issues importing the data. - */ - @Override - public void importData() throws MigrationException { - List wiGroups; - try { - wiGroups = this.getWIPlugin().getGroups(); - } catch (Exception e) { - throw new MigrationException("Unable to import from this version of WorldInventories!") - .setCauseException(e); - } catch (Error e) { - throw new MigrationException("Unable to import from this version of WorldInventories!"); - } - if (wiGroups == null) { - throw new MigrationException("No data to import from WorldInventories!"); - } - - if (!wiGroups.isEmpty()) { - WorldGroup defaultWorldGroup = this.inventories.getGroupManager().getDefaultGroup(); - if (defaultWorldGroup != null) { - this.inventories.getGroupManager().removeGroup(defaultWorldGroup); - Logging.info("Removed automatically created world group in favor of imported groups."); - } - } - - this.createGroups(wiGroups); - Set noGroupWorlds = this.getWorldsWithoutGroups(); - this.inventories.getMVIConfig().save(); - - OfflinePlayer[] offlinePlayers = Bukkit.getServer().getOfflinePlayers(); - Logging.info("Processing data for " + offlinePlayers.length + " players. The larger than number, the longer" - + " this process will take. Please be patient. :) Your server will freeze for the duration."); - int playerCount = 0; - for (OfflinePlayer player : offlinePlayers) { - playerCount++; - Logging.finer("(" + playerCount + "/" + offlinePlayers.length - + ")Processing WorldInventories data for player: " + player.getName()); - for (Group wiGroup : wiGroups) { - WorldGroup worldGroup = inventories.getGroupManager().getGroup(wiGroup.getName()); - if (worldGroup == null) { - Logging.finest("Could not import player data for WorldInventories group: " + wiGroup.getName() - + " because there is no Multiverse-Inventories group by that name."); - continue; - } - this.transferData(player, wiGroup, worldGroup.getGroupProfileContainer()); - } - for (ProfileContainer container : noGroupWorlds) { - this.transferData(player, null, container); - } - } - - Logging.info("Import from WorldInventories finished. Disabling WorldInventories."); - Bukkit.getPluginManager().disablePlugin(this.getWIPlugin()); - } - - private void createGroups(List wiGroups) { - for (Group wiGroup : wiGroups) { - if (wiGroup.getWorlds().isEmpty()) { - Logging.warning("Group '" + wiGroup.getName() + "' has no worlds." - + " You may need to add these manually!"); - } - WorldGroup newGroup = inventories.getGroupManager().newEmptyGroup(wiGroup.getName()); - for (String worldName : wiGroup.getWorlds()) { - newGroup.addWorld(worldName); - } - - try { - if (WorldInventories.doStats) { - newGroup.getShares().mergeShares(Sharables.allOf()); - } else { - newGroup.getShares().setSharing(Sharables.ALL_INVENTORY, true); - } - } catch (Exception ignore) { - Logging.warning("Group '" + wiGroup.getName() + "' unable to import fully, sharing only inventory."); - newGroup.getShares().setSharing(Sharables.ALL_INVENTORY, true); - } catch (Error e) { - Logging.warning("Group '" + wiGroup.getName() + "' unable to import fully, sharing only inventory."); - newGroup.getShares().setSharing(Sharables.ALL_INVENTORY, true); - } - this.inventories.getGroupManager().updateGroup(newGroup); - Logging.info("Created Multiverse-Inventories group: " + wiGroup.getName()); - } - } - - private Set getWorldsWithoutGroups() { - Set noGroupWorlds = new LinkedHashSet<>(); - for (World world : Bukkit.getWorlds()) { - if (this.inventories.getGroupManager().getGroupsForWorld(world.getName()).isEmpty()) { - Logging.fine("Added ungrouped world for importing."); - ProfileContainer container = this.inventories.getWorldProfileContainerStore().getContainer(world.getName()); - noGroupWorlds.add(container); - } - } - return noGroupWorlds; - } - - private void transferData(OfflinePlayer player, Group wiGroup, ProfileContainer profileContainer) { - PlayerProfile playerProfile = profileContainer.getPlayerData(ProfileTypes.SURVIVAL, player); - WIPlayerInventory wiInventory = this.loadPlayerInventory(player, wiGroup); - WIPlayerStats wiStats = this.loadPlayerStats(player, wiGroup); - if (wiInventory != null) { - playerProfile.set(Sharables.INVENTORY, wiInventory.getItems()); - playerProfile.set(Sharables.ARMOR, wiInventory.getArmour()); - } - if (wiStats != null) { - playerProfile.set(Sharables.HEALTH, (double) wiStats.getHealth()); - playerProfile.set(Sharables.SATURATION, wiStats.getSaturation()); - playerProfile.set(Sharables.EXPERIENCE, wiStats.getExp()); - playerProfile.set(Sharables.LEVEL, wiStats.getLevel()); - playerProfile.set(Sharables.EXHAUSTION, wiStats.getExhaustion()); - playerProfile.set(Sharables.FOOD_LEVEL, wiStats.getFoodLevel()); - } - this.inventories.getData().updatePlayerData(playerProfile); - Logging.finest("Player's data imported successfully for group: " + profileContainer.getContainerName()); - } - - private File getFile(OfflinePlayer player, Group group, DataType dataType) { - StringBuilder path = new StringBuilder(); - path.append(File.separator); - - // Use default group - if (group == null) { - path.append("default"); - } else { - path.append(group.getName()); - } - path.insert(0, this.getWIPlugin().getDataFolder().getAbsolutePath()); - path.append(File.separator).append(player.getName()).append(dataType.fileExtension); - - File file = new File(path.toString()); - if (!file.exists()) { - file = null; - } - return file; - } - - // Copied and modified from WorldInventories - private WIPlayerInventory loadPlayerInventory(OfflinePlayer player, Group group) { - File file = this.getFile(player, group, DataType.INVENTORY); - if (file == null) { - return null; - } - WIPlayerInventory playerInventory = null; - FileInputStream fIS = null; - ObjectInputStream obIn = null; - try { - fIS = new FileInputStream(file); - obIn = new ObjectInputStream(fIS); - playerInventory = (WIPlayerInventory) obIn.readObject(); - } catch (Exception ignore) { - } finally { - if (obIn != null) { - try { - obIn.close(); - } catch (IOException ignore) { - } - } - if (fIS != null) { - try { - fIS.close(); - } catch (IOException ignore) { - } - } - } - - return playerInventory; - } - - // Copied and modified from WorldInventories - private WIPlayerStats loadPlayerStats(OfflinePlayer player, Group group) { - File file = this.getFile(player, group, DataType.STATS); - if (file == null) { - return null; - } - WIPlayerStats playerstats = null; - FileInputStream fIS = null; - ObjectInputStream obIn = null; - try { - fIS = new FileInputStream(file); - obIn = new ObjectInputStream(fIS); - playerstats = (WIPlayerStats) obIn.readObject(); - } catch (Exception ignore) { - } finally { - if (obIn != null) { - try { - obIn.close(); - } catch (IOException ignore) { - } - } - if (fIS != null) { - try { - fIS.close(); - } catch (IOException ignore) { - } - } - } - - return playerstats; - } - - /** - * Indicates the type of data we're importing for. - */ - private enum DataType { - INVENTORY(".inventory"), - STATS(".stats"); - - private String fileExtension; - - DataType(String fileExtension) { - this.fileExtension = fileExtension; - } - } -} - +package com.onarandombox.multiverseinventories.dataimport.worldinventories; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.multiverseinventories.MultiverseInventories; +import com.onarandombox.multiverseinventories.WorldGroup; +import com.onarandombox.multiverseinventories.dataimport.AbstractDataImporter; +import com.onarandombox.multiverseinventories.dataimport.DataImportException; +import com.onarandombox.multiverseinventories.profile.PlayerProfile; +import com.onarandombox.multiverseinventories.profile.ProfileTypes; +import com.onarandombox.multiverseinventories.profile.container.ProfileContainer; +import com.onarandombox.multiverseinventories.share.Sharables; +import me.drayshak.WorldInventories.Group; +import me.drayshak.WorldInventories.WIPlayerInventory; +import me.drayshak.WorldInventories.WIPlayerStats; +import me.drayshak.WorldInventories.WorldInventories; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +public class WorldInventoriesImporter extends AbstractDataImporter { + + public WorldInventoriesImporter(MultiverseInventories plugin) { + super(plugin); + } + + /** + * {@inheritDoc} + */ + @Override + protected void doDataImport() throws DataImportException { + List wiGroups; + try { + wiGroups = this.importer.getGroups(); + } catch (Exception e) { + throw new DataImportException("Unable to import from this version of WorldInventories!") + .setCauseException(e); + } catch (Error e) { + throw new DataImportException("Unable to import from this version of WorldInventories!"); + } + if (wiGroups == null) { + throw new DataImportException("No data to import from WorldInventories!"); + } + + if (!wiGroups.isEmpty()) { + WorldGroup defaultWorldGroup = this.plugin.getGroupManager().getDefaultGroup(); + if (defaultWorldGroup != null) { + this.plugin.getGroupManager().removeGroup(defaultWorldGroup); + Logging.info("Removed automatically created world group in favor of imported groups."); + } + } + + this.createGroups(wiGroups); + Set noGroupWorlds = this.getWorldsWithoutGroups(); + this.plugin.getMVIConfig().save(); + + OfflinePlayer[] offlinePlayers = Bukkit.getServer().getOfflinePlayers(); + Logging.info("Processing data for " + offlinePlayers.length + " players. The larger than number, the longer" + + " this process will take. Please be patient. :) Your server will freeze for the duration."); + int playerCount = 0; + for (OfflinePlayer player : offlinePlayers) { + playerCount++; + Logging.finer("(" + playerCount + "/" + offlinePlayers.length + + ")Processing WorldInventories data for player: " + player.getName()); + for (Group wiGroup : wiGroups) { + WorldGroup worldGroup = this.plugin.getGroupManager().getGroup(wiGroup.getName()); + if (worldGroup == null) { + Logging.finest("Could not import player data for WorldInventories group: " + wiGroup.getName() + + " because there is no Multiverse-Inventories group by that name."); + continue; + } + this.transferData(player, wiGroup, worldGroup.getGroupProfileContainer()); + } + for (ProfileContainer container : noGroupWorlds) { + this.transferData(player, null, container); + } + } + } + + private void createGroups(List wiGroups) { + for (Group wiGroup : wiGroups) { + if (wiGroup.getWorlds().isEmpty()) { + Logging.warning("Group '" + wiGroup.getName() + "' has no worlds." + + " You may need to add these manually!"); + } + WorldGroup newGroup = plugin.getGroupManager().newEmptyGroup(wiGroup.getName()); + for (String worldName : wiGroup.getWorlds()) { + newGroup.addWorld(worldName); + } + + try { + if (WorldInventories.doStats) { + newGroup.getShares().mergeShares(Sharables.allOf()); + } else { + newGroup.getShares().setSharing(Sharables.ALL_INVENTORY, true); + } + } catch (Exception ignore) { + Logging.warning("Group '" + wiGroup.getName() + "' unable to import fully, sharing only inventory."); + newGroup.getShares().setSharing(Sharables.ALL_INVENTORY, true); + } catch (Error e) { + Logging.warning("Group '" + wiGroup.getName() + "' unable to import fully, sharing only inventory."); + newGroup.getShares().setSharing(Sharables.ALL_INVENTORY, true); + } + this.plugin.getGroupManager().updateGroup(newGroup); + Logging.info("Created Multiverse-Inventories group: " + wiGroup.getName()); + } + } + + private Set getWorldsWithoutGroups() { + Set noGroupWorlds = new LinkedHashSet<>(); + for (World world : Bukkit.getWorlds()) { + if (this.plugin.getGroupManager().getGroupsForWorld(world.getName()).isEmpty()) { + Logging.fine("Added ungrouped world for importing."); + ProfileContainer container = this.plugin.getWorldProfileContainerStore().getContainer(world.getName()); + noGroupWorlds.add(container); + } + } + return noGroupWorlds; + } + + private void transferData(OfflinePlayer player, Group wiGroup, ProfileContainer profileContainer) { + PlayerProfile playerProfile = profileContainer.getPlayerData(ProfileTypes.SURVIVAL, player); + WIPlayerInventory wiInventory = this.loadPlayerInventory(player, wiGroup); + WIPlayerStats wiStats = this.loadPlayerStats(player, wiGroup); + if (wiInventory != null) { + playerProfile.set(Sharables.INVENTORY, wiInventory.getItems()); + playerProfile.set(Sharables.ARMOR, wiInventory.getArmour()); + } + if (wiStats != null) { + playerProfile.set(Sharables.HEALTH, (double) wiStats.getHealth()); + playerProfile.set(Sharables.SATURATION, wiStats.getSaturation()); + playerProfile.set(Sharables.EXPERIENCE, wiStats.getExp()); + playerProfile.set(Sharables.LEVEL, wiStats.getLevel()); + playerProfile.set(Sharables.EXHAUSTION, wiStats.getExhaustion()); + playerProfile.set(Sharables.FOOD_LEVEL, wiStats.getFoodLevel()); + } + this.plugin.getData().updatePlayerData(playerProfile); + Logging.finest("Player's data imported successfully for group: " + profileContainer.getContainerName()); + } + + private File getFile(OfflinePlayer player, Group group, DataType dataType) { + StringBuilder path = new StringBuilder(); + path.append(File.separator); + + // Use default group + if (group == null) { + path.append("default"); + } else { + path.append(group.getName()); + } + path.insert(0, this.importer.getDataFolder().getAbsolutePath()); + path.append(File.separator).append(player.getName()).append(dataType.fileExtension); + + File file = new File(path.toString()); + if (!file.exists()) { + file = null; + } + return file; + } + + // Copied and modified from WorldInventories + private WIPlayerInventory loadPlayerInventory(OfflinePlayer player, Group group) { + File file = this.getFile(player, group, DataType.INVENTORY); + if (file == null) { + return null; + } + WIPlayerInventory playerInventory = null; + FileInputStream fIS = null; + ObjectInputStream obIn = null; + try { + fIS = new FileInputStream(file); + obIn = new ObjectInputStream(fIS); + playerInventory = (WIPlayerInventory) obIn.readObject(); + } catch (Exception ignore) { + } finally { + if (obIn != null) { + try { + obIn.close(); + } catch (IOException ignore) { + } + } + if (fIS != null) { + try { + fIS.close(); + } catch (IOException ignore) { + } + } + } + + return playerInventory; + } + + // Copied and modified from WorldInventories + private WIPlayerStats loadPlayerStats(OfflinePlayer player, Group group) { + File file = this.getFile(player, group, DataType.STATS); + if (file == null) { + return null; + } + WIPlayerStats playerstats = null; + FileInputStream fIS = null; + ObjectInputStream obIn = null; + try { + fIS = new FileInputStream(file); + obIn = new ObjectInputStream(fIS); + playerstats = (WIPlayerStats) obIn.readObject(); + } catch (Exception ignore) { + } finally { + if (obIn != null) { + try { + obIn.close(); + } catch (IOException ignore) { + } + } + if (fIS != null) { + try { + fIS.close(); + } catch (IOException ignore) { + } + } + } + + return playerstats; + } + + /** + * Indicates the type of data we're importing for. + */ + private enum DataType { + INVENTORY(".inventory"), + STATS(".stats"); + + private String fileExtension; + + DataType(String fileExtension) { + this.fileExtension = fileExtension; + } + } + + /** + * {@inheritDoc} + */ + @Override + public @NotNull String getPluginName() { + return "WorldInventories"; + } + + /** + * {@inheritDoc} + */ + @Override + public @NotNull Class getPluginClass() { + return WorldInventories.class; + } +} diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/worldinventories/package-info.java b/src/main/java/com/onarandombox/multiverseinventories/dataimport/worldinventories/package-info.java similarity index 54% rename from src/main/java/com/onarandombox/multiverseinventories/migration/worldinventories/package-info.java rename to src/main/java/com/onarandombox/multiverseinventories/dataimport/worldinventories/package-info.java index f82850c7..cf112728 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/worldinventories/package-info.java +++ b/src/main/java/com/onarandombox/multiverseinventories/dataimport/worldinventories/package-info.java @@ -1,5 +1,4 @@ /** * This package contains WorldInventories classes to handle importing their data. */ -package com.onarandombox.multiverseinventories.migration.worldinventories; - +package com.onarandombox.multiverseinventories.dataimport.worldinventories; diff --git a/src/main/java/com/onarandombox/multiverseinventories/locale/Message.java b/src/main/java/com/onarandombox/multiverseinventories/locale/Message.java index badd749d..95d27a05 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/locale/Message.java +++ b/src/main/java/com/onarandombox/multiverseinventories/locale/Message.java @@ -32,8 +32,8 @@ public enum Message { ERROR_NO_GROUP("&6There is no group with the name: &f%1"), ERROR_NO_WORLD("&6There is no world with the name: &f%1"), ERROR_NO_WORLD_PROFILE("&6There is no profile container for the world: &f%1"), - ERROR_PLUGIN_NOT_ENABLED("&f%1 &6is not enabled so you may not import data from it!"), - ERROR_UNSUPPORTED_IMPORT("&6Sorry, ''&f%1&6'' is not supported for importing."), + ERROR_PLUGIN_NOT_ENABLED("&f%1 &6is not enabled on your server, so you may not import data from it!"), + ERROR_UNSUPPORTED_IMPORT("&6Sorry, '&f%1&6' is not supported for importing."), ERROR_NO_SHARES_SPECIFIED("&cYou did not specify any valid shares!"), // Group Conflicts @@ -90,9 +90,12 @@ public enum Message { NO_OPTIONAL_SHARES("&f%1 &6is not an optional share!"), // Migrate Command MIGRATE_FAILED("Failed to migrate data from %1 to %2! Check logs for error details."), - MIGRATE_SUCCESSFUL("Migrated data from %1 to %2!"); - - // BEGIN CHECKSTYLE-SUPPRESSION: Javadoc + MIGRATE_SUCCESSFUL("Migrated data from %1 to %2!"), + // Import Command + IMPORT_ATTEMPT("&6Attempting to import data from %1..."), + IMPORT_FAILED("&cAn errored occurred while attempting to import from %1."), + IMPORT_SUCCESSFUL("&aImported data from %1! You can now remove &6%1 &aand restart your server. Enjoy!"); + // END CHECKSTYLE-SUPPRESSION: Javadoc private final List def; diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/DataImporter.java b/src/main/java/com/onarandombox/multiverseinventories/migration/DataImporter.java deleted file mode 100644 index e97a5fbd..00000000 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/DataImporter.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.onarandombox.multiverseinventories.migration; - -import org.bukkit.plugin.Plugin; - -/** - * Interface for data migration importers. - */ -public interface DataImporter { - - /** - * Imports the data from another plugin. - * - * @throws MigrationException If there was any MAJOR issue loading the data. - */ - void importData() throws MigrationException; - - /** - * @return The plugin associated with this Importer. - */ - Plugin getPlugin(); -} - diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/ImportManager.java b/src/main/java/com/onarandombox/multiverseinventories/migration/ImportManager.java deleted file mode 100644 index b1eefaeb..00000000 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/ImportManager.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.onarandombox.multiverseinventories.migration; - -import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.multiverseinventories.MultiverseInventories; -import com.onarandombox.multiverseinventories.migration.multiinv.MultiInvImporter; -import com.onarandombox.multiverseinventories.migration.worldinventories.WorldInventoriesImporter; -import me.drayshak.WorldInventories.WorldInventories; -import uk.co.tggl.pluckerpluck.multiinv.MultiInv; - -/** - * Manages the import heplers for other similar plugins. - */ -public class ImportManager { - - private MultiInvImporter multiInvImporter = null; - private WorldInventoriesImporter worldInventoriesImporter = null; - private MultiverseInventories inventories; - - public ImportManager(MultiverseInventories inventories) { - this.inventories = inventories; - } - - /** - * Hooks MultiInv for importing it's data. - * - * @param plugin Instance of MultiInv. - */ - public void hookMultiInv(MultiInv plugin) { - this.multiInvImporter = new MultiInvImporter(this.inventories, plugin); - Logging.info("Hooked MultiInv for importing!"); - } - - /** - * Hooks WorldInventories for importing it's data. - * - * @param plugin Instance of WorldInventories. - */ - public void hookWorldInventories(WorldInventories plugin) { - this.worldInventoriesImporter = new WorldInventoriesImporter(this.inventories, plugin); - Logging.info("Hooked WorldInventories for importing!"); - } - - /** - * @return The MultiInv importer class or null if not hooked. - */ - public MultiInvImporter getMultiInvImporter() { - return this.multiInvImporter; - } - - /** - * @return The WorldInventories importer class or null if not hooked. - */ - public WorldInventoriesImporter getWorldInventoriesImporter() { - return this.worldInventoriesImporter; - } - - /** - * Un-hooks MultiInv so we're not able to import from it anymore. - */ - public void unHookMultiInv() { - this.multiInvImporter = null; - } - - /** - * Un-hooks WorldInventories so we're not able to import from it anymore. - */ - public void unHookWorldInventories() { - this.worldInventoriesImporter = null; - } -} - diff --git a/src/main/java/com/onarandombox/multiverseinventories/migration/package-info.java b/src/main/java/com/onarandombox/multiverseinventories/migration/package-info.java deleted file mode 100644 index 139a8dbc..00000000 --- a/src/main/java/com/onarandombox/multiverseinventories/migration/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -/** - * This package contains thigns to help with importing player stats/inventory data from - * other similar plugins. - */ -package com.onarandombox.multiverseinventories.migration; - diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index a32fcf0d..52e6b1ad 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -21,7 +21,7 @@ ERROR_DATA_LOAD: 'Encountered an error while loading the data file. Disabling.. ERROR_NO_GROUP: '&6There is no group with the name: &f%1' ERROR_NO_WORLD: '&6There is no world with the name: &f%1' ERROR_NO_WORLD_PROFILE: '&6There is no world profile for the world: &f%1' -ERROR_PLUGIN_NOT_ENABLED: '&f%1 &6is not enabled so you may not import data from it!' +ERROR_PLUGIN_NOT_ENABLED: '&f%1 &6is not enabled on your server, so you may not import data from it!' ERROR_UNSUPPORTED_IMPORT: '&6Sorry, ''&f%1&6'' is not supported for importing.' ERROR_NO_SHARES_SPECIFIED: '&cYou did not specify any valid shares!' @@ -86,4 +86,9 @@ DEBUG_SET: # Toggle command NOW_USING_OPTIONAL: '&f%1 &6will now be considered when player''s change world.' NOW_NOT_USING_OPTIONAL: '&f%1 &6will no longer be considered when player''s change world.' -NO_OPTIONAL_SHARES: '&f%1 &6is not an optional share!' \ No newline at end of file +NO_OPTIONAL_SHARES: '&f%1 &6is not an optional share!' + +# Import command +IMPORT_ATTEMPT: '&6Attempting to import data from %1...' +IMPORT_FAILED: '&cAn errored occurred while attempting to import from %1.' +IMPORT_SUCCESSFUL: '&aImported data from %1! You can now remove &6%1 &aand restart your server. Enjoy!' \ No newline at end of file