feat: impl ProjectionEffect events

This commit is contained in:
xtex 2023-07-01 11:23:57 +08:00
parent 5b22f9087b
commit c1e090d59d
Signed by: xtex
GPG Key ID: B918086ED8045B91
4 changed files with 22 additions and 12 deletions

View File

@ -7,7 +7,7 @@ import net.minecraft.nbt.CompoundTag
import net.minecraft.resources.ResourceKey import net.minecraft.resources.ResourceKey
import net.minecraft.resources.ResourceLocation import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerLevel import net.minecraft.server.level.ServerLevel
import net.minecraft.util.RandomSource import net.minecraft.world.level.Level
import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.level.block.state.BlockState
abstract class ProjectionEffect { abstract class ProjectionEffect {
@ -24,14 +24,13 @@ abstract class ProjectionEffect {
override fun hashCode() = type.hashCode() override fun hashCode() = type.hashCode()
fun activated(level: ServerLevel, projectorPos: BlockPos) { open fun activate(level: Level, pos: BlockPos) {}
}
fun deactivated(level: ServerLevel, projectorPos: BlockPos) { open fun deactivate(level: Level, pos: BlockPos) {}
}
fun randomTick(level: ServerLevel, projectorPos: BlockPos, random: RandomSource) { open fun update(level: Level, pos: BlockPos, old: ProjectionEffect) {}
}
open fun randomTick(level: ServerLevel, pos: BlockPos) {}
} }
@ -45,7 +44,7 @@ data class ProjectionEffectType<T : ProjectionEffect>(val constructor: () -> T)
} }
val id by lazy { registry.getResourceKey(this).get().location()!! } val id: ResourceLocation by lazy { registry.getResourceKey(this).get().location() }
} }

View File

@ -1,6 +1,8 @@
package quaedam.projection.swarm package quaedam.projection.swarm
import net.minecraft.core.BlockPos
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.server.level.ServerLevel
import quaedam.projection.ProjectionEffect import quaedam.projection.ProjectionEffect
data class SwarmProjectionEffect( data class SwarmProjectionEffect(
@ -30,4 +32,7 @@ data class SwarmProjectionEffect(
withVillager = tag.getBoolean(TAG_WITH_VILLAGER) withVillager = tag.getBoolean(TAG_WITH_VILLAGER)
} }
override fun randomTick(level: ServerLevel, pos: BlockPos) {
}
} }

View File

@ -9,13 +9,11 @@ import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.player.Player import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.Level import net.minecraft.world.level.Level
import net.minecraft.world.level.LevelAccessor
import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.EntityBlock import net.minecraft.world.level.block.EntityBlock
import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.material.MapColor import net.minecraft.world.level.material.MapColor
import net.minecraft.world.phys.BlockHitResult import net.minecraft.world.phys.BlockHitResult
import quaedam.projection.ProjectionBlock
object ProjectorBlock : Block(Properties.of() object ProjectorBlock : Block(Properties.of()
.jumpFactor(0.8f) .jumpFactor(0.8f)
@ -53,8 +51,8 @@ object ProjectorBlock : Block(Properties.of()
pos: BlockPos, pos: BlockPos,
random: RandomSource random: RandomSource
) { ) {
// @TODO: call projectorRandomTick
checkUpdate(level, pos) checkUpdate(level, pos)
(level.getBlockEntity(pos) as ProjectorBlockEntity).effects.values.forEach { it.randomTick(level, pos) }
} }
@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION") @Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")

View File

@ -88,10 +88,18 @@ class ProjectorBlockEntity(pos: BlockPos, state: BlockState) :
fun updateEffects(effects: Map<ProjectionEffectType<*>, ProjectionEffect>, notify: Boolean = true) { fun updateEffects(effects: Map<ProjectionEffectType<*>, ProjectionEffect>, notify: Boolean = true) {
if (effects != this.effects) { if (effects != this.effects) {
val oldEffects = this.effects
val level = level!!
this.effects = effects this.effects = effects
if (!level!!.isClientSide) { if (!level.isClientSide) {
sendBlockUpdated() sendBlockUpdated()
} }
val addedEffects = effects.filterKeys { it !in oldEffects }
val removedEffects = oldEffects.filterKeys { it !in effects }
val updatedEffects = effects.filter { (k, v) -> oldEffects[k] != v }
addedEffects.values.forEach { it.activate(level, blockPos) }
removedEffects.values.forEach { it.deactivate(level, blockPos) }
updatedEffects.forEach { (k, v) -> v.update(level, blockPos, oldEffects[k]!!) }
} }
} }