We are doing little visuals for cargo crates in minecarts
This commit is contained in:
parent
b4b0afa9d5
commit
28f7f3a226
19
src/main/kotlin/ru/dbotthepony/mc/otm/entity/Ext.kt
Normal file
19
src/main/kotlin/ru/dbotthepony/mc/otm/entity/Ext.kt
Normal file
@ -0,0 +1,19 @@
|
||||
package ru.dbotthepony.mc.otm.entity
|
||||
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.world.entity.Entity
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class SynchedEntityDataDelegate<T>(private val type: EntityDataAccessor<T>, private val data: SynchedEntityData) : ReadWriteProperty<Entity, T> {
|
||||
override fun getValue(thisRef: Entity, property: KProperty<*>): T {
|
||||
return data.get(type)
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Entity, property: KProperty<*>, value: T) {
|
||||
data.set(type, value)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> SynchedEntityData.delegate(type: EntityDataAccessor<T>) = SynchedEntityDataDelegate(type, this)
|
@ -1,6 +1,10 @@
|
||||
package ru.dbotthepony.mc.otm.entity
|
||||
|
||||
import net.minecraft.core.NonNullList
|
||||
import net.minecraft.network.syncher.EntityDataAccessor
|
||||
import net.minecraft.network.syncher.EntityDataSerializers
|
||||
import net.minecraft.network.syncher.SynchedEntityData
|
||||
import net.minecraft.sounds.SoundSource
|
||||
import net.minecraft.world.entity.EntityType
|
||||
import net.minecraft.world.entity.player.Inventory
|
||||
import net.minecraft.world.entity.vehicle.AbstractMinecartContainer
|
||||
@ -10,11 +14,14 @@ import net.minecraft.world.item.Item
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.world.level.Level
|
||||
import net.minecraft.world.level.block.state.BlockState
|
||||
import net.minecraft.world.level.gameevent.GameEvent
|
||||
import ru.dbotthepony.mc.otm.block.CargoCrateBlock
|
||||
import ru.dbotthepony.mc.otm.block.entity.CargoCrateBlockEntity
|
||||
import ru.dbotthepony.mc.otm.core.position
|
||||
import ru.dbotthepony.mc.otm.menu.MinecartCargoCrateMenu
|
||||
import ru.dbotthepony.mc.otm.registry.MItems
|
||||
import ru.dbotthepony.mc.otm.registry.MRegistry
|
||||
import ru.dbotthepony.mc.otm.registry.MSoundEvents
|
||||
|
||||
class MinecartCargoCrate(
|
||||
type: EntityType<*>,
|
||||
@ -48,7 +55,7 @@ class MinecartCargoCrate(
|
||||
}
|
||||
|
||||
override fun getDefaultDisplayBlockState(): BlockState {
|
||||
return block.defaultBlockState().setValue(CargoCrateBlock.IS_OPEN, false)
|
||||
return block.defaultBlockState().setValue(CargoCrateBlock.IS_OPEN, interactingPlayers > 0)
|
||||
}
|
||||
|
||||
override fun getDefaultDisplayOffset(): Int {
|
||||
@ -66,4 +73,38 @@ class MinecartCargoCrate(
|
||||
override fun getPickResult(): ItemStack {
|
||||
return ItemStack(item, 1)
|
||||
}
|
||||
|
||||
override fun defineSynchedData() {
|
||||
super.defineSynchedData()
|
||||
entityData.define(INTERACTING_PLAYERS, 0)
|
||||
}
|
||||
|
||||
var interactingPlayers by entityData.delegate(INTERACTING_PLAYERS)
|
||||
|
||||
fun onPlayerOpen() {
|
||||
if (isRemoved)
|
||||
return
|
||||
|
||||
if (interactingPlayers++ == 0) {
|
||||
if (!isRemoved) {
|
||||
level.playSound(null, this, MSoundEvents.CARGO_CRATE_OPEN, SoundSource.BLOCKS, 1f, 0.8f + level.random.nextFloat() * 0.2f)
|
||||
level.gameEvent(GameEvent.CONTAINER_OPEN, position, GameEvent.Context.of(this))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onPlayerClose() {
|
||||
if (interactingPlayers <= 0)
|
||||
return
|
||||
|
||||
if (--interactingPlayers == 0) {
|
||||
if (!isRemoved) {
|
||||
level.gameEvent(GameEvent.CONTAINER_CLOSE, position, GameEvent.Context.of(this))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val INTERACTING_PLAYERS: EntityDataAccessor<Int> = SynchedEntityData.defineId(MinecartCargoCrate::class.java, EntityDataSerializers.INT)
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ class MinecartCargoCrateMenu @JvmOverloads constructor(
|
||||
) : MatteryMenu(MMenus.CARGO_CRATE, p_38852_, inventory) {
|
||||
override val storageSlots: List<MatterySlot>
|
||||
|
||||
private val trackedPlayerOpen = !inventory.player.isSpectator
|
||||
|
||||
init {
|
||||
val container = cart as Container? ?: SimpleContainer(CargoCrateBlockEntity.CAPACITY)
|
||||
|
||||
@ -25,9 +27,21 @@ class MinecartCargoCrateMenu @JvmOverloads constructor(
|
||||
return@ImmutableList slot
|
||||
}
|
||||
|
||||
if (trackedPlayerOpen) {
|
||||
cart?.onPlayerOpen()
|
||||
}
|
||||
|
||||
addInventorySlots()
|
||||
}
|
||||
|
||||
override fun removed(p_38940_: Player) {
|
||||
super.removed(p_38940_)
|
||||
|
||||
if (trackedPlayerOpen) {
|
||||
cart?.onPlayerClose()
|
||||
}
|
||||
}
|
||||
|
||||
override fun stillValid(player: Player): Boolean {
|
||||
return cart?.stillValid(player) ?: true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user