Improve code style

This commit is contained in:
Jakob K
2021-05-12 14:17:44 +02:00
parent 3e9b243d3d
commit cf48510756
69 changed files with 144 additions and 609 deletions

View File

@@ -4,7 +4,6 @@ import net.axay.kspigot.main.KSpigotMainInstance
import org.bukkit.Bukkit
import org.bukkit.NamespacedKey
import org.bukkit.command.CommandSender
import org.bukkit.entity.Entity
import org.bukkit.entity.Player
/**

View File

@@ -1,7 +1,5 @@
package net.axay.kspigot.extensions.bukkit
// FROM BUNGEE COLOR
/**
* Returns the corresponding Bukkit Color object.
*/
@@ -14,9 +12,7 @@ val net.md_5.bungee.api.ChatColor.bukkitColor
*/
val net.md_5.bungee.api.ChatColor.javaAwtColor: java.awt.Color
get() = color
// FROM BUKKIT COLOR
/**
* Returns the corresponding Bungee Color object.
*/
@@ -28,9 +24,7 @@ val org.bukkit.Color.bungeeColor: net.md_5.bungee.api.ChatColor
*/
val org.bukkit.Color.javaAwtColor: java.awt.Color
get() = java.awt.Color(asRGB())
// FROM JAVA AWT COLOR
/**
* Returns the corresponding Bukkit Color object.
*/
@@ -42,9 +36,7 @@ val java.awt.Color.bukkitColor
*/
val java.awt.Color.bungeeColor: net.md_5.bungee.api.ChatColor
get() = net.md_5.bungee.api.ChatColor.of(this)
// FROM BUKKIT CHAT COLOR
/**
* Returns the corresponding Bukkit Color object.
*/

View File

@@ -133,7 +133,7 @@ fun Player.title(
subText: String? = null,
fadeIn: Int = 10,
stay: Int = 70,
fadeOut: Int = 20
fadeOut: Int = 20,
) {
sendTitle(mainText, subText, fadeIn, stay, fadeOut)
}

View File

@@ -15,7 +15,7 @@ fun Inventory.closeForViewers() = HashSet(viewers).forEach { it.closeInventory()
val InventoryAction.isSimple
get() = when (this) {
InventoryAction.PLACE_ALL, InventoryAction.PLACE_ONE,
InventoryAction.PICKUP_ALL, InventoryAction.PICKUP_HALF, InventoryAction.PICKUP_ONE
InventoryAction.PICKUP_ALL, InventoryAction.PICKUP_HALF, InventoryAction.PICKUP_ONE,
-> true
else -> false
}

View File

@@ -1,13 +1,13 @@
package net.axay.kspigot.extensions.bukkit
import org.bukkit.inventory.meta.BookMeta
import java.lang.StringBuilder
val BookMeta.content get() =
StringBuilder().apply {
for (it in pages) {
if (isNotEmpty())
append('\n')
append(it)
}
}.toString()
val BookMeta.content
get() =
StringBuilder().apply {
for (it in pages) {
if (isNotEmpty())
append('\n')
append(it)
}
}.toString()

View File

@@ -40,16 +40,12 @@ val PlayerInteractEvent.clickedBlockExceptAir: Block?
get() {
return clickedBlock ?: kotlin.run {
return@run if (this.action == Action.RIGHT_CLICK_AIR) {
val p: Player = this.player
// check for sight blocking entities
for (nearbyEntity: Entity in p.getNearbyEntities(5.0, 5.0, 5.0))
if (p.hasLineOfSight(nearbyEntity)) return@run null
// get first block in line of sight which is not air
p.getLineOfSight(null, 5).find { block -> !block.type.isAir }
} else null
}
}

View File

@@ -9,7 +9,7 @@ import org.bukkit.util.Vector
data class SimpleLocation2D(
val x: Double,
val y: Double
val y: Double,
) {
constructor(x: Number, y: Number)
: this(x.toDouble(), y.toDouble())
@@ -19,30 +19,23 @@ data class SimpleLocation3D(
val x: Double,
val y: Double,
val z: Double,
val direction: Vector = vec(0, 0, 0)
val direction: Vector = vec(0, 0, 0),
) {
constructor(x: Number, y: Number, z: Number)
: this(x.toDouble(), y.toDouble(), z.toDouble())
val chunk: SimpleChunkLocation
get() = SimpleChunkLocation(x.toInt() shr 4, z.toInt() shr 4)
}
data class SimpleChunkLocation(
val x: Int,
val z: Int
val z: Int,
)
// CONVERTER
fun Location.toSimple() = SimpleLocation3D(x, y, z)
fun Chunk.toSimple() = SimpleChunkLocation(x, z)
fun SimpleLocation3D.withWorld(world: World) = Location(world, x, y, z).apply { direction = this@withWorld.direction }
fun SimpleChunkLocation.withWorld(world: World) = world.getChunkAt(x, z)
fun Vector.toSimpleLoc() = SimpleLocation3D(x, y, z)
fun SimpleLocation3D.toVector() = Vector(x, y, z)

View File

@@ -10,21 +10,17 @@ import kotlin.math.max
import kotlin.math.min
class SimpleLocationPair(loc1: Location, loc2: Location) {
val world = loc1.worldOrException.let {
if (it == loc2.worldOrException) it
else throw IllegalArgumentException("The given locations worlds are not the same!")
}
val minSimpleLoc = SimpleLocation3D(min(loc1.x, loc2.x), min(loc1.y, loc2.y), min(loc1.z, loc2.z))
val maxSimpleLoc = SimpleLocation3D(max(loc1.x, loc2.x), max(loc1.y, loc2.y), max(loc1.z, loc2.z))
fun isInArea(
loc: Location,
check3d: Boolean = true,
tolerance: Int = 0
tolerance: Int = 0,
): Boolean {
// checking world
if (loc.world != world) return false
@@ -37,11 +33,9 @@ class SimpleLocationPair(loc1: Location, loc2: Location) {
// checking y
if (check3d) loc.y >= minSimpleLoc.y - tolerance && loc.y <= maxSimpleLoc.y + tolerance else true
} else false
}
val touchedSimpleChunks: Set<SimpleChunkLocation> by lazy {
val foundChunks = HashSet<SimpleChunkLocation>()
(minSimpleLoc.chunk.x until maxSimpleLoc.chunk.x + 1).forEach { curX ->
@@ -51,13 +45,10 @@ class SimpleLocationPair(loc1: Location, loc2: Location) {
}
return@lazy foundChunks
}
}
class LocationArea(loc1: Location, loc2: Location) {
var loc1: Location = loc1
set(value) {
field = value
@@ -68,19 +59,14 @@ class LocationArea(loc1: Location, loc2: Location) {
field = value
simpleLocationPair = SimpleLocationPair(loc1, value)
}
var simpleLocationPair = SimpleLocationPair(loc1, loc2); private set
val world: World get() = simpleLocationPair.world
val minLoc: Location get() = simpleLocationPair.minSimpleLoc.withWorld(simpleLocationPair.world)
val maxLoc: Location get() = simpleLocationPair.maxSimpleLoc.withWorld(simpleLocationPair.world)
val touchedChunks: Set<Chunk> get() = simpleLocationPair.touchedSimpleChunks.mapTo(HashSet()) { it.withWorld(world) }
fun isInArea(
loc: Location,
check3d: Boolean = true,
tolerance: Int = 0
tolerance: Int = 0,
) = simpleLocationPair.isInArea(loc, check3d, tolerance)
}

View File

@@ -36,15 +36,10 @@ infix fun Location.reduceZ(distance: Number) = subtract(0.0, 0.0, distance)
infix fun Location.reduceXY(distance: Number) = subtract(distance, distance, 0.0)
infix fun Location.reduceYZ(distance: Number) = subtract(0.0, distance, distance)
infix fun Location.reduceXZ(distance: Number) = subtract(distance, 0.0, distance)
// extensions
fun Location.add(x: Number, y: Number, z: Number) = add(x.toDouble(), y.toDouble(), z.toDouble())
fun Location.subtract(x: Number, y: Number, z: Number) = subtract(x.toDouble(), y.toDouble(), z.toDouble())
val Location.blockLoc: Location get() = Location(world, blockX.toDouble(), blockY.toDouble(), blockZ.toDouble())
infix fun Location.relationTo(loc: Location) = this.subtract(loc).toSimple()
// operator functions
@@ -88,11 +83,9 @@ infix fun Location.increase(loc: Location) = add(loc)
infix fun Location.reduce(loc: Location) = subtract(loc)
infix fun Location.increase(loc: SimpleLocation3D) = add(loc.x, loc.y, loc.z)
infix fun Location.reduce(loc: SimpleLocation3D) = subtract(loc.x, loc.y, loc.z)
/*
* VECTOR
*/
val Vector.isFinite: Boolean get() = x.isFinite() && y.isFinite() && z.isFinite()
// fast construct