diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 16:55:25 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-27 16:55:25 +0100 |
commit | cb3fb4771a824ec13e3da1647e3278f3ebb9a5bc (patch) | |
tree | 6ef9f19608fdb628938a9ed48a2d05ab740178e6 /src/main/kotlin/io/dico/parcels2/listener/ParcelEntityTracker.kt | |
parent | 0de16eb1845e0573f9467e8e0b6b4b7f80534f4e (diff) |
Make progress with listeners
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/listener/ParcelEntityTracker.kt')
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/listener/ParcelEntityTracker.kt | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/listener/ParcelEntityTracker.kt b/src/main/kotlin/io/dico/parcels2/listener/ParcelEntityTracker.kt index fb94b8b..22a5486 100644 --- a/src/main/kotlin/io/dico/parcels2/listener/ParcelEntityTracker.kt +++ b/src/main/kotlin/io/dico/parcels2/listener/ParcelEntityTracker.kt @@ -1,14 +1,53 @@ package io.dico.parcels2.listener import io.dico.parcels2.Parcel +import io.dico.parcels2.Worlds +import io.dico.parcels2.util.editLoop +import io.dico.parcels2.util.isPresentAnd import org.bukkit.entity.Entity -class ParcelEntityTracker { - val map = mutableMapOf<Entity, Parcel>() +class ParcelEntityTracker(val worlds: Worlds) { + val map = mutableMapOf<Entity, Parcel?>() fun untrack(entity: Entity) { map.remove(entity) } + fun track(entity: Entity, parcel: Parcel?) { + map[entity] = parcel + } + + /* + * Tracks entities. If the entity is dead, they are removed from the collection. + * If the entity is found to have left the parcel it was created in, it will be removed from the world and from the list. + * If it is still in the parcel it was created in, and it is on the ground, it is removed from the list. + * + * Start after 5 seconds, run every 0.25 seconds + */ + fun tick() { + map.editLoop { entity, parcel -> + if (entity.isDead || entity.isOnGround) { + remove(); return@editLoop + } + if (parcel.isPresentAnd { hasBlockVisitors }) { + remove() + } + val newParcel = worlds.getParcelAt(entity.location) + if (newParcel !== parcel && !newParcel.isPresentAnd { hasBlockVisitors }) { + remove() + entity.remove() + } + } + } + + fun swapParcels(parcel1: Parcel, parcel2: Parcel) { + map.editLoop { + if (value === parcel1) { + value = parcel2 + } else if (value === parcel2) { + value = parcel1 + } + } + } }
\ No newline at end of file |