Skip to content

Commit

Permalink
feature: work on menu
Browse files Browse the repository at this point in the history
  • Loading branch information
broccolai committed Oct 27, 2023
1 parent a0af7c2 commit f1b1ce4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.interfaces.paper.PaperInterfaceListeners;

public final class BukkitTagsPlatform extends JavaPlugin implements TagsPlatform {

Expand Down Expand Up @@ -77,6 +78,8 @@ public void onEnable() {
}

this.plugin.commands(commandManager, BUKKIT_COMMANDS);

PaperInterfaceListeners.install(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,33 @@
public final class TagsMenuFactory {

private static final ItemStackElement<ChestPane> BOTTOM_BAR_ELEMENT = new ItemStackElement<>(
PaperItemBuilder.ofType(Material.WHITE_STAINED_GLASS_PANE)
.name(Component.empty())
.build(),
ClickHandler.cancel()
PaperItemBuilder.ofType(Material.WHITE_STAINED_GLASS_PANE)
.name(Component.empty())
.build(),
ClickHandler.cancel()
);

private static final ItemStack BACK_ITEM = PaperItemBuilder
.ofType(Material.RED_STAINED_GLASS_PANE)
.name(Component.text("Previous Page"))
.build();

private static final ItemStack FORWARD_ITEM = PaperItemBuilder
.ofType(Material.GREEN_STAINED_GLASS_PANE)
.name(Component.text("Next Page"))
.build();

private final Logger logger;
private final TagsService tagsService;
private final ActionService actionService;
private final LocaleConfiguration.MenuLocaleConfiguration locale;

@Inject
public TagsMenuFactory(
final @NonNull Logger logger,
final @NonNull TagsService tagsService,
final @NonNull ActionService actionService,
final @NonNull LocaleConfiguration locale
final @NonNull Logger logger,
final @NonNull TagsService tagsService,
final @NonNull ActionService actionService,
final @NonNull LocaleConfiguration locale
) {
this.logger = logger;
this.tagsService = tagsService;
Expand All @@ -55,11 +65,11 @@ public TagsMenuFactory(

public ChestInterface create(final @NonNull TagsUser user) {
return ChestInterface.builder()
.title(this.locale.title.asComponent())
.rows(4)
.addTransform(this::createFillTransform)
.addReactiveTransform(this.createTagTransform(user))
.build();
.title(this.locale.title.asComponent())
.rows(4)
.addTransform(this::createFillTransform)
.addReactiveTransform(this.createTagTransform(user))
.build();
}

private ChestPane createFillTransform(ChestPane pane, InterfaceView<ChestPane, PlayerViewer> view) {
Expand All @@ -73,28 +83,43 @@ private ChestPane createFillTransform(ChestPane pane, InterfaceView<ChestPane, P
}

private PaginatedTransform<ItemStackElement<ChestPane>, ChestPane, PlayerViewer> createTagTransform(final @NonNull TagsUser user) {
return new PaginatedTransform<>(
Vector2.at(0, 0),
Vector2.at(8, 2),
() -> this.createTagElements(user)
PaginatedTransform<ItemStackElement<ChestPane>, ChestPane, PlayerViewer> transform = new PaginatedTransform<>(
Vector2.at(0, 0),
Vector2.at(8, 2),
() -> this.createTagElements(user)
);

transform.backwardElement(Vector2.at(0, 3), unused -> {
return ItemStackElement.of(BACK_ITEM, ctx -> {
transform.previousPage();
});
});

transform.forwardElement(Vector2.at(8, 3), unused -> {
return ItemStackElement.of(FORWARD_ITEM, ctx -> {
transform.nextPage();
});
});

return transform;
}

private List<ItemStackElement<ChestPane>> createTagElements(final @NonNull TagsUser user) {
return this.tagsService.allTags(user)
.stream()
.map(tag -> this.createTagElement(user, tag))
.toList();
.stream()
.map(tag -> this.createTagElement(user, tag))
.toList();
}

private ItemStackElement<ChestPane> createTagElement(final @NonNull TagsUser user, final @NonNull ConstructedTag tag) {
Material material = this.matchMaterialOrDefault(tag.displayInformation().material());
ItemStack item = PaperItemBuilder.ofType(material)
.name(tag.component())
.lore(this.createTagLore(tag))
.build();
.name(tag.component())
.lore(this.createTagLore(tag))
.build();

return ItemStackElement.of(item, ctx -> {
ctx.cancel(true);
this.actionService.select(user, tag);
});
}
Expand All @@ -112,9 +137,9 @@ private List<Component> createTagLore(final @NonNull ConstructedTag tag) {

private List<Component> formatTagReason(final @NonNull String reason) {
return FormatingUtilites.splitString(reason, 30)
.stream()
.map(text -> Component.text(text, NamedTextColor.WHITE))
.collect(Collectors.toList());
.stream()
.map(text -> Component.text(text, NamedTextColor.WHITE))
.collect(Collectors.toList());
}

private Material matchMaterialOrDefault(final @NonNull String input) {
Expand Down

0 comments on commit f1b1ce4

Please sign in to comment.