pixlyzer/entities: load default entity attributes

This commit is contained in:
Bixilon 2021-04-23 19:27:55 +02:00
parent c7b8aa4094
commit 11a3b096d6
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 117 additions and 4 deletions

View File

@ -0,0 +1,28 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.entities.attributes
import de.bixilon.minosoft.util.KUtil.asResourceLocation
object DefaultEntityAttributes {
// ToDo
val GENERIC_MAX_HEALTH = "minecraft:generic.max_health".asResourceLocation()
val GENERIC_FOLLOW_RANGE = "minecraft:generic.follow_range".asResourceLocation()
val GENERIC_KNOCKBACK_RESISTANCE = "minecraft:generic.knockback_resistance".asResourceLocation()
val GENERIC_MOVEMENT_SPEED = "minecraft:generic.movement_speed".asResourceLocation()
val GENERIC_ATTACK_KNOCKBACK = "minecraft:generic.attack_knockback".asResourceLocation()
val GENERIC_ARMOR = "minecraft:generic.armor".asResourceLocation()
val GENERIC_ARMOR_TOUGHNESS = "minecraft:generic.armor_toughness".asResourceLocation()
}

View File

@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.entities.entities
import de.bixilon.minosoft.data.entities.EntityMetaDataFields
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.attributes.DefaultEntityAttributes
import de.bixilon.minosoft.data.mappings.entities.EntityType
import de.bixilon.minosoft.data.player.Hands
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
@ -44,7 +45,7 @@ abstract class LivingEntity(connection: PlayConnection, entityType: EntityType,
get() {
val meta = entityMetaData.sets.getFloat(EntityMetaDataFields.LIVING_ENTITY_HEALTH)
return if (meta == Float.MIN_VALUE) {
entityType.maxHealth
entityType.attributes[DefaultEntityAttributes.GENERIC_MAX_HEALTH] ?: 1.0f
} else {
meta
}

View File

@ -24,6 +24,7 @@ import de.bixilon.minosoft.data.mappings.registry.RegistryItem
import de.bixilon.minosoft.data.mappings.registry.ResourceLocationDeserializer
import de.bixilon.minosoft.data.mappings.registry.Translatable
import de.bixilon.minosoft.data.mappings.versions.VersionMapping
import de.bixilon.minosoft.datafixer.EntityAttributeFixer.fix
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import glm_.vec3.Vec3
@ -34,7 +35,7 @@ data class EntityType(
val height: Float,
val sizeFixed: Boolean,
val fireImmune: Boolean,
val maxHealth: Float,
val attributes: Map<ResourceLocation, Float>,
val factory: EntityFactory<out Entity>,
) : RegistryItem, Translatable {
@ -57,6 +58,14 @@ data class EntityType(
return null
}
val attributes: MutableMap<ResourceLocation, Float> = mutableMapOf()
data["attributes"]?.asJsonObject?.let {
for ((attributeResourceLocation, value) in it.entrySet()) {
attributes[ResourceLocation.getPathResourceLocation(attributeResourceLocation).fix()] = value.asFloat
}
}
return EntityType(
resourceLocation = resourceLocation,
translationKey = data["translation_key"]?.asString,
@ -64,7 +73,7 @@ data class EntityType(
height = data["height"].asFloat,
fireImmune = data["fire_immune"]?.asBoolean ?: false,
sizeFixed = data["size_fixed"]?.asBoolean ?: false,
maxHealth = data["minecraft:generic.max_health"]?.asFloat ?: Float.MAX_VALUE,
attributes = attributes.toMap(),
factory = DefaultEntityFactories.getEntityFactory(resourceLocation) ?: error("Can not find entity factory for $resourceLocation"),
)
}

View File

@ -0,0 +1,26 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.datafixer
import de.bixilon.minosoft.data.mappings.ResourceLocation
object DataFixerUtil {
fun Map<String, String>.asResourceLocationMap(): Map<ResourceLocation, ResourceLocation> {
val out: MutableMap<ResourceLocation, ResourceLocation> = mutableMapOf()
for ((key, value) in this) {
out[ResourceLocation.getResourceLocation(key)] = ResourceLocation.getResourceLocation(value)
}
return out.toMap()
}
}

View File

@ -0,0 +1,44 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.datafixer
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.datafixer.DataFixerUtil.asResourceLocationMap
object EntityAttributeFixer {
private val RENAMES: Map<ResourceLocation, ResourceLocation> = mapOf(
"generic.maxHealth" to "generic.max_health",
"zombie.spawnReinforcements" to "zombie.spawn_reinforcements",
"horse.jumpStrength" to "horse.jump_strength",
"generic.followRange" to "generic.follow_range",
"generic.knockbackResistance" to "generic.knockback_resistance",
"generic.movementSpeed" to "generic.movement_speed",
"generic.flyingSpeed" to "generic.flying_speed",
"generic.attackDamage" to "generic.attack_damage",
"generic.attackKnockback" to "generic.attack_knockback",
"generic.attackSpeed" to "generic.attack_speed",
"generic.armorToughness" to "generic.armor_toughness",
).asResourceLocationMap()
fun ResourceLocation.fix(): ResourceLocation {
return RENAMES.getOrDefault(this, this)
}
}

View File

@ -31,7 +31,7 @@ class PlayerAbilitiesS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
init {
val flags = buffer.readUnsignedByte()
if (buffer.versionId < ProtocolVersions.V_14W03B) { // ToDo: Find out correct version
isInvulnerable = flags isBit (0)
isInvulnerable = flags.isBit(0)
isFlying = flags.isBit(1)
canFly = flags.isBit(2)
canInstantBuild = flags.isBit(3)

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.util
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.util.enum.AliasableEnum
import java.util.*
@ -48,4 +49,8 @@ object KUtil {
}
return null
}
fun String.asResourceLocation(): ResourceLocation {
return ResourceLocation(this)
}
}