Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 5ca6425

Browse files
committed
PlayerExtensions
1 parent c440644 commit 5ca6425

File tree

4 files changed

+279
-0
lines changed

4 files changed

+279
-0
lines changed

src/main/java/com/redmagic/undefinedapi/UndefinedAPI.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.redmagic.undefinedapi.menu.MenuManager
55
import com.redmagic.undefinedapi.scheduler.TimeUnit
66
import com.redmagic.undefinedapi.scheduler.delay
77
import com.redmagic.undefinedapi.scheduler.repeatingTask
8+
import org.bukkit.Bukkit
89
import org.bukkit.plugin.java.JavaPlugin
910

1011

src/main/java/com/redmagic/undefinedapi/extension/JavaPluginExtensions.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ import org.reflections.scanners.SubTypesScanner
77
import org.reflections.util.ConfigurationBuilder
88
import java.lang.reflect.Method
99

10+
/**
11+
* Finds all classes in the specified package that are annotated with the given annotation.
12+
*
13+
* @param annotation the annotation to filter the classes by
14+
* @return a set of classes annotated with the given annotation
15+
*/
1016
fun JavaPlugin.findClasses(annotation: Class<out Annotation>): Set<Class<*>> = Reflections(
1117
ConfigurationBuilder()
1218
.forPackages(this::class.java.packageName)
1319
.setScanners(SubTypesScanner(false), ResourcesScanner())
1420
).getTypesAnnotatedWith(annotation)
1521

22+
/**
23+
* Finds all methods annotated with the specified annotation.
24+
*
25+
* @param annotation the annotation class to search for
26+
* @return a mutable set of methods annotated with the specified annotation
27+
*/
1628
fun JavaPlugin.findMethods(annotation: Class<out Annotation>): MutableSet<Method> = Reflections(
1729
ConfigurationBuilder()
1830
.forPackages(this::class.java.packageName)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.redmagic.undefinedapi.extension
2+
3+
import com.redmagic.undefinedapi.UndefinedAPI
4+
import com.redmagic.undefinedapi.scheduler.TimeUnit
5+
import com.redmagic.undefinedapi.scheduler.repeatingTask
6+
import net.kyori.adventure.text.Component
7+
import org.bukkit.Bukkit
8+
import org.bukkit.entity.Player
9+
10+
/**
11+
* Sends an action bar message to the player.
12+
*
13+
* @param component The component representing the action bar message to be sent.
14+
* @param time The duration for which the action bar message should be displayed.
15+
* @param timeUnit The time unit in which the duration is measured.
16+
*
17+
* @throws IllegalArgumentException if the provided `time` parameter is negative.
18+
*/
19+
fun Player.sendActionBar(component: Component, time: Int, timeUnit: TimeUnit = TimeUnit.TICKS) = repeatingTask(1, timeUnit.toTicks(time.toLong()).toInt()){ sendActionBar(component) }
20+
/**
21+
* Sends an action bar message to the player for a specified duration.
22+
*
23+
* @param component The action bar message to send.
24+
* @param time The duration in the specified time unit for which the action bar message should be displayed.
25+
*/
26+
fun Player.sendActionBar(component: Component, time: Int) = sendActionBar(component, time, TimeUnit.TICKS)
27+
/**
28+
* Sends an action bar message to the player for a specific duration.
29+
*
30+
* @param string The text content of the action bar message.
31+
* @param time The duration in ticks for which the action bar message should be displayed.
32+
*/
33+
fun Player.sendActionBar(string: String, time: Int) = sendActionBar(Component.text(string), time)
34+
/**
35+
* Sends an action bar message to the player for a specified duration.
36+
*
37+
* @param string The message to be displayed in the action bar.
38+
* @param time The duration to display the message, in the specified time unit.
39+
* @param timeUnit The time unit for the duration (default is `TimeUnit.TICKS`).
40+
*/
41+
fun Player.sendActionBar(string: String, time: Int, timeUnit: TimeUnit = TimeUnit.TICKS) = sendActionBar(Component.text(string), time, timeUnit)
42+
43+
/**
44+
* Sets the food level of the player to the maximum value.
45+
*/
46+
fun Player.feed() { foodLevel = 20 }
47+
48+
fun Player.heal() { health = maxHealth }
49+
50+
/**
51+
* Resets the walk speed of the player to the default value.
52+
*/
53+
fun Player.resetWalkSpeed() { walkSpeed = 0.2F }
54+
/**
55+
* Resets the fly speed of the player.
56+
* The fly speed is set to the default value of 0.1F.
57+
*/
58+
fun Player.resetFlySpeed() { flySpeed = 0.1F }
59+
60+
61+
62+
/**
63+
* Hides the player from all online players.
64+
*
65+
* This method will loop through all online players and hide the current player from each of them.
66+
*
67+
* @receiver The player to hide.
68+
* @see Player.showPlayer
69+
*/
70+
fun Player.hidePlayer() = Bukkit.getOnlinePlayers().forEach{
71+
it.hidePlayer(UndefinedAPI.plugin, this)
72+
}
73+
/**
74+
* This method is used to show the player to all online players on the server.
75+
* It iterates over all online players and calls the `showPlayer` method on each player.
76+
* The `showPlayer` method is a method provided by the Bukkit API to show a player to another player.
77+
* It takes two parameters - the plugin instance and the player to be shown.
78+
*
79+
* @see [Bukkit.getOnlinePlayers]
80+
* @see [Player.showPlayer]
81+
* @see [UndefinedAPI.plugin]
82+
*/
83+
fun Player.showPlayer() = Bukkit.getOnlinePlayers().forEach{
84+
it.showPlayer(UndefinedAPI.plugin, this)
85+
}
86+
/**
87+
* Removes all active potion effects from the player.
88+
*/
89+
fun Player.removeActivePotionEffects() {
90+
activePotionEffects.forEach { removePotionEffect(it.type) }
91+
}

src/main/java/com/redmagic/undefinedapi/scheduler/Scheduler.kt

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,30 @@ import com.redmagic.undefinedapi.UndefinedAPI
44
import org.bukkit.scheduler.BukkitRunnable
55
import org.bukkit.scheduler.BukkitTask
66

7+
/**
8+
* Schedules a synchronous task that will be executed by the Bukkit server.
9+
*
10+
* @param runnable the code to be executed synchronously
11+
* @return the BukkitTask that represents the scheduled task
12+
*/
713
fun sync(runnable: BukkitRunnable.() -> Unit): BukkitTask = createRunnable(runnable).runTask(UndefinedAPI.plugin)
14+
/**
15+
* Executes the given [runnable] asynchronously on a separate thread.
16+
*
17+
* @param runnable the code to be executed asynchronously.
18+
* @return the [BukkitTask] associated with the scheduled task.
19+
*/
820
fun async(runnable: BukkitRunnable.() -> Unit): BukkitTask = createRunnable(runnable).runTaskAsynchronously(UndefinedAPI.plugin)
921

22+
/**
23+
* Delays the execution of a given runnable task for a specified number of ticks.
24+
*
25+
* @param ticks The number of ticks to delay the execution of the task.
26+
* @param unit The time unit to use for the delay (default is TimeUnit.TICKS).
27+
* @param async Determines whether the task should be executed asynchronously (default is false).
28+
* @param runnable The runnable task to be executed after the delay.
29+
* @return The BukkitTask representing the delayed task.
30+
*/
1031
fun delay(ticks: Int, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit):BukkitTask{
1132
return if (async){
1233
createRunnable(runnable).runTaskLater(UndefinedAPI.plugin, unit.toTicks(ticks.toLong()))
@@ -15,30 +36,177 @@ fun delay(ticks: Int, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, r
1536
}
1637
}
1738

39+
/**
40+
* Delays the execution of the specified [runnable] by the specified number of [ticks].
41+
*
42+
* @param ticks The number of ticks to delay the execution by. Default is 1.
43+
* @param runnable The lambda function representing the code to be executed after the delay.
44+
* @return The scheduled BukkitTask that can be used to cancel the delayed execution if needed.
45+
*/
1846
fun delay(ticks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTask = delay(ticks, TimeUnit.TICKS, false, runnable)
47+
/**
48+
* Delays the execution of the specified function by the given number of ticks.
49+
*
50+
* @param ticks The number of ticks to delay the execution by (default is 1).
51+
* @param async Whether to execute the function asynchronously (default is false).
52+
* @param runnable The function to delay.
53+
* @return The Bukkit task representing the delayed execution.
54+
*/
1955
fun delay(ticks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = delay(ticks, TimeUnit.TICKS, async, runnable)
2056

2157

58+
/**
59+
* Schedules a repeating task with the given delay, period, and other optional parameters.
60+
*
61+
* @param delay The delay before the task starts, in the specified time unit.
62+
* @param period The time between each execution of the task, in the specified time unit.
63+
* @param times The number of times the task should repeat. Use -1 for indefinitely. Defaults to -1.
64+
* @param unit The time unit used for the delay and period. Defaults to TimeUnit.TICKS.
65+
* @param async Specifies whether to run the task asynchronously or synchronously. Defaults to false.
66+
* @param runnable The code to be executed by the task.
67+
*
68+
* @return The BukkitTask representing the scheduled repeating task.
69+
*/
2270
fun repeatingTask(delay: Int, period: Int, times: Int = -1, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit): BukkitTask{
2371
return if (async){
2472
createRunnable(times, runnable).runTaskTimerAsynchronously(UndefinedAPI.plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
2573
}else{
2674
createRunnable(times ,runnable).runTaskTimer(UndefinedAPI.plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
2775
}
2876
}
77+
/**
78+
* Executes a repeating task with the given ticks interval.
79+
*
80+
* @param ticks the number of ticks between each execution of the task (default is 1 tick)
81+
* @param runnable the code to be executed by the task
82+
* @return the BukkitTask representing the scheduled repeating task
83+
*/
2984
fun repeatingTask(ticks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(0, ticks, -1, TimeUnit.TICKS, false, runnable)
85+
/**
86+
* Schedules a repeating task with the specified options.
87+
*
88+
* @param ticks the delay before the task starts, in ticks (default is 1)
89+
* @param times the number of times the task should repeat (-1 for indefinitely, default is -1)
90+
* @param runnable the code to be executed repeatedly
91+
* @return the BukkitTask representing the scheduled repeating task
92+
*/
3093
fun repeatingTask(ticks: Int = 1, times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(0, ticks, times, TimeUnit.TICKS, false, runnable)
94+
/**
95+
* Schedules a repeating task to be executed with a specified delay between each execution.
96+
*
97+
* @param periodTicks The delay between each execution in ticks (default is 1).
98+
* @param async Specifies whether to run the task asynchronously or synchronously.
99+
* @param runnable The code to be executed in the task, defined as an extension function on [BukkitRunnable].
100+
* @return The scheduled BukkitTask instance representing the task.
101+
*/
31102
fun repeatingTask(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(periodTicks, periodTicks, -1, TimeUnit.TICKS, async, runnable)
103+
/**
104+
* Executes a repeating task with the specified parameters.
105+
*
106+
* @param periodTicks The number of ticks between each execution.
107+
* @param async Whether the task should be executed asynchronously.
108+
* @param times The number of times the task should be executed. Use -1 for unlimited executions.
109+
* @param runnable The code to be executed by the task.
110+
*
111+
* @return The BukkitTask representing the scheduled repeating task.
112+
*/
32113
fun repeatingTask(periodTicks: Int = 1, async: Boolean, times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(periodTicks, periodTicks, times, TimeUnit.TICKS, async, runnable)
114+
/**
115+
* Schedules a repeating task with the specified period and unit.
116+
*
117+
* @param period the time between each execution of the task
118+
* @param unit the unit of time used for the period
119+
* @param runnable the code to be executed by the task
120+
* @return the BukkitTask representing the scheduled task
121+
*/
33122
fun repeatingTask(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, -1, unit, false, runnable)
123+
/**
124+
* Executes a repeating task with the specified period and times.
125+
*
126+
* @param period The time between each execution of the task.
127+
* @param unit The time unit of the period.
128+
* @param times The number of times the task should repeat. Defaults to -1 which means the task will repeat indefinitely.
129+
* @param runnable The code to be executed by the task.
130+
*
131+
* @return The BukkitTask representing the repeating task.
132+
*/
34133
fun repeatingTask(period: Int, unit: TimeUnit, times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, times, unit, false, runnable)
134+
/**
135+
* Schedule a repeating task with the specified period and unit.
136+
*
137+
* @param period the time between each execution of the task
138+
* @param unit the time unit of the period value
139+
* @param async whether to run the task asynchronously or not
140+
* @param runnable the code to be executed by the task
141+
* @return the BukkitTask representing the scheduled task
142+
*/
35143
fun repeatingTask(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, -1, unit, async, runnable)
144+
/**
145+
* Executes a repeating task asynchronously or synchronously in a Bukkit server.
146+
*
147+
* @param period The period between each task execution.
148+
* @param unit The time unit of the period.
149+
* @param times The number of times the task should be executed.
150+
* If set to -1, the task will repeat indefinitely until cancelled.
151+
* Default value is -1.
152+
* @param async Specifies whether the task should be executed asynchronously or synchronously.
153+
* If set to true, the task will be executed asynchronously.
154+
* If set to false, the task will be executed synchronously.
155+
* @param runnable The code to be executed for each task execution.
156+
*
157+
* @return The BukkitTask representing the repeating task.
158+
*/
36159
fun repeatingTask(period: Int, unit: TimeUnit, times: Int = -1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, times, unit, async, runnable)
160+
/**
161+
* Schedules a repeating task on the Bukkit scheduler.
162+
*
163+
* @param delayTicks The number of ticks to wait before the task is first executed.
164+
* @param periodTicks The number of ticks between each execution of the task.
165+
* @param async Whether the task should be executed asynchronously.
166+
* @param runnable The code to be executed by the task. This code is specified as a lambda expression
167+
* that takes a [BukkitRunnable] object as its receiver.
168+
* @return The Bukkit task that has been scheduled.
169+
*/
37170
fun repeatingTask(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delayTicks, periodTicks, -1, TimeUnit.TICKS, async, runnable)
171+
/**
172+
* Schedules a repeating task with the specified delay, period, and number of times to repeat.
173+
*
174+
* @param delayTicks the delay in ticks before the task should start running
175+
* @param periodTicks the delay in ticks between each execution of the task
176+
* @param times the number of times the task should repeat (-1 for infinite)
177+
* @param async if true, the task will run asynchronously; otherwise, it will run synchronously
178+
* @param runnable the code to be executed by the task
179+
* @return a BukkitTask representing the scheduled task
180+
*/
38181
fun repeatingTask(delayTicks: Int, periodTicks: Int, times: Int = -1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delayTicks, periodTicks, times, TimeUnit.TICKS, async, runnable)
182+
/**
183+
* Schedules a repeating task with the given delay, period, time unit, and runnable function.
184+
*
185+
* @param delay the initial delay before the task is executed, in the specified time unit
186+
* @param period the time between consecutive executions of the task, in the specified time unit
187+
* @param unit the time unit of the delay and period parameters
188+
* @param runnable the function to be run repeatedly
189+
* @return the BukkitTask representing the scheduled task
190+
*/
39191
fun repeatingTask(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delay, period, -1, unit, false, runnable)
192+
/**
193+
* Schedules a repeating task with the given delay, period, times, unit, and runnable.
194+
*
195+
* @param delay The delay in time units before the task is executed for the first time.
196+
* @param period The time period in time units between subsequent task executions.
197+
* @param times The number of times the task should be repeated. A value of -1 means the task will repeat indefinitely.
198+
* @param unit The time unit for the delay and period parameters.
199+
* @param runnable The code to be executed as part of the task. It is defined as a lambda expression with the BukkitRunnable as the receiver.
200+
* @return The BukkitTask object representing the scheduled repeating task.
201+
*/
40202
fun repeatingTask(delay: Int, period: Int, times: Int = -1, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delay, period, times, unit, false, runnable)
41203

204+
/**
205+
* Creates a new instance of [BukkitRunnable] using the given lambda expression as the run method.
206+
*
207+
* @param runnable the lambda expression to be executed when the [BukkitRunnable] runs.
208+
* @return a new instance of [BukkitRunnable] with the specified run method.
209+
*/
42210
private fun createRunnable(runnable: BukkitRunnable.() -> Unit): BukkitRunnable{
43211
return object : BukkitRunnable(){
44212
override fun run() {
@@ -47,6 +215,13 @@ private fun createRunnable(runnable: BukkitRunnable.() -> Unit): BukkitRunnable{
47215
}
48216
}
49217

218+
/**
219+
* Creates a `BukkitRunnable` with the specified number of times to run and a lambda expression to execute.
220+
*
221+
* @param times The number of times to run the `BukkitRunnable`. Defaults to -1, which means the `BukkitRunnable` will run indefinitely.
222+
* @param runnable The lambda expression to execute when the `BukkitRunnable` is run.
223+
* @return The created `BukkitRunnable`.
224+
*/
50225
private fun createRunnable(times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitRunnable{
51226
var amount = 0
52227
return object : BukkitRunnable(){

0 commit comments

Comments
 (0)