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
4 changes: 4 additions & 0 deletions framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ dependencies {
compileOnlyApi libs.spigot.api
}

dependencies {
runtimeOnly(project(':framework:bukkit')) { because('Backward compatibility') }
}

javadoc {
exclude 'me/matsubara/**'
exclude 'me/saiintbrisson/minecraft/utils/**'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public final ViewItem slot(int row, int column, Object item) {
throw new UnsupportedOperationException("not available");
}

@Override
public void with(@NotNull ViewItem item) {
throw new UnsupportedOperationException("not available");
}

@Override
public final void update() {
throw new UnsupportedOperationException("not available");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public ViewItem() {
this(-1);
}

/**
* @deprecated Use {@link VirtualView#slot(int)} instead.
*/
@Deprecated
public ViewItem(final int slot) {
ViewItem(final int slot) {
this.slot = slot;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public interface VirtualView {
@NotNull
ViewItem slot(int row, int column, Object item);

void with(@NotNull ViewItem item);

void update();

void open(@NotNull final Object viewer, @NotNull final Map<String, Object> data);
Expand Down
15 changes: 15 additions & 0 deletions kotlin-dsl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
kotlin("jvm") version "1.6.21"
}

repositories {
mavenCentral()
}

dependencies {
implementation(project(":framework"))
}

kotlin {
explicitApi()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.saiintbrisson.minecraft

@DslMarker
@Target(AnnotationTarget.FUNCTION)
public annotation class ViewDsl

@DslMarker
@Target(AnnotationTarget.FUNCTION)
public annotation class ContextDsl

@DslMarker
@Target(AnnotationTarget.FUNCTION)
public annotation class SlotDsl
53 changes: 53 additions & 0 deletions kotlin-dsl/src/main/kotlin/me/saiintbrisson/minecraft/builders.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@file:Suppress("FunctionName")

package me.saiintbrisson.minecraft

public class ViewBuilder {

public var cancelOnClick: Boolean = false
public var cancelOnPickup: Boolean = false
public var cancelOnDrop: Boolean = false
public var cancelOnDrag: Boolean = false
public var cancelOnClone: Boolean = false
public var cancelOnMoveIn: Boolean = false
public var cancelOnMoveOut: Boolean = false
public var cancelOnShiftClick: Boolean = false
public var clearCursorOnClose: Boolean = false
public var closeOnOutsideClick: Boolean = false

internal var open: (OpenViewContext.() -> Unit)? = null
internal var render: ContextBlock? = null
internal var update: ContextBlock? = null
internal var click: SlotContextBlock? = null
internal var close: (CloseViewContext.() -> Unit)? = null
internal var hotbarInteract: HotbarInteractBlock? = null
internal var itemHold: SlotContextBlock? = null
internal var itemRelease: ItemReleaseBlock? = null
internal var moveOut: SlotMoveContextBlock? = null
internal var moveIn: SlotMoveContextBlock? = null

@PublishedApi
internal var slots: MutableList<ViewSlotBuilder> = mutableListOf()

}

public class ViewSlotBuilder(@PublishedApi internal val slot: Int) {

internal var render: SlotContextBlock? = null
internal var update: SlotContextBlock? = null
internal var click: SlotContextBlock? = null

public fun toItem(): ViewItem {
return ViewItem(slot).apply {
setHandler(click) { clickHandler = it }
setHandler(render) { renderHandler = it }
setHandler(update) { updateHandler = it }
}
}

private fun ViewItem.setHandler(
currentHandler: SlotContextBlock?,
assign: ViewItem.(ViewItemHandler) -> Unit
) = currentHandler?.let { it -> assign(it) }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@file:Suppress("FunctionName")

package me.saiintbrisson.minecraft

internal typealias ContextBlock = ViewContext.() -> Unit
internal typealias SlotContextBlock = ViewSlotContext.() -> Unit
internal typealias SlotMoveContextBlock = ViewSlotMoveContext.() -> Unit
internal typealias ItemReleaseBlock = ViewSlotContext.(to: ViewSlotContext) -> Unit
internal typealias HotbarInteractBlock = ViewSlotContext.(hotbarButton: Int) -> Unit

@ViewDsl
public inline fun View(
rows: Int = 0,
title: String? = null,
content: ViewBuilder.() -> Unit
): VirtualView {
val view = View(rows, title)
val builder = ViewBuilder().apply(content)
builder.slots.forEach { view.with(it.toItem()) }
return view
}

@SlotDsl
public inline fun ViewBuilder.slot(
slot: Int,
block: ViewSlotBuilder.() -> Unit
) {
slots.add(ViewSlotBuilder(slot).apply(block))
}

@ViewDsl
public fun ViewBuilder.onOpen(block: OpenViewContext.() -> Unit) {
open = block
}

@ViewDsl
public fun ViewBuilder.onClose(block: CloseViewContext.() -> Unit) {
close = block
}

@ViewDsl
public fun ViewBuilder.onRender(block: ContextBlock) {
render = block
}

@ViewDsl
public fun ViewBuilder.onUpdate(block: ContextBlock) {
update = block
}

@ViewDsl
public fun ViewBuilder.onClick(block: SlotContextBlock) {
click = block
}

@ViewDsl
public fun ViewBuilder.onHotbarInteract(block: HotbarInteractBlock) {
hotbarInteract = block
}

@ViewDsl
public fun ViewBuilder.onItemHold(block: SlotContextBlock) {
itemHold = block
}

@ViewDsl
public fun ViewBuilder.onItemRelease(block: ItemReleaseBlock) {
itemRelease = block
}

@ViewDsl
public fun ViewBuilder.onMoveIn(block: SlotMoveContextBlock) {
moveIn = block
}

@ViewDsl
public fun ViewBuilder.onMoveOut(block: SlotMoveContextBlock) {
moveOut = block
}

@ContextDsl
public fun ViewSlotBuilder.onRender(block: SlotContextBlock) {
render = block
}

@ContextDsl
public fun ViewSlotBuilder.onUpdate(block: SlotContextBlock) {
update = block
}

@ContextDsl
public fun ViewSlotBuilder.onClick(block: SlotContextBlock) {
click = block
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rootProject.name = 'inventory-framework'
enableFeaturePreview('VERSION_CATALOGS')
include 'framework', 'framework:bukkit', 'framework:compat', 'plugin', 'examples', 'bom'
include 'framework', 'framework:bukkit', 'framework:compat', 'plugin', 'examples', 'bom', 'kotlin-dsl'