mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 10:25:06 -04:00
fix some bugs in TimeWorker, more fixes
This commit is contained in:
parent
4edba60489
commit
770fe6551d
@ -18,4 +18,10 @@ data class StatusEffectInstance(
|
|||||||
val statusEffect: StatusEffect,
|
val statusEffect: StatusEffect,
|
||||||
val amplifier: Int,
|
val amplifier: Int,
|
||||||
val duration: Int,
|
val duration: Int,
|
||||||
)
|
) {
|
||||||
|
private val startMillis = System.currentTimeMillis()
|
||||||
|
|
||||||
|
val remainingMillis: Long
|
||||||
|
get() = System.currentTimeMillis() - startMillis
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@ import de.bixilon.minosoft.gui.rendering.util.VecUtil.chunkPosition
|
|||||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.inChunkPosition
|
import de.bixilon.minosoft.gui.rendering.util.VecUtil.inChunkPosition
|
||||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||||
|
import de.bixilon.minosoft.util.KUtil.synchronizedMapOf
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
import java.lang.reflect.InvocationTargetException
|
import java.lang.reflect.InvocationTargetException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -42,8 +43,8 @@ abstract class Entity(
|
|||||||
var position: Vec3,
|
var position: Vec3,
|
||||||
var rotation: EntityRotation,
|
var rotation: EntityRotation,
|
||||||
) {
|
) {
|
||||||
val equipment: MutableMap<EquipmentSlots, ItemStack> = mutableMapOf()
|
val equipment: MutableMap<EquipmentSlots, ItemStack> = synchronizedMapOf()
|
||||||
val effectList: MutableSet<StatusEffectInstance> = mutableSetOf()
|
val activeStatusEffects: MutableMap<StatusEffect, StatusEffectInstance> = synchronizedMapOf()
|
||||||
|
|
||||||
@JvmField
|
@JvmField
|
||||||
protected val versionId: Int = connection.version.versionId
|
protected val versionId: Int = connection.version.versionId
|
||||||
@ -69,12 +70,11 @@ abstract class Entity(
|
|||||||
|
|
||||||
fun addEffect(effect: StatusEffectInstance) {
|
fun addEffect(effect: StatusEffectInstance) {
|
||||||
// effect already applied, maybe the duration or the amplifier changed?
|
// effect already applied, maybe the duration or the amplifier changed?
|
||||||
effectList.removeIf { (statusEffect) -> statusEffect === effect.statusEffect }
|
activeStatusEffects[effect.statusEffect] = effect
|
||||||
effectList.add(effect)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeEffect(effect: StatusEffect) {
|
fun removeEffect(effect: StatusEffect) {
|
||||||
effectList.removeIf { (statusEffect) -> statusEffect === effect }
|
activeStatusEffects.remove(effect)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun attachTo(vehicleId: Int?) {
|
fun attachTo(vehicleId: Int?) {
|
||||||
@ -278,7 +278,7 @@ abstract class Entity(
|
|||||||
return delta
|
return delta
|
||||||
}
|
}
|
||||||
|
|
||||||
fun tick(deltaMillis: Long) {
|
private fun tickMovement(deltaMillis: Long) {
|
||||||
if (connection.world.getChunk(position.blockPosition.chunkPosition)?.isFullyLoaded != true) {
|
if (connection.world.getChunk(position.blockPosition.chunkPosition)?.isFullyLoaded != true) {
|
||||||
return // ignore update if chunk is not loaded yet
|
return // ignore update if chunk is not loaded yet
|
||||||
}
|
}
|
||||||
@ -301,6 +301,12 @@ abstract class Entity(
|
|||||||
move(velocity * deltaTime)
|
move(velocity * deltaTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun tick(deltaMillis: Long) {
|
||||||
|
check(deltaMillis > 0L) { "Delta tick time is <= 0: $deltaMillis" }
|
||||||
|
|
||||||
|
tickMovement(deltaMillis)
|
||||||
|
}
|
||||||
|
|
||||||
private val aabb: AABB
|
private val aabb: AABB
|
||||||
get() = defaultAABB + position
|
get() = defaultAABB + position
|
||||||
|
|
||||||
|
@ -40,6 +40,10 @@ class EntityMetaData(
|
|||||||
) {
|
) {
|
||||||
val sets: MetaDataHashMap = MetaDataHashMap()
|
val sets: MetaDataHashMap = MetaDataHashMap()
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return sets.toString()
|
||||||
|
}
|
||||||
|
|
||||||
fun getData(type: EntityMetaDataDataTypes, buffer: PlayInByteBuffer): Any? {
|
fun getData(type: EntityMetaDataDataTypes, buffer: PlayInByteBuffer): Any? {
|
||||||
return when (type) {
|
return when (type) {
|
||||||
EntityMetaDataDataTypes.BYTE -> buffer.readByte()
|
EntityMetaDataDataTypes.BYTE -> buffer.readByte()
|
||||||
@ -128,8 +132,8 @@ class EntityMetaData(
|
|||||||
get(index)?.let {
|
get(index)?.let {
|
||||||
try {
|
try {
|
||||||
return it as K
|
return it as K
|
||||||
} catch (e: ClassCastException) {
|
} catch (exception: ClassCastException) {
|
||||||
Log.log(LogMessageType.OTHER, level = LogLevels.WARN, message = e)
|
Log.log(LogMessageType.OTHER, level = LogLevels.WARN, message = exception)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return field.getDefaultValue()
|
return field.getDefaultValue()
|
||||||
@ -197,7 +201,7 @@ class EntityMetaData(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getChatComponent(field: EntityMetaDataFields): ChatComponent? {
|
fun getChatComponent(field: EntityMetaDataFields): ChatComponent? {
|
||||||
return ChatComponent.of(get(field), connection.version.localeManager)
|
return get<Any?>(field)?.let { ChatComponent.of(it, connection.version.localeManager) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getString(field: EntityMetaDataFields): String? {
|
fun getString(field: EntityMetaDataFields): String? {
|
||||||
|
@ -161,11 +161,6 @@ data class ItemStack(
|
|||||||
return nbt
|
return nbt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return fullDisplayName
|
|
||||||
}
|
|
||||||
|
|
||||||
val displayName: ChatComponent
|
val displayName: ChatComponent
|
||||||
get() {
|
get() {
|
||||||
customDisplayName?.let { return it }
|
customDisplayName?.let { return it }
|
||||||
@ -173,10 +168,6 @@ data class ItemStack(
|
|||||||
return ChatComponent.of(item.toString())
|
return ChatComponent.of(item.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToDo all properties
|
|
||||||
val fullDisplayName: String
|
|
||||||
get() = displayName.legacyText + ": " + super.toString()
|
|
||||||
|
|
||||||
fun shouldHideEnchantments(): Boolean {
|
fun shouldHideEnchantments(): Boolean {
|
||||||
return BitByte.isBitSet(hideFlags, HIDE_ENCHANTMENT_BIT)
|
return BitByte.isBitSet(hideFlags, HIDE_ENCHANTMENT_BIT)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ package de.bixilon.minosoft.data.world
|
|||||||
|
|
||||||
import de.bixilon.minosoft.data.entities.entities.Entity
|
import de.bixilon.minosoft.data.entities.entities.Entity
|
||||||
import de.bixilon.minosoft.util.KUtil.synchronizedMapOf
|
import de.bixilon.minosoft.util.KUtil.synchronizedMapOf
|
||||||
|
import de.bixilon.minosoft.util.KUtil.toSynchronizedMap
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class WorldEntities : Iterable<Entity> {
|
class WorldEntities : Iterable<Entity> {
|
||||||
@ -72,6 +73,6 @@ class WorldEntities : Iterable<Entity> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun iterator(): Iterator<Entity> {
|
override fun iterator(): Iterator<Entity> {
|
||||||
return idEntityMap.values.iterator()
|
return idEntityMap.toSynchronizedMap().values.iterator()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,10 +117,12 @@ class PlayConnection(
|
|||||||
velocityHandlerTask = TimeWorkerTask(ProtocolDefinition.TICK_TIME / 5) {
|
velocityHandlerTask = TimeWorkerTask(ProtocolDefinition.TICK_TIME / 5) {
|
||||||
val currentTime = System.currentTimeMillis()
|
val currentTime = System.currentTimeMillis()
|
||||||
val deltaTime = currentTime - velocityHandlerLastExecutionTime
|
val deltaTime = currentTime - velocityHandlerLastExecutionTime
|
||||||
for (entity in world.entities) {
|
if (deltaTime > 0L) {
|
||||||
entity.tick(deltaTime)
|
for (entity in world.entities) {
|
||||||
|
entity.tick(deltaTime)
|
||||||
|
}
|
||||||
|
renderer?.renderWindow?.inputHandler?.camera?.checkPosition()
|
||||||
}
|
}
|
||||||
renderer?.renderWindow?.inputHandler?.camera?.checkPosition()
|
|
||||||
velocityHandlerLastExecutionTime = currentTime
|
velocityHandlerLastExecutionTime = currentTime
|
||||||
}
|
}
|
||||||
TimeWorker.addTask(velocityHandlerTask)
|
TimeWorker.addTask(velocityHandlerTask)
|
||||||
|
@ -55,7 +55,7 @@ public class CommandEntities extends Command {
|
|||||||
tableData.add(new Object[]{"Location", entity.getPosition()});
|
tableData.add(new Object[]{"Location", entity.getPosition()});
|
||||||
tableData.add(new Object[]{"Rotation", entity.getRotation()});
|
tableData.add(new Object[]{"Rotation", entity.getRotation()});
|
||||||
tableData.add(new Object[]{"Equipment", entity.getEquipment()});
|
tableData.add(new Object[]{"Equipment", entity.getEquipment()});
|
||||||
tableData.add(new Object[]{"Effects", entity.getEffectList()});
|
tableData.add(new Object[]{"Effects", entity.getActiveStatusEffects()});
|
||||||
tableData.add(new Object[]{"Attached to", entity.getAttachedEntity() == -1 ? "" : entity.getAttachedEntity()});
|
tableData.add(new Object[]{"Attached to", entity.getAttachedEntity() == -1 ? "" : entity.getAttachedEntity()});
|
||||||
|
|
||||||
for (var entry : entity.getEntityMetaDataFormatted().entrySet()) {
|
for (var entry : entity.getEntityMetaDataFormatted().entrySet()) {
|
||||||
|
@ -1,20 +1,25 @@
|
|||||||
package de.bixilon.minosoft.util.time
|
package de.bixilon.minosoft.util.time
|
||||||
|
|
||||||
import de.bixilon.minosoft.Minosoft
|
import de.bixilon.minosoft.Minosoft
|
||||||
import de.bixilon.minosoft.util.KUtil.synchronizedListOf
|
import de.bixilon.minosoft.util.KUtil.synchronizedSetOf
|
||||||
import de.bixilon.minosoft.util.KUtil.toSynchronizedList
|
import de.bixilon.minosoft.util.KUtil.toSynchronizedSet
|
||||||
|
|
||||||
object TimeWorker {
|
object TimeWorker {
|
||||||
private val TASKS: MutableList<TimeWorkerTask> = synchronizedListOf()
|
private val TASKS: MutableSet<TimeWorkerTask> = synchronizedSetOf()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
Thread({
|
Thread({
|
||||||
while (true) {
|
while (true) {
|
||||||
val currentTime = System.currentTimeMillis()
|
val currentTime = System.currentTimeMillis()
|
||||||
for (task in TASKS.toSynchronizedList()) {
|
for (task in TASKS.toSynchronizedSet()) {
|
||||||
if (currentTime - task.lastExecution >= task.interval) {
|
if (!task.getsExecuted && currentTime - task.lastExecution >= task.interval) {
|
||||||
Minosoft.THREAD_POOL.execute(task.runnable)
|
Minosoft.THREAD_POOL.execute {
|
||||||
task.lastExecution = currentTime
|
task.getsExecuted = true
|
||||||
|
task.runnable.run()
|
||||||
|
task.lastExecution = currentTime
|
||||||
|
task.getsExecuted = false
|
||||||
|
}
|
||||||
|
TASKS -= task
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Thread.sleep(1)
|
Thread.sleep(1)
|
||||||
@ -22,6 +27,16 @@ object TimeWorker {
|
|||||||
}, "TimeWorkerThread").start()
|
}, "TimeWorkerThread").start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun runIn(millis: Int, runnable: Runnable) {
|
||||||
|
val task = TimeWorkerTask(
|
||||||
|
interval = millis,
|
||||||
|
runnable = runnable,
|
||||||
|
runOnce = true,
|
||||||
|
)
|
||||||
|
task.lastExecution = System.currentTimeMillis()
|
||||||
|
TASKS += task
|
||||||
|
}
|
||||||
|
|
||||||
fun addTask(task: TimeWorkerTask) {
|
fun addTask(task: TimeWorkerTask) {
|
||||||
TASKS += task
|
TASKS += task
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@ package de.bixilon.minosoft.util.time
|
|||||||
|
|
||||||
data class TimeWorkerTask(
|
data class TimeWorkerTask(
|
||||||
val interval: Int,
|
val interval: Int,
|
||||||
|
val runOnce: Boolean = true,
|
||||||
val runnable: Runnable,
|
val runnable: Runnable,
|
||||||
) {
|
) {
|
||||||
|
var getsExecuted: Boolean = false
|
||||||
var lastExecution: Long = 0L
|
var lastExecution: Long = 0L
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user