-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2562a0c
commit 675a057
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/cat/nyaa/nyaacore/configuration/NbtItemStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cat.nyaa.nyaacore.configuration; | ||
|
||
import cat.nyaa.nyaacore.utils.ItemStackUtils; | ||
import org.bukkit.configuration.serialization.ConfigurationSerializable; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class NbtItemStack implements ConfigurationSerializable { | ||
public ItemStack it; | ||
|
||
public NbtItemStack(ItemStack it) { | ||
this.it = it; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> serialize() { | ||
Map<String, Object> ret = new HashMap<>(); | ||
if (it != null) { | ||
try { | ||
ret.put("nbt", ItemStackUtils.itemToBase64(it)); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
ret.put("nbt", "<null>"); | ||
} | ||
} else { | ||
ret.put("nbt", "<null>"); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
public static NbtItemStack deserialize(Map<String, Object> map) { | ||
try { | ||
String nbt = (String) map.getOrDefault("nbt", 0); | ||
if (nbt == null || "<null>".equalsIgnoreCase(nbt)) return new NbtItemStack(null); | ||
return new NbtItemStack(ItemStackUtils.itemFromBase64(nbt)); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
return new NbtItemStack(null); | ||
} | ||
} | ||
} |