ChestShop-3/src/main/java/com/Acrobot/ChestShop/Metadata/ItemDatabase.java

112 lines
3.1 KiB
Java
Raw Normal View History

2013-01-24 22:35:28 +01:00
package com.Acrobot.ChestShop.Metadata;
import com.Acrobot.Breeze.Utils.Encoding.Base62;
import com.Acrobot.Breeze.Utils.Encoding.Base64;
2015-05-22 13:26:07 +02:00
import com.Acrobot.ChestShop.Database.DaoCreator;
import com.Acrobot.ChestShop.Database.Item;
import com.j256.ormlite.dao.Dao;
2013-01-24 22:35:28 +01:00
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.bukkit.inventory.ItemStack;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
2015-05-22 13:26:07 +02:00
import org.yaml.snakeyaml.nodes.Tag;
2013-01-24 22:35:28 +01:00
import java.io.IOException;
import java.sql.SQLException;
/**
* Saves items with Metadata in database, which allows for saving items on signs easily.
*
2013-01-24 22:35:28 +01:00
* @author Acrobot
*/
public class ItemDatabase {
2015-05-22 13:26:07 +02:00
private Dao<Item, Integer> itemDao;
2013-08-05 02:06:13 +02:00
private final Yaml yaml;
2013-01-24 22:35:28 +01:00
public ItemDatabase() {
2015-05-22 13:26:07 +02:00
yaml = new Yaml(new YamlBukkitConstructor(), new YamlRepresenter(), new DumperOptions());
2013-01-24 22:35:28 +01:00
try {
2015-05-22 13:26:07 +02:00
itemDao = DaoCreator.getDaoAndCreateTable(Item.class);
2013-01-24 22:35:28 +01:00
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Gets the item code for this item
*
* @param item Item
* @return Item code for this item
*/
public String getItemCode(ItemStack item) {
try {
2013-10-27 16:50:04 +01:00
ItemStack clone = new ItemStack(item);
clone.setAmount(1);
2015-05-22 13:26:07 +02:00
clone.setDurability((short) 0);
String code = Base64.encodeObject(yaml.dump(clone));
2015-05-22 13:26:07 +02:00
Item itemEntity = itemDao.queryBuilder().where().eq("code", code).queryForFirst();
if (itemEntity != null) {
return Base62.encode(itemEntity.getId());
}
itemEntity = new Item(code);
itemDao.create(itemEntity);
int id = itemEntity.getId();
2013-01-24 22:35:28 +01:00
return Base62.encode(id);
} catch (SQLException e) {
e.printStackTrace();
2015-05-22 13:26:07 +02:00
} catch (IOException e) {
e.printStackTrace();
2013-01-24 22:35:28 +01:00
}
2015-05-22 13:26:07 +02:00
return null;
2013-01-24 22:35:28 +01:00
}
/**
* Gets an ItemStack from a item code
*
* @param code Item code
* @return ItemStack represented by this code
*/
public ItemStack getFromCode(String code)
{
// TODO java.lang.StackOverflowError - http://pastebin.com/eRD8wUFM - Corrupt item DB?
2013-01-24 22:35:28 +01:00
try {
2015-05-22 13:26:07 +02:00
int id = Base62.decode(code);
Item item = itemDao.queryBuilder().where().eq("id", id).queryForFirst();
2013-01-24 22:35:28 +01:00
2015-05-22 13:26:07 +02:00
if (item == null) {
2013-01-24 22:35:28 +01:00
return null;
}
2015-05-22 13:26:07 +02:00
String serialized = item.getBase64ItemCode();
2013-08-05 02:06:13 +02:00
2015-05-22 13:26:07 +02:00
return yaml.loadAs((String) Base64.decodeToObject(serialized), ItemStack.class);
2013-01-24 22:35:28 +01:00
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
2015-05-22 13:26:07 +02:00
return null;
}
private class YamlBukkitConstructor extends YamlConstructor {
public YamlBukkitConstructor() {
this.yamlConstructors.put(new Tag(Tag.PREFIX + "org.bukkit.inventory.ItemStack"), yamlConstructors.get(Tag.MAP));
}
2013-01-24 22:35:28 +01:00
}
}