-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created an entry that works like the on interact block event but does…
…nt require a specific block or interaction type. All it requires is the coordinates.
- Loading branch information
1 parent
b5d7935
commit 792e387
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...BasicExtension/src/main/kotlin/com/typewritermc/basic/entries/event/InteractEventEntry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.devgiga.gigaextension.entries | ||
|
||
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.utils.point.Position | ||
import com.typewritermc.engine.paper.entry.TriggerableEntry | ||
import com.typewritermc.engine.paper.entry.entries.EventEntry | ||
import com.typewritermc.engine.paper.entry.startDialogueWithOrNextDialogue | ||
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") | ||
class InteractEventEntry( | ||
override val id: String = "", | ||
override val name: String = "", | ||
override val triggers: List<Ref<TriggerableEntry>> = emptyList(), | ||
val location: Optional<Position> = Optional.empty(), | ||
): 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>) { | ||
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 clicked on the correct location | ||
if (!entry.location.map { it.sameBlock(location.toPosition()) } | ||
.orElse(true)) return@findWhere false | ||
true | ||
}.toList() | ||
if (entries.isEmpty()) return | ||
entries startDialogueWithOrNextDialogue player | ||
} |