Skip to content

typst-io/bukkit-view

Repository files navigation

bukkit-view

A pure library to express minecraft chest view.

There is no side effect except BukkitView.class, all functions just pure, therefore this can be run in multithreads -- even Bukkit part can't -- and easy to write unit tests.

Also, this library is a good showcase how to do declarative programming in Java.

Example is here!

Import

Gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.typecraft:bukkit-view-core:5.1.2")
}

Maven

<dependencies>
    <dependency>
        <groupId>io.typecraft</groupId>
        <artifactId>bukkit-view-core</artifactId>
        <version>5.1.2</version>
    </dependency>
</dependencies>

Quickstart

https://github.com/typecraft-io/bukkit-view-template

git clone https://github.com/typecraft-io/bukkit-view-template

Initialize

public class MyPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        BukkitView.register(this);
    }
}

ChestView

ChestView subView = ...;

Map<Integer, ViewControl> map = new HashMap<>();
String title = "title";
int row = 1;
map.put(3, ViewControl.of(
    new ItemStack(Material.DIAMOND),
    e -> {
        // this view item don't -- also shouldn't -- know how to open view,
        // just tell what view want to open.
        return new ViewAction.Open(subView);
    }
));
ViewContents contents = ViewContents.ofControls(map);
ChestView view = ChestView.just(title, row, contents);
BukkitView.openView(view, player, plugin);

To open asynchronously, ViewAction.OpenAsync(Future<View>):

ViewControl.of(
    bukkitItemStack,
    e -> {
        Future<ChestView> myChestViewFuture;
        return new ViewAction.OpenAsync(myChestViewFuture);
    }
)

To update just contents, ViewAction.Update also ViewAction.UpdateAsync(Future<ViewContents>)

ViewControl.of(
    bukkitItemStack,
    e -> new ViewAction.Update(newContents)
    // UpdateAsync if needed
)

On close the view:

ChestView.of(title, row, map, closeEvent -> {
    return ViewAction.NOTHING; // or ViewAction.REOPEN
})

PageView

Default construction ofDefault() for PageViewLayout:

// Lazy `Function<PageContext, ViewControl>` not just `ViewControl`
List<Function<PageContext, ViewControl>> items = ...;
PageViewLayout layout = PageViewLayout.ofDefault(
    "title", 
    6, 
    Material.STONE_BUTTON, 
    items
);

Full construction for PageViewLayout:

// Paging elements
List<Function<PageContext, ViewControl>> items = ...;
// Paging elements will be put in this slots.
List<Integer> slots = ...;
// Control means fixed view-item, won't affected by view paging.
Map<Integer, Function<PageContext, ViewControl>> controls = ...;
String title = "title";
int row = 6;
PageViewLayout layout = PageViewLayout.of(title, row, items, slots, controls);

Evaluate a single page from the layout and open:

int page = 1;
ChestView view = layout.toView(page);
BukkitView.openView(view, player, plugin);

ViewControl

Constructions:

ViewControl.of(ItemStack, Function<ClickEvent, ViewAction>)

(ItemStack, ClickEvent -> ViewAction) -> ViewControl

ViewControl.just(ItemStack)

ItemStack -> ViewControl

ViewControl.consumer(ItemStack, Consumer<ViewAction>)

(ItemStack, ClickEvent -> Unit) -> ViewControl

About

Pure, functional view library for Minecraft.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages