Skip to content

Commit

Permalink
fix: Fixed asynchronous operation of player data.
Browse files Browse the repository at this point in the history
  • Loading branch information
blank038 committed Oct 30, 2024
1 parent 6447cc9 commit 5ccaac0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.aiyostudio.esync.internal.module.entity

import com.aiyostudio.esync.common.module.IEntity
import com.aiyostudio.esync.internal.plugin.EfficientSyncBukkit
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.util.logging.Level

class EnderChestEntity : IEntity {
val items = mutableMapOf<Int, ItemStack>()

override fun apply(player: Any): Boolean {
if (player is Player) {
player.enderChest.clear()
items.forEach { (slot, item) -> player.enderChest.setItem(slot, item) }
return true
try {
if (player is Player) {
player.enderChest.clear()
items.forEach { (slot, item) -> player.enderChest.setItem(slot, item) }
return true
}
} catch (e: Exception) {
EfficientSyncBukkit.instance.logger.log(Level.WARNING, e) { "Failed to apply 'ender-chest' data for ${(player as Player).uniqueId}" }
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.aiyostudio.esync.internal.module.entity

import com.aiyostudio.esync.common.module.IEntity
import com.aiyostudio.esync.internal.plugin.EfficientSyncBukkit
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.util.logging.Level

class PlayerInventoryEntity : IEntity {
val inventory = mutableMapOf<Int, ItemStack>()

override fun apply(player: Any): Boolean {
if (player is Player) {
player.inventory.clear()
inventory.forEach { (slot, item) -> player.inventory.setItem(slot, item) }
return true
try {
if (player is Player) {
player.inventory.clear()
inventory.forEach { (slot, item) -> player.inventory.setItem(slot, item) }
return true
}
} catch (e: Exception) {
EfficientSyncBukkit.instance.logger.log(Level.WARNING, e) { "Failed to apply 'inventory' data for ${(player as Player).uniqueId}" }
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.aiyostudio.esync.internal.module.entity

import com.aiyostudio.esync.common.module.IEntity
import com.aiyostudio.esync.internal.plugin.EfficientSyncBukkit
import org.bukkit.attribute.Attribute
import org.bukkit.entity.Player
import org.bukkit.potion.PotionEffect
import java.util.logging.Level

class PlayerStatusEntity : IEntity {
val potions = mutableListOf<PotionEffect>()
var health = 20.0
var maxHealth = 20.0

override fun apply(player: Any): Boolean {
if (player is Player) {
player.getAttribute(Attribute.GENERIC_MAX_HEALTH).baseValue = maxHealth
player.health = health
potions.forEach { player.addPotionEffect(it) }
return true
try {
if (player is Player) {
player.getAttribute(Attribute.GENERIC_MAX_HEALTH).baseValue = maxHealth
player.health = health
potions.forEach { player.addPotionEffect(it) }
return true
}
} catch (e: Exception) {
EfficientSyncBukkit.instance.logger.log(Level.WARNING, e) { "Failed to apply 'player-status' data for ${(player as Player).uniqueId}" }
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class InventoryModuleImpl(
val slot = byteBuf.readInt()
val length = byteBuf.readInt()
val bytes = ByteArray(length)
byteBuf.readBytes(length)
byteBuf.readBytes(bytes)
val item = SerializerUtil.deserializerItem(bytes)
entity.inventory[slot] = item
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ class PlayerStatusModuleImpl(
val str = String(byteArray, Charsets.UTF_8)
val yaml = YamlConfiguration()
yaml.loadFromString(str)
val map = yaml.getConfigurationSection("potion").getValues(false)
val option = ConfigurationSerialization.deserializeObject(map, PotionEffect::class.java) as PotionEffect
result.potions.add(option)
val potion = yaml.get("potion") as PotionEffect
result.potions.add(potion)
}
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ class SyncTransaction(
val repository = RepositoryHandler.repository ?: return
this.modules.forEach {
repository.updateState(uuid, it, SyncState.LOCKED)
val module = ModuleHandler.findByKey(it)!!
module.apply(player.uniqueId)
Bukkit.getScheduler().runTask(EfficientSyncBukkit.instance) {
val module = ModuleHandler.findByKey(it)!!
if (module.apply(player.uniqueId)) {
CacheHandler.playerCaches[player.uniqueId]!!.load(module.uniqueKey)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class MysqlRepositoryImpl(
}

private fun connect(block: (connect: Connection) -> Unit) {
with(DriverManager.getConnection(url, user, password)) { block(this) }
with(DriverManager.getConnection(url, user, password)) {
try {
block(this)
} finally {
this.close()
}
}
}
}

0 comments on commit 5ccaac0

Please sign in to comment.