Encode actions as base64
This commit is contained in:
parent
84a8c1bc58
commit
a677af1b86
1 changed files with 21 additions and 9 deletions
|
@ -1,7 +1,9 @@
|
|||
package dev.zontreck.ase.guis;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
@ -107,7 +109,7 @@ public class PrimitiveAction {
|
|||
}
|
||||
}
|
||||
|
||||
// Attach a list of actions to an item
|
||||
// Attach a list of actions to an item with Base64 encoding
|
||||
public static ItemStack attachActions(List<PrimitiveAction> actions, ItemStack item) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) {
|
||||
|
@ -117,20 +119,26 @@ public class PrimitiveAction {
|
|||
// Serialize the list of actions into a string
|
||||
StringBuilder serializedActions = new StringBuilder();
|
||||
for (PrimitiveAction action : actions) {
|
||||
String actionString = action.actionType + ":" + action.actionData;
|
||||
String actionString = action.actionType + ":"
|
||||
+ Base64.getEncoder().encodeToString(action.actionData.getBytes(StandardCharsets.UTF_8));
|
||||
serializedActions.append(actionString).append(";");
|
||||
}
|
||||
|
||||
// Store the serialized actions as lore or in a custom tag (like NBT)
|
||||
// Base64 encode the serialized actions string
|
||||
String base64EncodedActions = Base64.getEncoder()
|
||||
.encodeToString(serializedActions.toString().getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// Store the Base64 encoded actions in the persistent data container
|
||||
PersistentDataContainer data = meta.getPersistentDataContainer();
|
||||
data.set(NamespacedKey.fromString(ACTIONS, AriasServerEssentials.getSelf()), PersistentDataType.STRING,
|
||||
serializedActions.toString());
|
||||
base64EncodedActions);
|
||||
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
// Get the actions stored in the item's PersistentDataContainer
|
||||
// Get the actions stored in the item's PersistentDataContainer with Base64
|
||||
// decoding
|
||||
public static List<PrimitiveAction> getActions(ItemStack item) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) {
|
||||
|
@ -138,15 +146,18 @@ public class PrimitiveAction {
|
|||
}
|
||||
|
||||
PersistentDataContainer data = meta.getPersistentDataContainer();
|
||||
String actionsData = data.get(NamespacedKey.fromString(ACTIONS, AriasServerEssentials.getSelf()),
|
||||
String base64EncodedActions = data.get(NamespacedKey.fromString(ACTIONS, AriasServerEssentials.getSelf()),
|
||||
PersistentDataType.STRING);
|
||||
|
||||
if (actionsData == null || actionsData.isEmpty()) {
|
||||
if (base64EncodedActions == null || base64EncodedActions.isEmpty()) {
|
||||
return new ArrayList<>(); // No actions stored
|
||||
}
|
||||
|
||||
// Base64 decode the actions string
|
||||
String decodedActions = new String(Base64.getDecoder().decode(base64EncodedActions), StandardCharsets.UTF_8);
|
||||
|
||||
List<PrimitiveAction> actions = new ArrayList<>();
|
||||
String[] actionsArray = actionsData.split(";");
|
||||
String[] actionsArray = decodedActions.split(";");
|
||||
|
||||
for (String actionString : actionsArray) {
|
||||
if (!actionString.isEmpty()) {
|
||||
|
@ -154,7 +165,8 @@ public class PrimitiveAction {
|
|||
if (actionParts.length == 2) {
|
||||
try {
|
||||
int actionType = Integer.parseInt(actionParts[0]);
|
||||
String actionData = actionParts[1];
|
||||
String actionData = new String(Base64.getDecoder().decode(actionParts[1]),
|
||||
StandardCharsets.UTF_8);
|
||||
actions.add(new PrimitiveAction(actionType, actionData));
|
||||
} catch (NumberFormatException e) {
|
||||
// Handle invalid action type (logging, skipping, etc.)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue