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

Add some checks to prevent errors in per world inventories import #523

Draft
wants to merge 1 commit into
base: per-world-inventory
Choose a base branch
from
Draft
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 @@ -19,8 +19,8 @@ public class ImportCommand extends InventoriesCommand {

public ImportCommand(MultiverseInventories plugin) {
super(plugin);
this.setName("Import from MultiInv/WorldInventories");
this.setCommandUsage("/mvinv import " + ChatColor.GREEN + "{MultiInv|WorldInventories}");
this.setName("Import from MultiInv/WorldInventories/PerWorldInventory");
this.setCommandUsage("/mvinv import " + ChatColor.GREEN + "{MultiInv|WorldInventories|PerWorldInventory}");
this.setArgRange(1, 1);
this.addKey("mvinv import");
this.addKey("mvinvim");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public boolean importData(boolean disableOnSuccess) {
doDataImport();
} catch (DataImportException e) {
Logging.severe(e.getMessage());
Logging.severe("Cause: %s", e.getCauseException().getMessage());
if(e.getCauseException() != null) {
Logging.severe("Cause: %s", e.getCauseException().getMessage());
}
e.printStackTrace();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package com.onarandombox.multiverseinventories.dataimport.perworldinventory;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.OfflinePlayer;
import org.bukkit.potion.PotionEffect;

import com.dumptruckman.bukkit.configuration.util.SerializationHelper;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
Expand All @@ -9,6 +21,7 @@
import com.onarandombox.multiverseinventories.profile.ProfileTypes;
import com.onarandombox.multiverseinventories.profile.container.ContainerType;
import com.onarandombox.multiverseinventories.share.Sharables;

import me.ebonjaeger.perworldinventory.Group;
import me.ebonjaeger.perworldinventory.GroupManager;
import me.ebonjaeger.perworldinventory.api.PerWorldInventoryAPI;
Expand All @@ -21,18 +34,6 @@
import me.ebonjaeger.perworldinventory.data.ProfileManager;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

class PwiImportHelper {

Expand Down Expand Up @@ -67,7 +68,7 @@ void importData() throws DataImportException {
}
}

/**
/**
* Do the necessary reflection to get access to the classes needed for data import.
*/
private void pwiSetUp() throws DataImportException {
Expand Down Expand Up @@ -169,14 +170,24 @@ private PlayerProfile getPWIPlayerData(ProfileKey pwiKey) throws DataImportExcep
Logging.finer("No data for %s.", pwiKey.toString());
return null;
}

PlayerProfile pwiPlayerProfile;
PlayerProfile pwiPlayerProfile = null;
try {
JSONObject jsonObject = (JSONObject) PARSER.parse(new FileInputStream(pwiPlayerDataFile));
pwiPlayerProfile = (PlayerProfile) SerializationHelper.deserialize(jsonObject);
JSONObject jsonObject = (JSONObject) PARSER.parse(new FileInputStream(pwiPlayerDataFile));

if((!jsonObject.containsKey("data-format") || ((int)jsonObject.get("data-format")) < 2)) {
Logging.finer("Inventory format too old to import. %s.", pwiPlayerDataFile.getAbsolutePath());
return null;
}

pwiPlayerProfile = (PlayerProfile) SerializationHelper.deserialize(jsonObject);
} catch (Exception e) {
e.printStackTrace();
throw new DataImportException("Unable to parse file into profile: " + pwiPlayerDataFile.getAbsolutePath());
if(Bukkit.getOfflinePlayer(pwiKey.getUuid()).hasPlayedBefore()){
e.printStackTrace();
throw new DataImportException("Unable to parse file into profile: " + pwiPlayerDataFile.getAbsolutePath());
}else {
//Migration command for PWI does not take into account players that aren't present in the offline player directory.
Logging.finer("We have a profile that failed to decode, because they haven't played on the server before. %s.", pwiPlayerDataFile.getAbsolutePath());
}
}

if (pwiPlayerProfile == null) {
Expand Down