Skip to content
This repository was archived by the owner on Oct 2, 2022. It is now read-only.

Serializer

Zoyn edited this page Apr 26, 2019 · 8 revisions

Serializer

BukkitObjectSerializerUtils

When we want to save a bukkit object to a sql server, we need to serialize the object to string, so this util can deal with it easily

Just see the code, i don't want to write it..

BukkitObjectSerializerUtils

EffectsSerializerUtils

When we want to save the effects to a sql server, we need to serialize effects to string, so this util can deal with it easily

Collection<PotionEffect> effects = player.getActivePotionEffects();
String datas = EffectsSerializerUtils.toBase64(effects);

Collection<PotionEffect> dataToEffects = EffectsSerializerUtils.fromBase64(datas);

ItemSerializerUtils

When we want to save the items to a sql server, we need to serialize items to string, so this util can deal with it easily

ItemStack[] items = // your items;
String base64ItemData = ItemSerializerUtils.toBase64(items);

ItemStack[] dataToItems = ItemSerializerUtils.fromBase64(base64ItemData);

ItemSerializer

This util is in the common module, it can convert an item to json, and you can save it or load it

String mojangson = ItemSerializer.getItemStackJson(itemStack);

The output will like this

{
	id: "minecraft:paper",
	Count: 1 b,
	tag: {
		ench: [{
			lvl: 1 s,
			id: 34 s
		}],
		display: {
			Lore: ["§bLine1", "§eLin2"],
			Name: "§6My Paper~"
		}
	},
	Damage: 0 s
}

Yeah it just a mojangson..

BTW... if i want to load it, how can i do that?

ItemStack itemStack = player.getItemInHand();
String mojangson = ItemSerializer.getItemStackJson(itemStack);
ItemStack itemStackFromMojangson = ItemSerializer.loadItemStackJson(mojangson);

WARN: if you System.out.print(itemStack.equals(itemStackFromMojangson)), it will return true!

This wiki hopes to help you!

没有中文教程...自己看代码吧..

Clone this wiki locally