Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework data importing from other plugin. #432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<? extends Plugin> dataImporter = this.inventories.getImportManager().getImporter(event.getPlugin());
if (dataImporter == null) {
return;
}
dataImporter.enable(event.getPlugin());
}

/**
Expand All @@ -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<? extends Plugin> dataImporter = this.inventories.getImportManager().getImporter(event.getPlugin());
if (dataImporter == null) {
return;
}
dataImporter.disable();
}

@EventHandler(priority = EventPriority.MONITOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,27 +30,23 @@ public ImportCommand(MultiverseInventories plugin) {

@Override
public void runCommand(CommandSender sender, List<String> 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<? extends Plugin> 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());
}
}

Original file line number Diff line number Diff line change
@@ -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<T extends Plugin> implements DataImporter<T> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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;
}
Expand Down
Loading