mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-27 04:25:14 +01:00
Better catch for potential exceptions when de-serializing items
For some reason items might corrupt in the encoding and decoding process leading to invalid YAML strings. There's also another issue of the server not being able to load certain old itemstacks. (seems to be especially an issue with old entity types)
This commit is contained in:
parent
52492f8469
commit
8a09e91e62
@ -15,13 +15,12 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
import org.yaml.snakeyaml.error.YAMLException;
|
||||||
import org.yaml.snakeyaml.nodes.Tag;
|
import org.yaml.snakeyaml.nodes.Tag;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@ -85,7 +84,6 @@ public class ItemDatabase {
|
|||||||
AtomicInteger i = new AtomicInteger();
|
AtomicInteger i = new AtomicInteger();
|
||||||
AtomicInteger updated = new AtomicInteger();
|
AtomicInteger updated = new AtomicInteger();
|
||||||
CloseableIterator<Item> it = itemDao.iterator();
|
CloseableIterator<Item> it = itemDao.iterator();
|
||||||
List<Item> toUpdate = new ArrayList<>();
|
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
@ -97,14 +95,19 @@ public class ItemDatabase {
|
|||||||
try {
|
try {
|
||||||
String serialized = (String) Base64.decodeToObject(item.getBase64ItemCode());
|
String serialized = (String) Base64.decodeToObject(item.getBase64ItemCode());
|
||||||
if (previousVersion < 0 || !serialized.contains("\nv: " + newVersion + "\n")) { // Hacky way to quickly check the version as it's not too big of an issue if some items don't convert
|
if (previousVersion < 0 || !serialized.contains("\nv: " + newVersion + "\n")) { // Hacky way to quickly check the version as it's not too big of an issue if some items don't convert
|
||||||
ItemStack itemStack = yaml.loadAs(serialized, ItemStack.class);
|
try {
|
||||||
item.setBase64ItemCode(Base64.encodeObject(yaml.dump(itemStack)));
|
ItemStack itemStack = yaml.loadAs(serialized, ItemStack.class);
|
||||||
toUpdate.add(item);
|
item.setBase64ItemCode(Base64.encodeObject(yaml.dump(itemStack)));
|
||||||
itemDao.update(item);
|
itemDao.update(item);
|
||||||
updated.getAndIncrement();
|
updated.getAndIncrement();
|
||||||
|
} catch (YAMLException e) {
|
||||||
|
ChestShop.getBukkitLogger().log(Level.SEVERE, "YAML of the item with ID " + Base62.encode(item.getId()) + " (" + item.getId() + ") is corrupted: \n" + serialized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException | ClassNotFoundException | SQLException e) {
|
} catch (IOException | ClassNotFoundException | SQLException e) {
|
||||||
e.printStackTrace();
|
ChestShop.getBukkitLogger().log(Level.SEVERE, "Unable to convert item with ID " + Base62.encode(item.getId()) + " (" + item.getId() + ")", e);
|
||||||
|
} catch (StackOverflowError e) {
|
||||||
|
ChestShop.getBukkitLogger().log(Level.SEVERE, "Item with ID " + Base62.encode(item.getId()) + " (" + item.getId() + ") is corrupted. Sorry :(");
|
||||||
}
|
}
|
||||||
if (i.get() % 1000 == 0) {
|
if (i.get() % 1000 == 0) {
|
||||||
ChestShop.getBukkitLogger().info("Checked " + i + " items. Updated " + updated + "...");
|
ChestShop.getBukkitLogger().info("Checked " + i + " items. Updated " + updated + "...");
|
||||||
@ -120,7 +123,7 @@ public class ItemDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ChestShop.getBukkitLogger().info("Finished updating database in " + (System.currentTimeMillis() - start) / 1000.0 + "s. " +
|
ChestShop.getBukkitLogger().info("Finished updating database in " + (System.currentTimeMillis() - start) / 1000.0 + "s. " +
|
||||||
toUpdate.size() + " items out of " + i + " were updated!");
|
updated + " items out of " + i + " were updated!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,8 +172,8 @@ public class ItemDatabase {
|
|||||||
{
|
{
|
||||||
// TODO java.lang.StackOverflowError - http://pastebin.com/eRD8wUFM - Corrupt item DB?
|
// TODO java.lang.StackOverflowError - http://pastebin.com/eRD8wUFM - Corrupt item DB?
|
||||||
|
|
||||||
|
int id = Base62.decode(code);
|
||||||
try {
|
try {
|
||||||
int id = Base62.decode(code);
|
|
||||||
Item item = itemDao.queryBuilder().where().eq("id", id).queryForFirst();
|
Item item = itemDao.queryBuilder().where().eq("id", id).queryForFirst();
|
||||||
|
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
@ -179,13 +182,15 @@ public class ItemDatabase {
|
|||||||
|
|
||||||
String serialized = item.getBase64ItemCode();
|
String serialized = item.getBase64ItemCode();
|
||||||
|
|
||||||
return yaml.loadAs((String) Base64.decodeToObject(serialized), ItemStack.class);
|
try {
|
||||||
} catch (SQLException e) {
|
return yaml.loadAs((String) Base64.decodeToObject(serialized), ItemStack.class);
|
||||||
e.printStackTrace();
|
} catch (YAMLException e) {
|
||||||
} catch (IOException e) {
|
ChestShop.getBukkitLogger().log(Level.SEVERE, "YAML of the item with ID " + Base62.encode(item.getId()) + " (" + item.getId() + ") is corrupted: \n" + serialized);
|
||||||
e.printStackTrace();
|
}
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (IOException | ClassNotFoundException | SQLException | YAMLException e) {
|
||||||
e.printStackTrace();
|
ChestShop.getBukkitLogger().log(Level.SEVERE, "Unable to load item with ID " + code + " (" + id + ")", e);
|
||||||
|
} catch (StackOverflowError e) {
|
||||||
|
ChestShop.getBukkitLogger().log(Level.SEVERE, "Item with ID " + code + " (" + id + ") is corrupted. Sorry :(");
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
Loading…
Reference in New Issue
Block a user