made lingering particles disappear (they start lingering when the server saves while particles are alive)

This commit is contained in:
2024-11-23 02:46:38 +01:00
parent 98955f7e5d
commit a9108941d7
5 changed files with 41 additions and 10 deletions
@@ -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<ParticleEmitter> = 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()
@@ -127,3 +146,12 @@ fun emitterParamsToJson(params: EmitterParams) : Map<String, Any> {
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
}
@@ -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()
@@ -230,7 +230,7 @@ class ArgConfigParticleKeys {
}
fun rotVelRandom() : LiteralArgumentBuilder<ServerCommandSource> {
// 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())
@@ -243,6 +243,7 @@ fun argConfig() : LiteralArgumentBuilder<ServerCommandSource> {
.then(particleConfigKeys.shape())
.then(particleConfigKeys.blockCurve())
.then(particleConfigKeys.rotRandom())
.then(particleConfigKeys.rotVelRandom())
)
)
}
@@ -75,7 +75,7 @@ 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)
@@ -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