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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ItemComponent implements Component, InteractionHandler {

private boolean isVisible;
private volatile String lastKey;
private volatile Object lastRenderedItem;

public ItemComponent(
Function<? extends IFContext, String> keyFactory,
Expand Down Expand Up @@ -67,6 +68,7 @@ public ItemComponent(
this.updateOnClick = updateOnClick;
this.isVisible = isVisible;
this.reference = reference;
this.lastRenderedItem = stack;
}

@Override
Expand Down Expand Up @@ -145,12 +147,16 @@ public void render(@NotNull IFSlotRenderContext context) {
if (getRenderHandler() != null) {
final int initialSlot = getPosition();

if (lastRenderedItem != null) context.setResult(lastRenderedItem);

try {
getRenderHandler().accept(context);
} catch (Exception e) {
throw new InventoryFrameworkException("Failed to render item component", e);
}

lastRenderedItem = context.getResult();

// Externally managed components have its own displacement measures
if (!isManagedExternally()) {
final int updatedSlot = context.getSlot();
Expand Down Expand Up @@ -197,6 +203,7 @@ public void updated(@NotNull IFSlotRenderContext context) {
boolean isWatchingAnyState =
getWatchingStates() != null && !getWatching().isEmpty();
if (isVisible() && getUpdateHandler() != null) {
if (lastRenderedItem != null) context.setResult(lastRenderedItem);
getUpdateHandler().accept(context);
if (context.isCancelled()) return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ public interface IFSlotRenderContext extends IFSlotContext, IFConfinedContext {
@ApiStatus.Internal
Object getResult();

/**
* Sets the item that was rendered by the owning component in a previous render.
*
* <p><b><i>This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided.</i></b>
*/
@ApiStatus.Internal
void setResult(Object result);

boolean isCancelled();

void setCancelled(boolean cancelled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public final void setItem(ItemStack item) {
setChanged(true);
}

@Override
public final void setResult(Object result) {
this.item = (ItemStack) result;
}

@Override
public final boolean isCancelled() {
return cancelled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class SlotRenderContext

override fun getResult(): ItemStack = item

override fun setResult(result: Any?) {
item = result as? ItemStack ?: ItemStack.AIR
}

override fun isCancelled(): Boolean = cancelled

override fun setCancelled(cancelled: Boolean) {
Expand Down
Loading