Last active
January 1, 2024 00:36
-
-
Save sathonay/9fbe2d0248dda785168e1dc6acb79f6c to your computer and use it in GitHub Desktop.
Utility to serialize itemstack array
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 com.google.gson.Gson; | |
import org.bukkit.inventory.ItemStack; | |
import java.util.Base64; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class InventorySerialization { | |
private static final Gson GSON = new Gson(); | |
public static String itemStackArrayToBase64(ItemStack[] content) { | |
Map<Integer, ItemStack> itemsMap = generateItemsMap(content); | |
SerializeSlot[] slots = generateSlotArray(content, itemsMap); | |
return Base64.getEncoder().encodeToString(GSON.toJson(new SerializeItemStackArray(itemsMap, slots)).getBytes()); | |
} | |
public static String itemStackArrayToBase64(ItemStack[] content, Map<Integer, ItemStack> itemsMap) { | |
SerializeSlot[] slots = generateSlotArray(content, itemsMap); | |
return Base64.getEncoder().encodeToString(GSON.toJson(slots).getBytes()); | |
} | |
public static ItemStack[] itemStackArrayFromBase64(String base64) { | |
String json = new String(Base64.getDecoder().decode(base64)); | |
return GSON.fromJson(json, SerializeItemStackArray.class).toItemStackArray(); | |
} | |
public static ItemStack[] itemStackArrayFromBase64(String base64, Map<Integer, ItemStack> itemsMap) { | |
String json = new String(Base64.getDecoder().decode(base64)); | |
SerializeSlot[] slots = GSON.fromJson(json, SerializeSlot[].class); | |
return new SerializeItemStackArray(itemsMap, slots).toItemStackArray(); | |
} | |
public static class SerializeItemStackArray { | |
final Map<Integer, ItemStack> itemsMap; | |
final SerializeSlot[] slots; | |
public SerializeItemStackArray(Map<Integer, ItemStack> itemsMap, SerializeSlot[] slots) { | |
this.itemsMap = itemsMap; | |
this.slots = slots; | |
} | |
public ItemStack[] toItemStackArray() { | |
ItemStack[] itemStack = new ItemStack[slots.length]; | |
for (int i = 0; i < slots.length; i++) { | |
SerializeSlot slot = slots[i]; | |
if (slot == null) continue; | |
ItemStack clone = itemsMap.get(slot.mapRef()).clone(); | |
clone.setAmount(slot.amount()); | |
itemStack[i] = clone; | |
} | |
return itemStack; | |
} | |
} | |
public static class SerializeSlot { | |
private final Integer[] data = new Integer[2]; | |
public SerializeSlot(int mapRef, int stackAmount) { | |
data[0] = mapRef; | |
data[1] = stackAmount; | |
} | |
public int mapRef() { | |
return data[0]; | |
} | |
public int amount() { | |
return data[1]; | |
} | |
} | |
private static ItemStack itemToMapItem(ItemStack stack) { | |
if (stack == null) return null; | |
ItemStack mapItem = stack.clone(); | |
mapItem.setAmount(1); | |
return mapItem; | |
} | |
private static Integer itemToMapRef(ItemStack stack) { | |
if (stack == null) return null; | |
return itemToMapItem(stack).hashCode(); | |
} | |
public static SerializeSlot[] generateSlotArray(ItemStack[] content, Map<Integer, ItemStack> itemsMap) { | |
SerializeSlot[] slots = new SerializeSlot[content.length]; | |
for (int i = 0; i < content.length; i++) { | |
ItemStack item = content[i]; | |
Integer mapRef = itemToMapRef(item); | |
if (itemsMap.containsKey(itemToMapRef(item))) { | |
slots[i] = new SerializeSlot(mapRef, item.getAmount()); | |
} else { | |
slots[i] = null; | |
} | |
} | |
return slots; | |
} | |
public static Map<Integer, ItemStack> generateItemsMap(ItemStack[] content) { | |
Map<Integer, ItemStack> itemsMap = new HashMap<>(); | |
for (int i = 0; i < content.length; i++) { | |
ItemStack item = content[i]; | |
if (item == null) continue; | |
ItemStack mapItem = itemToMapItem(item); | |
Integer mapRef = itemToMapRef(mapItem); | |
itemsMap.putIfAbsent(mapRef, mapItem); | |
} | |
return itemsMap; | |
} | |
} | |
###### Exemple ######## | |
ItemStack[] defaultContent = new ItemStack[4 * 9]; | |
defaultContent[0] = new ItemBuilder(Material.DIAMOND_SWORD).setName(ChatColor.BLUE + "Epée" + ChatColor.GRAY + " (3 coups)").toItemStack(); | |
defaultContent[1] = new ItemBuilder(Material.BOW).setName(ChatColor.BLUE + "Arc " + ChatColor.GRAY + " (1 coup)").toItemStack(); | |
for (int i = 2; i < 8; i++) { | |
ItemStack it = new ItemStack(Material.CLAY, 64); | |
ItemMeta itemMeta = it.getItemMeta(); | |
itemMeta.setDisplayName(ChatColor.BLUE + "Blocks" + ChatColor.GRAY + " (illimité)"); | |
it.setItemMeta(itemMeta); | |
System.out.println(itemMeta.hashCode()); | |
defaultContent[i] = it; | |
} | |
defaultContent[1] = new ItemBuilder(Material.DIAMOND_PICKAXE).setName(ChatColor.BLUE + "Pioche " + ChatColor.GRAY + " (Efficacité 5)").toItemStack(); | |
defaultContent[9] = new ItemStack(Material.ARROW); | |
ItemStack[] content = new ItemStack[4 * 9]; | |
content[0] = new ItemBuilder(Material.DIAMOND_SWORD).setName(ChatColor.BLUE + "Epée" + ChatColor.GRAY + " (3 coups)").toItemStack(); | |
content[1] = new ItemBuilder(Material.BOW).setName(ChatColor.BLUE + "Arc " + ChatColor.GRAY + " (1 coup)").toItemStack(); | |
for (int i = 2; i < 8; i++) { | |
ItemStack it = new ItemStack(Material.CLAY, 64); | |
ItemMeta itemMeta = it.getItemMeta(); | |
itemMeta.setDisplayName(ChatColor.BLUE + "Blocks" + ChatColor.GRAY + " (illimité)"); | |
it.setItemMeta(itemMeta); | |
System.out.println(itemMeta.hashCode()); | |
content[i] = it; | |
} | |
content[1] = new ItemBuilder(Material.DIAMOND_PICKAXE).setName(ChatColor.BLUE + "Pioche " + ChatColor.GRAY + " (Efficacité 5)").toItemStack(); | |
content[9] = new ItemStack(Material.ARROW); | |
Map<Integer, ItemStack> itemsMap = InventorySerialization.generateItemsMap(defaultContent); | |
String contentBase = InventorySerialization.itemStackArrayToBase64(content, itemsMap); | |
ItemStack[] unbaseContent = InventorySerialization.itemStackArrayFromBase64(contentBase, itemsMap); | |
for (int i = 0; i < unbaseContent.length; i++) { | |
if (unbaseContent[i] == null) continue; | |
System.out.println(i + " -> " + unbaseContent[i].getType().name() + " " + unbaseContent[i].getAmount()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment