-
-
Save CyberFlameGO/44775258e07701e45f003414c64f9e86 to your computer and use it in GitHub Desktop.
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
/* | |
* Copyright (c) 2016. Starlis LLC / dba Empire Minecraft | |
* | |
* This source code is proprietary software and must not be redistributed without Starlis LLC's approval | |
* | |
*/ | |
package com.empireminecraft.util.serialization; | |
import com.empireminecraft.util.ItemUtil; | |
import com.empireminecraft.util.Log; | |
import com.empireminecraft.util.json.JsonConfiguration; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.configuration.InvalidConfigurationException; | |
import org.bukkit.entity.Player; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.PlayerInventory; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.jetbrains.annotations.NotNull; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
@SuppressWarnings("WeakerAccess") | |
public final class Serialization { | |
private Serialization() {} | |
public static void initialize() { | |
} | |
public static String serializeInventory(Inventory inventory) { | |
return serializeInventory(inventory.getTitle(), inventory.getSize(), inventory.getStorageContents()); | |
} | |
public static String serializeInventory(String title, int size, ItemStack[] items) { | |
JsonConfiguration json = new JsonConfiguration(); | |
json.set("size", size); | |
json.set("name", title); | |
int idx = 0; | |
Map<String, ItemStack> itemMap = new HashMap<>(); | |
for (ItemStack item : items) { | |
int i = idx++; | |
if (!ItemUtil.isValid(item)) { | |
continue; | |
} | |
itemMap.put("" + i, item); | |
} | |
json.createSection("items", itemMap); | |
return json.saveToString(); | |
} | |
public static String serializeItem(ItemStack item) { | |
return serializeItem(item, true); | |
} | |
public static String serializeItem(ItemStack item, boolean singleCount) { | |
int count = item.getAmount(); | |
if (singleCount) { | |
item.setAmount(1); | |
} | |
JsonConfiguration json = new JsonConfiguration(); | |
json.set("item", item); | |
String result = json.saveToString(); | |
item.setAmount(count); | |
return result; | |
} | |
public static ItemStack deserializeItem(String json) { | |
try { | |
return new JsonConfiguration(json).getItemStack("item"); | |
} catch (InvalidConfigurationException e) { | |
Log.exception(e); | |
} | |
return null; | |
} | |
public static Inventory deserializeInventory(String jsons) throws InvalidConfigurationException { | |
return deserializeInventory(jsons, null); | |
} | |
public static Inventory deserializeInventory(String jsons, String title) throws InvalidConfigurationException { | |
try { | |
JsonConfiguration json = new JsonConfiguration(); | |
json.loadFromString(jsons); | |
int size = json.getInt("size", 54); | |
if (title == null) { | |
title = json.getString("name"); | |
} | |
Inventory inventory = Bukkit.createInventory(null, size, title); | |
Map<String, Object> items = json.getConfigurationSection("items").getValues(false); | |
for (Map.Entry<String, Object> item : items.entrySet()) { | |
ItemStack itemstack = (ItemStack) item.getValue(); | |
int idx = Integer.parseInt(item.getKey()); | |
inventory.setItem(idx, itemstack); | |
} | |
return inventory; | |
} catch (InvalidConfigurationException e) { | |
Log.exception("Exception in deserializeYaml" + jsons, e); | |
return null; | |
} | |
} | |
public static String serializePlayerInventory(Player player, String extra, String filter, boolean clear) { | |
JsonConfiguration json = new JsonConfiguration(); | |
int idx = 0; | |
HashMap<String, ItemStack> storageContents = new HashMap<>(); | |
PlayerInventory inventory = player.getInventory(); | |
StringBuilder logBuilder = new StringBuilder(64); | |
logBuilder.append("Serializing PlInv:").append(player.getName()).append(":"); | |
if (clear) { | |
logBuilder.append("clearing:"); | |
} | |
for (ItemStack item : inventory.getStorageContents()) { | |
if (ItemUtil.isValid(item) && checkFilter(item, filter)) { | |
storageContents.put("" + idx, item); | |
appendItem(logBuilder, item); | |
if (clear) { | |
inventory.setItem(idx, null); | |
} | |
} | |
idx++; | |
} | |
json.createSection("storageContents", storageContents); | |
HashMap<String, ItemStack> armorContents = new HashMap<>(); | |
idx = 0; | |
for (ItemStack item : inventory.getArmorContents()) { | |
if (ItemUtil.isValid(item) && checkFilter(item, filter)) { | |
armorContents.put("" + idx, item); | |
appendItem(logBuilder, item); | |
if (clear) { | |
inventory.setItem(36 + idx, null); | |
} | |
} | |
idx++; | |
} | |
json.createSection("armorContents", armorContents); | |
HashMap<String, ItemStack> extraContents = new HashMap<>(); | |
idx = 0; | |
for (ItemStack item : inventory.getExtraContents()) { | |
if (ItemUtil.isValid(item) && checkFilter(item, filter)) { | |
extraContents.put("" + idx, item); | |
appendItem(logBuilder, item); | |
if (clear) { | |
ItemStack[] extraCon = inventory.getExtraContents(); | |
extraCon[idx] = null; | |
inventory.setExtraContents(extraCon); | |
} | |
} | |
idx++; | |
} | |
json.createSection("extraContents", extraContents); | |
ItemStack item = player.getItemOnCursor(); | |
if (ItemUtil.isValid(item) && checkFilter(item, filter)) { | |
json.set("onCursor", player.getItemOnCursor()); | |
appendItem(logBuilder, item); | |
if (clear) { | |
player.setItemOnCursor(null); | |
} | |
} | |
Log.info(logBuilder.toString()); | |
json.set("extra", extra != null ? extra : "drop"); | |
return json.saveToString(); | |
} | |
public static void appendItem(StringBuilder sb, ItemStack item) { | |
sb.append(item.getI18NDisplayName()).append("#").append(item.getAmount()); | |
if (item.hasItemMeta()) { | |
ItemMeta meta = item.getItemMeta(); | |
if (meta.hasDisplayName()) { | |
sb.append("-").append(item.getType()); | |
} | |
if (meta.hasLore()) { | |
Iterator<String> lines = meta.getLore().iterator(); | |
for (int i = 0; i < 3 && lines.hasNext(); i++) { | |
sb.append("|").append(lines.next()); | |
} | |
} | |
} | |
sb.append(","); | |
} | |
private static boolean checkFilter(@NotNull ItemStack item, String filter) { | |
if (filter == null) { | |
return true; | |
} | |
switch (filter) { | |
case "test": | |
if (item.getType() != Material.DIRT) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static Map<String, Object> deserializePlayerInventory(String jsons) throws InvalidConfigurationException { | |
JsonConfiguration json = new JsonConfiguration(); | |
json.loadFromString(jsons); | |
Map<String, Object> inventoryMap = new HashMap<>(5); | |
inventoryMap.put("storageContents", json.getConfigurationSection("storageContents").getValues(false)); | |
inventoryMap.put("armorContents", json.getConfigurationSection("armorContents").getValues(false)); | |
inventoryMap.put("extraContents", json.getConfigurationSection("extraContents").getValues(false)); | |
inventoryMap.put("onCursor", json.get("onCursor")); | |
inventoryMap.put("extra", json.get("extra")); | |
return inventoryMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment