Compare commits

...

3 Commits

7 changed files with 46 additions and 20 deletions

View File

@ -126,10 +126,10 @@ object SmartInstrumentBlock : Block(
|| CausalityAnchor.checkEffect(level, pos)
) {
val entity = level.getBlockEntity(pos) as SmartInstrumentBlockEntity
if (entity.player == null) {
if (entity.player == null && !level.isClientSide) {
entity.startMusic()
}
return InteractionResult.SUCCESS
return InteractionResult.sidedSuccess(level.isClientSide)
}
return super.use(state, level, pos, player, hand, hit)
}

View File

@ -34,7 +34,7 @@ object Projector {
fun findNearbyProjectors(level: Level, pos: BlockPos) = level.getChunksNearby(pos, currentEffectRadius)
.flatMap {
it.blockEntities.filter { (_, v) -> v is ProjectorBlockEntity }
it.blockEntities.filter { (_, v) -> v is ProjectorBlockEntity && pos in v }
.keys
.filterNotNull()
}

View File

@ -1,6 +1,7 @@
package quaedam.projector
import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerLevel
import net.minecraft.util.RandomSource
import net.minecraft.world.InteractionHand
@ -15,6 +16,8 @@ import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.material.MapColor
import net.minecraft.world.level.material.PushReaction
import net.minecraft.world.phys.BlockHitResult
import quaedam.shell.ProjectionShellItem
import quaedam.utils.sendBlockUpdated
object ProjectorBlock : Block(Properties.of()
.jumpFactor(0.8f)
@ -41,6 +44,21 @@ object ProjectorBlock : Block(Properties.of()
interactionHand: InteractionHand,
blockHitResult: BlockHitResult
): InteractionResult {
if (player.getItemInHand(interactionHand).item == ProjectionShellItem) {
if (!level.isClientSide) {
val entity = level.getBlockEntity(blockPos) as ProjectorBlockEntity
var newRadius = entity.effectRadius + 1
if (newRadius > Projector.currentEffectRadius) {
newRadius = 0
}
entity.updateEffectArea(newRadius)
entity.setChanged()
entity.sendBlockUpdated()
checkUpdate(level, blockPos)
player.sendSystemMessage(Component.translatable("quaedam.projector.radius_updated", newRadius))
}
return InteractionResult.sidedSuccess(level.isClientSide)
}
checkUpdate(level, blockPos)
return InteractionResult.PASS
}

View File

@ -1,6 +1,7 @@
package quaedam.projector
import net.minecraft.core.BlockPos
import net.minecraft.core.SectionPos
import net.minecraft.core.Vec3i
import net.minecraft.nbt.CompoundTag
import net.minecraft.network.protocol.Packet
@ -17,32 +18,23 @@ import quaedam.projection.ProjectionEffect
import quaedam.projection.ProjectionEffectType
import quaedam.projection.ProjectionProvider
import quaedam.utils.sendBlockUpdated
import kotlin.math.max
import kotlin.math.min
class ProjectorBlockEntity(pos: BlockPos, state: BlockState) :
BlockEntity(Projector.blockEntity.get(), pos, state) {
companion object {
const val TAG_EFFECT_RADIUS = "EffectRadius"
const val TAG_PROJECTION_EFFECTS = "ProjectionEffects"
}
val effectAreaChunk by lazy {
val chunk = level!!.getChunk(pos).pos
ChunkPos(chunk.x - Projector.currentEffectRadius, chunk.z - Projector.currentEffectRadius) to
ChunkPos(chunk.x + Projector.currentEffectRadius, chunk.z + Projector.currentEffectRadius)
}
var effectRadius: Int = 0
lateinit var effectArea: BoundingBox
lateinit var effectAreaAABB: AABB
val effectArea: BoundingBox by lazy {
val (minChunk, maxChunk) = effectAreaChunk
val minBlock = BlockPos(minChunk.minBlockX, level!!.minBuildHeight, minChunk.minBlockZ)
val maxBlock = BlockPos(maxChunk.maxBlockX, level!!.maxBuildHeight, maxChunk.maxBlockZ)
BoundingBox.fromCorners(minBlock, maxBlock)
}
val effectAreaAABB by lazy {
val (minChunk, maxChunk) = effectAreaChunk
val minBlock = BlockPos(minChunk.minBlockX, level!!.minBuildHeight, minChunk.minBlockZ)
val maxBlock = BlockPos(maxChunk.maxBlockX, level!!.maxBuildHeight, maxChunk.maxBlockZ)
AABB(minBlock, maxBlock)
init {
updateEffectArea(Projector.currentEffectRadius)
}
val checkArea: BoundingBox by lazy {
@ -57,11 +49,13 @@ class ProjectorBlockEntity(pos: BlockPos, state: BlockState) :
effects.map { (type, effect) ->
effectsTag.put(type.id.toString(), effect.toNbt())
}
tag.putInt(TAG_EFFECT_RADIUS, effectRadius)
tag.put(TAG_PROJECTION_EFFECTS, effectsTag)
}
override fun load(tag: CompoundTag) {
super.load(tag)
updateEffectArea(max(min(tag.getInt(TAG_EFFECT_RADIUS), Projector.currentEffectRadius), 0))
val effectsTag = tag[TAG_PROJECTION_EFFECTS]
val effects = mutableMapOf<ProjectionEffectType<*>, ProjectionEffect>()
if (effectsTag != null && effectsTag is CompoundTag) {
@ -76,6 +70,17 @@ class ProjectorBlockEntity(pos: BlockPos, state: BlockState) :
updateEffects(effects, notify = false)
}
fun updateEffectArea(radius: Int) {
effectRadius = radius
val chunk = ChunkPos(SectionPos.blockToSectionCoord(blockPos.x), SectionPos.blockToSectionCoord(blockPos.z))
val minChunk = ChunkPos(chunk.x - radius, chunk.z - radius)
val maxChunk = ChunkPos(chunk.x + radius, chunk.z + radius)
val minBlock = BlockPos(minChunk.minBlockX, Int.MIN_VALUE, minChunk.minBlockZ)
val maxBlock = BlockPos(maxChunk.maxBlockX, Int.MAX_VALUE, maxChunk.maxBlockZ)
effectArea = BoundingBox.fromCorners(minBlock, maxBlock)
effectAreaAABB = AABB(minBlock, maxBlock)
}
override fun getUpdateTag(): CompoundTag = saveWithoutMetadata()
override fun getUpdatePacket(): Packet<ClientGamePacketListener> = ClientboundBlockEntityDataPacket.create(this)

View File

@ -13,6 +13,7 @@
"item.quaedam.projection_shell": "Projection Shell",
"item.quaedam.iron_copper_metal": "Copper-iron Alloy",
"item.quaedam.projection_metal": "Projection Metal",
"quaedam.projector.radius_updated": "Current effect radius: %s",
"quaedam.screen.projection_shell": "Projection Shell",
"quaedam.screen.projection_shell.lock_revoked": "Timeout! Connection Lost",
"quaedam.screen.projection_shell.lock_failed": "Permission denied!",

View File

@ -13,6 +13,7 @@
"item.quaedam.projection_shell": "投影操作面板",
"item.quaedam.iron_copper_metal": "铜铁合金",
"item.quaedam.projection_metal": "投影金属",
"quaedam.projector.radius_updated": "当前效果半径:%s",
"quaedam.screen.projection_shell": "投影操作",
"quaedam.screen.projection_shell.lock_revoked": "超时!连接丢失",
"quaedam.screen.projection_shell.lock_failed": "正被使用",

View File

@ -13,6 +13,7 @@
"item.quaedam.projection_shell": "投射滥权终端",
"item.quaedam.iron_copper_metal": "有点生锈的铁锭",
"item.quaedam.projection_metal": "投影Metal©",
"quaedam.projector.radius_updated": "现在乱七八糟的效果能碰到的范围:%s",
"quaedam.screen.projection_shell": "控制面板",
"quaedam.screen.projection_shell.lock_revoked": "土豆熟了",
"quaedam.screen.projection_shell.lock_failed": "宁配吗?",