diff --git a/CHANGELOG.md b/CHANGELOG.md index 212a0bc..64b69a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,7 @@ -# v0.1.0 - Snapshot +## v0.1.1 - Snapshot +* Added Dialogs +* Moved `xyz.theprogramsrc.uismodule.simple.SimpleUi` to `xyz.theprogramsrc.uismodule.ui.SimpleUi` +* Added TranslationsModule as dependency + +## v0.1.0 - Snapshot Hello, World! diff --git a/build.gradle b/build.gradle index 6fa1912..92c4992 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { id 'org.jetbrains.dokka' version '1.6.0' } -def projectVersion = (System.getenv("VERSION") ?: '0.1.0-SNAPSHOT').replaceFirst("v", "").replace('/', '') +def projectVersion = (System.getenv("VERSION") ?: '0.1.1-SNAPSHOT').replaceFirst("v", "").replace('/', '') group 'xyz.theprogramsrc' version projectVersion @@ -33,8 +33,9 @@ dependencies { compileOnly 'net.md-5:bungeecord-api:1.18-R0.1-SNAPSHOT' compileOnly 'xyz.theprogramsrc:simplecoreapi:0.2.0-SNAPSHOT' compileOnly 'xyz.theprogramsrc:tasksmodule:0.1.0-SNAPSHOT' + compileOnly 'xyz.theprogramsrc:translationsmodule:0.1.3-SNAPSHOT' - implementation 'com.github.cryptomorin:XSeries:8.6.0.0.1' + implementation 'com.github.cryptomorin:XSeries:8.6.2' testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2' } diff --git a/src/main/kotlin/xyz/theprogramsrc/uismodule/Dialog.kt b/src/main/kotlin/xyz/theprogramsrc/uismodule/Dialog.kt new file mode 100644 index 0000000..b8e6c98 --- /dev/null +++ b/src/main/kotlin/xyz/theprogramsrc/uismodule/Dialog.kt @@ -0,0 +1,140 @@ +package xyz.theprogramsrc.uismodule + +import com.cryptomorin.xseries.messages.ActionBar +import com.cryptomorin.xseries.messages.Titles +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import org.bukkit.event.EventHandler +import org.bukkit.event.EventPriority +import org.bukkit.event.HandlerList +import org.bukkit.event.Listener +import org.bukkit.event.block.Action +import org.bukkit.event.player.AsyncPlayerChatEvent +import org.bukkit.event.player.PlayerInteractEvent +import org.bukkit.event.player.PlayerMoveEvent +import xyz.theprogramsrc.simplecoreapi.spigot.SpigotLoader +import xyz.theprogramsrc.tasksmodule.objects.RecurringTask +import xyz.theprogramsrc.tasksmodule.spigot.SpigotTasks +import xyz.theprogramsrc.uismodule.objects.bukkitColor +import xyz.theprogramsrc.uismodule.objects.bukkitStripColor + +/** + * Representation of a dialog + * @param player The player that will see the dialog + * @param title The title of the dialog. If null it will be empty. (Defaults to null) + * @param subtitle The subtitle of the dialog. If null it will be empty. (Defaults to null) + * @param actionbar The actionbar of the dialog. If null it will be empty. (Defaults to null) + * @param onClose The function that will be called when the player closes the dialog. (Defaults to empty function) + */ +class Dialog( + val player: Player, + val title: String? = null, + val subtitle: String? = null, + val actionbar: String? = null, + val onClose: (Player) -> Unit = {}, + val onChat: (Player, String) -> Boolean = { _: Player, _: String -> true }, +): Listener{ + + private var task: RecurringTask? = null + private var lastMovementAt = 0L + + /** + * Should the player be able to close this dialog? + */ + var canBeClosed = true + + /** + * Was this dialog closed manually or programmatically? + */ + var manuallyClosed = false + private set + + private fun send() { + if(this.player.isOnline) { // Check if the player is online + if(this.title != null || this.subtitle != null) { + Titles.sendTitle( + this.player, + 0, // Fade in + 20, // Stay + 0, // Fade out + (this.title ?: "&7").bukkitColor(), + (this.subtitle ?: "&7").bukkitColor() + ) + } else { + Titles.clearTitle(this.player) + } + + val now = System.currentTimeMillis() // Get the current time + val calc = now - lastMovementAt // Calculate the difference between the last movement and now. (If the player hasn't moved the calc will be equals to now) + // Show the close actionbar translation if the player has moved in the last 5 seconds, otherwise show the actionbar + val actionbar = if(calc < 5000L && calc != now && this.canBeClosed) { + Main.HOW_TO_CLOSE_DIALOG_TRANSLATION.translate() + } else { + this.actionbar + } + + if(actionbar != null){ + ActionBar.sendActionBar(this.player, actionbar.bukkitColor()) + }else{ + ActionBar.clearActionBar(this.player) + } + } else { + this.task?.stop() + } + } + + fun open(): Dialog = this.apply { + if(this.task == null){ + this.task = SpigotTasks.instance.runTaskTimerAsynchronously(delay = 1L, period = 5L, task = this::send) + } + this.task?.stop() // Stop the previous task if there is any + HandlerList.unregisterAll(this) // Unregister all the listeners + SpigotTasks.instance.runTask(this.player::closeInventory) // Close the inventory of the player + Bukkit.getPluginManager().registerEvents(this, SpigotLoader.instance) // Register the listeners + this.task?.start() // Start the task + this.manuallyClosed = false // Reset the manually closed flag + } + + fun close(sendMessage: Boolean = true): Dialog = this.apply { + this.task?.stop() // Stop the task + HandlerList.unregisterAll(this) // Unregister all the listeners + this.manuallyClosed = true // Set the manually closed flag + Titles.clearTitle(this.player) // Clear the title + ActionBar.clearActionBar(this.player) // Clear the actionbar + if(sendMessage) { // Send the message if the flag is true + this.player.sendMessage(Main.DIALOG_CLOSED_TRANSLATION.translate().bukkitColor()) + } + } + + @EventHandler(priority = EventPriority.LOWEST) + fun onInteract(e: PlayerInteractEvent) { + if(this.player == e.player && this.canBeClosed) { + if(e.action == Action.LEFT_CLICK_AIR || e.action == Action.LEFT_CLICK_BLOCK) { + this.close() + } + } + } + + @EventHandler(priority = EventPriority.LOWEST) + fun onMessageReceived(event: AsyncPlayerChatEvent) { + if(this.player == event.player) { + event.isCancelled = true + val msg = event.message.bukkitStripColor() + SpigotTasks.instance.runTask { + if(this.onChat(this.player, msg)) { + this.manuallyClosed = false + this.close() + } + } + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) + fun onMove(e: PlayerMoveEvent) { + if(this.player == e.player){ // Check if the player is the same + this.lastMovementAt = System.currentTimeMillis() // Update the last movement time + } + } + + +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/theprogramsrc/uismodule/Main.kt b/src/main/kotlin/xyz/theprogramsrc/uismodule/Main.kt index f856be4..40e9e4c 100644 --- a/src/main/kotlin/xyz/theprogramsrc/uismodule/Main.kt +++ b/src/main/kotlin/xyz/theprogramsrc/uismodule/Main.kt @@ -1,5 +1,23 @@ package xyz.theprogramsrc.uismodule +import org.bukkit.event.Listener import xyz.theprogramsrc.simplecoreapi.global.module.Module +import xyz.theprogramsrc.translationsmodule.Translation -class Main: Module() \ No newline at end of file +class Main: Module(), Listener { + + companion object { + val HOW_TO_CLOSE_DIALOG_TRANSLATION = Translation( + id = "Dialog.HowToClose", + defaultValue = "Use **Left Click** to **close** this Dialog.", + colors = arrayOf("&b","&c"), + mainColor = "&7", + ) + val DIALOG_CLOSED_TRANSLATION = Translation( + id = "Dialog.Closed", + defaultValue = "You've **closed** this Dialog.", + colors = arrayOf("&c"), + mainColor = "&7" + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/theprogramsrc/uismodule/simple/SimpleUi.kt b/src/main/kotlin/xyz/theprogramsrc/uismodule/ui/SimpleUi.kt similarity index 99% rename from src/main/kotlin/xyz/theprogramsrc/uismodule/simple/SimpleUi.kt rename to src/main/kotlin/xyz/theprogramsrc/uismodule/ui/SimpleUi.kt index 434f662..00dba75 100644 --- a/src/main/kotlin/xyz/theprogramsrc/uismodule/simple/SimpleUi.kt +++ b/src/main/kotlin/xyz/theprogramsrc/uismodule/ui/SimpleUi.kt @@ -1,4 +1,4 @@ -package xyz.theprogramsrc.uismodule.simple +package xyz.theprogramsrc.uismodule.ui import org.bukkit.Bukkit import org.bukkit.entity.Player diff --git a/src/main/resources/module.properties b/src/main/resources/module.properties index 7086f4b..9684241 100644 --- a/src/main/resources/module.properties +++ b/src/main/resources/module.properties @@ -4,4 +4,5 @@ description=@description@ version=@version@ author=TheProgramSrc repository-id=uismodule -dependencies=tasksmodule +dependencies=tasksmodule,translationsmodule +github-repository=TheProgramSrc/SimpleCore-UIsModule