diff --git a/src/main/kotlin/com/bytedice/bde_particles/Bde_particles.kt b/src/main/kotlin/com/bytedice/bde_particles/Bde_particles.kt index 10f0a4f..b0f9601 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/Bde_particles.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/Bde_particles.kt @@ -18,6 +18,7 @@ import net.minecraft.util.ActionResult import net.minecraft.util.Hand import net.minecraft.util.TypedActionResult import net.minecraft.world.World +import org.joml.Vector3f // TODO: finish the particle ticking @@ -77,7 +78,7 @@ fun onRightClick(player: PlayerEntity, world: ServerWorld, hand: Hand) : TypedAc val handItem = player.getStackInHand(hand) val (isEmitterTool, emitterId) = ParticleEmitterTool.getToolDetails(handItem) val hitResult = raycastFromPlayer(player as ServerPlayerEntity, 50.0) - val emitterParams = getParticleEmitterParams(emitterId) + val emitterParams = getEmitterParams(emitterId) if ( !isEmitterTool @@ -94,7 +95,7 @@ fun onRightClick(player: PlayerEntity, world: ServerWorld, hand: Hand) : TypedAc } -fun emitterParamsToJSON(params: ParticleEmitterParams) : Map { +fun emitterParamsToJson(params: ParticleEmitterParams) : Map { val allParticleParamsJSON: MutableList> = mutableListOf() for (particle in params.particleTypes) { @@ -128,4 +129,54 @@ fun emitterParamsToJSON(params: ParticleEmitterParams) : Map { ) return emitterParamsJSON -} \ No newline at end of file +} + + +fun jsonToEmitterParams(json: Map): ParticleEmitterParams { + val particleTypes = json["particleTypes"] as? List<*> + ?: throw IllegalArgumentException("particleTypes must be a list of ParticleParams") + + val allParticleTypes: MutableList = mutableListOf() + + for (particle in particleTypes.iterator()) { + val particleParams = particle as? ParticleParams ?: ParticleParams.DEFAULT + + val updatedParticle = ParticleParams( + shape = (particleParams.shape ?: ParticleParams.DEFAULT.shape), + blockCurve = (json["blockCurve"] as? Array) ?: particleParams.blockCurve, + rotRandom = (json["rotRandom"] as? Pair) ?: particleParams.rotRandom, + rotVelRandom = (json["rotVelRandom"] as? Pair) ?: particleParams.rotVelRandom, + rotVelCurve = (json["rotVelCurve"] as? Array) ?: particleParams.rotVelCurve, + sizeRandom = (json["sizeRandom"] as? Pair) ?: particleParams.sizeRandom, + uniformSize = (json["uniformSize"] as? Boolean) ?: particleParams.uniformSize, + sizeCurve = (json["sizeCurve"] as? Array) ?: particleParams.sizeCurve, + velRandom = (json["velRandom"] as? Pair) ?: particleParams.velRandom, + forceFields = (json["forceFields"] as? Array) ?: particleParams.forceFields, + gravity = (json["gravity"] as? Vector3f) ?: particleParams.gravity, + drag = (json["drag"] as? Float) ?: particleParams.drag, + minVel = (json["minVel"] as? Float) ?: particleParams.minVel, + lifeTime = (json["lifeTime"] as? Pair) ?: particleParams.lifeTime + ) + + allParticleTypes.add(updatedParticle) + } + + val params = ParticleEmitterParams( + maxCount = (json["maxCount"] as? Int) ?: ParticleEmitterParams.DEFAULT.maxCount, + spawnsPerTick = (json["spawnsPerTick"] as? Int) ?: ParticleEmitterParams.DEFAULT.spawnsPerTick, + loopDur = (json["loopDur"] as? Int) ?: ParticleEmitterParams.DEFAULT.loopDur, + loopDelay = (json["loopDelay"] as? Int) ?: ParticleEmitterParams.DEFAULT.loopDelay, + loopCount = (json["loopCount"] as? Int) ?: ParticleEmitterParams.DEFAULT.loopCount, + particleTypes = allParticleTypes.toTypedArray() + ) + + return params +} + + + +fun stringToMap(input: String): Map { + return input.split(",") + .map { it.split("=") } + .associate { it[0].trim() to it[1].trim() } +} diff --git a/src/main/kotlin/com/bytedice/bde_particles/commands/ManageEmitters.kt b/src/main/kotlin/com/bytedice/bde_particles/commands/ManageEmitters.kt index 692caa4..7266fae 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/commands/ManageEmitters.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/commands/ManageEmitters.kt @@ -1,7 +1,9 @@ package com.bytedice.bde_particles.commands -import com.bytedice.bde_particles.emitterParamsToJSON +import com.bytedice.bde_particles.emitterParamsToJson +import com.bytedice.bde_particles.jsonToEmitterParams import com.bytedice.bde_particles.particle.* +import com.bytedice.bde_particles.stringToMap import com.mojang.brigadier.Command import com.mojang.brigadier.CommandDispatcher import com.mojang.brigadier.arguments.StringArgumentType @@ -34,12 +36,12 @@ import java.util.concurrent.CompletableFuture // [emitter id] // output -> removes the particle with that id - // config // TODO + // config // [emitter id] // <[particle index] / EMITTER> // - // single param (if EMITTER, use emitter params instead) + // single param (if EMITTER, use emitter params instead) // TODO // [param key] // [new param value] // output -> update the selected parameter of the selected emitter id to the new value @@ -97,7 +99,7 @@ fun argCreate() : LiteralArgumentBuilder { .executes { context -> val emitterIdVal = StringArgumentType.getString(context, "Emitter ID") val emitterPresetVal = StringArgumentType.getString(context, "Registered Emitter ID") ?: "DEFAULT" - val emitterPresetParams = getParticleEmitterParams(emitterPresetVal) + val emitterPresetParams = getEmitterParams(emitterPresetVal) var newEmitterId = "NULL" val emitterRegisterData: Pair @@ -130,6 +132,7 @@ fun argCreate() : LiteralArgumentBuilder { fun argRemove() : LiteralArgumentBuilder { return CommandManager.literal("remove") .then( + // [emitter id] emitterIdSuggestion .executes { context -> val emitterIdVal = StringArgumentType.getString(context, "Registered Emitter ID") ?: "NULL" @@ -152,13 +155,65 @@ fun argRemove() : LiteralArgumentBuilder { } +// config + // [emitter id] + // <[particle index] / EMITTER> + + // + // single param (if EMITTER, use emitter params instead) + // [param key] + // [new param value] + // output -> update the selected parameter of the selected emitter id to the new value + + // JSON + // [new params] (unspecified values is treated as current) + // output -> parse the JSON and update all params + + fun argConfig() : LiteralArgumentBuilder { return CommandManager.literal("config") - .executes { context -> - val feedback = Text.literal("remove") - context.source.sendFeedback({ feedback }, false) - Command.SINGLE_SUCCESS - } + .then( + // [emitter id] + emitterIdSuggestion + .then( + // <[particle index] / EMITTER> + makeIndexSuggestion() + // + //.then(argSingleParam()) + .then(argJson()) + ) + ) +} + +/* +fun argSingleParam() { + CommandManager.literal("SingleParam") + .then( + // [param key] + ) +} +*/ + +fun argJson() : LiteralArgumentBuilder { + return CommandManager.literal("JSON") + .then( + // [new params] + CommandManager.argument("JSON Value", StringArgumentType.string()) + .executes { context -> + val jsonString = StringArgumentType.getString(context, "JSON Value") + val jsonMap = stringToMap(jsonString) + val jsonParams = jsonToEmitterParams(jsonMap) + + val emitterIdVal = StringArgumentType.getString(context, "Registered Emitter ID") + + updateEmitterParams(emitterIdVal, jsonParams) + + val feedback = Text.literal("BPS - Updated parameters of Emitter ID \"$emitterIdVal\"!\n") + .setStyle(Style.EMPTY.withColor(TextColor.fromRgb(Color(200, 0, 0).rgb))) + + Command.SINGLE_SUCCESS + } + ) } @@ -184,12 +239,13 @@ fun argList() : LiteralArgumentBuilder { fun argCopy() : LiteralArgumentBuilder { return CommandManager.literal("copy") .then( + // [emitter id] emitterIdSuggestion .executes { context -> val emitterIdVal = StringArgumentType.getString(context, "Registered Emitter ID") val commandBlock = makeCommandBlock( - emitterParamsToJSON( - getParticleEmitterParams( + emitterParamsToJson( + getEmitterParams( emitterIdVal )!! ) @@ -236,4 +292,61 @@ fun makeCommandBlock(emitterData: Map): ItemStack { i.set(DataComponentTypes.BLOCK_ENTITY_DATA, component) return i -} \ No newline at end of file +} + + +fun makeIndexSuggestion() : RequiredArgumentBuilder { + return CommandManager.argument("Particle Index", StringArgumentType.string()) + .suggests { context, builder -> + val emitterParams = getEmitterParams(StringArgumentType.getString(context, "Registered Emitter ID"))!! + val suggestionsBuilder = SuggestionsBuilder(builder.remaining, 0) + + emitterParams.particleTypes.indices.forEach { i -> + suggestionsBuilder.suggest(i.toString()) + } + + suggestionsBuilder.suggest("EMITTER") + + CompletableFuture.completedFuture(suggestionsBuilder.build()) + } +} + + +/* +fun paramSuggestion(particleIndex: Int, emitterId: String) : RequiredArgumentBuilder { + val emitterParams = getParticleEmitterParams(emitterId) + + val particleParams = if (emitterParams?.particleTypes!!.isNotEmpty()) { + emitterParams.particleTypes[particleIndex] + } + else { null } + + val emitterProperties = emitterParams::class.memberProperties + val emitterPropertyNames: MutableList = mutableListOf() + val emitterPropertyTypes: MutableList = mutableListOf() + + var particleProperties: Collection>? = null + + if (particleParams != null) { + particleProperties = particleParams::class.memberProperties + } + + if (particleIndex != -1) { + for (property in emitterProperties) { + emitterPropertyNames.add(property.name) + emitterPropertyTypes.add(property.returnType) + } + } + else if (particleProperties != null) { + for (property in particleProperties) {} + } + + // + return CommandManager.argument("Param Key", StringArgumentType.string()) + .suggests { context, builder -> + val suggestionsBuilder = SuggestionsBuilder(builder.remaining, 0) + + CompletableFuture.completedFuture(suggestionsBuilder.build()) + } +} +*/ \ No newline at end of file diff --git a/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleEmitterParams.kt b/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleEmitterParams.kt index 5620b4f..7ac20f9 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleEmitterParams.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleEmitterParams.kt @@ -30,22 +30,7 @@ data class ParticleEmitterParams ( 25, 0, 0, - arrayOf(ParticleParams( - null, - arrayOf("minecraft:shroomlight", "minecraft:orange_concrete", "minecraft:orange_stained_glass", "minecraft:gray_stained_glass", "minecraft:light_gray_stained_glass"), - Pair(Vector3f(0.0f, 0.0f, 0.0f), Vector3f(360.0f, 360.0f, 360.0f)), - Pair(Vector3f(-0.2f, -0.2f, -0.2f), Vector3f(0.2f, 0.2f, 0.2f)), - arrayOf(Vector3f(1.0f, 1.0f, 1.0f), Vector3f(0.0f, 0.0f, 0.0f)), - Pair(Vector3f(0.5f, 0.5f, 0.5f), Vector3f(1.0f, 1.0f, 1.0f)), - true, - arrayOf(Vector3f(1.0f, 1.0f, 1.0f), Vector3f(0.5f, 0.5f, 0.5f)), - Pair(Vector3f(-0.1f, 0.4f, -0.1f), Vector3f(0.1f, 0.8f, 0.1f)), - emptyArray(), - Vector3f(0.0f, -0.01f, 0.0f), - 0.075f, - 0.0f, - Pair(15, 45) - )) + arrayOf(ParticleParams.FIRE_GEYSER) ) } } \ No newline at end of file diff --git a/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleParams.kt b/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleParams.kt index c41540b..183f860 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleParams.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/particle/ParticleParams.kt @@ -35,4 +35,24 @@ data class ParticleParams ( val drag: Float = 0.075f, val minVel: Float = 0.0f, val lifeTime: Pair = Pair(15, 45) -) \ No newline at end of file +) { + companion object Presets { + val DEFAULT = ParticleParams() + val FIRE_GEYSER = ParticleParams( + null, + arrayOf("minecraft:shroomlight", "minecraft:orange_concrete", "minecraft:orange_stained_glass", "minecraft:gray_stained_glass", "minecraft:light_gray_stained_glass"), + Pair(Vector3f(0.0f, 0.0f, 0.0f), Vector3f(360.0f, 360.0f, 360.0f)), + Pair(Vector3f(-0.2f, -0.2f, -0.2f), Vector3f(0.2f, 0.2f, 0.2f)), + arrayOf(Vector3f(1.0f, 1.0f, 1.0f), Vector3f(0.0f, 0.0f, 0.0f)), + Pair(Vector3f(0.5f, 0.5f, 0.5f), Vector3f(1.0f, 1.0f, 1.0f)), + true, + arrayOf(Vector3f(1.0f, 1.0f, 1.0f), Vector3f(0.5f, 0.5f, 0.5f)), + Pair(Vector3f(-0.1f, 0.4f, -0.1f), Vector3f(0.1f, 0.8f, 0.1f)), + emptyArray(), + Vector3f(0.0f, -0.01f, 0.0f), + 0.075f, + 0.0f, + Pair(15, 45) + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/bytedice/bde_particles/particle/particleIdRegister.kt b/src/main/kotlin/com/bytedice/bde_particles/particle/particleIdRegister.kt index 160f188..804710e 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/particle/particleIdRegister.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/particle/particleIdRegister.kt @@ -24,6 +24,11 @@ fun removeFromEmitterRegister(id: String) : Boolean { } -fun getParticleEmitterParams(id: String) : ParticleEmitterParams? { +fun getEmitterParams(id: String) : ParticleEmitterParams? { return emitterIdRegister[id] +} + + +fun updateEmitterParams(id: String, newParams: ParticleEmitterParams) { + emitterIdRegister[id] = newParams } \ No newline at end of file