Skip to content

Commit

Permalink
NbtItemStack
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursiveG committed Feb 8, 2019
1 parent 2562a0c commit 675a057
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/cat/nyaa/nyaacore/NyaaCoreLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import cat.nyaa.nyaacore.component.IMessageQueue;
import cat.nyaa.nyaacore.component.NyaaComponent;
import cat.nyaa.nyaacore.configuration.NbtItemStack;
import cat.nyaa.nyaacore.http.client.HttpClient;
import cat.nyaa.nyaacore.timer.TimerManager;
import cat.nyaa.nyaacore.utils.ClickSelectionUtils;
import cat.nyaa.nyaacore.utils.OfflinePlayerUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.craftbukkit.v1_13_R2.util.CraftMagicNumbers;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -20,6 +22,10 @@ public static NyaaCoreLoader getInstance() {

public static final String TARGET_MAPPING = "00ed8e5c39debc3ed194ad7c5645cc45";

static {
ConfigurationSerialization.registerClass(NbtItemStack.class);
}

@Override
public void onLoad() {
instance = this;
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/cat/nyaa/nyaacore/configuration/NbtItemStack.java
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);
}
}
}

0 comments on commit 675a057

Please sign in to comment.