Created
November 10, 2021 22:16
-
-
Save Shawn11564/567d72d072e9054632cc2d5a6853ce93 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
import de.leonhard.storage.Json; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.util.io.BukkitObjectInputStream; | |
import org.bukkit.util.io.BukkitObjectOutputStream; | |
import org.jetbrains.annotations.Nullable; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.util.Base64; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class SerializationUtils { | |
@Nullable | |
public static String itemStackToBase64(ItemStack item) { | |
try { | |
ByteArrayOutputStream io = new ByteArrayOutputStream(); | |
BukkitObjectOutputStream os = new BukkitObjectOutputStream(io); | |
os.writeObject(item); | |
os.close(); | |
byte[] serializedObject = io.toByteArray(); | |
return Base64.getEncoder().encodeToString(serializedObject); | |
} catch (final IOException ex) { | |
Bukkit.getLogger().severe("Unable to serialize item: " + item); | |
ex.printStackTrace(); | |
return null; | |
} | |
} | |
@Nullable | |
public static ItemStack base64ToItemStack(String base64) { | |
try { | |
byte[] serializedObject = Base64.getDecoder().decode(base64); | |
ByteArrayInputStream in = new ByteArrayInputStream(serializedObject); | |
BukkitObjectInputStream is = new BukkitObjectInputStream(in); | |
ItemStack item = (ItemStack) is.readObject(); | |
is.close(); | |
return item; | |
} catch (final IOException | ClassNotFoundException ex) { | |
Bukkit.getLogger().severe("Unable to deserialize item from string: " + base64); | |
ex.printStackTrace(); | |
return null; | |
} | |
} | |
public static Inventory inventoryMapToInventory(Map<Integer, ItemStack> inventoryMap) { | |
final Inventory inventory = Bukkit.createInventory(null, inventoryMap.size()); | |
for (Map.Entry<Integer, ItemStack> entry : inventoryMap.entrySet()) { | |
inventory.setItem(entry.getKey(), entry.getValue() != null ? entry.getValue() : new ItemStack(Material.AIR)); | |
} | |
return inventory; | |
} | |
public static Map<Integer, ItemStack> inventoryToInventoryMap(Inventory inventory) { | |
final Map<Integer, ItemStack> inventoryMap = new HashMap<>(); | |
for (int i = 0; i < inventory.getSize(); i++) { | |
inventoryMap.put(i, inventory.getItem(i) != null ? inventory.getItem(i) : null); | |
} | |
return inventoryMap; | |
} | |
public static Map<Integer, String> inventoryToBase64Map(Inventory inventory) { | |
final Map<Integer, String> base64 = new HashMap<>(); | |
for (int i = 0; i < inventory.getSize(); i++) { | |
base64.put(i, inventory.getItem(i) != null ? itemStackToBase64(inventory.getItem(i)) : "AIR"); | |
} | |
return base64; | |
} | |
public static Map<Integer, String> fileListToBase64Map(String characterName, Json file, String resetPathPrefix) { | |
file.setPathPrefix(null); | |
final Map<Integer, String> base64Map = new HashMap<>(); | |
for (String key : file.singleLayerKeySet("characters." + characterName + ".inventory")) { | |
base64Map.put(Integer.valueOf(key), (String) file.get("characters." + characterName + ".inventory." + key)); | |
} | |
file.setPathPrefix(resetPathPrefix); | |
return base64Map; | |
} | |
public static Map<Integer, ItemStack> base64ToInventoryContents(Map<Integer, String> base64Map) { | |
final Map<Integer, ItemStack> contents = new HashMap<>(); | |
for (Map.Entry<Integer, String> entry : base64Map.entrySet()) { | |
contents.put(entry.getKey(), !entry.getValue().equals("AIR") ? base64ToItemStack(entry.getValue()) : null); | |
} | |
return contents; | |
} | |
public static Map<Integer, ItemStack> createBlankInventoryMap(int size) { | |
final Map<Integer, ItemStack> inventoryMap = new HashMap<>(); | |
for (int i = 0; i < size; i++) { | |
inventoryMap.put(i, null); | |
} | |
return inventoryMap; | |
} | |
public static Map<Integer, String> convertItemMapToBase64Map(Map<Integer, ItemStack> itemMap) { | |
final Map<Integer, String> base64Map = new HashMap<>(); | |
for (Map.Entry<Integer, ItemStack> entry : itemMap.entrySet()) { | |
base64Map.put(entry.getKey(), entry.getValue() != null ? itemStackToBase64(entry.getValue()) : "AIR"); | |
} | |
return base64Map; | |
} | |
public static String convertBase64MapToString(Map<Integer, String> base64Map) { | |
StringBuilder sb = new StringBuilder(); | |
int count = 0; | |
for (Map.Entry<Integer, String> entry : base64Map.entrySet()) { | |
sb.append(entry.getKey()) | |
.append(":") | |
.append(entry.getValue()); | |
if (count < base64Map.size()) { | |
sb.append(","); | |
} | |
count++; | |
} | |
return sb.toString(); | |
} | |
public static Map<Integer, ItemStack> convertBase64StringToItemMap(String base64) { | |
final Map<Integer, ItemStack> itemMap = new HashMap<>(); | |
String[] sections = base64.split(","); | |
for (String section : sections) { | |
String[] parts = section.split(":"); | |
int slot = Integer.parseInt(parts[0]); | |
ItemStack item = parts[1].equals("AIR") ? null : base64ToItemStack(parts[1]); | |
itemMap.put(slot, item); | |
} | |
return itemMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment