mirror of
https://github.com/ViaVersion/VIAaaS.git
synced 2025-01-24 22:01:49 +01:00
1.7 protocol cleanup, try to fix metadata
This commit is contained in:
parent
72a166e2be
commit
68e790f06c
@ -9,42 +9,38 @@ import de.gerrygames.viarewind.protocol.protocol1_8to1_7_6_10.metadata.MetaIndex
|
||||
object MetadataRewriter {
|
||||
fun transform(type: Entity1_10Types.EntityType?, list: MutableList<Metadata>) {
|
||||
for (entry in ArrayList(list)) {
|
||||
val metaIndex = MetaIndex1_8to1_7_6_10.searchIndex(type, entry.id())
|
||||
try {
|
||||
if (metaIndex == null) throw Exception("Could not find valid metadata")
|
||||
if (metaIndex.newType == MetaType1_8.NonExistent || metaIndex.newType == null) {
|
||||
list.remove(entry)
|
||||
return
|
||||
}
|
||||
val value = entry.value
|
||||
if (value == null
|
||||
|| !value.javaClass.isAssignableFrom(metaIndex.oldType.type().outputClass)) {
|
||||
val metaIndex = MetaIndex1_8to1_7_6_10.searchIndex(type, entry.id())
|
||||
|
||||
if (metaIndex == null || metaIndex.newType == MetaType1_8.NonExistent) {
|
||||
list.remove(entry)
|
||||
return
|
||||
continue
|
||||
}
|
||||
|
||||
entry.setMetaType(metaIndex.newType)
|
||||
entry.setId(metaIndex.newIndex)
|
||||
when (metaIndex.newType) {
|
||||
MetaType1_8.Int -> entry.value = (value as Number).toInt()
|
||||
MetaType1_8.Byte -> {
|
||||
var byteValue = (value as Number).toByte()
|
||||
entry.value = byteValue
|
||||
|
||||
if (metaIndex == MetaIndex1_8to1_7_6_10.HUMAN_SKIN_FLAGS) {
|
||||
val cape = byteValue.toInt() == 2
|
||||
byteValue = (if (cape) 127 else 125).toByte()
|
||||
entry.value = byteValue
|
||||
}
|
||||
entry.value = byteValue
|
||||
}
|
||||
MetaType1_8.Int -> entry.value = (value as Number).toInt()
|
||||
MetaType1_8.Short -> entry.value = (value as Number).toShort()
|
||||
MetaType1_8.String -> entry.value = value.toString()
|
||||
MetaType1_8.Float -> entry.value = (value as Number).toFloat()
|
||||
MetaType1_8.String -> entry.value = value.toString()
|
||||
MetaType1_8.Slot, MetaType1_8.Position, MetaType1_8.Rotation -> entry.value = value
|
||||
else -> {
|
||||
Via.getPlatform().logger.warning("[Out] Unhandled MetaDataType: " + metaIndex.newType)
|
||||
list.remove(entry)
|
||||
}
|
||||
else -> throw Exception("unknown metatype ${metaIndex.newType}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
if (!Via.getPlatform().conf.isSuppressMetadataErrors) {
|
||||
Via.getPlatform().logger.warning("Metadata Exception: $e $list")
|
||||
}
|
||||
list.remove(entry)
|
||||
}
|
||||
}
|
||||
|
@ -59,14 +59,14 @@ fun Protocol1_8To1_7_6.registerEntityPackets() {
|
||||
|
||||
this.registerClientbound(ClientboundPackets1_7.SPAWN_ENTITY, object : PacketRemapper() {
|
||||
override fun registerMap() {
|
||||
map(Type.VAR_INT)
|
||||
map(Type.BYTE)
|
||||
map(Type.INT)
|
||||
map(Type.INT)
|
||||
map(Type.INT)
|
||||
map(Type.BYTE)
|
||||
map(Type.BYTE)
|
||||
map(Type.INT)
|
||||
map(Type.VAR_INT) // id
|
||||
map(Type.BYTE) // type
|
||||
map(Type.INT) // x
|
||||
map(Type.INT) // y
|
||||
map(Type.INT) // z
|
||||
map(Type.BYTE) // pitch
|
||||
map(Type.BYTE) // yaw
|
||||
map(Type.INT) // data
|
||||
handler { packetWrapper ->
|
||||
val type = packetWrapper.get(Type.BYTE, 0)
|
||||
var x = packetWrapper.get(Type.INT, 0)
|
||||
@ -107,12 +107,12 @@ fun Protocol1_8To1_7_6.registerEntityPackets() {
|
||||
packetWrapper.set(Type.INT, 3, data)
|
||||
}
|
||||
handler { packetWrapper ->
|
||||
val entityID: Int = packetWrapper.get(Type.VAR_INT, 0)
|
||||
val entityID = packetWrapper.get(Type.VAR_INT, 0)
|
||||
val typeID = packetWrapper.get(Type.BYTE, 0).toInt()
|
||||
val tracker = packetWrapper.user().get(EntityTracker::class.java)!!
|
||||
val type: Entity1_10Types.EntityType = Entity1_10Types.getTypeFromId(typeID, true)
|
||||
tracker.clientEntityTypes[entityID] = type
|
||||
tracker.sendMetadataBuffer(entityID)
|
||||
tracker.addEntity(entityID, type)
|
||||
tracker.flushMetadataBuffer(entityID)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -135,18 +135,11 @@ fun Protocol1_8To1_7_6.registerEntityPackets() {
|
||||
val entityID: Int = packetWrapper.get(Type.VAR_INT, 0)
|
||||
val typeID = packetWrapper.get(Type.UNSIGNED_BYTE, 0).toInt()
|
||||
val tracker = packetWrapper.user().get(EntityTracker::class.java)!!
|
||||
tracker.clientEntityTypes.put(entityID, Entity1_10Types.getTypeFromId(typeID, false))
|
||||
tracker.sendMetadataBuffer(entityID)
|
||||
}
|
||||
handler { wrapper ->
|
||||
val metadataList = wrapper.get(Types1_8.METADATA_LIST, 0)
|
||||
val entityID: Int = wrapper.get(Type.VAR_INT, 0)
|
||||
val tracker = wrapper.user().get(EntityTracker::class.java)!!
|
||||
if (tracker.clientEntityTypes.containsKey(entityID)) {
|
||||
MetadataRewriter.transform(tracker.clientEntityTypes[entityID], metadataList)
|
||||
} else {
|
||||
wrapper.cancel()
|
||||
}
|
||||
tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, false))
|
||||
tracker.flushMetadataBuffer(entityID)
|
||||
|
||||
val metadataList = packetWrapper.get(Types1_8.METADATA_LIST, 0)
|
||||
MetadataRewriter.transform(tracker.getType(entityID), metadataList)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -160,8 +153,8 @@ fun Protocol1_8To1_7_6.registerEntityPackets() {
|
||||
handler { packetWrapper ->
|
||||
val entityID = packetWrapper.get(Type.VAR_INT, 0)
|
||||
val tracker = packetWrapper.user().get(EntityTracker::class.java)!!
|
||||
tracker.clientEntityTypes.put(entityID, Entity1_10Types.EntityType.PAINTING)
|
||||
tracker.sendMetadataBuffer(entityID)
|
||||
tracker.addEntity(entityID, Entity1_10Types.EntityType.PAINTING)
|
||||
tracker.flushMetadataBuffer(entityID)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -176,8 +169,8 @@ fun Protocol1_8To1_7_6.registerEntityPackets() {
|
||||
handler { packetWrapper ->
|
||||
val entityID: Int = packetWrapper.get(Type.VAR_INT, 0)
|
||||
val tracker = packetWrapper.user().get(EntityTracker::class.java)!!
|
||||
tracker.clientEntityTypes.put(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB)
|
||||
tracker.sendMetadataBuffer(entityID)
|
||||
tracker.addEntity(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB)
|
||||
tracker.flushMetadataBuffer(entityID)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -270,8 +263,8 @@ fun Protocol1_8To1_7_6.registerEntityPackets() {
|
||||
val metadataList = wrapper.get(Types1_8.METADATA_LIST, 0)
|
||||
val entityID: Int = wrapper.get(Type.VAR_INT, 0)
|
||||
val tracker = wrapper.user().get(EntityTracker::class.java)!!
|
||||
if (tracker.clientEntityTypes.containsKey(entityID)) {
|
||||
MetadataRewriter.transform(tracker.clientEntityTypes[entityID], metadataList)
|
||||
if (tracker.hasEntity(entityID)) {
|
||||
MetadataRewriter.transform(tracker.getType(entityID), metadataList)
|
||||
if (metadataList.isEmpty()) wrapper.cancel()
|
||||
} else {
|
||||
tracker.addMetadataToBuffer(entityID, metadataList)
|
||||
@ -330,8 +323,8 @@ fun Protocol1_8To1_7_6.registerEntityPackets() {
|
||||
handler { packetWrapper ->
|
||||
val entityID: Int = packetWrapper.get(Type.VAR_INT, 0)
|
||||
val tracker = packetWrapper.user().get(EntityTracker::class.java)!!
|
||||
tracker.clientEntityTypes[entityID] = Entity1_10Types.EntityType.LIGHTNING
|
||||
tracker.sendMetadataBuffer(entityID)
|
||||
tracker.addEntity(entityID, Entity1_10Types.EntityType.LIGHTNING)
|
||||
tracker.flushMetadataBuffer(entityID)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -182,8 +182,8 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() {
|
||||
handler { packetWrapper ->
|
||||
val entityID: Int = packetWrapper.get(Type.VAR_INT, 0)
|
||||
val tracker: EntityTracker = packetWrapper.user().get(EntityTracker::class.java)!!
|
||||
tracker.clientEntityTypes[entityID] = Entity1_10Types.EntityType.PLAYER
|
||||
tracker.sendMetadataBuffer(entityID)
|
||||
tracker.addEntity(entityID, Entity1_10Types.EntityType.PLAYER)
|
||||
tracker.flushMetadataBuffer(entityID)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -375,29 +375,28 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() {
|
||||
map(Type.STRING)
|
||||
handler { packetWrapper ->
|
||||
val channel = packetWrapper.get(Type.STRING, 0)
|
||||
if (channel.equals("MC|ItemName", ignoreCase = true)) {
|
||||
val name: ByteArray = packetWrapper.read(Type.STRING).toByteArray(Charsets.UTF_8)
|
||||
packetWrapper.write(Type.REMAINING_BYTES, name)
|
||||
} else if (channel.equals("MC|BEdit", ignoreCase = true) || channel.equals(
|
||||
"MC|BSign",
|
||||
ignoreCase = true
|
||||
)
|
||||
) {
|
||||
packetWrapper.read(Type.SHORT) //length
|
||||
val book: Item = packetWrapper.read(Types1_7_6_10.COMPRESSED_NBT_ITEM)
|
||||
val tag = book.tag()
|
||||
if (tag != null && tag.contains("pages")) {
|
||||
val pages = tag.get<ListTag>("pages")
|
||||
if (pages != null) {
|
||||
(0 until pages.size()).forEach { i ->
|
||||
val page = pages.get<StringTag>(i)
|
||||
var value: String? = page.value
|
||||
value = ChatUtil.jsonToLegacy(value)
|
||||
page.value = value
|
||||
when (channel) {
|
||||
"MC|ItemName" -> {
|
||||
val name: ByteArray = packetWrapper.read(Type.STRING).toByteArray(Charsets.UTF_8)
|
||||
packetWrapper.write(Type.REMAINING_BYTES, name)
|
||||
}
|
||||
"MC|BEdit", "MC|BSign" ->{
|
||||
packetWrapper.read(Type.SHORT) //length
|
||||
val book: Item = packetWrapper.read(Types1_7_6_10.COMPRESSED_NBT_ITEM)
|
||||
val tag = book.tag()
|
||||
if (tag != null && tag.contains("pages")) {
|
||||
val pages = tag.get<ListTag>("pages")
|
||||
if (pages != null) {
|
||||
(0 until pages.size()).forEach { i ->
|
||||
val page = pages.get<StringTag>(i)
|
||||
var value: String? = page.value
|
||||
value = ChatUtil.jsonToLegacy(value)
|
||||
page.value = value
|
||||
}
|
||||
}
|
||||
}
|
||||
packetWrapper.write(Type.ITEM, book)
|
||||
}
|
||||
packetWrapper.write(Type.ITEM, book)
|
||||
}
|
||||
packetWrapper.cancel()
|
||||
packetWrapper.packetType = null
|
||||
|
@ -197,7 +197,7 @@ fun Protocol1_8To1_7_6.registerWorldPackets() {
|
||||
|
||||
this.registerServerbound(ServerboundPackets1_8.PLAYER_DIGGING, object : PacketRemapper() {
|
||||
override fun registerMap() {
|
||||
map(Type.UNSIGNED_BYTE, Type.BYTE) //Status
|
||||
map(Type.VAR_INT) //Status
|
||||
map(TypeRemapper(Type.POSITION), xyzUBytePosWriter)
|
||||
map(Type.BYTE) //Face
|
||||
}
|
||||
|
@ -10,32 +10,39 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper
|
||||
import com.viaversion.viaversion.api.type.Type
|
||||
import com.viaversion.viaversion.api.type.types.version.Types1_8
|
||||
import de.gerrygames.viarewind.protocol.protocol1_7_6_10to1_8.ClientboundPackets1_7
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class EntityTracker(user: UserConnection) : StoredObject(user) {
|
||||
val clientEntityTypes = ConcurrentHashMap<Int, Entity1_10Types.EntityType>()
|
||||
private val metadataBuffer = ConcurrentHashMap<Int, MutableList<Metadata>>()
|
||||
private val clientEntityTypes = HashMap<Int, Entity1_10Types.EntityType>()
|
||||
private val metadataBuffer = HashMap<Int, MutableList<Metadata>>()
|
||||
fun removeEntity(entityId: Int) {
|
||||
clientEntityTypes.remove(entityId)
|
||||
metadataBuffer.remove(entityId)
|
||||
}
|
||||
|
||||
fun getType(entityId: Int) = clientEntityTypes[entityId]
|
||||
|
||||
fun addEntity(entityId: Int, type: Entity1_10Types.EntityType) {
|
||||
clientEntityTypes[entityId] = type
|
||||
}
|
||||
|
||||
fun hasEntity(entityId: Int) = clientEntityTypes.containsKey(entityId)
|
||||
|
||||
fun addMetadataToBuffer(entityID: Int, metadataList: MutableList<Metadata>) {
|
||||
metadataBuffer.computeIfAbsent(entityID) { mutableListOf() }.addAll(metadataList)
|
||||
}
|
||||
|
||||
fun getBufferedMetadata(entityId: Int): List<Metadata> {
|
||||
return metadataBuffer[entityId]!!
|
||||
}
|
||||
fun flushMetadataBuffer(entityId: Int) {
|
||||
val buffer = metadataBuffer[entityId] ?: return
|
||||
|
||||
fun sendMetadataBuffer(entityId: Int) {
|
||||
if (!metadataBuffer.containsKey(entityId)) return
|
||||
val wrapper = PacketWrapper.create(ClientboundPackets1_7.ENTITY_METADATA, null, this.user)
|
||||
wrapper.write(Type.VAR_INT, entityId)
|
||||
wrapper.write(Types1_8.METADATA_LIST, metadataBuffer[entityId])
|
||||
MetadataRewriter.transform(clientEntityTypes[entityId], metadataBuffer[entityId]!!)
|
||||
if (metadataBuffer[entityId]!!.isNotEmpty()) {
|
||||
wrapper.write(Types1_8.METADATA_LIST, buffer)
|
||||
|
||||
MetadataRewriter.transform(clientEntityTypes[entityId], buffer)
|
||||
|
||||
if (buffer.isNotEmpty()) {
|
||||
try {
|
||||
wrapper.send(Protocol1_8To1_7_6::class.java)
|
||||
wrapper.scheduleSend(Protocol1_8To1_7_6::class.java)
|
||||
} catch (ex: Exception) {
|
||||
ex.printStackTrace()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user