Skip to content

Commit

Permalink
Add GUI block entity binding
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Aug 17, 2024
1 parent 439e68f commit 21d4d9c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class HarvestBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite

override val allowedFacing = listOf(Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST)
private val inventory = DefaultedList.ofSize(4, ItemStack.EMPTY)
private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X3).setTitle(Text.translatable("block.HARVEST")).apply {
private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X3).setTitle(Text.translatable("block.HARVEST")).blockEntity(this).apply {
var i = 0
addSlot(4, 0, object : Slot(this@HarvestBlockEntity, i++, 0, 0) { // Tool
override fun canInsert(item: ItemStack) = isHoe(item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import one.oktw.galaxy.item.Gui
class TestGuiBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: ItemStack) : ModelCustomBlockEntity(type, pos, modelItem),
CustomBlockClickListener, Inventory {
private val inventory = DefaultedList.ofSize(3 * 9, ItemStack.EMPTY)
private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X6).setTitle(Text.of("Test GUI")).apply {
private val gui = GUI.Builder(ScreenHandlerType.GENERIC_9X6).setTitle(Text.of("Test GUI")).blockEntity(this).apply {
var i = 0
for (x in 0 until 9) addSlot(x, 0, Slot(this@TestGuiBlockEntity, i++, 0, 0))
for (y in 4 until 6) for (x in 0 until 9) addSlot(x, y, Slot(this@TestGuiBlockEntity, i++, 0, 0))
Expand All @@ -57,7 +57,7 @@ class TestGuiBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: Ite
GUISBackStackManager.openGUI(player, gui2)
}
}
private val gui2 = GUI.Builder(ScreenHandlerType.GENERIC_9X4).setTitle(Text.of("Test GUI2")).apply {
private val gui2 = GUI.Builder(ScreenHandlerType.GENERIC_9X4).setTitle(Text.of("Test GUI2")).blockEntity(this).apply {
var i = 0
for (y in 0 until 3) for (x in 0 until 9) addSlot(x, y, Slot(this@TestGuiBlockEntity, i++, 0, 0))
}.build().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,10 @@ class TrashcanBlockEntity(type: BlockEntityType<*>, pos: BlockPos, modelItem: It

val gui = GUI
.Builder(ScreenHandlerType.GENERIC_9X4)
.setTitle(Text.of("Trashcan"))
.blockEntity(this)
.apply {
setTitle(Text.of("Trashcan"))

var i = 0
// val inv = object : SimpleInventory(9 * 4) {
// override fun canPlayerUse(player: PlayerEntity): Boolean {
// val trashcanBlock = this@TrashcanBlockEntity
// if (trashcanBlock.world!!.getBlockEntity(trashcanBlock.pos) != trashcanBlock) {
// return false
// }
//
// return player.squaredDistanceTo(trashcanBlock.pos.x + 0.5, trashcanBlock.pos.y + 0.5, trashcanBlock.pos.z + 0.5) <= 64
// }
// }
val inv = SimpleInventory(9 * 4)

for (y in 0 until 4) for (x in 0 until 9) addSlot(x, y, Slot(inv, i++, 0, 0))
Expand Down
22 changes: 18 additions & 4 deletions src/main/kotlin/one/oktw/galaxy/gui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ package one.oktw.galaxy.gui
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import net.minecraft.block.entity.BlockEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.inventory.Inventory
import net.minecraft.inventory.SimpleInventory
import net.minecraft.item.ItemStack
import net.minecraft.screen.NamedScreenHandlerFactory
Expand All @@ -41,7 +43,12 @@ import org.apache.logging.log4j.LogManager
import java.util.concurrent.ConcurrentHashMap

@Suppress("unused", "MemberVisibilityCanBePrivate")
class GUI private constructor(private val type: ScreenHandlerType<out ScreenHandler>, private val title: Text, private val slotBindings: HashMap<Int, Slot>) :
class GUI private constructor(
private val type: ScreenHandlerType<out ScreenHandler>,
private val title: Text,
private val slotBindings: HashMap<Int, Slot>,
private val blockEntity: BlockEntity? = null
) :
NamedScreenHandlerFactory {
private val inventory = when (type) {
GENERIC_9X1, GENERIC_3X3 -> SimpleInventory(9)
Expand Down Expand Up @@ -118,6 +125,8 @@ class GUI private constructor(private val type: ScreenHandlerType<out ScreenHand
private val inventoryUtils = InventoryUtils(type)
private var title: Text = Text.empty()
private val slotBindings = HashMap<Int, Slot>()
private var blockEntity: BlockEntity? = null

fun setTitle(title: Text): Builder {
this.title = title
return this
Expand All @@ -133,8 +142,13 @@ class GUI private constructor(private val type: ScreenHandlerType<out ScreenHand

fun addSlot(x: Int, y: Int, slot: Slot) = this.addSlot(inventoryUtils.xyToIndex(x, y), slot)

fun blockEntity(entity: BlockEntity): Builder {
this.blockEntity = entity
return this
}

fun build(): GUI {
return GUI(type, title, slotBindings)
return GUI(type, title, slotBindings, blockEntity)
}
}

Expand Down Expand Up @@ -212,6 +226,7 @@ class GUI private constructor(private val type: ScreenHandlerType<out ScreenHand

return
}

PICKUP_ALL -> { // Rewrite PICKUP_ALL only take from allow use slot & player inventory.
if (slot < 0) return

Expand Down Expand Up @@ -252,8 +267,7 @@ class GUI private constructor(private val type: ScreenHandlerType<out ScreenHand
}

override fun canUse(player: PlayerEntity): Boolean {
// TODO close GUI
return true
return (blockEntity as? Inventory)?.canPlayerUse(player) ?: true
}

fun insertItemToBinding(item: ItemStack, fromLast: Boolean): Boolean {
Expand Down

0 comments on commit 21d4d9c

Please sign in to comment.