Skip to content

Commit

Permalink
feat: 对GuiDraw、Slot添加更多Kotlin函数支持
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmeng-qwq committed Jan 7, 2025
1 parent 5cb4fb2 commit 8d0e2a0
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author huanmeng_qwq
*/
@SuppressWarnings("unused")
public class GuiDraw<@NonNull G extends AbstractGui<G>> {
public class GuiDraw<G extends AbstractGui<G>> {
@NonNull
private final G gui;

Expand All @@ -43,15 +43,15 @@ public G gui() {
* @return this
*/
@NonNull
public GuiDraw<@NonNull G> vertical(int x1, int y1, int x2, int y2, @Nullable Button button) {
public GuiDraw<G> 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) {
for (int y : yRange) {
set(x, y, button);
}
}
return this;
return self();
}

/**
Expand All @@ -63,9 +63,9 @@ public G gui() {
* @return this
*/
@NonNull
public GuiDraw<@NonNull G> set(int x, int y, @Nullable Button button) {
public GuiDraw<G> set(int x, int y, @Nullable Button button) {
set(Slot.ofBukkit(x, y), button);
return this;
return self();
}

/**
Expand All @@ -76,9 +76,9 @@ public G gui() {
* @return this
*/
@NonNull
public GuiDraw<@NonNull G> set(int slot, @Nullable Button button) {
public GuiDraw<G> set(int slot, @Nullable Button button) {
set(Slot.of(slot), button);
return this;
return self();
}

/**
Expand All @@ -89,11 +89,11 @@ public G gui() {
* @return this
*/
@NonNull
public GuiDraw<@NonNull G> set(@NonNull Slots slots, @Nullable Button button) {
public GuiDraw<G> set(@NonNull Slots slots, @Nullable Button button) {
for (Slot slot : slots.slots(gui)) {
set(slot, button);
}
return this;
return self();
}

/**
Expand All @@ -104,15 +104,15 @@ public G gui() {
* @return this
*/
@NonNull
public GuiDraw<@NonNull G> set(@NonNull Slots slots, @NonNull List<@Nullable ? extends Button> buttons) {
public GuiDraw<G> 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()) {
break;
}
set(slotArray[i], buttons.get(i));
}
return this;
return self();
}

/**
Expand All @@ -123,8 +123,12 @@ public G gui() {
* @return this
*/
@NonNull
public GuiDraw<@NonNull G> set(@NonNull Slot slot, @Nullable Button button) {
public GuiDraw<G> set(@NonNull Slot slot, @Nullable Button button) {
gui.addAttachedButton(new GuiButton(slot, button));
return this;
return self();
}

public GuiDraw<G> self() {
return self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,42 @@ fun GuiDraw<out AbstractGuiCustom<*>>.setButton(slots: Slots, lambda: ButtonDsl.
*/
fun GuiDraw<out AbstractGuiCustom<*>>.setButton(slots: Slots, lambda: ButtonList.() -> Unit) =
set(slots, ButtonList().apply(lambda))

class SetButton<G : AbstractGui<G>>(private val draw: GuiDraw<G>, private val slot: Slot) {
infix fun button(button: Button) {
draw.set(slot, button)
}
}

class SetButtons<G : AbstractGui<G>>(private val draw: GuiDraw<G>, 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 <G : AbstractGui<G>> GuiDraw<G>.slot(slot: Int): SetButton<G> {
return SetButton(this, Slot.of(slot))
}

infix fun <G : AbstractGui<G>> GuiDraw<G>.slot(slot: Slot): SetButton<G> {
return SetButton(this, slot)
}

infix fun <G : AbstractGui<G>> GuiDraw<G>.slot(slot: Pair<Int, Int>): SetButton<G> {
return SetButton(this, Slot.ofBukkit(slot.first, slot.second))
}

infix fun <G : AbstractGui<G>> GuiDraw<G>.gameSlot(slot: Pair<Int, Int>): SetButton<G> {
return SetButton(this, Slot.ofGame(slot.first, slot.second))
}

infix fun <G : AbstractGui<G>> GuiDraw<G>.slots(slots: Slots): SetButtons<G> {
return SetButtons(this, slots)
}
/* end */

/* Pattern to Gui*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ fun buildSlotsByLine(lambda: SlotsDsl.(line: Int) -> List<Slot>): 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
}
Expand Down Expand Up @@ -108,4 +107,124 @@ class SlotsDsl(list: MutableList<Slot>) : MutableList<Slot> by list {
fun slot(lambda: () -> Slot) = add(lambda())

fun slot(lambda: SlotDsl.() -> Unit) = add(buildSlot(lambda))
}
}

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)

0 comments on commit 8d0e2a0

Please sign in to comment.