ItemStack: Replace version with connection

This commit is contained in:
Bixilon 2021-05-23 15:41:19 +02:00
parent 142d55b7fd
commit a106e1c5b6
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
17 changed files with 42 additions and 42 deletions

View File

@ -41,7 +41,7 @@ class ItemStackParser : CommandParser() {
}
return ItemStack(
item = item,
version = connection.version,
connection = connection,
nbt = nbt,
)
}

View File

@ -30,7 +30,7 @@ class CampfireBlockEntity(connection: PlayConnection) : BlockEntity(connection)
val itemStack = ItemStack(
item = connection.registries.itemRegistry[slot["id"]?.nullCast<String>()!!]!!,
version = connection.version,
connection = connection,
count = slot["Count"]?.nullCast<Number>()?.toInt() ?: 1,
)

View File

@ -22,7 +22,7 @@ import glm_.vec3.Vec3
class LargeFireball(connection: PlayConnection, entityType: EntityType, location: Vec3, rotation: EntityRotation) : Fireball(connection, entityType, location, rotation) {
override val defaultItem: ItemStack
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection.version)
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection)
companion object : EntityFactory<LargeFireball> {
private val DEFAULT_ITEM = ResourceLocation("fire_charge")

View File

@ -22,7 +22,7 @@ import glm_.vec3.Vec3
class SmallFireball(connection: PlayConnection, entityType: EntityType, location: Vec3, rotation: EntityRotation) : Fireball(connection, entityType, location, rotation) {
override val defaultItem: ItemStack
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection.version)
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection)
companion object : EntityFactory<SmallFireball> {
private val DEFAULT_ITEM = ResourceLocation("fire_charge")

View File

@ -22,7 +22,7 @@ import glm_.vec3.Vec3
class ThrownEgg(connection: PlayConnection, entityType: EntityType, location: Vec3, rotation: EntityRotation) : ThrowableItemProjectile(connection, entityType, location, rotation) {
override val defaultItem: ItemStack
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection.version)
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection)
companion object : EntityFactory<ThrownEgg> {
private val DEFAULT_ITEM = ResourceLocation("egg")

View File

@ -22,7 +22,7 @@ import glm_.vec3.Vec3
class ThrownEnderPearl(connection: PlayConnection, entityType: EntityType, location: Vec3, rotation: EntityRotation) : ThrowableItemProjectile(connection, entityType, location, rotation) {
override val defaultItem: ItemStack
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection.version)
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection)
companion object : EntityFactory<ThrownEnderPearl> {
private val DEFAULT_ITEM = ResourceLocation("ender_pearl")

View File

@ -22,7 +22,7 @@ import glm_.vec3.Vec3
class ThrownExperienceBottle(connection: PlayConnection, entityType: EntityType, location: Vec3, rotation: EntityRotation) : ThrowableItemProjectile(connection, entityType, location, rotation) {
override val defaultItem: ItemStack
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection.version)
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection)
companion object : EntityFactory<ThrownExperienceBottle> {
private val DEFAULT_ITEM = ResourceLocation("experience_bottle")

View File

@ -30,7 +30,7 @@ class ThrownEyeOfEnder(connection: PlayConnection, entityType: EntityType, locat
get() = entityMetaData.sets.getItemStack(EntityMetaDataFields.THROWN_EYE_OF_ENDER_ITEM) ?: defaultItem
val defaultItem: ItemStack
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection.version)
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection)
companion object : EntityFactory<ThrownEyeOfEnder> {
private val DEFAULT_ITEM = ResourceLocation("ender_eye")

View File

@ -23,7 +23,7 @@ import glm_.vec3.Vec3
class ThrownSnowball(connection: PlayConnection, entityType: EntityType, location: Vec3, rotation: EntityRotation) : ThrowableItemProjectile(connection, entityType, location, rotation) {
override val defaultItem: ItemStack
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection.version)
get() = ItemStack(connection.registries.itemRegistry[DEFAULT_ITEM]!!, connection)
companion object : EntityFactory<ThrownSnowball> {
private val DEFAULT_ITEM = ResourceLocation("snowball")

View File

@ -26,8 +26,8 @@ import de.bixilon.minosoft.data.inventory.ItemNBTValues.UNBREAKABLE_TAG
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.mappings.enchantment.Enchantment
import de.bixilon.minosoft.data.mappings.items.Item
import de.bixilon.minosoft.data.mappings.versions.Version
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.util.BitByte.isBit
import de.bixilon.minosoft.util.KUtil.nullCast
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.compoundCast
@ -36,13 +36,13 @@ import de.bixilon.minosoft.util.nbt.tag.NBTUtil.listCast
data class ItemStack(
val item: Item,
private val version: Version? = null,
private val connection: PlayConnection? = null,
var count: Int = 0,
val enchantments: MutableMap<Enchantment, Int> = mutableMapOf(),
val lore: MutableList<ChatComponent> = mutableListOf(),
var repairCost: Int = 0,
var customDisplayName: ChatComponent? = null,
var isUnbreakable: Boolean = false,
var unbreakable: Boolean = false,
var durability: Int = 0,
val nbt: MutableMap<String, Any> = mutableMapOf(),
) {
@ -61,22 +61,22 @@ data class ItemStack(
nbt.getAndRemove(DISPLAY_TAG)?.compoundCast()?.let {
it.getAndRemove(DISPLAY_MAME_TAG)?.nullCast<String>()?.let { nameTag ->
customDisplayName = ChatComponent.of(nameTag, translator = version?.localeManager)
customDisplayName = ChatComponent.of(nameTag, translator = connection?.version?.localeManager)
}
it.getAndRemove(DISPLAY_LORE_TAG)?.listCast<String>()?.let { loreTag ->
for (lore in loreTag) {
this.lore.add(ChatComponent.of(lore, translator = version?.localeManager))
this.lore.add(ChatComponent.of(lore, translator = connection?.version?.localeManager))
}
}
}
nbt.getAndRemove(UNBREAKABLE_TAG)?.nullCast<Number>()?.let {
isUnbreakable = it.toInt() == 0x01
unbreakable = it.toInt() == 0x01
}
nbt.getAndRemove(UNBREAKABLE_TAG)?.nullCast<Number>()?.let {
isUnbreakable = it.toInt() == 0x01
unbreakable = it.toInt() == 0x01
}
nbt.getAndRemove(HIDE_FLAGS_TAG)?.nullCast<Number>()?.let {
@ -88,10 +88,10 @@ data class ItemStack(
val enchantment = enchantmentTag[ENCHANTMENT_ID_TAG]?.let { enchantmentId ->
when (enchantmentId) {
is Number -> {
version!!.mapping.enchantmentRegistry[enchantmentId.toInt()]
connection!!.registries.enchantmentRegistry[enchantmentId.toInt()]
}
is String -> {
version!!.mapping.enchantmentRegistry[ResourceLocation.getPathResourceLocation(enchantmentId)]
connection!!.registries.enchantmentRegistry[ResourceLocation.getPathResourceLocation(enchantmentId)]
}
else -> TODO()
}
@ -126,7 +126,7 @@ data class ItemStack(
}
}
if (isUnbreakable) {
if (unbreakable) {
nbt[UNBREAKABLE_TAG] = true
}
@ -137,19 +137,19 @@ data class ItemStack(
val enchantmentList: MutableList<Map<String, Any>> = mutableListOf()
for ((enchantment, level) in enchantments) {
val enchantmentTag: MutableMap<String, Any> = mutableMapOf()
if (version!!.isFlattened()) {
if (connection!!.version.isFlattened()) {
enchantmentTag[ENCHANTMENT_ID_TAG] = enchantment.resourceLocation.full
} else {
enchantmentTag[ENCHANTMENT_ID_TAG] = version.mapping.enchantmentRegistry.getId(enchantment)
enchantmentTag[ENCHANTMENT_ID_TAG] = connection.registries.enchantmentRegistry.getId(enchantment)
}
enchantmentTag[ENCHANTMENT_LEVEL_TAG] = if (version.isFlattened()) {
enchantmentTag[ENCHANTMENT_LEVEL_TAG] = if (connection.version.isFlattened()) {
level
} else {
level.toShort()
}
}
if (version!!.isFlattened()) {
if (connection!!.version.isFlattened()) {
nbt[ENCHANTMENT_FLATTENING_TAG] = enchantmentList
} else {
nbt[ENCHANTMENT_PRE_FLATTENING_TAG] = enchantmentList
@ -161,7 +161,7 @@ data class ItemStack(
val displayName: ChatComponent
get() {
customDisplayName?.let { return it }
item.translationKey?.let { version?.localeManager?.translate(it)?.let { translatedName -> return translatedName } }
item.translationKey?.let { connection?.version?.localeManager?.translate(it)?.let { translatedName -> return translatedName } }
return ChatComponent.of(item.toString())
}
@ -206,6 +206,9 @@ data class ItemStack(
}
}
val damageable: Boolean
get() = item.maxDamage > 0 || !unbreakable
companion object {
private const val HIDE_ENCHANTMENT_BIT = 0
private const val HIDE_MODIFIERS_BIT = 1

View File

@ -16,5 +16,6 @@ package de.bixilon.minosoft.data.mappings.enchantment
import de.bixilon.minosoft.util.KUtil.asResourceLocation
object DefaultEnchantments {
val UNBREAKING = "minecraft:unbreaking".asResourceLocation()
val EFFICIENCY = "minecraft:efficiency".asResourceLocation()
}

View File

@ -29,7 +29,7 @@ open class Container(
fun validate() {
for ((slot, itemStack) in slots.toSynchronizedMap()) {
if (itemStack.count <= 0) {
if (itemStack.count <= 0 || itemStack.durability < 0) {
slots.remove(slot)
}
}

View File

@ -37,7 +37,7 @@ data class Version(
) {
var isLoaded = false
var isGettingLoaded = false
val mapping: Registries = Registries()
val registries: Registries = Registries()
lateinit var assetsManager: MinecraftAssetsManager
lateinit var localeManager: MinecraftLocaleManager
@ -92,9 +92,9 @@ data class Version(
if (versionId == ProtocolDefinition.PRE_FLATTENING_VERSION_ID) {
Versions.PRE_FLATTENING_MAPPING = mapping
Versions.PRE_FLATTENING_MAPPING = registries
} else if (!isFlattened()) {
mapping.parentMapping = Versions.PRE_FLATTENING_MAPPING
registries.parentMapping = Versions.PRE_FLATTENING_MAPPING
}
val pixlyzerData = try {
Util.readJsonFromStream(assetsManager.readAssetAsStream(Resources.getPixLyzerDataHashByVersion(this)))
@ -110,7 +110,7 @@ data class Version(
JsonObject()
}
latch.addCount(1)
mapping.load(this, pixlyzerData)
registries.load(this, pixlyzerData)
latch.countDown()
if (pixlyzerData.size() > 0) {
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.INFO) { "Loaded mappings for $this (${versionName} in ${System.currentTimeMillis() - startTime}ms" }
@ -123,9 +123,9 @@ data class Version(
}
fun unload() {
mapping.clear()
if (mapping.parentMapping == mapping) {
mapping.parentMapping = null
registries.clear()
if (registries.parentMapping == registries) {
registries.parentMapping = null
}
isLoaded = false
isGettingLoaded = false

View File

@ -133,7 +133,7 @@ class WorldRenderer(
}
override fun init() {
allBlocks = getAllBlocks(connection.version.mapping)
allBlocks = getAllBlocks(connection.version.registries)
renderWindow.textures.allTextures.addAll(resolveBlockTextureIds(allBlocks!!))

View File

@ -121,14 +121,10 @@ class LeftClickHandler(
}
fun finishDigging() {
// ToDo: Check for acknowledgment
connection.sendPacket(BlockBreakC2SP(BlockBreakC2SP.BreakType.FINISHED_DIGGING, raycastHit.blockPosition, raycastHit.hitDirection))
clearDigging()
connection.world.setBlockState(raycastHit.blockPosition, null)
if (connection.player.entity.gamemode != Gamemodes.CREATIVE) {
// decrease durability
// ToDo
}
}
val canStartBreaking = currentTime - breakSent >= ProtocolDefinition.TICK_TIME

View File

@ -152,7 +152,7 @@ class PlayConnection(
try {
version.load(latch) // ToDo: show gui loader
assetsManager = MultiAssetsManager(version.assetsManager, Minosoft.MINOSOFT_ASSETS_MANAGER, Minosoft.MINECRAFT_FALLBACK_ASSETS_MANAGER)
registries.parentMapping = version.mapping
registries.parentMapping = version.registries
player = Player(account, this)
if (!RenderConstants.DISABLE_RENDERING) {

View File

@ -82,7 +82,7 @@ class PlayInByteBuffer : InByteBuffer {
// ToDo: Replace with dynamic particle type calling
if (this.versionId < V_17W45A) {
return when (type.resourceLocation.full) {
"minecraft:iconcrack" -> ItemParticleData(ItemStack(item = connection.registries.itemRegistry[readVarInt() shl 16 or readVarInt()], connection.version), type)
"minecraft:iconcrack" -> ItemParticleData(ItemStack(item = connection.registries.itemRegistry[readVarInt() shl 16 or readVarInt()], connection), type)
"minecraft:blockcrack", "minecraft:blockdust", "minecraft:falling_dust" -> BlockParticleData(connection.registries.getBlockState(readVarInt() shl 4), type) // ToDo: What about meta data?
else -> ParticleData(type)
}
@ -114,7 +114,7 @@ class PlayInByteBuffer : InByteBuffer {
val nbt = readNBTTag(versionId < V_14W28B)?.compoundCast()
return ItemStack(
item = connection.registries.itemRegistry[id shl 16 or metaData],
version = connection.version,
connection = connection,
count = count,
durability = metaData,
nbt = nbt ?: mutableMapOf(),
@ -123,8 +123,8 @@ class PlayInByteBuffer : InByteBuffer {
return if (readBoolean()) {
ItemStack(
version = connection.version,
item = connection.registries.itemRegistry[readVarInt()],
connection = connection,
count = readUnsignedByte(),
nbt = readNBT()?.compoundCast() ?: mutableMapOf(),
)