Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import java.util.stream.Collectors;

import static com.extendedclip.deluxemenus.utils.Constants.PLACEHOLDER_PREFIX;
import static com.extendedclip.deluxemenus.utils.Constants.STACK_PREFIX;
import static com.extendedclip.deluxemenus.utils.Constants.PLAYER_ITEMS;
import static com.extendedclip.deluxemenus.utils.Constants.WATER_BOTTLE;

Expand All @@ -90,6 +91,7 @@ public class DeluxeMenusConfig {
VALID_MATERIALS.add(WATER_BOTTLE);

VALID_MATERIAL_PREFIXES.add(PLACEHOLDER_PREFIX);
VALID_MATERIAL_PREFIXES.add(STACK_PREFIX);
}

private final String separator = File.separator;
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
import java.util.logging.Level;
import java.util.stream.Collectors;

import static com.extendedclip.deluxemenus.utils.Constants.INVENTORY_ITEM_ACCESSORS;
import static com.extendedclip.deluxemenus.utils.Constants.PLACEHOLDER_PREFIX;
import static com.extendedclip.deluxemenus.utils.Constants.*;

public class MenuItem {

Expand All @@ -65,6 +64,25 @@ public MenuItem(@NotNull final DeluxeMenus plugin, @NotNull final MenuItemOption
this.options = options;
}

public static ItemStack base64ToItemStack(String data) {
try {
byte[] bytes = Base64.getDecoder().decode(data);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
dataInput.close();
Object object = dataInput.readObject();
if (object instanceof ItemStack) {
return (ItemStack) object;
}
return null;
} catch (IllegalArgumentException e) {
return null;
} catch (IOException e) {
return null;
} catch (ClassNotFoundException e) {
return null;
}
}
public ItemStack getItemStack(@NotNull final MenuHolder holder) {
final Player viewer = holder.getViewer();

Expand All @@ -78,6 +96,16 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {
stringMaterial = holder.setPlaceholdersAndArguments(stringMaterial.substring(PLACEHOLDER_PREFIX.length()));
lowercaseStringMaterial = stringMaterial.toLowerCase(Locale.ENGLISH);
}
if (ItemUtils.isItemStackOption(lowercaseStringMaterial)) {
stringMaterial = holder.setPlaceholdersAndArguments(stringMaterial.substring(STACK_PREFIX.length()));
ItemStack base64Item = base64ToItemStack(stringMaterial);
if (base64Item != null) {
itemStack = base64Item;
amount = itemStack.getAmount();
lowercaseStringMaterial = itemStack.getType().toString().toLowerCase(Locale.ENGLISH);
}
}


if (ItemUtils.isPlayerItem(lowercaseStringMaterial)) {
final ItemStack playerItem = INVENTORY_ITEM_ACCESSORS.get(lowercaseStringMaterial).apply(viewer.getInventory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private Constants() {
public static final String BASE64_HEAD_TYPE = "basehead";
public static final String HDB_HEAD_TYPE = "hdb";

public static final String STACK_PREFIX = "stack-";
public static final String PLACEHOLDER_PREFIX = "placeholder-";
public static final String WATER_BOTTLE = "water_bottle";

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/extendedclip/deluxemenus/utils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.jetbrains.annotations.NotNull;

import java.util.Locale;

import static com.extendedclip.deluxemenus.utils.Constants.INVENTORY_ITEM_ACCESSORS;
import static com.extendedclip.deluxemenus.utils.Constants.PLACEHOLDER_PREFIX;
import static com.extendedclip.deluxemenus.utils.Constants.STACK_PREFIX;
import static com.extendedclip.deluxemenus.utils.Constants.WATER_BOTTLE;

public final class ItemUtils {
Expand All @@ -29,6 +29,16 @@ public static boolean isPlaceholderOption(@NotNull final String material) {
return material.toLowerCase(Locale.ROOT).startsWith(PLACEHOLDER_PREFIX);
}

/**
* Checks if the string starts with the substring "stack-". The check is case-insensitive.
*
* @param itemstack The string to check
* @return true if the string starts with "stack-", false otherwise
*/
public static boolean isItemStackOption(@NotNull final String material) {
return material.toLowerCase(Locale.ROOT).startsWith(STACK_PREFIX);
}

/**
* Checks if the string is a player item. The check is case-sensitive.
* Player items are: "main_hand", "off_hand", "armor_helmet", "armor_chestplate", "armor_leggings", "armor_boots"
Expand Down
Loading