Skip to content

Commit

Permalink
Merge pull request #40 from flytegg/feat/skedule-menus-items
Browse files Browse the repository at this point in the history
Feat/skedule menus items
  • Loading branch information
Dawsson authored Jan 21, 2024
2 parents 5eec75a + 60e58b2 commit 66ef9a0
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 20 deletions.
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "gg.flyte"
version = "1.0.36"
version = "1.0.37"

repositories {
mavenLocal()
Expand All @@ -19,13 +19,18 @@ repositories {
maven("https://repo.papermc.io/repository/maven-public/")
maven("https://oss.sonatype.org/content/repositories/snapshots")
maven("https://oss.sonatype.org/content/repositories/central")
maven("https://jitpack.io")
}

dependencies {
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")

implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
implementation("org.mongodb:mongodb-driver-sync:4.9.0")
implementation("com.google.code.gson:gson:2.10.1")

implementation("com.github.okkero:Skedule:v1.2.6")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")
}

tasks {
Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/gg/flyte/twilight/extension/Permissible.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
MIT License
Copyright (c) 2018 DevSrSouza
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package gg.flyte.twilight.extension

import org.bukkit.permissions.Permissible

fun Permissible.anyPermission(vararg permissions: String): Boolean = permissions.any { hasPermission(it) }
fun Permissible.allPermission(vararg permissions: String): Boolean = permissions.all { hasPermission(it) }
fun Permissible.hasPermissionOrStar(permission: String): Boolean = hasPermission(permission) || hasPermission(permission.replaceAfterLast('.', "*"))
7 changes: 4 additions & 3 deletions src/main/kotlin/gg/flyte/twilight/extension/Player.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.flyte.twilight.extension

import gg.flyte.twilight.Twilight
import net.kyori.adventure.text.Component.text
import net.md_5.bungee.api.ChatMessageType
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.Bukkit
Expand Down Expand Up @@ -43,9 +44,9 @@ fun Player.clearActionBar() {
* filling the player's hunger bar completely. The player will be fully fed after
* calling this method.
*/
fun Player.feed() {
foodLevel = 20
}
fun Player.feed() { foodLevel = 20 }
fun Player.resetWalkSpeed() { walkSpeed = 0.2F }
fun Player.resetFlySpeed() { flySpeed = 0.1F}

/**
* Adds the specified [itemStack] to the player's inventory and/or drops remaining on the ground once inventory is full.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gg.flyte.twilight.extension

import org.bukkit.inventory.PlayerInventory

fun PlayerInventory.clearArmor() { armorContents = arrayOf(null, null, null, null) }
fun PlayerInventory.clearAll() {
clear() // Clear Items
clearArmor()
}
15 changes: 4 additions & 11 deletions src/main/kotlin/gg/flyte/twilight/inventory/GuiBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@ class GuiBuilder(
init { apply(block) }

private var onOpen: InventoryOpenEvent.() -> Unit = {}
private var onClick: InventoryClickEvent.() -> Unit = {

}
private var onClick: InventoryClickEvent.() -> Unit = {}
private var onClose: InventoryCloseEvent.() -> Unit = {}

fun onOpen(block: InventoryOpenEvent.() -> Unit) { onOpen = block }
fun onClick(block: InventoryClickEvent.() -> Unit) {

onClick = block
}
fun onClose(block: InventoryCloseEvent.() -> Unit) { onClose = block }
fun onClick(block: InventoryClickEvent.() -> Unit) { onClick = block }
fun onClose(block: InventoryCloseEvent.() -> Unit) { onClose = block }

override fun onOpen(event: InventoryOpenEvent) { onOpen.invoke(event) }
override fun onClick(event: InventoryClickEvent) {

onClick.invoke(event) }
override fun onClick(event: InventoryClickEvent) { onClick.invoke(event) }
override fun onClose(event: InventoryCloseEvent) { onClose.invoke(event) }


Expand Down
175 changes: 171 additions & 4 deletions src/main/kotlin/gg/flyte/twilight/scheduler/Scheduler.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package gg.flyte.twilight.scheduler

import com.okkero.skedule.BukkitSchedulerController
import com.okkero.skedule.SynchronizationContext
import com.okkero.skedule.schedule
import gg.flyte.twilight.Twilight
import gg.flyte.twilight.time.TimeUnit
import org.bukkit.Bukkit
import org.bukkit.scheduler.BukkitRunnable
import org.bukkit.scheduler.BukkitTask

Expand Down Expand Up @@ -34,7 +38,12 @@ fun async(runnable: BukkitRunnable.() -> Unit): BukkitTask {
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
*/
fun delay(value: Int, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit): BukkitTask {
fun delay(
value: Int,
unit: TimeUnit = TimeUnit.TICKS,
async: Boolean = false,
runnable: BukkitRunnable.() -> Unit
): BukkitTask {
return if (async) {
createBukkitRunnable(runnable).runTaskLaterAsynchronously(Twilight.plugin, unit.toTicks(value.toLong()))
} else {
Expand Down Expand Up @@ -80,11 +89,25 @@ fun delay(ticks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit):
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
*/
fun repeat(delay: Int, period: Int, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit): BukkitTask {
fun repeat(
delay: Int,
period: Int,
unit: TimeUnit = TimeUnit.TICKS,
async: Boolean = false,
runnable: BukkitRunnable.() -> Unit
): BukkitTask {
return if (async) {
createBukkitRunnable(runnable).runTaskTimerAsynchronously(Twilight.plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
createBukkitRunnable(runnable).runTaskTimerAsynchronously(
Twilight.plugin,
unit.toTicks(delay.toLong()),
unit.toTicks(period.toLong())
)
} else {
createBukkitRunnable(runnable).runTaskTimer(Twilight.plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
createBukkitRunnable(runnable).runTaskTimer(
Twilight.plugin,
unit.toTicks(delay.toLong()),
unit.toTicks(period.toLong())
)
}
}

Expand Down Expand Up @@ -177,6 +200,150 @@ fun repeat(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.()
return repeat(delay, period, unit, false, runnable)
}








/**
* Schedules a repeating task to be executed by the Bukkit scheduler.
*
* @param delay The duration value for the initial delay.
* @param period The duration value for the period between subsequent executions.
* @param unit The TimeUnit representing the time unit of the delay and period (default: MILLISECONDS).
* @param async Whether the task should be executed asynchronously (default: false).
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
*/
fun repeatingTask(
delay: Int,
period: Int,
unit: TimeUnit = TimeUnit.TICKS,
async: Boolean = false,
runnable: BukkitRunnable.() -> Unit
): BukkitTask {
return if (async) {
createBukkitRunnable(runnable).runTaskTimerAsynchronously(
Twilight.plugin,
unit.toTicks(delay.toLong()),
unit.toTicks(period.toLong())
)
} else {
createBukkitRunnable(runnable).runTaskTimer(
Twilight.plugin,
unit.toTicks(delay.toLong()),
unit.toTicks(period.toLong())
)
}
}

/**
* A convenience wrapper around the main [repeatingTask] method, allowing you to schedule a repeating task
* using ticks instead of specifying time values and time units.
*
* @param periodTicks The number of ticks for the period between subsequent executions (default: 1).
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
* @see repeatingTask
*/
fun repeatingTask(periodTicks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTask {
return repeatingTask(periodTicks, periodTicks, TimeUnit.TICKS, false, runnable)
}

/**
* A convenience wrapper around the main [repeatingTask] method, allowing you to schedule a repeatingTasking task
* using ticks instead of specifying time values and time units. Additionally, you can choose to execute the task
* asynchronously if necessary.
*
* @param periodTicks The number of ticks for the period between subsequent executions (default: 1).
* @param async Whether the task should be executed asynchronously.
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
* @see repeatingTask
*/
fun repeatingTask(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask {
return repeatingTask(periodTicks, periodTicks, TimeUnit.TICKS, async, runnable)
}

/**
* A convenience wrapper around the main [repeatingTask] method, allowing you to schedule a repeatingTasking task
* with a fixed period by specifying the period value and the time unit.
*
* @param period The duration value for the period between subsequent executions.
* @param unit The TimeUnit representing the time unit of the period.
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
* @see repeatingTask
*/
fun repeatingTask(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask {
return repeatingTask(period, period, unit, false, runnable)
}

/**
* A convenience wrapper around the main [repeatingTask] method, allowing you to schedule a repeatingTasking task
* with a fixed period by specifying the period value, the time unit, and whether the task should be executed
* asynchronously.
*
* @param period The duration value for the period between subsequent executions.
* @param unit The TimeUnit representing the time unit of the period.
* @param async Whether the task should be executed asynchronously.
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
* @see repeatingTask
*/
fun repeatingTask(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask {
return repeatingTask(period, period, unit, async, runnable)
}

/**
* A convenience wrapper around the main [repeatingTask] method, allowing you to schedule a repeatingTasking task
* with a fixed initial delay and a fixed period using ticks instead of specifying time values and time units.
* Additionally, you can choose to execute the task asynchronously if necessary.
*
* @param delayTicks The number of ticks for the initial delay.
* @param periodTicks The number of ticks for the period between subsequent executions.
* @param async Whether the task should be executed asynchronously.
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
* @see repeatingTask
*/
fun repeatingTask(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask {
return repeatingTask(delayTicks, periodTicks, TimeUnit.TICKS, async, runnable)
}

/**
* A convenience wrapper around the main [repeatingTask] method, allowing you to schedule a repeatingTasking task
* with a fixed initial delay and a fixed period by specifying the delay and period values and the time unit.
*
* @param delay The duration value for the initial delay.
* @param period The duration value for the period between subsequent executions.
* @param unit The TimeUnit representing the time unit of the delay and period.
* @param runnable The function representing the task to be executed.
* @return The BukkitTask representing the scheduled task.
* @see repeatingTask
*/
fun repeatingTask(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask {
return repeatingTask(delay, period, unit, false, runnable)
}

/**
* Schedules a coroutine-based task to be executed by the Bukkit scheduler.
*
* @param initialContext The initial synchronization context for the task (default: `SynchronizationContext.SYNC`).
* @param block The coroutine block representing the task to be executed.
* @return The CoroutineTask representing the scheduled task.
*/
fun skedule(
initialContext: SynchronizationContext = SynchronizationContext.SYNC,
block: suspend BukkitSchedulerController.() -> Unit
) = Bukkit.getScheduler().schedule(
Twilight.plugin,
initialContext,
block
)

/**
* Converts to a BukkitRunnable instance based on the provided [runnable] function.
*
Expand Down

0 comments on commit 66ef9a0

Please sign in to comment.