diff --git a/core/src/main/java/me/huanmeng/opensource/bukkit/gui/draw/GuiDraw.java b/core/src/main/java/me/huanmeng/opensource/bukkit/gui/draw/GuiDraw.java index 6cca2bd..4e2ece7 100644 --- a/core/src/main/java/me/huanmeng/opensource/bukkit/gui/draw/GuiDraw.java +++ b/core/src/main/java/me/huanmeng/opensource/bukkit/gui/draw/GuiDraw.java @@ -19,7 +19,7 @@ * @author huanmeng_qwq */ @SuppressWarnings("unused") -public class GuiDraw<@NonNull G extends AbstractGui> { +public class GuiDraw> { @NonNull private final G gui; @@ -43,7 +43,7 @@ public G gui() { * @return this */ @NonNull - public GuiDraw<@NonNull G> vertical(int x1, int y1, int x2, int y2, @Nullable Button button) { + public GuiDraw vertical(int x1, int y1, int x2, int y2, @Nullable Button button) { int[] xRange = MathUtil.range(x1, x2); int[] yRange = MathUtil.range(y1, y2); for (int x : xRange) { @@ -51,7 +51,7 @@ public G gui() { set(x, y, button); } } - return this; + return self(); } /** @@ -63,9 +63,9 @@ public G gui() { * @return this */ @NonNull - public GuiDraw<@NonNull G> set(int x, int y, @Nullable Button button) { + public GuiDraw set(int x, int y, @Nullable Button button) { set(Slot.ofBukkit(x, y), button); - return this; + return self(); } /** @@ -76,9 +76,9 @@ public G gui() { * @return this */ @NonNull - public GuiDraw<@NonNull G> set(int slot, @Nullable Button button) { + public GuiDraw set(int slot, @Nullable Button button) { set(Slot.of(slot), button); - return this; + return self(); } /** @@ -89,11 +89,11 @@ public G gui() { * @return this */ @NonNull - public GuiDraw<@NonNull G> set(@NonNull Slots slots, @Nullable Button button) { + public GuiDraw set(@NonNull Slots slots, @Nullable Button button) { for (Slot slot : slots.slots(gui)) { set(slot, button); } - return this; + return self(); } /** @@ -104,7 +104,7 @@ public G gui() { * @return this */ @NonNull - public GuiDraw<@NonNull G> set(@NonNull Slots slots, @NonNull List<@Nullable ? extends Button> buttons) { + public GuiDraw set(@NonNull Slots slots, @NonNull List<@Nullable ? extends Button> buttons) { Slot[] slotArray = slots.slots(gui); for (int i = 0; i < slotArray.length; i++) { if (i >= buttons.size()) { @@ -112,7 +112,7 @@ public G gui() { } set(slotArray[i], buttons.get(i)); } - return this; + return self(); } /** @@ -123,8 +123,12 @@ public G gui() { * @return this */ @NonNull - public GuiDraw<@NonNull G> set(@NonNull Slot slot, @Nullable Button button) { + public GuiDraw set(@NonNull Slot slot, @Nullable Button button) { gui.addAttachedButton(new GuiButton(slot, button)); - return this; + return self(); + } + + public GuiDraw self() { + return self(); } } diff --git a/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/GuiDsl.kt b/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/GuiDsl.kt index d3a8594..bad8a6a 100644 --- a/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/GuiDsl.kt +++ b/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/GuiDsl.kt @@ -123,6 +123,42 @@ fun GuiDraw>.setButton(slots: Slots, lambda: ButtonDsl. */ fun GuiDraw>.setButton(slots: Slots, lambda: ButtonList.() -> Unit) = set(slots, ButtonList().apply(lambda)) + +class SetButton>(private val draw: GuiDraw, private val slot: Slot) { + infix fun button(button: Button) { + draw.set(slot, button) + } +} + +class SetButtons>(private val draw: GuiDraw, private val slots: Slots) { + infix fun button(button: Button) { + draw.set(slots, button) + } + + infix fun buttons(lambda: ButtonList.() -> Unit) { + draw.set(slots, ButtonList().apply(lambda)) + } +} + +infix fun > GuiDraw.slot(slot: Int): SetButton { + return SetButton(this, Slot.of(slot)) +} + +infix fun > GuiDraw.slot(slot: Slot): SetButton { + return SetButton(this, slot) +} + +infix fun > GuiDraw.slot(slot: Pair): SetButton { + return SetButton(this, Slot.ofBukkit(slot.first, slot.second)) +} + +infix fun > GuiDraw.gameSlot(slot: Pair): SetButton { + return SetButton(this, Slot.ofGame(slot.first, slot.second)) +} + +infix fun > GuiDraw.slots(slots: Slots): SetButtons { + return SetButtons(this, slots) +} /* end */ /* Pattern to Gui*/ diff --git a/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/SlotDsl.kt b/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/SlotDsl.kt index ff401c2..ad09d4a 100644 --- a/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/SlotDsl.kt +++ b/kotlin-dsl/src/main/kotlin/me/huanmeng/opensource/bukkit/gui/dsl/SlotDsl.kt @@ -58,8 +58,7 @@ fun buildSlotsByLine(lambda: SlotsDsl.(line: Int) -> List): Slots { class SlotDsl { var index: Int? = null var onClick: (( - button: Button, player: Player, click: ClickType, action: InventoryAction, slotType: SlotType, - slot: Int, hotbarKey: Int, e: InventoryClickEvent + button: Button, player: Player, click: ClickType, action: InventoryAction, slotType: SlotType, slot: Int, hotbarKey: Int, e: InventoryClickEvent ) -> Result)? = null var tryPlace: ((button: Button, player: Player) -> Boolean)? = null } @@ -108,4 +107,124 @@ class SlotsDsl(list: MutableList) : MutableList by list { fun slot(lambda: () -> Slot) = add(lambda()) fun slot(lambda: SlotDsl.() -> Unit) = add(buildSlot(lambda)) -} \ No newline at end of file +} + +enum class IntSlotType { + X { + override fun IntSlot.createSlot(other: IntSlot): Slot { + when (other.type) { + X -> { + throw IllegalArgumentException("Cannot add X to X") + } + + Y -> { + return Slot.ofBukkit(this.value, other.value) + } + + GAME_X -> { + throw IllegalArgumentException("Cannot add X to GameX") + } + + GAME_Y -> { + return Slot.ofGame(this.value + 1, other.value) + } + } + } + }, + Y { + override fun IntSlot.createSlot(other: IntSlot): Slot { + when (other.type) { + X -> { + return Slot.ofBukkit(other.value, this.value) + } + + Y -> { + throw IllegalArgumentException("Cannot add Y to X") + } + + GAME_X -> { + return Slot.ofGame(other.value, this.value + 1) + } + + GAME_Y -> { + throw IllegalArgumentException("Cannot add GameY to X") + } + } + } + }, + GAME_X { + override fun IntSlot.createSlot(other: IntSlot): Slot { + when (other.type) { + X -> { + throw IllegalArgumentException("Cannot add X to X") + } + + Y -> { + return Slot.ofGame(this.value, other.value + 1) + } + + GAME_X -> { + throw IllegalArgumentException("Cannot add X to GameX") + } + + GAME_Y -> { + return Slot.ofGame(this.value, other.value) + } + } + } + }, + GAME_Y { + override fun IntSlot.createSlot(other: IntSlot): Slot { + when (other.type) { + X -> { + return Slot.ofGame(other.value + 1, this.value) + } + + Y -> { + throw IllegalArgumentException("Cannot add GameY to X") + } + + GAME_X -> { + return Slot.ofGame(other.value, this.value) + } + + GAME_Y -> { + throw IllegalArgumentException("Cannot add GameY to GameY") + } + } + } + }, + ; + + abstract fun IntSlot.createSlot(other: IntSlot): Slot +} + +class IntSlot(val value: Int, val type: IntSlotType) { + operator fun plus(other: IntSlot): Slot { + return type.run { createSlot(other) } + } + operator fun plus(other: Int): IntSlot { + return IntSlot(value+ other, type) + } + operator fun minus(other: Int): IntSlot { + return IntSlot(value - other, type) + } + operator fun times(other: Int): IntSlot { + return IntSlot(value * other, type) + } + operator fun div(other: Int): IntSlot { + return IntSlot(value / other, type) + } + operator fun rem(other: Int): IntSlot { + return IntSlot(value % other, type) + } + + override fun toString(): String { + return "IntSlot(value=$value, type=$type)" + } +} + +val Int.x: IntSlot get() = IntSlot(this, IntSlotType.X) +val Int.y: IntSlot get() = IntSlot(this, IntSlotType.Y) +val Int.gx: IntSlot get() = IntSlot(this, IntSlotType.GAME_X) +val Int.gy: IntSlot get() = IntSlot(this, IntSlotType.GAME_Y) \ No newline at end of file