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.ServerLifecycleEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
import net.fabricmc.fabric.api.event.player.UseItemCallback 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.entity.player.PlayerEntity
import net.minecraft.item.ItemStack 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.network.ServerPlayerEntity
import net.minecraft.server.world.ServerWorld import net.minecraft.server.world.ServerWorld
import net.minecraft.util.ActionResult import net.minecraft.util.ActionResult
import net.minecraft.util.Hand import net.minecraft.util.Hand
import net.minecraft.util.TypedActionResult import net.minecraft.util.TypedActionResult
import net.minecraft.world.World import net.minecraft.world.World
import org.apache.logging.log4j.core.jmx.Server
import java.util.*
// TODO: finish the particle ticking // 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) // TODO: make custom commands to create particles easier (only temporarily saved)
var ALL_PARTICLE_EMITTERS: Array<ParticleEmitter> = emptyArray() var ALL_PARTICLE_EMITTERS: Array<ParticleEmitter> = emptyArray()
val sessionUuid = UUID.randomUUID()
class Bde_particles : ModInitializer { class Bde_particles : ModInitializer {
@@ -34,8 +41,8 @@ class Bde_particles : ModInitializer {
init() init()
} }
ServerTickEvents.START_SERVER_TICK.register { _ -> ServerTickEvents.START_SERVER_TICK.register { server ->
tick() tick(server)
} }
CommandRegistrationCallback.EVENT.register { dispatcher, registryAccess, _ -> 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) { for (emitter in ALL_PARTICLE_EMITTERS) {
if (emitter.isDead) { if (emitter.isDead) {
ALL_PARTICLE_EMITTERS = ALL_PARTICLE_EMITTERS.toMutableList().apply { remove(emitter) }.toTypedArray() ALL_PARTICLE_EMITTERS = ALL_PARTICLE_EMITTERS.toMutableList().apply { remove(emitter) }.toTypedArray()
@@ -126,4 +145,13 @@ fun emitterParamsToJson(params: EmitterParams) : Map<String, Any> {
) )
return emitterParamsJSON 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 this.hasSpawned = true
} }
fun updateProperties(properties: DisplayEntityProperties) { fun updateProperties(properties: DisplayEntityProperties) {
val nbt = NbtCompound().apply { val nbt = NbtCompound().apply {
val tagList = NbtList() val tagList = NbtList()
properties.tags.forEach { tag -> properties.tags.forEach {
tagList.add(NbtString.of(tag)) tagList.add(NbtString.of(it))
} }
put("Tags", tagList) put("Tags", tagList)
@@ -72,6 +73,7 @@ class DisplayEntity(private var properties: DisplayEntityProperties?) {
this.properties = properties this.properties = properties
} }
fun kill() { fun kill() {
if (entity == null) { println("BPS - Particle Emitter entity is null!"); return } if (entity == null) { println("BPS - Particle Emitter entity is null!"); return }
entity?.kill() entity?.kill()
@@ -230,7 +230,7 @@ class ArgConfigParticleKeys {
} }
fun rotVelRandom() : LiteralArgumentBuilder<ServerCommandSource> { fun rotVelRandom() : LiteralArgumentBuilder<ServerCommandSource> {
// I'd like to introduce you to, drumroll please... *drumroll*... The pyramid of "X1,Y1,Z1,X2,Y2,Z2" // 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("X1", FloatArgumentType.floatArg())
.then(CommandManager.argument("Y1", FloatArgumentType.floatArg()) .then(CommandManager.argument("Y1", FloatArgumentType.floatArg())
.then(CommandManager.argument("Z1", FloatArgumentType.floatArg()) .then(CommandManager.argument("Z1", FloatArgumentType.floatArg())
@@ -243,6 +243,7 @@ fun argConfig() : LiteralArgumentBuilder<ServerCommandSource> {
.then(particleConfigKeys.shape()) .then(particleConfigKeys.shape())
.then(particleConfigKeys.blockCurve()) .then(particleConfigKeys.blockCurve())
.then(particleConfigKeys.rotRandom()) .then(particleConfigKeys.rotRandom())
.then(particleConfigKeys.rotVelRandom())
) )
) )
} }
@@ -75,9 +75,9 @@ class Particle(private val particleParams: ParticleParams) {
translation = newOffset.add(offset), translation = newOffset.add(offset),
leftRotation = quatRot, leftRotation = quatRot,
scale = scale, scale = scale,
tags = arrayOf("BPS_Particle") tags = arrayOf("BPS_Particle", "BPS_UUID", sessionUuid.toString())
) )
blockDisplay = DisplayEntity(properties) blockDisplay = DisplayEntity(properties)
isInit = true isInit = true
@@ -183,7 +183,7 @@ class Particle(private val particleParams: ParticleParams) {
translation = combinedOffset, translation = combinedOffset,
leftRotation = quatRot, leftRotation = quatRot,
scale = newScale, scale = newScale,
tags = arrayOf("BPS_Particle") tags = arrayOf("BPS_Particle", "BPS_UUID", sessionUuid.toString())
) )
return newProperties return newProperties