Skip to content

Commit

Permalink
fix: player skull
Browse files Browse the repository at this point in the history
  • Loading branch information
cccgh5 committed Aug 15, 2024
1 parent 6f09595 commit 5ab9140
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ open class HQContainer(
baseInventory?.apply {
buttons.filter { it.value.isRemovable() }
.forEach { inventory.setItem(it.key, null) }

buttons.entries.removeIf { it.value.isRemovable() }
initialize(this)
}
Expand All @@ -73,7 +74,7 @@ open class HQContainer(
if (prevHolder is HQContainer) {
if (plugin == null) plugin = Bukkit.getPluginManager().getPlugin("HQFramework")
val plugin = plugin!!
plugin.getScheduler().runTaskLater(1) { open(player) }
plugin.getScheduler(player.location).runTaskLater(1) { open(player) }
} else {
player.openInventory(inventory)
openedPlayers.add(player)
Expand All @@ -83,7 +84,7 @@ open class HQContainer(
onOpen(*openedPlayers.toTypedArray())
}

@Suppress("deprecation")
@Suppress("DEPRECATION")
private fun ItemStack.isMatchedType(other: ItemStack): Boolean {
return type == other.type && durability == other.durability
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.hqservice.framework.inventory.util

import com.destroystokyo.paper.profile.PlayerProfile
import com.google.gson.Gson
import com.google.gson.JsonObject
import kotlinx.coroutines.CoroutineScope
Expand All @@ -11,6 +12,7 @@ import org.bukkit.Server
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.inventory.meta.SkullMeta
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL
Expand All @@ -23,7 +25,7 @@ class PlayerSkullRepository(
private val server: Server,
private val coroutineScope: CoroutineScope
) {
private val skinTagMap = mutableMapOf<UUID, String>()
private val skinTagMap = mutableMapOf<UUID, PlayerProfile>()
private val lambdaQueueMap = mutableMapOf<UUID, ConcurrentLinkedQueue<ItemStack>>()
private val gson = Gson()

Expand All @@ -36,38 +38,39 @@ class PlayerSkullRepository(
) {
val itemStack = inventory.getItem(slot) ?: return
if (skinTagMap.containsKey(targetUniqueId)) {
if (!itemStack.type.isAir && targetedItemStack.isSimilar(itemStack))
server.unsafe.modifyItemStack(itemStack, skinTagMap[targetUniqueId]!!).apply {
itemMeta = itemMeta?.also(metaScope)

if (!itemStack.type.isAir && targetedItemStack.isSimilar(itemStack)) {
val skullMeta = itemStack.itemMeta as SkullMeta
// set Player Profile
skullMeta.playerProfile = skinTagMap[targetUniqueId]
skullMeta.also(metaScope)

itemStack.apply {
itemMeta = skullMeta
}
}
} else {
if (lambdaQueueMap.containsKey(targetUniqueId)) lambdaQueueMap[targetUniqueId]?.offer(itemStack)
else {
val queue = ConcurrentLinkedQueue<ItemStack>()
lambdaQueueMap[targetUniqueId] = queue
queue.offer(itemStack)
coroutineScope.launch(Dispatchers.BukkitAsync) {
val contents =
getURLContents("https://sessionserver.mojang.com/session/minecraft/profile/$targetUniqueId")
val jsonObject = gson.fromJson(contents, JsonObject::class.java)
val value = jsonObject.getAsJsonArray("properties")[0].asJsonObject["value"].asString
val decoded = String(Base64.getDecoder().decode(value))
val newObject = gson.fromJson(decoded, JsonObject::class.java)
val skinUrl = newObject.getAsJsonObject("textures").getAsJsonObject("SKIN")["url"].asString
val skin = "{\"textures\":{\"SKIN\":{\"url\":\"$skinUrl\"}}}".toByteArray()
val encoded = Base64.getEncoder().encode(skin)
val hash = Arrays.hashCode(encoded).toLong()
val hashAsId = UUID(hash, hash)
val tag = "{SkullOwner:{Id:\"$hashAsId\", Properties:{textures:[{Value:\"$value\"}]}}}"
skinTagMap[targetUniqueId] = tag
val profile = server.getOfflinePlayer(targetUniqueId).playerProfile
skinTagMap[targetUniqueId] = profile
lambdaQueueMap.remove(targetUniqueId)
while (queue.isNotEmpty()) {
try {
val element = queue.poll()
if (!element.type.isAir && targetedItemStack.isSimilar(element))
server.unsafe.modifyItemStack(element, tag).apply {
itemMeta = itemMeta?.also(metaScope)
if (!element.type.isAir && targetedItemStack.isSimilar(element)) {
val skullMeta = element.itemMeta as SkullMeta
skullMeta.playerProfile = profile
skullMeta.also(metaScope)

element.apply {
itemMeta = skullMeta
}
}
} catch (_: Exception) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import kr.hqservice.framework.nms.virtual.message.VirtualMessageImpl
import kr.hqservice.framework.nms.wrapper.chat.BaseComponentWrapper
import net.minecraft.network.chat.Component
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket
import net.minecraft.world.inventory.MenuType
import org.bukkit.craftbukkit.entity.CraftPlayer
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryType
import org.bukkit.inventory.InventoryView
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
Expand All @@ -18,14 +20,37 @@ open class VirtualContainer(
private val player: Player,
private val title: String
) : Virtual, KoinComponent {
companion object {
private val genericContainers = listOf(
MenuType.GENERIC_3x3,
MenuType.GENERIC_9x1,
MenuType.GENERIC_9x2,
MenuType.GENERIC_9x3,
MenuType.GENERIC_9x4,
MenuType.GENERIC_9x5,
MenuType.GENERIC_9x6
)
}

protected val baseComponentService: NmsService<String, BaseComponentWrapper> by inject(named("base-component"))
override fun createVirtualMessage(): VirtualMessage? {
val container = (player as CraftPlayer).handle.containerMenu

val bukkitView = container.bukkitView
if (bukkitView !is InventoryView) throw UnsupportedOperationException("error1")

val virtualContainerType = container.type
val type = bukkitView.topInventory.type
val size = bukkitView.topInventory.size

val virtualContainerType = if (type == InventoryType.CHEST || type == InventoryType.ENDER_CHEST) {
genericContainers[size / 9]
} else {
try {
container.type
} catch (_: Exception) {
return null
}
}

return VirtualMessageImpl(
ClientboundOpenScreenPacket(
Expand Down

0 comments on commit 5ab9140

Please sign in to comment.