Skip to content
Open
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 @@ -14,11 +14,13 @@ public final class LayoutSlot {
private final char character;
private final IntFunction<ComponentFactory> factory;
private final int[] positions;
private final boolean fill;

public LayoutSlot(char character, @Nullable IntFunction<ComponentFactory> factory, int[] positions) {
public LayoutSlot(char character, @Nullable IntFunction<ComponentFactory> factory, int[] positions, boolean fill) {
this.character = character;
this.factory = factory;
this.positions = positions;
this.fill = fill;
}

public char getCharacter() {
Expand All @@ -30,9 +32,13 @@ public IntFunction<ComponentFactory> getFactory() {
}

public LayoutSlot withFactory(@Nullable IntFunction<ComponentFactory> factory) {
return new LayoutSlot(character, factory, positions);
return new LayoutSlot(character, factory, positions, fill);
}

public LayoutSlot withFill(boolean fill) {
return new LayoutSlot(character, factory, positions, fill);
}

public int[] getPositions() {
return positions;
}
Expand All @@ -41,6 +47,10 @@ public boolean isDefinedByTheUser() {
return factory != null;
}

public boolean isToFill() {
return fill;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -57,6 +67,6 @@ public int hashCode() {
@Override
public String toString() {
return "LayoutSlot{" + "character=" + character + ", factory=" + factory + ", positions="
+ Arrays.toString(positions) + '}';
+ Arrays.toString(positions) + ", fill=" + fill + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ public void intercept(PipelineContext<VirtualView> pipeline, VirtualView subject
continue;
}

int iterationIndex = 0;
for (final int slot : layoutSlot.getPositions()) {
final ComponentFactory componentFactory = factory.apply(iterationIndex++);
if (componentFactory instanceof ItemComponentBuilder)
((ItemComponentBuilder<?, ?>) componentFactory).withSlot(slot);

final Component component = componentFactory.create();
renderContext.addComponent(component);
}
if (layoutSlot.isToFill()) {
int iterationIndex = 0;
for (final int slot : layoutSlot.getPositions()) {
renderContext.addComponent(createComponent(slot, iterationIndex++, factory));
}
} else {
final int nextAvailableSlot = layoutSlot.
renderContext.addComponent(createComponent(nextAvailableSlot, 0 /* TODO not supported for now */, factory));
}
}
}

private Component createComponent(int slot, int index, IntFunction<ComponentFactory> factory) {
final ComponentFactory componentFactory = factory.apply(index);
if (componentFactory instanceof ItemComponentBuilder)
((ItemComponentBuilder<?, ?>) componentFactory).withSlot(slot);

return componentFactory.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public void intercept(PipelineContext<VirtualView> pipeline, VirtualView subject
final LayoutSlot layoutSlot = new LayoutSlot(
entry.getKey(),
null,
entry.getValue().stream().mapToInt($ -> $).toArray());
entry.getValue().stream().mapToInt($ -> $).toArray(),,
true);
renderContext.addLayoutSlot(layoutSlot);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void resolveFromLayoutMustBeEmptyIfPositionsIsNull() {
when(context.getConfig()).thenReturn(createLayoutConfig(defaultLayout));
when(context.getLayoutSlots())
.thenReturn(Collections.singletonList(
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> mock(ComponentFactory.class), null)));
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> mock(ComponentFactory.class), null, true)));

assertTrue(new AvailableSlotInterceptor().resolveFromLayoutSlot(context).isEmpty());
}
Expand All @@ -56,7 +56,7 @@ void resolveFromLayoutMustBeEmptyIfPositionsIsEmpty() {
when(context.getConfig()).thenReturn(createLayoutConfig(defaultLayout));

LayoutSlot layoutSlot =
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> mock(ComponentFactory.class), new int[0]);
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> mock(ComponentFactory.class), new int[0], true);

when(context.getLayoutSlots()).thenReturn(Collections.singletonList(layoutSlot));

Expand All @@ -83,7 +83,7 @@ void resolveFromLayoutSlot() {
when(context.getAvailableSlotFactory()).thenReturn(availableSlotFactory);

LayoutSlot layoutSlot =
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> componentFactory, defaultLayoutSlotRange);
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> componentFactory, defaultLayoutSlotRange, true);
when(context.getLayoutSlots()).thenReturn(Collections.singletonList(layoutSlot));

List<ComponentFactory> resolved = new AvailableSlotInterceptor().resolveFromLayoutSlot(context);
Expand Down Expand Up @@ -111,7 +111,7 @@ void interceptFromLayoutSlot() {
when(context.getAvailableSlotFactory()).thenReturn(availableSlotFactory);

LayoutSlot layoutSlot =
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> componentFactory, defaultLayoutSlotRange);
new LayoutSlot(LayoutSlot.FILLED_RESERVED_CHAR, $ -> componentFactory, defaultLayoutSlotRange, true);
when(context.getLayoutSlots()).thenReturn(Collections.singletonList(layoutSlot));

new AvailableSlotInterceptor().intercept(mock(PipelineContext.class), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ public void layoutSlot(char character, @NotNull BiConsumer<Integer, T> factory)
}));
}

/**
* <p><b><i> This API is experimental and is not subject to the general compatibility guarantees
* such API may be changed or may be removed completely in any further release. </i></b>
*/
@ApiStatus.Experimental
public @NotNull T singleLayoutSlot(char character) {
requireNonReservedLayoutCharacter(character);

// TODO More detailed exception message
final LayoutSlot layoutSlot = getLayoutSlots().stream()
.filter(value -> value.getCharacter() == character)
.findFirst()
.orElseThrow(() -> new InventoryFrameworkException("Missing layout character: " + character));

final T builder = createBuilder();
getLayoutSlots().add(layoutSlot.withFactory($ -> (ComponentFactory) builder).withFill(false));
return builder;
}

/**
* <p><b><i> This API is experimental and is not subject to the general compatibility guarantees
* such API may be changed or may be removed completely in any further release. </i></b>
Expand Down