Skip to content

Commit

Permalink
refactor: drop Axiom compat for normal teleport handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jan 15, 2025
1 parent 3a34fbe commit db519b7
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 31 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ dependencies {
compileOnly(idofrontLibs.minecraft.customblockdata)
compileOnly(idofrontLibs.creative.api)
compileOnly(idofrontLibs.creative.serializer.minecraft)
compileOnly(libs.minecraft.plugin.axiompaper)

implementation(libs.minecraft.plugin.protectionlib)
}
Expand Down
5 changes: 2 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[versions]
gearyPaper = "0.31.3"
gearyPaper = "0.31.10"
guiy="0.12.5"

[libraries]
geary-papermc = { module = "com.mineinabyss:geary-papermc", version.ref = "gearyPaper" }
guiy = { module = "com.mineinabyss:guiy-compose", version.ref = "guiy" }

minecraft-plugin-protectionlib = "io.th0rgal:protectionlib:1.7.0"
minecraft-plugin-axiompaper = "com.moulberry:axiom:4.0.1"
minecraft-plugin-protectionlib = "io.th0rgal:protectionlib:1.8.0"
4 changes: 4 additions & 0 deletions src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.messaging.observeLogger
import com.mineinabyss.idofront.plugin.listeners
import io.papermc.paper.configuration.GlobalConfiguration
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.CraftWorld
import org.bukkit.plugin.java.JavaPlugin

class BlockyPlugin : JavaPlugin() {
Expand All @@ -27,6 +29,8 @@ class BlockyPlugin : JavaPlugin() {
override fun onEnable() {
createBlockyContext()

(Bukkit.getWorlds().first() as CraftWorld).handle.dataStorage

gearyPaper.worldManager.global.apply {
createFurnitureOutlineSystem()
createFurnitureSpawner()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object BlockyFurnitures {
val ItemDisplay.blockySeat
get() = this.seats.minByOrNull { it.location.distanceSquared(this.location) }

fun placeFurniture(prefabKey: PrefabKey, location: Location) = placeFurniture(prefabKey, location, 0f)
fun placeFurniture(prefabKey: PrefabKey, location: Location) = placeFurniture(prefabKey, location, location.yaw)

fun placeFurniture(prefabKey: PrefabKey, location: Location, yaw: Float) =
location.withGeary { getAddon(ItemTracking).createItem(prefabKey) }?.let { FurnitureHelpers.placeBlockyFurniture(prefabKey, location, yaw, it) }
Expand Down
89 changes: 89 additions & 0 deletions src/main/kotlin/com/mineinabyss/blocky/helpers/FastUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.mineinabyss.blocky.helpers

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
import it.unimi.dsi.fastutil.objects.ObjectArrayList
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import java.util.*

inline fun <T, R> Iterable<T>.flatMapFast(transform: (T) -> Iterable<R>): ObjectArrayList<R> {
return flatMapTo(ObjectArrayList<R>(), transform)
}

inline fun <T, R> Iterable<T>.flatMapSetFast(transform: (T) -> Iterable<R>): ObjectOpenHashSet<R> {
return flatMapTo(ObjectOpenHashSet<R>(), transform)
}

inline fun <T, R> Iterable<T>.mapFast(transform: (T) -> R): ObjectArrayList<R> {
return mapTo(ObjectArrayList<R>((this as? Collection)?.size ?: 10), transform)
}

inline fun <T, R : Any> Iterable<T>.mapNotNullFast(transform: (T) -> R?): ObjectArrayList<R> {
return mapNotNullTo(ObjectArrayList<R>(), transform)
}

inline fun <T, R> Iterable<T>.mapFastSet(transform: (T) -> R): ObjectOpenHashSet<R> {
return mapTo(ObjectOpenHashSet<R>((this as? Collection)?.size ?: 10), transform)
}

inline fun <T, K, V> Iterable<T>.associateFast(transform: (T) -> Pair<K, V>): Object2ObjectOpenHashMap<K, V> {
val capacity = mapCapacity((this as? Collection)?.size ?: 10).coerceAtLeast(16)
return associateTo(Object2ObjectOpenHashMap<K, V>(capacity), transform)
}

inline fun <K, V> Iterable<K>.associateFastWith(valueSelector: (K) -> V): Object2ObjectOpenHashMap<K, V> {
val result = Object2ObjectOpenHashMap<K, V>(mapCapacity((this as? Collection)?.size ?: 10).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}

inline fun <reified R> Iterable<*>.filterFastIsInstance(): ObjectArrayList<R> {
return filterIsInstanceTo(ObjectArrayList<R>())
}

inline fun <reified R> Iterable<*>.filterFastIsInstance(predicate: (R) -> Boolean): ObjectArrayList<R> {
val result = ObjectArrayList<R>()
for (element in this) if (element is R && predicate(element)) result.add(element)
return result
}

inline fun <T> Iterable<T>.filterFast(predicate: (T) -> Boolean): ObjectArrayList<T> {
return filterTo(ObjectArrayList<T>(), predicate)
}

inline fun <T> Iterable<T>.filterFastSet(predicate: (T) -> Boolean): ObjectOpenHashSet<T> {
return filterTo(ObjectOpenHashSet<T>(), predicate)
}

inline fun <K, V> Map<out K, V>.filterFast(predicate: (Map.Entry<K, V>) -> Boolean): Object2ObjectOpenHashMap<K, V> {
return filterTo(Object2ObjectOpenHashMap<K, V>(), predicate)
}

fun <K, V> Iterable<Pair<K, V>>.toFastMap(): Object2ObjectOpenHashMap<K, V> {
if (this is Collection) {
return when (size) {
0 -> Object2ObjectOpenHashMap()
1 -> fastMapOf(if (this is List) this[0] else iterator().next())
else -> toMap(Object2ObjectOpenHashMap<K, V>(mapCapacity(size)))
}
}
return toMap(LinkedHashMap<K, V>()).optimizeReadOnlyMap()
}

fun <K, V> fastMapOf(pair: Pair<K, V>): Object2ObjectOpenHashMap<K, V> = Object2ObjectOpenHashMap<K, V>().apply { put(pair.first, pair.second) }

fun mapCapacity(expectedSize: Int): Int = when {
// We are not coercing the value to a valid one and not throwing an exception. It is up to the caller to
// properly handle negative values.
expectedSize < 0 -> expectedSize
expectedSize < 3 -> expectedSize + 1
expectedSize < INT_MAX_POWER_OF_TWO -> ((expectedSize / 0.75F) + 1.0F).toInt()
// any large value
else -> Int.MAX_VALUE
}

internal fun <K, V> Map<K, V>.optimizeReadOnlyMap() = when (size) {
0 -> Object2ObjectOpenHashMap()
1 -> Object2ObjectOpenHashMap(with(entries.iterator().next()) { Collections.singletonMap(key, value) })
else -> Object2ObjectOpenHashMap(this)
}

private const val INT_MAX_POWER_OF_TWO: Int = 1 shl (Int.SIZE_BITS - 2)
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object FurniturePacketHelpers {
private val outlinePlayerMap = mutableMapOf<UUID, FurnitureUUID>()

fun baseFurnitureFromInteractionHitbox(id: Int) =
interactionHitboxIds.firstOrNull { id in it.entityIds }?.furniture
interactionHitboxIds.firstOrNull { id in it.entityIds }?.furniture ?: outlineIds.firstOrNull { id in it.entityIds }?.furniture

fun baseFurnitureFromCollisionHitbox(pos: BlockPos) =
collisionHitboxPosMap.entries.firstOrNull { pos in it.value }?.key?.toEntity() as? ItemDisplay
Expand Down Expand Up @@ -183,8 +183,7 @@ object FurniturePacketHelpers {
}

fun removeHitboxOutlinePacket(player: Player) {
val outlinePlayerId = outlinePlayerMap.remove(player.uniqueId)
val entityIds = outlinePlayerId?.let { pId -> outlineIds.filter { it.furnitureUUID == pId } }?.takeUnless { it.isEmpty() } ?: outlineIds
val entityIds = outlinePlayerMap.remove(player.uniqueId)?.let { pId -> outlineIds.filter { it.furnitureUUID == pId } }?.takeUnless { it.isEmpty() } ?: outlineIds
(player as CraftPlayer).handle.connection.send(ClientboundRemoveEntitiesPacket(*entityIds.flatMap { it.entityIds }.toIntArray()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.idofront.plugin.Plugins
import com.mineinabyss.idofront.time.ticks
import com.mineinabyss.idofront.util.to
import com.moulberry.axiom.event.AxiomManipulateEntityEvent
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent
import io.papermc.paper.event.player.PlayerTrackEntityEvent
import io.papermc.paper.event.player.PlayerUntrackEntityEvent
Expand All @@ -37,6 +36,7 @@ import org.bukkit.event.block.Action
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockDamageEvent
import org.bukkit.event.block.BlockFromToEvent
import org.bukkit.event.entity.EntityTeleportEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.util.Vector
Expand Down Expand Up @@ -72,6 +72,22 @@ class BlockyFurnitureListener : Listener {
FurniturePacketHelpers.removeLightPacket(baseEntity)
}

@EventHandler
fun EntityTeleportEvent.onTeleportEntity() {
val baseEntity = entity as? ItemDisplay ?: return
FurniturePacketHelpers.removeInteractionHitboxPacket(baseEntity)
FurniturePacketHelpers.removeHitboxOutlinePacket(baseEntity)
FurniturePacketHelpers.removeCollisionHitboxPacket(baseEntity)
FurniturePacketHelpers.removeLightPacket(baseEntity)

blocky.plugin.launch {
delay(2.ticks)
FurniturePacketHelpers.sendInteractionHitboxPackets(baseEntity)
FurniturePacketHelpers.sendCollisionHitboxPacket(baseEntity)
FurniturePacketHelpers.sendLightPacket(baseEntity)
}
}

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
fun PlayerInteractEvent.prePlacingFurniture() {
val (block, item, hand) = (clickedBlock ?: return) to (item ?: return) to (hand ?: return)
Expand Down Expand Up @@ -156,28 +172,6 @@ class BlockyFurnitureListener : Listener {
}
}, blocky.plugin)
}

if (Plugins.isEnabled("AxiomPaper")) {
blocky.logger.s("AxiomPaper detected, enabling AxiomPaper Listener!")
Bukkit.getPluginManager().registerEvents(object : Listener {
@EventHandler
fun AxiomManipulateEntityEvent.onAxiomManipFurniture() {
val baseEntity = entity as? ItemDisplay ?: return

FurniturePacketHelpers.removeInteractionHitboxPacket(baseEntity)
FurniturePacketHelpers.removeHitboxOutlinePacket(baseEntity)
FurniturePacketHelpers.removeCollisionHitboxPacket(baseEntity)
FurniturePacketHelpers.removeLightPacket(baseEntity)

blocky.plugin.launch {
delay(2.ticks)
FurniturePacketHelpers.sendInteractionHitboxPackets(baseEntity)
FurniturePacketHelpers.sendCollisionHitboxPacket(baseEntity)
FurniturePacketHelpers.sendLightPacket(baseEntity)
}
}
}, blocky.plugin)
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
Expand Down

0 comments on commit db519b7

Please sign in to comment.