From 78755b0e9a89e651ce8d16c9c6f1c099de18579f Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 7 Oct 2021 14:34:40 +0100 Subject: [PATCH] (Hopefully) massively optimized display (PacketWindowItems) with DisplayFrame.kt --- .../proxy/v1_16_R3/fast/NMSFastItemStack.kt | 12 ++++++++ .../proxy/v1_17_R1/fast/NMSFastItemStack.kt | 13 +++++++++ .../eco/spigot/display/PacketWindowItems.kt | 24 ++++++++++++---- .../eco/spigot/display/frame/DisplayFrame.kt | 28 +++++++++++++++++++ 4 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/frame/DisplayFrame.kt diff --git a/eco-core/core-nms/v1_16_R3/src/main/kotlin/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.kt b/eco-core/core-nms/v1_16_R3/src/main/kotlin/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.kt index c53e79d5..fe0f4293 100644 --- a/eco-core/core-nms/v1_16_R3/src/main/kotlin/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.kt +++ b/eco-core/core-nms/v1_16_R3/src/main/kotlin/com/willfp/eco/proxy/v1_16_R3/fast/NMSFastItemStack.kt @@ -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 diff --git a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.kt b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.kt index e297c310..0873dbcd 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.kt +++ b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/proxy/v1_17_R1/fast/NMSFastItemStack.kt @@ -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 diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/PacketWindowItems.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/PacketWindowItems.kt index 38406e01..10ac3659 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/PacketWindowItems.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/PacketWindowItems.kt @@ -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() + + 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 } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/frame/DisplayFrame.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/frame/DisplayFrame.kt new file mode 100644 index 00000000..c6ae0922 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/spigot/display/frame/DisplayFrame.kt @@ -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) { + fun getChangedSlots(newFrame: DisplayFrame): List { + val changes = mutableListOf() + + for ((slot, hash) in newFrame.items) { + if (items[slot] != hash) { + changes.add(slot) + } + } + + return changes + } +} + +val frames = mutableMapOf() + +var Player.lastDisplayFrame: DisplayFrame + get() { + return frames[this.uniqueId] ?: DisplayFrame(emptyMap()) + } + set(value) { + frames[this.uniqueId] = value + }