Skip to content

Commit b402e56

Browse files
add documentation
1 parent ec57ffd commit b402e56

22 files changed

Lines changed: 316 additions & 64 deletions

File tree

src/client/kotlin/com/enginemachiner/harmony/client/Command.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ import com.enginemachiner.harmony.Command
44
import com.enginemachiner.harmony.CommandSetup
55
import com.enginemachiner.harmony.HarmonyArgumentBuilder
66
import com.mojang.brigadier.arguments.ArgumentType
7-
import com.mojang.brigadier.tree.CommandNode
8-
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal
97
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument
8+
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal
109
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
1110

1211
private typealias ClientCommandType = Command<FabricClientCommandSource, ClientCommand>
1312
private typealias Builder = HarmonyArgumentBuilder<FabricClientCommandSource>
1413
private typealias Setup = CommandSetup<ClientCommand>
1514

15+
/**
16+
* Creates and configures a client-side command.
17+
* @param name The name of the command
18+
* @param setup The setup lambda to configure the command
19+
* @return A configured CommandNode for registration
20+
*/
1621
fun clientCommand( name: String, setup: Setup ) = ClientCommand(name).apply(setup).build()
1722

1823
class ClientCommand( builder: Builder ) : ClientCommandType(builder) {

src/client/kotlin/com/enginemachiner/harmony/client/KeyBinding.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class KeybindsManager( mod: Mod ) {
1111

1212
private val id = mod.id
1313

14+
/**
15+
* Creates and configures a key binding.
16+
* @param subKey The key identifier suffix
17+
* @param subCategory The category identifier suffix
18+
* @param code The default key code (default: UNKNOWN)
19+
* @param type The input type (default: KEYSYM)
20+
*/
1421
inner class Binder( subKey: String, subCategory: String, code: Int = Keys.UNKNOWN, type: InputUtil.Type = KEYSYM ) {
1522

1623
val key = "key.$id.$subKey"; val category = "category.$id.$subCategory"
@@ -23,6 +30,7 @@ class KeybindsManager( mod: Mod ) {
2330

2431
}
2532

33+
/** Key code constants for common keyboard keys. */
2634
object Keys {
2735

2836
const val F1 = GLFW.GLFW_KEY_F1

src/client/kotlin/com/enginemachiner/harmony/client/Networking.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ import net.minecraft.network.PacketByteBuf
1212
/** Checks if the client has a network handler. */
1313
fun MinecraftClient.isConnected() = networkHandler != null
1414

15+
/** Manages client-side networking for packet sending and receiving. */
1516
class ClientNetworking( private val mod: Mod ) {
1617

1718
private fun id( path: String ) = mod.id(path)
1819

20+
/**
21+
* Registers a receiver for deserialized packets.
22+
*
23+
* @param path The packet identifier path.
24+
* @param packet The packet instance to deserialize into
25+
* @param handler The handler lambda with deserialized packet context
26+
*/
1927
fun <T: Packet> receive( path: String, packet: T, handler: DeserializedContext<T>.() -> Unit ) {
2028

2129
val id = id(path)
@@ -28,13 +36,19 @@ class ClientNetworking( private val mod: Mod ) {
2836

2937
}
3038

39+
/** Sends a packet to the server. */
3140
fun send( path: String, packet: Packet ) {
3241

3342
val id = id(path); val buf = packet.write(); ClientPlayNetworking.send( id, buf )
3443

3544
}
3645

3746

47+
/**
48+
* Registers a receiver for raw packet buffers.
49+
* @param buf The packet buffer (unused in registration, context provides actual buffer)
50+
* @param handler The handler lambda with raw buffer context
51+
*/
3852
fun receive( path: String, buf: PacketByteBuf, handler: RawContext.() -> Unit ) {
3953

4054
val id = id(path)
@@ -47,6 +61,7 @@ class ClientNetworking( private val mod: Mod ) {
4761

4862
}
4963

64+
/** Sends a raw packet buffer to the server. */
5065
fun send( path: String, buf: PacketByteBuf ) {
5166

5267
val id = id(path); ClientPlayNetworking.send( id, buf )

src/client/kotlin/com/enginemachiner/harmony/client/Renderer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import net.minecraft.item.Item
1111
import net.minecraft.item.ItemConvertible
1212
import java.awt.Color
1313

14+
/** Sets the shader color for rendering. */
1415
fun setShaderColor( color: Color ) {
1516

1617
val color = color.getRGBComponents(null); color.forEachIndexed { i, value -> color[i] = value / 255f }

src/client/kotlin/com/enginemachiner/harmony/client/Screen.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.enginemachiner.harmony.client
22

3-
import com.mojang.blaze3d.systems.RenderSystem
4-
import com.mojang.blaze3d.systems.RenderSystem.setShaderColor
53
import com.mojang.blaze3d.systems.RenderSystem.setShaderTexture
6-
import net.minecraft.client.gui.DrawableHelper
74
import net.minecraft.client.gui.DrawableHelper.drawTexture
8-
import net.minecraft.client.gui.widget.*
5+
import net.minecraft.client.gui.widget.ClickableWidget
96
import net.minecraft.client.util.math.MatrixStack
107
import net.minecraft.text.OrderedText
118
import net.minecraft.text.Text
@@ -27,7 +24,7 @@ interface Positionable {
2724

2825
}
2926

30-
// It trims by default.
27+
/** Wrapper for clickable widgets with automatic text trimming. */
3128
open class Widget( initPos: Vec2f, initSize: Vec2f, message: Text, val widget: ClickableWidget ) : Positionable {
3229

3330
override var pos = initPos; var size = initSize; override fun size() = size
@@ -64,6 +61,7 @@ open class Widget( initPos: Vec2f, initSize: Vec2f, message: Text, val widget: C
6461

6562
}
6663

64+
/** Rendering text with automatic line wrapping. */
6765
abstract class HarmonyText( initPos: Vec2f, initText: Text, maxWidth: Float? = null ) : Positionable {
6866

6967
var text = initText; set(value) { field = value; refresh() }

src/client/kotlin/com/enginemachiner/harmony/client/Tween.kt

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@ import net.minecraft.util.math.MathHelper.lerpAngleDegrees
66
import java.awt.Color
77
import java.awt.Color.RGBtoHSB
88

9+
/**
10+
* Base interface for objects that can be animated over time.
11+
*/
912
interface Tweenable {
1013

11-
fun restart(); fun isDone(): Boolean
14+
/** Restarts the animation from the beginning. */
15+
fun restart()
1216

13-
fun delay( delay: Float ); fun update( delta: Float )
17+
/** Returns true if the animation has completed. */
18+
fun isDone(): Boolean
1419

15-
}
20+
/** Sets a delay in seconds before the animation starts. */
21+
fun delay( delay: Float )
22+
23+
/** Updates the animation by the given delta time in ticks. */
24+
fun update( delta: Float )
1625

17-
// Duration in seconds.
26+
}
1827

28+
/**
29+
* Interpolates a value from [start] to [end] over a specified [duration].
30+
*
31+
* @param start The starting value.
32+
* @param end The ending value.
33+
* @param duration The duration of the tween in seconds.
34+
* @param easing The easing enum to apply to the interpolation.
35+
*/
1936
open class Tween(
2037

2138
private val start: Float, private val end: Float,
@@ -27,14 +44,22 @@ open class Tween(
2744

2845
override fun delay( delay: Float ) { this.delay = delay }
2946

47+
/**
48+
* Returns the current progress of the tween as a value between 0 and 1.
49+
*/
3050
fun progress(): Float {
3151

3252
var t = time - delay; t = t.coerceIn( 0f, 1f ); return t / duration
3353

3454
}
3555

56+
/**
57+
* Interpolates between start and end values using the given progress [t].
58+
* Can be overridden for custom interpolation behavior.
59+
*/
3660
open fun interpolate( t: Float ) = lerp( t, start, end )
3761

62+
/** Returns the current interpolated value with easing applied. */
3863
fun value(): Float {
3964

4065
var t = progress(); t = easing.calculate(t); return interpolate(t)
@@ -47,6 +72,10 @@ open class Tween(
4772

4873
}
4974

75+
/**
76+
* A specialized tween for interpolating angles in degrees.
77+
* Uses angular interpolation to handle wrapping around 360 degrees correctly.
78+
*/
5079
class AngleTween(
5180

5281
private val start: Float, private val end: Float,
@@ -58,14 +87,17 @@ class AngleTween(
5887

5988
}
6089

90+
/**
91+
* A tween for smoothly interpolating between two colors in HSB color space.
92+
* Interpolates hue as an angle for smooth color transitions.
93+
*/
6194
class ColorTween(
6295

6396
start: Color, end: Color,
6497
duration: Float, easing: Easing = Easing.LINEAR
6598

6699
) : Tweenable {
67100

68-
// The hue is in range [0, 1] for RGBtoHSB() and lerpAngleDegrees() will work fine with it.
69101
private val startHSB = RGBtoHSB( start.red, start.green, start.blue, null )
70102
private val endHSB = RGBtoHSB( end.red, end.green, end.blue, null )
71103

@@ -75,6 +107,7 @@ class ColorTween(
75107

76108
private val group = TweenGroup( hue, saturation, brightness )
77109

110+
/** Returns the current interpolated color. */
78111
fun value(): Color {
79112

80113
val hue = hue.value()
@@ -93,10 +126,20 @@ class ColorTween(
93126

94127
}
95128

129+
/**
130+
* Groups multiple tweens to run simultaneously.
131+
* The group is considered done when all tweens are done.
132+
*
133+
* @param tweens The tweens to group together.
134+
*/
96135
class TweenGroup( vararg tweens: Tweenable ) : Tweenable {
97136

98137
private val tweens = tweens.toMutableList()
99138

139+
/**
140+
* Adds additional tweens to this group.
141+
* @return This group for method chaining.
142+
*/
100143
fun add( vararg tween: Tweenable ): TweenGroup { tweens.addAll(tween); return this }
101144

102145
override fun delay( delay: Float ) { tweens.forEach { it.delay(delay) } }
@@ -109,17 +152,29 @@ class TweenGroup( vararg tweens: Tweenable ) : Tweenable {
109152

110153
}
111154

155+
/**
156+
* Runs multiple tweens in sequence, one after another.
157+
* Each tween starts when the previous one completes.
158+
*
159+
* @param tweens The tweens to run in sequence.
160+
*/
112161
class TweenSequence( vararg tweens: Tweenable ) : Tweenable {
113162

163+
/**
164+
* Returns the currently active tween in the sequence.
165+
*/
114166
fun currentTween() = tweens[i]
115167

116168
private var i = 0; private val tweens = tweens.toMutableList()
117169

170+
/**
171+
* Adds additional tweens to run after the current sequence.
172+
* @return This sequence for method chaining.
173+
*/
118174
fun then( vararg tween: Tweenable ): TweenSequence { tweens.addAll(tween); return this }
119175

120176
override fun delay( delay: Float ) { currentTween().delay(delay) }
121177

122-
// Move to next tween when current tween is done.
123178
override fun update( delta: Float ) {
124179

125180
if ( isDone() ) return; val tween = currentTween()

src/main/java/com/enginemachiner/harmony/mixin/PlayerInventoryMixin.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,34 @@
1212
import org.spongepowered.asm.mixin.injection.Inject;
1313
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1414

15+
/**
16+
* Mixin for {@link PlayerInventory} to track item stack changes and notify modded items.
17+
*/
1518
@Mixin( PlayerInventory.class )
1619
public abstract class PlayerInventoryMixin implements Inventory {
1720

21+
/**
22+
* Stores the previous item stack before it's replaced.
23+
* Used to compare old and new stacks when notifying modded items of inventory changes.
24+
*/
1825
@Unique
1926
private ItemStack oldStack = ItemStack.EMPTY;
2027

28+
/**
29+
* Captures the old item stack before it's replaced.
30+
* Injected at the HEAD of {@code setStack} to save the current stack.
31+
*/
2132
@Inject( at = @At("HEAD"), method = "setStack(ILnet/minecraft/item/ItemStack;)V" )
2233
private void harmonySetOldStack( int slot, ItemStack newStack, CallbackInfo callback ) {
2334

2435
oldStack = getStack(slot).copy();
2536

2637
}
2738

39+
/**
40+
* Notifies modded items when their stack changes in the inventory.
41+
* Injected at the TAIL of {@code setStack} after the stack has been replaced.
42+
*/
2843
@Inject( at = @At("TAIL"), method = "setStack(ILnet/minecraft/item/ItemStack;)V" )
2944
private void harmonyOnStackSet( int slot, ItemStack newStack, CallbackInfo callback ) {
3045

src/main/kotlin/com/enginemachiner/harmony/Advancement.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import net.minecraft.text.Text
88
import net.minecraft.util.Identifier
99
import java.util.function.Consumer
1010

11-
/** Advancement builder for custom advancements in Data Generation. */
11+
/**
12+
* Abstract builder to create custom advancements when generating data.
13+
* Provides a structured way to define advancement properties and to build them.
14+
*/
1215
abstract class AdvancementBuilder {
1316

17+
/** Set after calling [build]. */
1418
lateinit var advancement: Advancement
1519

1620
open val parent: AdvancementBuilder? = null
@@ -27,6 +31,10 @@ abstract class AdvancementBuilder {
2731
abstract val criterion: String
2832
abstract val conditions: CriterionConditions
2933

34+
/**
35+
* Hook for additional builder configuration.
36+
* Override to add custom criteria or other builder settings.
37+
*/
3038
open fun configureBuilder( builder: Advancement.Builder ) {}
3139

3240
abstract fun id(): String
@@ -51,8 +59,15 @@ abstract class AdvancementBuilder {
5159

5260
}
5361

62+
/** Advancement builder factory for a specific mod. */
5463
class ModAdvancement( private val mod: Mod ) {
5564

65+
/**
66+
* Base builder for mod-specific advancements.
67+
* Automatically configures title, description, and ID based on the mod and path.
68+
*
69+
* @param path The advancement path within the mod namespace.
70+
*/
5671
abstract inner class Builder( val path: String ) : AdvancementBuilder() {
5772

5873
val translation = mod.translation.advancement

src/main/kotlin/com/enginemachiner/harmony/Chat.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import net.minecraft.text.Text
44
import net.minecraft.text.MutableText
55
import net.minecraft.util.Formatting
66

7+
/** Provides chat message formatting utilities for a mod. */
78
open class Chat( private val mod: Mod ) {
89

10+
/** Creates a formatted title prefix for chat messages. */
911
open fun title(): MutableText {
1012

1113
val name = mod.name.uppercase(); val gray = Formatting.GRAY

src/main/kotlin/com/enginemachiner/harmony/Collection.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ package com.enginemachiner.harmony
77
* @param value The element to start from
88
* @param offset The number of positions to move (positive or negative)
99
* @return The element at the calculated position
10-
* @throws NoSuchElementException if the collection is empty
11-
* @throws NoSuchElementException if the collection doesn't contain the specified [value]
10+
* @throws NoSuchElementException if the collection is empty or the collection doesn't contain the specified [value]
1211
*
1312
*/
1413
fun <T> Collection<T>.cycle( value: T, offset: Int = 1 ): T {

0 commit comments

Comments
 (0)