Skip to content

Commit

Permalink
fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek committed Sep 30, 2024
1 parent a363b5e commit d2765cf
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.infumia.frame.context.view;

import java.util.Map;
import java.util.Collection;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.element.ElementContainer;
import net.infumia.frame.element.ElementItemBuilderFactory;
Expand All @@ -11,6 +11,7 @@
import net.infumia.frame.view.ViewContainer;
import net.infumia.frame.view.config.ViewConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;

public interface ContextRender
extends
Expand All @@ -25,7 +26,8 @@ public interface ContextRender
ViewConfig config();

@NotNull
Map<Character, LayoutSlot> layouts();
@UnmodifiableView
Collection<LayoutSlot> layouts();

void back();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.infumia.frame.pipeline.context;

import java.util.Collection;
import java.util.Map;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.context.view.ContextClick;
import net.infumia.frame.context.view.ContextClose;
Expand Down Expand Up @@ -92,7 +91,7 @@ interface LayoutResolution extends PipelineContextView {
ViewContainer container();

@NotNull
Map<Character, LayoutSlot> layouts();
Collection<LayoutSlot> layouts();

void addLayout(char character, @NotNull Collection<Integer> indexes);
}
Expand All @@ -108,7 +107,7 @@ interface CreateRender extends PipelineContextView {
ViewContainer container();

@NotNull
Map<Character, LayoutSlot> layouts();
Collection<LayoutSlot> layouts();
}

interface Click extends PipelineContextView, Cancellable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.infumia.frame.pipeline.executor;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.context.view.ContextInit;
Expand Down Expand Up @@ -60,9 +59,7 @@ > executeModifyContainer(
);

@NotNull
CompletableFuture<
Pair<ConsumerService.State, Map<Character, LayoutSlot>>
> executeLayoutResolution(
CompletableFuture<Pair<ConsumerService.State, Collection<LayoutSlot>>> executeLayoutResolution(
@NotNull ContextBase context,
@NotNull ViewConfig config,
@NotNull ViewContainer container
Expand All @@ -73,7 +70,7 @@ CompletableFuture<ContextRender> executeCreateRender(
@NotNull ContextBase context,
@NotNull ViewConfig config,
@NotNull ViewContainer container,
@NotNull Map<Character, LayoutSlot> layouts
@NotNull Collection<LayoutSlot> layouts
);

@NotNull
Expand Down
3 changes: 2 additions & 1 deletion common/src/main/java/net/infumia/frame/slot/LayoutSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public interface LayoutSlot {
@Nullable
IntFunction<ElementItemBuilder> builderFactory();

void builderFactory(@Nullable IntFunction<ElementItemBuilder> builderFactory);
@NotNull
LayoutSlot withBuilderFactory(@Nullable IntFunction<ElementItemBuilder> builderFactory);

boolean contains(int slot);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.infumia.frame.context.view;

import net.infumia.frame.slot.LayoutSlot;
import net.infumia.frame.view.ViewContainer;
import net.infumia.frame.viewer.ContextualViewer;
import org.bukkit.event.inventory.ClickType;
Expand Down Expand Up @@ -105,16 +104,17 @@ public boolean entityContainer() {

@Override
public boolean isLayoutSlot() {
return this.layouts().values().stream().anyMatch(slot -> slot.contains(this.clickedSlot()));
return this.layouts().stream().anyMatch(slot -> slot.contains(this.clickedSlot()));
}

@Override
public boolean isLayoutSlot(final char character) {
final LayoutSlot slot = this.layouts().get(character);
if (slot == null) {
return false;
}
return slot.contains(this.clickedSlot());
return this.layouts()
.stream()
.filter(slot -> slot.character() == character)
.findFirst()
.map(slot -> slot.contains(this.clickedSlotRaw()))
.orElse(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import net.infumia.frame.context.ContextBase;
Expand Down Expand Up @@ -41,14 +40,14 @@ public class ContextRenderImpl extends ContextBaseImpl implements ContextRenderR
private final List<Element> elements;
private final ViewContainer container;
private final ViewConfig config;
private final Map<Character, LayoutSlot> layouts;
private final Collection<LayoutSlot> layouts;
private Closeable updateTask;

public ContextRenderImpl(
@NotNull final ContextBase context,
@NotNull final ViewContainer container,
@NotNull final ViewConfig config,
@NotNull final Map<Character, LayoutSlot> layouts
@NotNull final Collection<LayoutSlot> layouts
) {
super(context);
this.container = container;
Expand Down Expand Up @@ -86,8 +85,8 @@ public ViewConfig config() {

@NotNull
@Override
public Map<Character, LayoutSlot> layouts() {
return this.layouts;
public Collection<LayoutSlot> layouts() {
return Collections.unmodifiableCollection(this.layouts);
}

@Override
Expand Down Expand Up @@ -242,7 +241,7 @@ public ElementItemBuilder layoutSlot(final char layout) {
layout
);
final ElementItemBuilder builder = this.newUnregisteredBuilder();
layoutSlot.builderFactory(__ -> builder);
this.layouts.add(layoutSlot.withBuilderFactory(value -> builder));
return builder;
}

Expand All @@ -262,7 +261,7 @@ public void layoutSlot(
"Missing layout character '%s'",
layout
);
layoutSlot.builderFactory(index -> {
layoutSlot.withBuilderFactory(index -> {
final ElementItemBuilder builder = this.newUnregisteredBuilder();
configurer.accept(index, builder);
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,16 @@ private LayoutSlot layoutSlotFor(@NotNull final ContextRender context) {
if (this.currentLayoutSlot != null) {
return this.currentLayoutSlot;
}
final LayoutSlot layoutSlot = Preconditions.argumentNotNull(
context.layouts().get(this.layout),
"Layout slot target not found: %c",
this.layout
);
final LayoutSlot layoutSlot = context
.layouts()
.stream()
.filter(slot -> slot.character() == this.layout)
.findFirst()
.orElseThrow(() ->
new IllegalArgumentException(
String.format("Layout slot target not found: %c", this.layout)
)
);
return this.currentLayoutSlot = layoutSlot;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.infumia.frame.pipeline.context;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.context.view.ContextClick;
import net.infumia.frame.context.view.ContextClose;
Expand Down Expand Up @@ -224,7 +223,7 @@ public void modifyContainer(@NotNull final ViewContainer newContainer) {

final class LayoutResolution implements PipelineContextView.LayoutResolution {

private final Map<Character, LayoutSlot> layouts = new ConcurrentHashMap<>();
private final Collection<LayoutSlot> layouts = new ArrayList<>();
private final ContextBase context;
private final ViewConfig config;
private final ViewContainer container;
Expand Down Expand Up @@ -259,13 +258,13 @@ public ViewContainer container() {

@NotNull
@Override
public Map<Character, LayoutSlot> layouts() {
return Collections.unmodifiableMap(this.layouts);
public Collection<LayoutSlot> layouts() {
return Collections.unmodifiableCollection(this.layouts);
}

@Override
public void addLayout(final char character, @NotNull final Collection<Integer> indexes) {
this.layouts.computeIfAbsent(character, __ ->
this.layouts.add(
new LayoutSlotImpl(
character,
indexes.stream().mapToInt(value -> value).toArray()
Expand All @@ -279,13 +278,13 @@ final class CreateRender implements PipelineContextView.CreateRender {
private final ContextBase context;
private final ViewConfig config;
private final ViewContainer container;
private final Map<Character, LayoutSlot> layouts;
private final Collection<LayoutSlot> layouts;

public CreateRender(
@NotNull final ContextBase context,
@NotNull final ViewConfig config,
@NotNull final ViewContainer container,
@NotNull final Map<Character, LayoutSlot> layouts
@NotNull final Collection<LayoutSlot> layouts
) {
this.context = context;
this.config = config;
Expand Down Expand Up @@ -313,7 +312,7 @@ public ViewContainer container() {

@NotNull
@Override
public Map<Character, LayoutSlot> layouts() {
public Collection<LayoutSlot> layouts() {
return this.layouts;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.infumia.frame.pipeline.executor;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import net.infumia.frame.context.ContextBase;
import net.infumia.frame.context.view.ContextClickImpl;
Expand Down Expand Up @@ -115,7 +114,7 @@ > executeModifyContainer(
@NotNull
@Override
public CompletableFuture<
Pair<ConsumerService.State, Map<Character, LayoutSlot>>
Pair<ConsumerService.State, Collection<LayoutSlot>>
> executeLayoutResolution(
@NotNull final ContextBase context,
@NotNull final ViewConfig config,
Expand All @@ -134,7 +133,7 @@ public CompletableFuture<ContextRender> executeCreateRender(
@NotNull final ContextBase context,
@NotNull final ViewConfig config,
@NotNull final ViewContainer container,
@NotNull final Map<Character, LayoutSlot> layouts
@NotNull final Collection<LayoutSlot> layouts
) {
return this.pipelines.createRender()
.completeWith(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.infumia.frame.pipeline.service.render;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import net.infumia.frame.context.view.ContextRender;
import net.infumia.frame.context.view.ContextRenderRich;
Expand Down Expand Up @@ -39,10 +39,14 @@ public void accept(@NotNull final PipelineContextRender.FirstRender ctx) {

private void fromAvailableSlots(@NotNull final PipelineContextRender.FirstRender ctx) {
final ContextRenderRich context = (ContextRenderRich) ctx.context();
final Map<Character, LayoutSlot> layouts = context.layouts();
final LayoutSlot layout = layouts.get(
ServiceFirstRenderAvailableSlotResolution.AVAILABLE_SLOT
);
final Collection<LayoutSlot> layouts = context.layouts();
final LayoutSlot layout = layouts
.stream()
.filter(
slot -> slot.character() == ServiceFirstRenderAvailableSlotResolution.AVAILABLE_SLOT
)
.findFirst()
.orElse(null);
if (layout == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String key() {
@Override
public void accept(@NotNull final PipelineContextRender.FirstRender ctx) {
final ContextRender context = ctx.context();
for (final LayoutSlot layout : context.layouts().values()) {
for (final LayoutSlot layout : context.layouts()) {
final IntFunction<ElementItemBuilder> builderFactory = layout.builderFactory();
if (builderFactory == null) {
continue;
Expand Down
18 changes: 15 additions & 3 deletions core/src/main/java/net/infumia/frame/slot/LayoutSlotImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ public final class LayoutSlotImpl implements LayoutSlot {
private final int@NotNull[] slots;
private IntFunction<ElementItemBuilder> builderFactory;

public LayoutSlotImpl(final char character, final int@NotNull[] slots) {
private LayoutSlotImpl(
final char character,
final int@NotNull[] slots,
@Nullable final IntFunction<ElementItemBuilder> builderFactory
) {
this.character = character;
this.slots = slots;
this.builderFactory = builderFactory;
}

public LayoutSlotImpl(final char character, final int@NotNull[] slots) {
this(character, slots, null);
}

@Override
Expand All @@ -33,9 +42,12 @@ public IntFunction<ElementItemBuilder> builderFactory() {
return this.builderFactory;
}

@NotNull
@Override
public void builderFactory(@Nullable final IntFunction<ElementItemBuilder> builderFactory) {
this.builderFactory = builderFactory;
public LayoutSlot withBuilderFactory(
@Nullable final IntFunction<ElementItemBuilder> builderFactory
) {
return new LayoutSlotImpl(this.character, this.slots, builderFactory);
}

@Override
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/net/infumia/frame/slot/SlotFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public SlotFinder(@NotNull final ContextRender context) {

@Nullable
public LayoutSlot findLayoutSlot(final char character) {
return this.context.layouts().get(character);
return this.context.layouts()
.stream()
.filter(slot -> slot.character() == character)
.findFirst()
.orElse(null);
}

public int findFirstSlot() {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/net/infumia/frame/view/ViewImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.infumia.frame.view;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import net.infumia.frame.config.ViewConfigRich;
Expand Down Expand Up @@ -205,7 +204,7 @@ private CompletableFuture<ContextRender> executeCreateRender(
@NotNull final ContextBase context,
@NotNull final ViewConfig config,
@NotNull final ViewContainer container,
@NotNull final Map<Character, LayoutSlot> layouts
@NotNull final Collection<LayoutSlot> layouts
) {
return this.pipelines.executeCreateRender(context, config, container, layouts).thenCompose(
render -> ((ContextRenderRich) render).simulateFirstRender().thenApply(__ -> render)
Expand Down

0 comments on commit d2765cf

Please sign in to comment.