Skip to content

Commit f020179

Browse files
feat: Added useful functions
1 parent 573cf6e commit f020179

7 files changed

Lines changed: 133 additions & 9 deletions

File tree

src/main/kotlin/cc/modlabs/kpaper/extensions/BlockExtension.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cc.modlabs.kpaper.extensions
33
import org.bukkit.Material
44
import org.bukkit.block.Barrel
55
import org.bukkit.block.Block
6+
import org.bukkit.block.BlockFace
67
import org.bukkit.block.Chest
78
import org.bukkit.block.Container
89
import org.bukkit.inventory.ItemStack
@@ -21,4 +22,12 @@ fun Block.getConnectedStorageContainers() = sequenceOf(
2122

2223
fun Material.asQuantity(amount: Int): ItemStack {
2324
return ItemStack(this, amount)
24-
}
25+
}
26+
27+
val BlockFace.yaw: Float
28+
get() = when (this) {
29+
BlockFace.EAST -> -90f
30+
BlockFace.WEST -> 90f
31+
BlockFace.NORTH -> 180f
32+
else -> 0f
33+
}

src/main/kotlin/cc/modlabs/kpaper/extensions/InventoryExtensions.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cc.modlabs.kpaper.consts.NAMESPACE_ITEM_IDENTIFIER
55
import cc.modlabs.kpaper.coroutines.taskRunLater
66
import cc.modlabs.kpaper.inventory.ItemBuilder
77
import cc.modlabs.kpaper.inventory.toItemBuilder
8+
import cc.modlabs.kpaper.main.PluginInstance
89
import dev.fruxz.ascend.extension.isNull
910
import dev.fruxz.stacked.text
1011
import org.bukkit.Bukkit
@@ -152,13 +153,27 @@ fun ItemStack.hasKey(namespacedKey: NamespacedKey): Boolean {
152153
) == true
153154
}
154155

156+
fun <T : Any> ItemStack.hasKey(namespacedKey: NamespacedKey, type: PersistentDataType<T, T>): Boolean {
157+
return this.itemMeta?.persistentDataContainer?.has(
158+
namespacedKey,
159+
type
160+
) == true
161+
}
162+
155163
fun ItemStack.getKey(namespacedKey: NamespacedKey): String? {
156164
return this.itemMeta?.persistentDataContainer?.get(
157165
namespacedKey,
158166
PersistentDataType.STRING
159167
)
160168
}
161169

170+
fun <T : Any> ItemStack.getKey(namespacedKey: NamespacedKey, type: PersistentDataType<T, T>): T? {
171+
return this.itemMeta?.persistentDataContainer?.get(
172+
namespacedKey,
173+
type
174+
)
175+
}
176+
162177
fun ItemStack.addCustomString(value: String): ItemStack {
163178
return this.toItemBuilder {
164179
addCustomString(value)
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package cc.modlabs.kpaper.extensions
22

3-
import cc.modlabs.kpaper.main.PluginInstance
4-
import org.slf4j.LoggerFactory
3+
import cc.modlabs.kpaper.functions.getInternalKPaperLogger
4+
import cc.modlabs.kpaper.functions.getLogger
55

6-
fun getLogger(): org.slf4j.Logger {
7-
return LoggerFactory.getLogger(PluginInstance::class.java)
8-
}
6+
@Deprecated("Moved into functions folder", replaceWith = ReplaceWith("getLogger()", "cc.modlabs.kpaper.functions"))
7+
fun getLogger(): org.slf4j.Logger = getLogger()
98

10-
fun getInternalKPaperLogger(): org.slf4j.Logger {
11-
return LoggerFactory.getLogger("cc.modlabs.kpaper")
12-
}
9+
@Deprecated("Moved into functions folder", replaceWith = ReplaceWith("getInternalKPaperLogger()", "cc.modlabs.kpaper.functions"))
10+
fun getInternalKPaperLogger(): org.slf4j.Logger = getInternalKPaperLogger()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cc.modlabs.kpaper.functions
2+
3+
const val ANSI_RESET = "\u001B[0m"
4+
const val ANSI_BLACK = "\u001B[30m"
5+
const val ANSI_RED = "\u001B[31m"
6+
const val ANSI_GREEN = "\u001B[32m"
7+
const val ANSI_YELLOW = "\u001B[33m"
8+
const val ANSI_BLUE = "\u001B[34m"
9+
const val ANSI_PURPLE = "\u001B[35m"
10+
const val ANSI_CYAN = "\u001B[36m"
11+
const val ANSI_WHITE = "\u001B[37m"
12+
13+
14+
fun printColor(text: String, color: String) {
15+
print("$color$text$ANSI_RESET")
16+
}
17+
18+
fun printlnColor(text: String, color: String) {
19+
println("$color$text$ANSI_RESET")
20+
}
21+
22+
// Quick shortcuts
23+
fun printlnRed(text: String) = printlnColor(text, ANSI_RED)
24+
fun printlnGreen(text: String) = printlnColor(text, ANSI_GREEN)
25+
fun printlnYellow(text: String) = printlnColor(text, ANSI_YELLOW)
26+
fun printlnBlue(text: String) = printlnColor(text, ANSI_BLUE)
27+
fun printlnCyan(text: String) = printlnColor(text, ANSI_CYAN)
28+
fun printlnPurple(text: String) = printlnColor(text, ANSI_PURPLE)
29+
30+
31+
// --- SECTION TITLES AND DIVIDERS ---
32+
33+
fun sectionTitle(title: String, color: String = ANSI_CYAN) {
34+
val line = "".repeat(title.length + 4)
35+
printlnColor("$line", color)
36+
printlnColor("$title", color)
37+
printlnColor("$line", color)
38+
}
39+
40+
fun divider(length: Int = 50, color: String = ANSI_WHITE) {
41+
printlnColor("".repeat(length), color)
42+
}
43+
44+
45+
// --- PROGRESS BAR ---
46+
47+
fun progressBar(progress: Double, length: Int = 30, color: String = ANSI_GREEN) {
48+
require(progress in 0.0..1.0) { "Progress must be between 0.0 and 1.0" }
49+
val filled = (progress * length).toInt()
50+
val bar = "".repeat(filled) + "".repeat(length - filled)
51+
printColor("\r[$bar] ${(progress * 100).toInt()}%", color)
52+
if (progress >= 1.0) println()
53+
}
54+
55+
56+
// --- USER INPUT (with prompt color) ---
57+
58+
fun readLinePrompt(prompt: String, color: String = ANSI_YELLOW): String? {
59+
printColor(prompt, color)
60+
return readLine()
61+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cc.modlabs.kpaper.functions
2+
3+
import cc.modlabs.kpaper.main.PluginInstance
4+
import org.slf4j.Logger
5+
import org.slf4j.LoggerFactory
6+
7+
fun getLogger(): Logger {
8+
return LoggerFactory.getLogger(PluginInstance::class.java)
9+
}
10+
11+
fun getInternalKPaperLogger(): Logger {
12+
return LoggerFactory.getLogger("cc.modlabs.kpaper")
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cc.modlabs.kpaper.functions
2+
3+
import kotlin.math.abs
4+
import kotlin.random.Random
5+
6+
/**
7+
* Returns a Random object with a seed derived from the given seed and a series of integers.
8+
*
9+
* @param seed the base seed used to generate the initial Random object
10+
* @param ints the additional integers used to further seed the Random object
11+
* @return a Random object with the seeded value
12+
*/
13+
fun getMultiSeededRandom(seed: Long, vararg ints: Int): Random {
14+
var seeder = seed
15+
for (num in ints) {
16+
seeder = Random(seeder + num).nextLong()
17+
}
18+
return Random(seeder)
19+
}
20+
21+
fun getRandomIntAt(x: Int, y: Int, seed: Long, max: Int): Int {
22+
return abs(getMultiSeededRandom(seed, x, y).nextInt(max))
23+
}
24+
25+
fun getRandomFloatAt(x: Int, y: Int, seed: Long): Float {
26+
return getMultiSeededRandom(seed, x, y).nextFloat()
27+
}

src/main/kotlin/cc/modlabs/kpaper/inventory/ItemStackConverter.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.modlabs.kpaper.inventory
22

33
import org.bukkit.Bukkit
4+
import org.bukkit.Material
45
import org.bukkit.inventory.Inventory
56
import org.bukkit.inventory.ItemStack
67
import org.bukkit.inventory.PlayerInventory

0 commit comments

Comments
 (0)