Skip to content

Commit

Permalink
Add additional options to the interact event
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Dec 18, 2024
1 parent 792e387 commit d8957ed
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ enum class BlockBreakContextKeys(override val klass: KClass<*>) : EntryContextKe
CENTER_POSITION(Position::class)
}

private fun hasItemInHand(player: Player, item: Item): Boolean {
return item.isSameAs(player, player.inventory.itemInMainHand, context()) || item.isSameAs(
player,
player.inventory.itemInOffHand,
context(),
)
}

@EntryListener(BlockBreakEventEntry::class)
fun onBlockBreak(event: BlockBreakEvent, query: Query<BlockBreakEventEntry>) {
val player = event.player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ class FishEventEntry(
val caught: Var<Item> = ConstVar(Item.Empty),
) : EventEntry


private fun hasItemInHand(player: Player, item: Item): Boolean {
return item.isSameAs(player, player.inventory.itemInMainHand, context()) || item.isSameAs(
player,
player.inventory.itemInOffHand,
context(),
)
}

@EntryListener(FishEventEntry::class)
fun onPlayerFish(event: PlayerFishEvent, query: Query<FishEventEntry>) {
if (event.state != PlayerFishEvent.State.CAUGHT_FISH) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,14 @@ enum class InteractBlockContextKeys(override val klass: KClass<*>) : EntryContex
POSITION(Position::class),
}

private fun hasItemInHand(player: Player, item: Item): Boolean {
fun hasItemInHand(player: Player, item: Item): Boolean {
return item.isSameAs(player, player.inventory.itemInMainHand, context()) || item.isSameAs(
player,
player.inventory.itemInOffHand,
context(),
)
}

fun Location.isSameBlock(location: Location): Boolean {
return this.world == location.world && this.blockX == location.blockX && this.blockY == location.blockY && this.blockZ == location.blockZ
}

@EntryListener(InteractBlockEventEntry::class)
fun onInteractBlock(event: PlayerInteractEvent, query: Query<InteractBlockEventEntry>) {
val player = event.player
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
package io.devgiga.gigaextension.entries
package com.typewritermc.basic.entries.event

import com.typewritermc.core.books.pages.Colors
import com.typewritermc.core.entries.Query
import com.typewritermc.core.entries.Ref
import com.typewritermc.core.extension.annotations.Entry
import com.typewritermc.core.extension.annotations.EntryListener
import com.typewritermc.core.extension.annotations.Help
import com.typewritermc.core.interaction.context
import com.typewritermc.core.utils.point.Position
import com.typewritermc.engine.paper.entry.TriggerableEntry
import com.typewritermc.engine.paper.entry.entries.ConstVar
import com.typewritermc.engine.paper.entry.entries.EventEntry
import com.typewritermc.engine.paper.entry.startDialogueWithOrNextDialogue
import com.typewritermc.engine.paper.entry.entries.Var
import com.typewritermc.engine.paper.entry.triggerAllFor
import com.typewritermc.engine.paper.utils.item.Item
import com.typewritermc.engine.paper.utils.toPosition
import org.bukkit.Location
import org.bukkit.event.player.PlayerInteractEvent
import java.util.*

@Entry("interact_event_entry", "triggers when a player interacts with a block", Colors.YELLOW, "hugeicons:touch-interaction-02")
@Entry("interact_event_entry", "triggers when a player clicks", Colors.YELLOW, "hugeicons:touch-interaction-02")
class InteractEventEntry(
override val id: String = "",
override val name: String = "",
override val triggers: List<Ref<TriggerableEntry>> = emptyList(),
val location: Optional<Position> = Optional.empty(),
val location: Optional<Var<Position>> = Optional.empty(),
@Help("The item the player must be holding when the block is interacted with.")
val itemInHand: Var<Item> = ConstVar(Item.Empty),
@Help("""
Cancel the event when triggered.
It will only cancel the event if all the criteria are met.
If set to false, it will not modify the event.
""")
val cancel: Boolean = false,
val interactionType: InteractionType = InteractionType.ALL,
val shiftType: ShiftType = ShiftType.ANY,
): EventEntry

fun Location.isSameBlock(location: Location): Boolean {
return this.world == location.world && this.blockX == location.blockX && this.blockY == location.blockY && this.blockZ == location.blockZ
}

@EntryListener(InteractEventEntry::class)
fun onInteractBlock(event: PlayerInteractEvent, query: Query<InteractEventEntry>) {
fun onInteract(event: PlayerInteractEvent, query: Query<InteractEventEntry>) {
val player = event.player
val block = event.clickedBlock
val location = block?.location ?: player.location
// The even triggers twice. Both for the main hand and offhand.
// We only want to trigger once.
println("interact detected")
if (event.hand == org.bukkit.inventory.EquipmentSlot.OFF_HAND) return // Disable off-hand interactions
val entries = query.findWhere { entry ->
println("is this working")
// Check if the player is sneaking
if (!entry.shiftType.isApplicable(player)) return@findWhere false

// Check if the player is interacting with the block in the correct way
if (!entry.interactionType.actions.contains(event.action)) return@findWhere false

// Check if the player clicked on the correct location
if (!entry.location.map { it.sameBlock(location.toPosition()) }
if (!entry.location.map { it.get(player).sameBlock(location.toPosition()) }
.orElse(true)) return@findWhere false

// Check if the player is holding the correct item
if (!hasItemInHand(player, entry.itemInHand.get(player))) return@findWhere false

true
}.toList()
if (entries.isEmpty()) return
entries startDialogueWithOrNextDialogue player
entries.triggerAllFor(player, context())
if (entries.any { it.cancel }) event.isCancelled = true
}

0 comments on commit d8957ed

Please sign in to comment.