Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit 2c6f8d6

Browse files
committed
feat: add fill operations to item containers
Closes #14
1 parent 638c00b commit 2c6f8d6

5 files changed

Lines changed: 79 additions & 42 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.undefined.cassini.container.item
2+
3+
import com.undefined.cassini.element.item.ItemElement
4+
5+
/**
6+
* Operations for filling areas of an item container with elements.
7+
*/
8+
interface FillOperations {
9+
10+
fun setElement(row: Int, column: Int, element: ItemElement)
11+
fun removeElement(row: Int, column: Int)
12+
13+
/**
14+
* Fills the border of the container with [element].
15+
*/
16+
fun fillBorder(element: ItemElement)
17+
18+
/**
19+
* Fills the entire container with [element].
20+
*/
21+
fun fill(element: ItemElement)
22+
23+
/**
24+
* Fills a rectangular area with the specified item. Rows and columns specified are inclusive.
25+
*/
26+
fun fill(fromRow: Int, fromColumn: Int, toRow: Int, toColumn: Int, element: ItemElement) {
27+
for (row in fromRow..toRow) {
28+
for (column in fromColumn..toColumn) {
29+
setElement(row, column, element)
30+
}
31+
}
32+
}
33+
34+
/**
35+
* Clears all items in the specified rectangular area. Rows and columns specified are inclusive.
36+
*/
37+
fun clear(fromRow: Int, fromColumn: Int, toRow: Int, toColumn: Int) {
38+
for (row in fromRow..toRow) {
39+
for (column in fromColumn..toColumn) {
40+
removeElement(row, column)
41+
}
42+
}
43+
}
44+
45+
/**
46+
* Fills the specified row with [element].
47+
*/
48+
fun fillRow(row: Int, element: ItemElement)
49+
50+
/**
51+
* Fills the specified column with [element].
52+
*/
53+
fun fillColumn(column: Int, element: ItemElement)
54+
55+
}

core/src/main/kotlin/com/undefined/cassini/container/item/ItemContainer.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package com.undefined.cassini.container.item
33
import com.undefined.cassini.container.Container
44
import com.undefined.cassini.element.CartesianCoordinate
55
import com.undefined.cassini.element.item.ItemElement
6+
import com.undefined.cassini.menu.item.ItemMenu
67

78
/**
8-
* Represents a container in an [com.undefined.cassini.menu.item.ItemMenu]. Any null values are simply empty items.
9+
* Represents a container in an [ItemMenu]. Any null values are simply empty items.
910
*/
10-
interface ItemContainer<T : ItemContainer<T>> : Container<T, ItemElement?> {
11+
interface ItemContainer<T : ItemContainer<T>> : Container<T, ItemElement?>, FillOperations {
1112

12-
fun removeElement(row: Int, column: Int)
13-
fun setElement(row: Int, column: Int, element: ItemElement)
13+
override fun removeElement(row: Int, column: Int)
14+
override fun setElement(row: Int, column: Int, element: ItemElement)
1415

1516
/**
1617
* Calculates a [CartesianCoordinate] from a [slot].

core/src/main/kotlin/com/undefined/cassini/container/item/ItemContainerImpl.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package com.undefined.cassini.container.item
33
import com.undefined.cassini.container.Container
44
import com.undefined.cassini.element.CartesianCoordinate
55
import com.undefined.cassini.element.item.ItemElement
6-
import com.undefined.cassini.element.item.StaticItemElement
76
import com.undefined.cassini.menu.item.ItemMenu
87
import org.jetbrains.annotations.Range
9-
import kotlin.collections.plus
108

119
/**
1210
* The primary implementation of [ItemContainer]; representing a container in an [ItemMenu]. Any null values are simply empty items.
@@ -21,7 +19,6 @@ open class ItemContainerImpl(
2119
*/
2220
lateinit var menu: ItemMenu<*>
2321

24-
// private val slotIterator: BoxSlotIterator by lazy { BoxSlotIterator(menu, startSlot = 0, rows, columns) }
2522
private val elements: Array<Array<ItemElement?>> = Array(height) { Array(width) { null } } // grid map of elements
2623
private val containers: LinkedHashMap<Int, ItemContainerImpl> = linkedMapOf() // slot to container
2724

@@ -30,9 +27,6 @@ open class ItemContainerImpl(
3027
}
3128

3229
override fun setElement(row: Int, column: Int, element: ItemElement) {
33-
// require(row <= slotIterator.width)
34-
// require(column <= slotIterator.width)
35-
3630
elements[row][column] = element
3731
element.containers.add(this)
3832
}
@@ -101,4 +95,18 @@ open class ItemContainerImpl(
10195
return currentElements
10296
}
10397

98+
// FILL OPERATIONS
99+
override fun fillBorder(element: ItemElement) {
100+
fillRow(0, element)
101+
fillRow(height - 1, element)
102+
103+
fillColumn(0, element)
104+
fillColumn(width - 1, element)
105+
}
106+
107+
override fun fill(element: ItemElement) = fill(0, 0, height - 1, width - 1, element)
108+
109+
override fun fillRow(row: Int, element: ItemElement) = fill(row, 0, row, width - 1, element)
110+
override fun fillColumn(column: Int, element: ItemElement) = fill(0, column, height - 1, column, element)
111+
104112
}

modules/chest/src/main/kotlin/com/undefined/cassini/menu/item/ChestMenu.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ abstract class ChestMenu(
1010
rows: Int,
1111
parent: Menu<*, *>? = null,
1212
override val settings: ItemMenuSettings = ItemMenuSettings(),
13-
rootContainer: ItemContainerImpl = ItemContainerImpl(MAX_WIDTH, MAX_HEIGHT)
13+
rootContainer: ItemContainerImpl = ItemContainerImpl(MAX_WIDTH, rows)
1414
) : SingleContainerItemMenu<ChestMenu>(
1515
title = title,
1616
size = rows * MAX_WIDTH,

server/src/main/kotlin/TestMenu.kt

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,11 @@ class TestMenu(parent: Menu<*, *>? = null) : ChestMenu(!"Change Lore", 3, parent
1212
override fun initialize(player: Player) {
1313
preventClicking()
1414
createPattern {
15-
setElement(6, StaticItemElement(Material.DIAMOND))
15+
fillBorder(StaticItemElement(Material.GLASS))
16+
// fill(StaticItemElement(Material.STONE))
17+
// fillRow(1, StaticItemElement(Material.GOLD_BLOCK))
18+
// fillColumn(4, StaticItemElement(Material.EMERALD_BLOCK))
1619
}
17-
18-
val container = ItemContainerImpl(3, 3)
19-
for (slot in 0..<(container.width * container.height)) {
20-
container.setElement(slot, StaticItemElement(Material.REDSTONE))
21-
}
22-
addContainer(container)
23-
24-
// val iterator = BoxSlotIterator(this, 0, 2, 2)
25-
// for ((i, _) in iterator) {
26-
// setElement(i, StaticItemElement(Material.REDSTONE))
27-
// }
28-
// update()
29-
// val yayElement = StaticItemElement(Material.PAPER) {
30-
// val element = element as? StaticItemElement ?: return@StaticItemElement
31-
// element.item.editMeta { meta ->
32-
// meta.displayName(!"<green>YAY!")
33-
// }
34-
// element.update()
35-
// }
36-
// setElement(13, yayElement)
37-
//
38-
// val otherElement = StaticItemElement(Material.RED_CONCRETE) {
39-
// back()
40-
// }
41-
// setElement(14, otherElement)
42-
//
43-
// val randomElement = StaticItemElement(Material.GREEN_CONCRETE) {
44-
// player.openMenu(OtherTestMenu(this@TestMenu))
45-
// }
46-
// setElement(12, randomElement)
4720
}
4821

4922
}

0 commit comments

Comments
 (0)