Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/src/main/java/me/zort/containr/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;

@Getter
public abstract class Element implements Component {

Expand All @@ -23,4 +25,14 @@ public void click(ContextClickInfo info) {}
@Nullable
public abstract ItemStack item(Player player);

/**
* Post-process the created ItemStack before it is applied to the GUI.
*
* @param player The player for which the item has been created.
* @param item The created item. Is not the same as the returned from {@link #item(Player)}.
* @param newItemConsumer The handler that MUST be called after the item has been modified to update the menu.
*/
public void postProcessItem(Player player, ItemStack item, Consumer<ItemStack> newItemConsumer) {
// No modifications by default.
}
}
3 changes: 3 additions & 0 deletions core/src/main/java/me/zort/containr/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ public void update(@NotNull Player p, boolean clear, Class<? extends Element>...
item = NBT.modifyNBT(item, nbtItem -> nbtItem.setString(Constants.ELEMENT_ID_KEY, element.getId()));
}
if(slot < inventory.getSize() && slot >= 0) {
Consumer<ItemStack> processFinalItem = (finalItem) -> inventory.setItem(slot, finalItem);
element.postProcessItem(p, item, processFinalItem);

inventory.setItem(slot, item);
} else {
String err = "Cannot complete menu %s for player %s because index out of bounds: %d >= %d";
Expand Down