ItemStackWrapper - Simplix-Softworks/Cirrus GitHub Wiki

ItemStackWrapper

Since Cirrus aims for platform independence, we need to wrap an actual platform dependent ItemStack inside an abstraction. This abstraction is called ItemStackWrapper.

Design decisions

To minimize the development effort on all current platforms, Cirrus uses on currently all platforms the ItemStack class of Protocolize. Converting from an de.exceptionflug.protocolize.items.ItemStack to a org.bukkit.inventory.ItemStack is possible using the Converters class on the bukkit platform.

ItemStack bukkitItemStack = Converters.convert(protocolizeItemStack, ItemStack.class);

Subtypes of ItemStackWrapper

  • ProtocolizeItemStackWrapper
  • InventoryItemWrapper
  • DataInventoryItemWrapper

ProtocolizeItemStackWrapper

As stated above, there is currently no other platform dependent implementation of ItemStackWrapper as the ProtocolizeItemStackWrapper.

InventoryItemWrapper

This class is used for storing ItemStackWrappers with some additional information inside in Containers. Additonal information stored:

  • The id of the ActionHandler to be called on click
  • A list of strings passed to the ActionHandler as arguments

DataInventoryItemWrapper<T>

This class extends InventoryItemWrapper. This class allows additionally to store an instance of T as payload.

Conversion

Most of the Cirrus API is designed to let you pass different types of wrappers to the menu methods. If you have to convert your ItemStack or wrappers by hand you can do so.

ItemStackWrapper

It is possible to use the Converters class to convert a platform dependent ItemStack to an ItemStackWrapper. If you are inside of a menu class, you can use the wrapItemStack method.

// Inside a menu class
ItemStackWrapper wrapper = wrapItemStack(itemStack);
// Everywhere else
ItemStackWrapper wrapper = Converters.convert(itemStack, ItemStackWrapper.class);

This works also backwards:

ItemStack itemStack = Converters.convert(itemStackWrapper, ItemStack.class);

If you are using Cirrus on BungeeCord, you can also directly use the handle of the wrapper:

ItemStack itemStack = itemStackWrapper.handle();

NOTICE: Since the Converters class of SimplixCore is able to do multiple conversion steps automatically, you don't have to worry about first converting your org.bukkit.inventory.ItemStack to an de.exceptionflug.protocolize.items.ItemStack. SimplixCore and Cirrus will do the work for you, if needed.

(Data)InventoryItemWrapper

You can create an InventoryItemWrapper or DataInventoryItemWrapper by calling the constructor. Conversion from a given LocalizedItemStackModel to an InventoryItemWrapper is possible by using the Converters class.

InventoryItemWrapper wrapper = Converters.convert(localizedModel, InventoryItemWrapper.class);

If you just have an ItemStackModel, you need to localize it first:

LocalizedItemStackModel localizedModel = Localizer.localize(itemStackModel, Locale.ENGLISH /** or whatever locale you want **/);
// You can additionally pass your replacements to the method

After that you can use the Converters class like shown above.