(Hopefully) massively optimized display (PacketWindowItems) with DisplayFrame.kt

This commit is contained in:
Auxilor
2021-10-07 14:34:40 +01:00
parent 63d7bdab6b
commit 78755b0e9a
4 changed files with 71 additions and 6 deletions

View File

@@ -150,6 +150,18 @@ class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemS
handle.repairCost = cost
}
override fun equals(other: Any?): Boolean {
if (other !is NMSFastItemStack) {
return false
}
return other.hashCode() == this.hashCode()
}
override fun hashCode(): Int {
return handle.tag.hashCode() * 31 + Item.getId(handle.item)
}
private fun apply() {
if (bukkit !is CraftItemStack) {
bukkit.itemMeta = CraftItemStack.asCraftMirror(handle).itemMeta

View File

@@ -6,6 +6,7 @@ import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag
import net.minecraft.nbt.StringTag
import net.minecraft.world.item.EnchantedBookItem
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack
@@ -170,6 +171,18 @@ class NMSFastItemStack(itemStack: org.bukkit.inventory.ItemStack) : EcoFastItemS
handle.setRepairCost(cost)
}
override fun equals(other: Any?): Boolean {
if (other !is NMSFastItemStack) {
return false
}
return other.hashCode() == this.hashCode()
}
override fun hashCode(): Int {
return handle.tag.hashCode() * 31 + Item.getId(handle.item)
}
private fun apply() {
if (bukkit !is CraftItemStack) {
bukkit.itemMeta = CraftItemStack.asCraftMirror(handle).itemMeta

View File

@@ -6,9 +6,11 @@ import com.comphenix.protocol.events.PacketEvent
import com.willfp.eco.core.AbstractPacketAdapter
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.display.Display
import com.willfp.eco.core.fast.FastItemStack
import com.willfp.eco.spigot.display.frame.DisplayFrame
import com.willfp.eco.spigot.display.frame.lastDisplayFrame
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.util.function.Consumer
class PacketWindowItems(plugin: EcoPlugin) : AbstractPacketAdapter(plugin, PacketType.Play.Server.WINDOW_ITEMS, false) {
override fun onSend(
@@ -20,11 +22,21 @@ class PacketWindowItems(plugin: EcoPlugin) : AbstractPacketAdapter(plugin, Packe
if (itemStacks == null) {
return@modify null
}
itemStacks.forEach(Consumer { item: ItemStack ->
Display.display(
item, player
)
})
val frameMap = mutableMapOf<Byte, Int>()
for (index in itemStacks.indices) {
frameMap[index.toByte()] = FastItemStack.wrap(itemStacks[index]).hashCode()
}
val newFrame = DisplayFrame(frameMap)
val changes = player.lastDisplayFrame.getChangedSlots(newFrame)
player.lastDisplayFrame = newFrame
for (index in changes) {
Display.display(itemStacks[index.toInt()], player)
}
itemStacks
}
}

View File

@@ -0,0 +1,28 @@
package com.willfp.eco.spigot.display.frame
import org.bukkit.entity.Player
import java.util.*
data class DisplayFrame(val items: Map<Byte, Int>) {
fun getChangedSlots(newFrame: DisplayFrame): List<Byte> {
val changes = mutableListOf<Byte>()
for ((slot, hash) in newFrame.items) {
if (items[slot] != hash) {
changes.add(slot)
}
}
return changes
}
}
val frames = mutableMapOf<UUID, DisplayFrame>()
var Player.lastDisplayFrame: DisplayFrame
get() {
return frames[this.uniqueId] ?: DisplayFrame(emptyMap())
}
set(value) {
frames[this.uniqueId] = value
}