scoreboard api improvements

This commit is contained in:
Bixilon 2021-11-02 12:47:32 +01:00
parent ae63181159
commit 441c35f541
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
10 changed files with 72 additions and 10 deletions

View File

@ -21,6 +21,7 @@ open class ResourceLocation(
val namespace: String = ProtocolDefinition.DEFAULT_NAMESPACE,
val path: String,
) : Comparable<ResourceLocation>, Translatable { // compare is for moshi
private val hashCode = Objects.hash(namespace, path)
open val full: String = "$namespace:$path"
override val translationKey: ResourceLocation
@ -30,7 +31,7 @@ open class ResourceLocation(
override fun hashCode(): Int {
return Objects.hash(namespace, path)
return hashCode
}
override fun equals(other: Any?): Boolean {

View File

@ -12,9 +12,40 @@
*/
package de.bixilon.minosoft.data.scoreboard
import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.ScoreboardPositionSetS2CP
import de.bixilon.minosoft.util.KUtil.synchronizedMapOf
class ScoreboardManager {
val teams: MutableMap<String, Team> = synchronizedMapOf()
val objectives: MutableMap<String, ScoreboardObjective> = synchronizedMapOf()
val positions: MutableMap<ScoreboardPositionSetS2CP.ScoreboardPositions, ScoreboardObjective> = synchronizedMapOf()
fun getTeamsOf(member: String): Set<Team> {
val teams: MutableSet<Team> = mutableSetOf()
for ((name, team) in this.teams) {
if (!team.members.contains(member)) {
continue
}
teams += team
}
return teams
}
fun updateScoreTeams(team: Team, members: Set<String>, remove: Boolean = false) {
for ((_, objective) in objectives) {
for ((_, score) in objective.scores) {
if (score.entity in members) {
if (remove) {
score.teams -= team
} else {
score.teams += team
}
}
}
}
}
}

View File

@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.scoreboard
class ScoreboardScore(
val entity: String,
var objective: String,
var score: Int,
var objective: ScoreboardObjective,
val teams: MutableSet<Team>,
var value: Int,
)

View File

@ -12,6 +12,7 @@
*/
package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.util.KUtil
@ -22,10 +23,19 @@ import de.bixilon.minosoft.util.logging.LogMessageType
class ScoreboardPositionSetS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
private val position: ScoreboardPositions = ScoreboardPositions[buffer.readUnsignedByte()]
private val score: String = buffer.readString()
private val objective: String? = buffer.readNullString()
override fun handle(connection: PlayConnection) {
val scoreboardManager = connection.scoreboardManager
if (objective == null) {
scoreboardManager.positions -= position
return
}
scoreboardManager.positions[position] = scoreboardManager.objectives[objective] ?: return
}
override fun log() {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Scoreboard position set (position=$position, score=$score)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Scoreboard position set (position=$position, objective=$objective)" }
}
enum class ScoreboardPositions {

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.util.KUtil.toSynchronizedMap
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
@ -28,6 +29,13 @@ class RemoveScoreboardObjectiveS2CP(val objective: String, buffer: PlayInByteBuf
override fun handle(connection: PlayConnection) {
connection.scoreboardManager.objectives.remove(objective)
for ((position, objective) in connection.scoreboardManager.positions.toSynchronizedMap()) {
if (objective.name != this.objective) {
continue
}
connection.scoreboardManager.positions -= position
}
}
}

View File

@ -18,12 +18,13 @@ import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
import de.bixilon.minosoft.util.KUtil.toSynchronizedSet
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
class PutScoreboardScoreS2CP(val entity: String, val objective: String?, buffer: PlayInByteBuffer) : PlayS2CPacket() {
val score: Int = if (buffer.versionId < ProtocolVersions.V_14W04A) { // ToDo
val value: Int = if (buffer.versionId < ProtocolVersions.V_14W04A) { // ToDo
buffer.readInt()
} else {
buffer.readVarInt()
@ -31,11 +32,12 @@ class PutScoreboardScoreS2CP(val entity: String, val objective: String?, buffer:
override fun handle(connection: PlayConnection) {
check(objective != null) { "Can not update null objective!" }
connection.scoreboardManager.objectives[objective]?.scores?.put(entity, ScoreboardScore(entity, objective, score))
val objective = connection.scoreboardManager.objectives[objective] ?: return
objective.scores[entity] = ScoreboardScore(entity, objective, connection.scoreboardManager.getTeamsOf(entity).toSynchronizedSet(), value)
}
override fun log() {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Put scoreboard score (entity=$entity§r, objective=$objective§r, score=$score)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Put scoreboard score (entity=$entity§r, objective=$objective§r, value=$value)" }
}
}

View File

@ -24,6 +24,7 @@ import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
import de.bixilon.minosoft.util.BitByte.isBitMask
import de.bixilon.minosoft.util.KUtil.toSynchronizedSet
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
@ -111,13 +112,15 @@ class TeamCreateS2CP(val name: String, buffer: PlayInByteBuffer) : PlayS2CPacket
collisionRule = collisionRule,
nameTagVisibility = nameTagVisibility,
formattingCode = formattingCode,
members = members.toMutableSet(),
members = members.toSynchronizedSet(),
)
connection.scoreboardManager.teams[name] = team
for (member in members) {
connection.tabList.tabListItemsByName[member]?.team = team
}
connection.scoreboardManager.updateScoreTeams(team, members)
}
override fun log() {

View File

@ -38,6 +38,8 @@ class TeamMemberAddS2CP(val name: String, buffer: PlayInByteBuffer) : PlayS2CPac
for (member in members) {
connection.tabList.tabListItemsByName[member]?.team = team
}
connection.scoreboardManager.updateScoreTeams(team, members)
}
override fun log() {

View File

@ -41,6 +41,8 @@ class TeamMemberRemoveS2CP(val name: String, buffer: PlayInByteBuffer) : PlayS2C
}
item.team = team
}
connection.scoreboardManager.updateScoreTeams(team, members, true)
}
override fun log() {

View File

@ -22,7 +22,9 @@ import de.bixilon.minosoft.util.logging.LogMessageType
class TeamRemoveS2CP(val name: String) : PlayS2CPacket() {
override fun handle(connection: PlayConnection) {
connection.scoreboardManager.teams.remove(name)
val team = connection.scoreboardManager.teams.remove(name) ?: return
connection.scoreboardManager.updateScoreTeams(team, team.members, true)
}
override fun log() {