Skip to content

Commit 9059d23

Browse files
Replace T.builder helper function with T.apply
1 parent e263ba9 commit 9059d23

2 files changed

Lines changed: 18 additions & 33 deletions

File tree

  • modules
    • core/common/src/main/kotlin/com/teamwizardry/librarianlib/core/util/kotlin
    • glitter/common/src/main/kotlin/com/teamwizardry/librarianlib/glitter/modules

modules/core/common/src/main/kotlin/com/teamwizardry/librarianlib/core/util/kotlin/Misc.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ import kotlin.contracts.contract
1212
public fun Identifier.makeTranslationKey(type: String, suffix: String? = null): String
1313
= "$type.$namespace.$path${suffix?.let { ".$it" } ?: ""}"
1414

15-
/**
16-
* Runs a block and then returns `this`.
17-
*
18-
* Technically this is the same as [also], but names matter.
19-
*/
20-
@OptIn(ExperimentalContracts::class)
21-
public inline fun <T> T.builder(block: (T) -> Unit): T {
22-
contract {
23-
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
24-
}
25-
block(this)
26-
return this
27-
}
28-
2915
/**
3016
* Used for cases where code is unreachable, but the language demands a value be returned. For the case where the
3117
* condition should never fail, but theoretically could, use [inconceivable].

modules/glitter/common/src/main/kotlin/com/teamwizardry/librarianlib/glitter/modules/SpriteRenderModule.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.teamwizardry.librarianlib.albedo.base.state.DefaultRenderStates
55
import com.teamwizardry.librarianlib.albedo.buffer.Primitive
66
import com.teamwizardry.librarianlib.albedo.state.RenderState
77
import com.teamwizardry.librarianlib.core.util.Client
8-
import com.teamwizardry.librarianlib.core.util.kotlin.builder
98
import com.teamwizardry.librarianlib.glitter.GlitterLightingCache
109
import com.teamwizardry.librarianlib.glitter.ParticleRenderContext
1110
import com.teamwizardry.librarianlib.glitter.ParticleRenderModule
@@ -298,30 +297,30 @@ public class SpriteRenderModule private constructor(
298297
/**
299298
* The position of the particle last tick, used to interpolate between ticks
300299
*/
301-
public fun previousPosition(value: ReadParticleBinding): Builder = builder {
300+
public fun previousPosition(value: ReadParticleBinding): Builder = apply {
302301
value.require(3)
303302
previousPosition = value
304303
}
305304

306305
/**
307306
* The tint color of the particle
308307
*/
309-
public fun color(value: ReadParticleBinding): Builder = builder {
308+
public fun color(value: ReadParticleBinding): Builder = apply {
310309
value.require(4)
311310
color = value
312311
}
313312

314313
/**
315314
* The tint color of the particle
316315
*/
317-
public fun color(value: Color): Builder = builder {
316+
public fun color(value: Color): Builder = apply {
318317
color = ConstantBinding(value.red / 255.0, value.green / 255.0, value.blue / 255.0, value.alpha / 255.0)
319318
}
320319

321320
/**
322321
* The tint color of the particle
323322
*/
324-
public fun color(red: Double, green: Double, blue: Double, alpha: Double): Builder = builder {
323+
public fun color(red: Double, green: Double, blue: Double, alpha: Double): Builder = apply {
325324
color = ConstantBinding(red, green, blue, alpha)
326325
}
327326

@@ -330,47 +329,47 @@ public class SpriteRenderModule private constructor(
330329
* height, if it's a 2D binding the two axes are used as the width and height. Note that this does not affect UV
331330
* coordinates, so if you set this to non-square and have a square texture it will be distorted.
332331
*/
333-
public fun size(value: ReadParticleBinding): Builder = builder {
332+
public fun size(value: ReadParticleBinding): Builder = apply {
334333
value.require(1, 2)
335334
size = value
336335
}
337336

338337
/**
339338
* The size of the particle in meters.
340339
*/
341-
public fun size(value: Double): Builder = builder {
340+
public fun size(value: Double): Builder = apply {
342341
size = ConstantBinding(value)
343342
}
344343

345344
/**
346345
* The width and height of the particle in meters. Note that this does not affect UV coordinates, so if you set
347346
* this to non-square and have a square texture it will be distorted.
348347
*/
349-
public fun size(width: Double, height: Double): Builder = builder {
348+
public fun size(width: Double, height: Double): Builder = apply {
350349
size = ConstantBinding(width, height)
351350
}
352351

353352
/**
354353
* If present, an artificial facing vector used instead of the player's look vector. This vector _does not need
355354
* to be normalized._
356355
*/
357-
public fun facingVector(value: ReadParticleBinding): Builder = builder {
356+
public fun facingVector(value: ReadParticleBinding): Builder = apply {
358357
value.require(3)
359358
facingVector = value
360359
}
361360

362361
/**
363362
* If present, an artificial direction for the particle's "up" axis. This vector _does not need to be normalized._
364363
*/
365-
public fun upVector(value: ReadParticleBinding): Builder = builder {
364+
public fun upVector(value: ReadParticleBinding): Builder = apply {
366365
value.require(3)
367366
upVector = value
368367
}
369368

370369
/**
371370
* The alpha multiplier for the color. Defaults to 1 if not present.
372371
*/
373-
public fun alphaMultiplier(value: ReadParticleBinding): Builder = builder {
372+
public fun alphaMultiplier(value: ReadParticleBinding): Builder = apply {
374373
value.require(1)
375374
alphaMultiplier = value
376375
}
@@ -381,7 +380,7 @@ public class SpriteRenderModule private constructor(
381380
* @param size The size of the sprite sheet (must be a power of 2)
382381
* @param index A binding for the sprite index (indexed left-to-right, top-to-bottom)
383382
*/
384-
public fun spriteSheet(size: Int, index: ReadParticleBinding): Builder = builder {
383+
public fun spriteSheet(size: Int, index: ReadParticleBinding): Builder = apply {
385384
if (size and (size - 1) != 0) {
386385
throw IllegalArgumentException("Sprite sheet size $size is not a power of 2")
387386
}
@@ -394,7 +393,7 @@ public class SpriteRenderModule private constructor(
394393
* If present, an artificial U/V texture size. When used in combination with sprite sheets, this will be a size
395394
* within the particle's specific sprite.
396395
*/
397-
public fun uvSize(value: ReadParticleBinding): Builder = builder {
396+
public fun uvSize(value: ReadParticleBinding): Builder = apply {
398397
value.require(2)
399398
uvSize = value
400399
}
@@ -403,7 +402,7 @@ public class SpriteRenderModule private constructor(
403402
* If present, an offset to apply to the UV coordinates of the sprite. When used in combination with sprite sheets,
404403
* this will be an offset within the particle's specific sprite.
405404
*/
406-
public fun uvOffset(value: ReadParticleBinding): Builder = builder {
405+
public fun uvOffset(value: ReadParticleBinding): Builder = apply {
407406
value.require(2)
408407
uvOffset = value
409408
}
@@ -455,7 +454,7 @@ public class SpriteRenderOptions private constructor(
455454
private var worldLight: Boolean = false
456455
private var diffuseLight: Boolean = false
457456

458-
public fun addState(state: RenderState.State): Builder = builder {
457+
public fun addState(state: RenderState.State): Builder = apply {
459458
renderState.add(state)
460459
}
461460

@@ -477,30 +476,30 @@ public class SpriteRenderOptions private constructor(
477476
/**
478477
* Whether to blur the texture (i.e. interpolate colors vs. use nearest-neighbor scaling)
479478
*/
480-
public fun blur(value: Boolean): Builder = builder {
479+
public fun blur(value: Boolean): Builder = apply {
481480
blur = value
482481
}
483482

484483
/**
485484
* Whether to enable backface culling (defaults to true)
486485
*/
487-
public fun cull(value: Boolean): Builder = builder {
486+
public fun cull(value: Boolean): Builder = apply {
488487
cull = value
489488
}
490489

491490
/**
492491
* Whether to apply world light (i.e. block and sky light) to the particles. Note: At large scales this may have
493492
* some performance impact.
494493
*/
495-
public fun worldLight(value: Boolean): Builder = builder {
494+
public fun worldLight(value: Boolean): Builder = apply {
496495
worldLight = value
497496
}
498497

499498
/**
500499
* Whether to apply diffuse lighting (e.g. particles appear darker from below) to the particles. Note: At large
501500
* scales this may have some performance impact.
502501
*/
503-
public fun diffuseLight(value: Boolean): Builder = builder {
502+
public fun diffuseLight(value: Boolean): Builder = apply {
504503
diffuseLight = value
505504
}
506505

0 commit comments

Comments
 (0)