Skip to content

Commit

Permalink
Refactoring more methods and fix player change health
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ committed Jun 23, 2023
1 parent 26087da commit 7315452
Show file tree
Hide file tree
Showing 37 changed files with 254 additions and 357 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ tasks {

transform(Log4j2PluginsCacheFileTransformer())

// Backwards compatible jar directory
destinationDirectory.set(file("$projectDir/target"))
archiveClassifier.set("")
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/org/distril/beengine/command/impl/TestCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ package org.distril.beengine.command.impl
import org.distril.beengine.command.Command
import org.distril.beengine.command.CommandSender
import org.distril.beengine.command.data.Args
import org.distril.beengine.player.Player

class TestCommand : Command("test", "Test command", "command.test") {
class TestCommand : Command("test", "Test command") {

init {
this.addArguments {
Float("health", true)
}
}

override fun execute(sender: CommandSender, args: Args) {
TODO("This class only used for tests")
val player = sender as Player

player.health = args.getFloat("health")!!

player.sendMessage("Done: " + player.health)
}
}
41 changes: 17 additions & 24 deletions src/main/kotlin/org/distril/beengine/entity/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import com.nukkitx.protocol.bedrock.data.entity.EntityFlag
import com.nukkitx.protocol.bedrock.packet.AddEntityPacket
import com.nukkitx.protocol.bedrock.packet.RemoveEntityPacket
import com.nukkitx.protocol.bedrock.packet.SetEntityDataPacket
import org.distril.beengine.entity.attribute.Attribute
import org.distril.beengine.entity.attribute.EntityAttributes
import org.distril.beengine.entity.data.EntityMetadata
import org.distril.beengine.player.Player
import org.distril.beengine.server.Server
import org.distril.beengine.world.World
import org.distril.beengine.world.chunk.Chunk
import org.distril.beengine.world.util.Location
import java.util.*
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -30,20 +30,20 @@ abstract class Entity(val type: EntityType) : EntityMetadata.Listener {
var yaw = 0f

lateinit var location: Location
var maxHealth = 20f
open var health = 20f
set(value) {
if (field == value) return

if (field < 1) {
if (this.isAlive) this.kill()
} else if (value <= this.maxHealth || value < field) {
field = value
} else {
field = this.maxHealth
}
val attributes = EntityAttributes()

this.metadata.setInt(EntityData.HEALTH, field.toInt())
var maxHealth: Float
get() = this.attributes[Attribute.Type.HEALTH].maxValue
set(value) {
this.attributes[Attribute.Type.HEALTH].maxValue(value)
}

open var health: Float
get() = this.attributes[Attribute.Type.HEALTH].value
set(value) {
val newValue = this.attributes[Attribute.Type.HEALTH].value(value).value
this.metadata.setInt(EntityData.HEALTH, newValue.toInt())
}

var isSpawned = false
Expand Down Expand Up @@ -120,11 +120,9 @@ abstract class Entity(val type: EntityType) : EntityMetadata.Listener {

fun despawnForAll() = this.viewers.forEach { this.despawnFor(it) }

val world: World
get() = this.location.world
val world get() = this.location.world

val chunk: Chunk
get() = this.location.chunk
val chunk get() = this.location.chunk

open var position: Vector3f
get() = this.location.position
Expand All @@ -137,12 +135,7 @@ abstract class Entity(val type: EntityType) : EntityMetadata.Listener {
this.yaw = yaw
}

val isAlive: Boolean
get() = this.health > 0

open fun kill() {
this.health = 0f
}
val isAlive get() = this.health > this.attributes[Attribute.Type.HEALTH].minValue

fun close() {
if (this.isSpawned) {
Expand Down
19 changes: 9 additions & 10 deletions src/main/kotlin/org/distril/beengine/entity/EntityLiving.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@ import org.distril.beengine.server.Server

abstract class EntityLiving(type: EntityType) : Entity(type) {

override var health = super.health
override var health: Float
get() = super.health
set(value) {
val wasAlive = this.isAlive
super.health = value
if (this.isAlive && !wasAlive) {

if (!this.isAlive) {
this.kill()

val packet = EntityEventPacket()
packet.runtimeEntityId = this.id
packet.type = EntityEventType.RESPAWN

Server.broadcastPacket(this.viewers, packet)
}

field = value
}

override fun kill() {
if (this.isAlive) {
super.kill()
fun kill() {
if (!this.isAlive) return

// TODO: drop all items
}
// TODO: drop all items
}
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,53 @@
package org.distril.beengine.player.data.attribute
package org.distril.beengine.entity.attribute

import com.nukkitx.protocol.bedrock.data.AttributeData
import kotlin.math.max
import kotlin.math.min

class Attribute(
val type: Type,
private var minValue: Float,
private var maxValue: Float,
private var defaultValue: Float
minValue: Float,
maxValue: Float,
defaultValue: Float
) {

var minValue: Float = minValue
private set
var maxValue: Float = maxValue
private set
var defaultValue: Float = defaultValue
private set
var value: Float = defaultValue
private set

fun minValue(minValue: Float): Attribute {
var minValue = minValue
if (minValue > this.maxValue) minValue = this.maxValue

this.minValue = minValue
this.minValue = min(this.maxValue, max(this.maxValue, minValue))
return this
}

fun maxValue(maxValue: Float): Attribute {
var maxValue = maxValue
if (maxValue < this.minValue) maxValue = this.minValue
this.maxValue = max(this.minValue, maxValue)

this.maxValue = maxValue
this.defaultValue(this.defaultValue)
this.value(this.value)
return this
}

fun defaultValue(defaultValue: Float): Attribute {
var defaultValue = defaultValue
if (defaultValue > this.maxValue) {
defaultValue = this.maxValue
} else if (defaultValue < this.minValue) {
defaultValue = this.minValue
}

this.value = defaultValue
this.defaultValue = max(this.minValue, min(this.maxValue, defaultValue))
return this
}

fun value(value: Float): Attribute {
var value = value
if (value > this.maxValue) {
value = this.maxValue
} else if (value < this.minValue) {
value = this.minValue
}

this.value = value
this.value = max(this.minValue, min(this.maxValue, value))
return this
}

fun toNetwork() = AttributeData(this.type.networkName, this.minValue, this.maxValue, this.value, this.defaultValue)
fun toNetwork() = AttributeData(this.type.networkId, this.minValue, this.maxValue, this.value, this.defaultValue)

fun clone() = Attribute(this.type, this.minValue, this.maxValue, this.defaultValue).value(this.value)

enum class Type(val networkName: String) {
enum class Type(val networkId: String) {

ABSORPTION("minecraft:absorption"),
SATURATION("minecraft:player.saturation"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.distril.beengine.entity.attribute

import com.nukkitx.protocol.bedrock.packet.UpdateAttributesPacket
import org.distril.beengine.player.Player
import java.util.*

class EntityAttributes {

private val attributes: MutableMap<Attribute.Type, Attribute> = EnumMap(Attribute.Type::class.java)

init {
this.setAttribute(Attribute(Attribute.Type.HEALTH, 0f, 20f, 20f))
this.setAttribute(Attribute(Attribute.Type.MOVEMENT_SPEED, 0f, Float.MAX_VALUE, 0.10f))
}

fun send(player: Player) {
val packet = UpdateAttributesPacket()
packet.runtimeEntityId = player.id
packet.attributes.addAll(this.attributes.values.map { it.toNetwork() })

player.sendPacket(packet)
}

fun setAttribute(attribute: Attribute) {
this.attributes[attribute.type] = attribute
}

operator fun get(type: Attribute.Type) = this.attributes[type]!!
}
22 changes: 10 additions & 12 deletions src/main/kotlin/org/distril/beengine/entity/data/EntityMetadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EntityMetadata(private val listener: Listener) {
private val dataChangeSet = EntityDataMap()

fun update() {
if (!this.dataChangeSet.isEmpty()) {
if (this.dataChangeSet.isNotEmpty()) {
this.listener.onDataChange(this.dataChangeSet)
this.dataChangeSet.clear()
}
Expand All @@ -39,24 +39,22 @@ class EntityMetadata(private val listener: Listener) {
fun getShort(data: EntityData) = this.data.getShort(data)

fun setShort(data: EntityData, value: Int) {
var value = value
value = value.toShort().toInt()
val newValue = value.toShort().toInt()
val oldValue = this.getShort(data)
if (oldValue.toInt() != value) {
this.data.putShort(data, value)
this.dataChangeSet.putShort(data, value)
if (oldValue.toInt() != newValue) {
this.data.putShort(data, newValue)
this.dataChangeSet.putShort(data, newValue)
}
}

fun getByte(data: EntityData) = this.data.getByte(data)

fun setByte(data: EntityData, value: Int) {
var value = value
value = value.toByte().toInt()
val newValue = value.toByte().toInt()
val oldValue = this.getByte(data)
if (oldValue.toInt() != value) {
this.data.putByte(data, value)
dataChangeSet.putByte(data, value)
if (oldValue.toInt() != newValue) {
this.data.putByte(data, newValue)
dataChangeSet.putByte(data, newValue)
}
}

Expand Down Expand Up @@ -142,7 +140,7 @@ class EntityMetadata(private val listener: Listener) {
}
}

interface Listener {
fun interface Listener {

fun onDataChange(dataMap: EntityDataMap)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class Inventory(
}

protected open fun onOpen(player: Player) {
/**/
// functional method
}

fun closeFor(player: Player): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.distril.beengine.inventory.InventoryType
import org.distril.beengine.material.item.Item
import org.distril.beengine.player.Player
import org.distril.beengine.util.ItemUtils
import kotlin.reflect.KProperty

open class CreatureInventory(
holder: InventoryHolder,
Expand All @@ -18,37 +19,10 @@ open class CreatureInventory(

override val holder = super.holder as EntityCreature

var helmet: Item? = null
get() = ItemUtils.getAirIfNull(field)
set(value) {
field = value

this.sendArmor()
}

var chestplate: Item? = null
get() = ItemUtils.getAirIfNull(field)
set(value) {
field = value

this.sendArmor()
}

var leggings: Item? = null
get() = ItemUtils.getAirIfNull(field)
set(value) {
field = value

this.sendArmor()
}

var boots: Item? = null
get() = ItemUtils.getAirIfNull(field)
set(value) {
field = value

this.sendArmor()
}
var helmet: Item? by ArmorDelegate()
var chestplate: Item? by ArmorDelegate()
var leggings: Item? by ArmorDelegate()
var boots: Item? by ArmorDelegate()

var heldItemIndex: Int = 0
set(value) {
Expand Down Expand Up @@ -110,10 +84,10 @@ open class CreatureInventory(
protected fun sendArmor() {
val packet = MobArmorEquipmentPacket()
packet.runtimeEntityId = this.holder.id
packet.helmet = ItemUtils.toNetwork(helmet!!)
packet.chestplate = ItemUtils.toNetwork(chestplate!!)
packet.leggings = ItemUtils.toNetwork(leggings!!)
packet.boots = ItemUtils.toNetwork(boots!!)
packet.helmet = ItemUtils.toNetwork(helmet)
packet.chestplate = ItemUtils.toNetwork(chestplate)
packet.leggings = ItemUtils.toNetwork(leggings)
packet.boots = ItemUtils.toNetwork(boots)

this.holder.viewers.forEach { it.sendPacket(packet) }
}
Expand All @@ -122,4 +96,17 @@ open class CreatureInventory(

private const val HOTBAR_SIZE = 9
}

inner class ArmorDelegate {

private var field: Item? = null

operator fun getValue(thisRef: Any?, property: KProperty<*>) = ItemUtils.getAirIfNull(this.field)

operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Item?) {
this.field = value

sendArmor()
}
}
}
Loading

0 comments on commit 7315452

Please sign in to comment.