From a9108941d7818a901155fa5ad3c5e1850c84bba7 Mon Sep 17 00:00:00 2001 From: ByteDice Date: Sat, 23 Nov 2024 02:46:38 +0100 Subject: [PATCH] made lingering particles disappear (they start lingering when the server saves while particles are alive) --- .../bytedice/bde_particles/Bde_particles.kt | 36 ++++++++++++++++--- .../bytedice/bde_particles/DisplayEntity.kt | 6 ++-- .../commands/ManageConfigArgs.kt | 2 +- .../bde_particles/commands/ManageEmitters.kt | 1 + .../particleIdRegister/Particle.kt | 6 ++-- 5 files changed, 41 insertions(+), 10 deletions(-) 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 bdebe5f..295abfa 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/Bde_particles.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/Bde_particles.kt @@ -10,22 +10,29 @@ import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents import net.fabricmc.fabric.api.event.player.UseItemCallback +import net.minecraft.entity.EntityType +import net.minecraft.entity.decoration.DisplayEntity.BlockDisplayEntity import net.minecraft.entity.player.PlayerEntity import net.minecraft.item.ItemStack +import net.minecraft.nbt.NbtCompound +import net.minecraft.nbt.NbtList +import net.minecraft.server.MinecraftServer import net.minecraft.server.network.ServerPlayerEntity import net.minecraft.server.world.ServerWorld import net.minecraft.util.ActionResult import net.minecraft.util.Hand import net.minecraft.util.TypedActionResult import net.minecraft.world.World +import org.apache.logging.log4j.core.jmx.Server +import java.util.* // TODO: finish the particle ticking -// TODO: save particle emitters in world file, or kill them on server restart (so they don't linger forever) // TODO: make custom commands to create particles easier (only temporarily saved) var ALL_PARTICLE_EMITTERS: Array = emptyArray() +val sessionUuid = UUID.randomUUID() class Bde_particles : ModInitializer { @@ -34,8 +41,8 @@ class Bde_particles : ModInitializer { init() } - ServerTickEvents.START_SERVER_TICK.register { _ -> - tick() + ServerTickEvents.START_SERVER_TICK.register { server -> + tick(server) } CommandRegistrationCallback.EVENT.register { dispatcher, registryAccess, _ -> @@ -61,7 +68,19 @@ fun init() { } -fun tick() { +fun tick(server: MinecraftServer) { + for (world in server.worlds) { + for (entity in world.iterateEntities()) { + if (entity.type == EntityType.BLOCK_DISPLAY) { + if (displayEntityContainsTag(entity as BlockDisplayEntity, "BPS_UUID") + && !displayEntityContainsTag(entity as BlockDisplayEntity, sessionUuid.toString())) { + + entity.kill() + } + } + } + } + for (emitter in ALL_PARTICLE_EMITTERS) { if (emitter.isDead) { ALL_PARTICLE_EMITTERS = ALL_PARTICLE_EMITTERS.toMutableList().apply { remove(emitter) }.toTypedArray() @@ -126,4 +145,13 @@ fun emitterParamsToJson(params: EmitterParams) : Map { ) return emitterParamsJSON +} + + +fun displayEntityContainsTag(entity: BlockDisplayEntity, tag: String) : Boolean { + val nbt = NbtCompound() + entity.writeNbt(nbt) + val tagsNbtList = nbt.getList("Tags", 8) + + return tagsNbtList?.any { it.asString() == tag } == true } \ No newline at end of file diff --git a/src/main/kotlin/com/bytedice/bde_particles/DisplayEntity.kt b/src/main/kotlin/com/bytedice/bde_particles/DisplayEntity.kt index c3c8b36..32a227e 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/DisplayEntity.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/DisplayEntity.kt @@ -23,12 +23,13 @@ class DisplayEntity(private var properties: DisplayEntityProperties?) { this.hasSpawned = true } + fun updateProperties(properties: DisplayEntityProperties) { val nbt = NbtCompound().apply { val tagList = NbtList() - properties.tags.forEach { tag -> - tagList.add(NbtString.of(tag)) + properties.tags.forEach { + tagList.add(NbtString.of(it)) } put("Tags", tagList) @@ -72,6 +73,7 @@ class DisplayEntity(private var properties: DisplayEntityProperties?) { this.properties = properties } + fun kill() { if (entity == null) { println("BPS - Particle Emitter entity is null!"); return } entity?.kill() diff --git a/src/main/kotlin/com/bytedice/bde_particles/commands/ManageConfigArgs.kt b/src/main/kotlin/com/bytedice/bde_particles/commands/ManageConfigArgs.kt index 0bfe0f2..2bafae3 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/commands/ManageConfigArgs.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/commands/ManageConfigArgs.kt @@ -230,7 +230,7 @@ class ArgConfigParticleKeys { } fun rotVelRandom() : LiteralArgumentBuilder { // I'd like to introduce you to, drumroll please... *drumroll*... The pyramid of "X1,Y1,Z1,X2,Y2,Z2" - return CommandManager.literal("rotVelRandom") + return CommandManager.literal("rotVelRandom") // TODO: convert this to a reusable variable .then(CommandManager.argument("X1", FloatArgumentType.floatArg()) .then(CommandManager.argument("Y1", FloatArgumentType.floatArg()) .then(CommandManager.argument("Z1", FloatArgumentType.floatArg()) 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 8ef0d0e..f7b77a9 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/commands/ManageEmitters.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/commands/ManageEmitters.kt @@ -243,6 +243,7 @@ fun argConfig() : LiteralArgumentBuilder { .then(particleConfigKeys.shape()) .then(particleConfigKeys.blockCurve()) .then(particleConfigKeys.rotRandom()) + .then(particleConfigKeys.rotVelRandom()) ) ) } diff --git a/src/main/kotlin/com/bytedice/bde_particles/particleIdRegister/Particle.kt b/src/main/kotlin/com/bytedice/bde_particles/particleIdRegister/Particle.kt index 46194d6..835519f 100644 --- a/src/main/kotlin/com/bytedice/bde_particles/particleIdRegister/Particle.kt +++ b/src/main/kotlin/com/bytedice/bde_particles/particleIdRegister/Particle.kt @@ -75,9 +75,9 @@ class Particle(private val particleParams: ParticleParams) { translation = newOffset.add(offset), leftRotation = quatRot, scale = scale, - tags = arrayOf("BPS_Particle") + tags = arrayOf("BPS_Particle", "BPS_UUID", sessionUuid.toString()) ) - + blockDisplay = DisplayEntity(properties) isInit = true @@ -183,7 +183,7 @@ class Particle(private val particleParams: ParticleParams) { translation = combinedOffset, leftRotation = quatRot, scale = newScale, - tags = arrayOf("BPS_Particle") + tags = arrayOf("BPS_Particle", "BPS_UUID", sessionUuid.toString()) ) return newProperties