Skip to content

Commit e8cd2b4

Browse files
authored
Change item click handler type (#161)
1 parent 8d05ca5 commit e8cd2b4

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

bukkit-api/src/main/java/me/saiintbrisson/minecraft/ItemClickInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void intercept(
2525
subject.setCancelled(item.isCancelOnClick());
2626

2727
if (item.getClickHandler() != null)
28-
subject.getRoot().runCatching(subject, () -> item.getClickHandler().handle(subject));
28+
subject.getRoot().runCatching(subject, () -> item.getClickHandler().accept(subject));
2929

3030
event.setCancelled(subject.isCancelled());
3131
}

kotlin-dsl/src/main/kotlin/me/saiintbrisson/minecraft/Builders.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package me.saiintbrisson.minecraft
55

6+
import java.util.function.Consumer
7+
68
@ViewDsl
79
public class ViewBuilder {
810

@@ -19,7 +21,7 @@ public class ViewBuilder {
1921
internal var open: (OpenViewContext.() -> Unit)? = null
2022
internal var render: ContextBlock? = null
2123
internal var update: ContextBlock? = null
22-
internal var click: SlotContextBlock? = null
24+
internal var click: SlotClickContextBlock? = null
2325
internal var close: (CloseViewContext.() -> Unit)? = null
2426
internal var hotbarInteract: HotbarInteractBlock? = null
2527
internal var itemHold: SlotContextBlock? = null
@@ -36,15 +38,18 @@ public class ViewSlotBuilder(@PublishedApi internal val slot: Int) {
3638

3739
internal var render: SlotContextBlock? = null
3840
internal var update: SlotContextBlock? = null
39-
internal var click: SlotContextBlock? = null
41+
internal var click: SlotClickContextBlock? = null
4042
internal var itemHold: SlotContextBlock? = null
4143
internal var itemRelease: ItemReleaseBlock? = null
4244
internal var moveOut: SlotMoveContextBlock? = null
4345
internal var moveIn: SlotMoveContextBlock? = null
4446

4547
public fun toItem(): ViewItem {
4648
return ViewItem(slot).apply {
47-
setHandler(click) { clickHandler = it }
49+
click?.let {
50+
clickHandler = Consumer { context -> it.invoke(context) }
51+
}
52+
4853
setHandler(render) { renderHandler = it }
4954
setHandler(update) { updateHandler = it }
5055
}

kotlin-dsl/src/main/kotlin/me/saiintbrisson/minecraft/Types.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package me.saiintbrisson.minecraft
44

55
internal typealias ContextBlock = @ViewDsl ViewContext.() -> Unit
66
internal typealias SlotContextBlock = @ViewDsl ViewSlotContext.() -> Unit
7+
internal typealias SlotClickContextBlock = @ViewDsl ViewSlotClickContext.() -> Unit
78
internal typealias SlotMoveContextBlock = @ViewDsl ViewSlotMoveContext.() -> Unit
89
internal typealias ItemReleaseBlock = @ViewDsl ViewSlotContext.(to: ViewSlotContext) -> Unit
910
internal typealias HotbarInteractBlock = @ViewDsl ViewSlotContext.(hotbarButton: Int) -> Unit

shared/src/main/java/me/saiintbrisson/minecraft/ViewItem.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ enum State {
4141

4242
@Setter(AccessLevel.PUBLIC)
4343
private boolean closeOnClick, cancelOnClick, cancelOnShiftClick;
44-
private ViewItemHandler renderHandler, updateHandler, clickHandler;
44+
private ViewItemHandler renderHandler, updateHandler;
45+
private Consumer<ViewSlotClickContext> clickHandler;
4546
private Consumer<ViewSlotMoveContext> moveInHandler, moveOutHandler;
4647
private Consumer<ViewSlotContext> itemHoldHandler;
4748
private BiConsumer<ViewSlotContext, ViewSlotContext> itemReleaseHandler;
@@ -341,7 +342,7 @@ public ViewItem onUpdate(@Nullable ViewItemHandler handler) {
341342
* @return This item.
342343
*/
343344
@Contract(value = "_ -> this", mutates = "this")
344-
public ViewItem onClick(@Nullable ViewItemHandler handler) {
345+
public ViewItem onClick(@Nullable Consumer<ViewSlotClickContext> handler) {
345346
setClickHandler(handler);
346347
return this;
347348
}

0 commit comments

Comments
 (0)