Skip to content

Commit

Permalink
adding opt packetevents support
Browse files Browse the repository at this point in the history
  • Loading branch information
unldenis committed Nov 3, 2024
1 parent 378f2aa commit 06cf396
Show file tree
Hide file tree
Showing 53 changed files with 764 additions and 1,042 deletions.
19 changes: 19 additions & 0 deletions holoeasy-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</properties>

<repositories>
<!-- This adds the ProtocolLib Maven repository to the build -->
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
Expand All @@ -28,6 +29,16 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>

<!-- This adds the PacketEvents Maven repository to the build -->
<repository>
<id>codemc-releases</id>
<url>https://repo.codemc.io/repository/maven-releases/</url>
</repository>
<repository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository>
</repositories>

<build>
Expand Down Expand Up @@ -146,6 +157,14 @@
<scope>provided</scope>
</dependency>


<dependency>
<groupId>com.github.retrooper</groupId>
<artifactId>packetevents-spigot</artifactId>
<version>2.5.0</version>
<scope>provided</scope>
</dependency>

</dependencies>


Expand Down
15 changes: 14 additions & 1 deletion holoeasy-core/src/main/kotlin/org/holoeasy/HoloEasy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package org.holoeasy
import org.bukkit.plugin.Plugin
import org.holoeasy.action.ClickAction
import org.holoeasy.hologram.Hologram
import org.holoeasy.packet.IPacket
import org.holoeasy.packet.PacketImpl
import org.holoeasy.pool.HologramPool
import org.holoeasy.pool.IHologramPool
import org.holoeasy.pool.InteractiveHologramPool
Expand All @@ -13,16 +15,27 @@ object HoloEasy {

private var PLUGIN: Plugin? = null

private var PACKET_IMPL : IPacket? = null

fun plugin(): Plugin {
if (PLUGIN == null) {
throw IllegalStateException("HoloEasy Plugin is not set")
}
return PLUGIN!!
}

fun packetImpl() : IPacket {
if (PACKET_IMPL == null) {
throw IllegalStateException("HoloEasy PacketImpl is not set")
}
return PACKET_IMPL!!
}

@JvmStatic
fun bind(plugin: Plugin) {
@JvmOverloads
fun bind(plugin: Plugin, packetImpl : PacketImpl = PacketImpl.ProtocolLib) {
this.PLUGIN = plugin
this.PACKET_IMPL = packetImpl.impl
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.holoeasy.animation

import org.bukkit.Bukkit
import org.bukkit.scheduler.BukkitScheduler

import org.bukkit.scheduler.BukkitTask
import org.holoeasy.HoloEasy
import org.holoeasy.ext.send
import org.holoeasy.line.ILine
import org.holoeasy.packet.PacketType

enum class Animations(val task : (ILine<*>) -> BukkitTask) {

Expand All @@ -17,10 +14,13 @@ enum class Animations(val task : (ILine<*>) -> BukkitTask) {


Bukkit.getScheduler().runTaskTimerAsynchronously(HoloEasy.plugin(), java.lang.Runnable {
val packet = PacketType.ROTATE.rotate(line.entityId, yaw = yaw)


holo.pvt.seeingPlayers.forEach { player ->
packet.send(player)

HoloEasy.packetImpl()
.rotate(player, line.entityId, yaw = yaw)

}

yaw += 10
Expand Down
15 changes: 6 additions & 9 deletions holoeasy-core/src/main/kotlin/org/holoeasy/line/BlockLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import org.bukkit.Location
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin
import org.holoeasy.ext.send
import org.holoeasy.packet.PacketType
import org.holoeasy.HoloEasy
import org.holoeasy.reactive.MutableState
import org.holoeasy.util.VersionEnum
import org.holoeasy.util.VersionUtil

class BlockLine(obj: MutableState<ItemStack>) : ILine<ItemStack> {
constructor(obj: ItemStack) : this(MutableState(obj))
Expand Down Expand Up @@ -41,8 +37,9 @@ class BlockLine(obj: MutableState<ItemStack>) : ILine<ItemStack> {

override fun show(player: Player) {
line.spawn(player)
PacketType.METADATA_TEXT
.metadata(entityId, nameTag = null, invisible = true).send(player)

HoloEasy.packetImpl()
.metadataText(player, entityId, nameTag = null, invisible = true)

this.update(player)

Expand All @@ -61,8 +58,8 @@ class BlockLine(obj: MutableState<ItemStack>) : ILine<ItemStack> {
}

override fun update(player: Player) {
PacketType.EQUIPMENT
.equip(entityId, helmet = _mutableStateOf.get()).send(player)
HoloEasy.packetImpl()
.metadataItem(player, entityId, item = _mutableStateOf.get())
}

}
Expand Down
12 changes: 5 additions & 7 deletions holoeasy-core/src/main/kotlin/org/holoeasy/line/ItemLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import org.bukkit.Location
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin
import org.holoeasy.ext.send
import org.holoeasy.packet.PacketType
import org.holoeasy.HoloEasy
import org.holoeasy.reactive.MutableState
import org.holoeasy.util.VersionEnum
import org.holoeasy.util.VersionUtil
Expand All @@ -23,7 +21,6 @@ class ItemLine(item: MutableState<ItemStack>) : ILine<ItemStack> {
}

private val line: Line = Line(EntityType.DROPPED_ITEM)
private val resetVelocity = PacketType.VELOCITY.velocity(line.entityID, 0, 0,0)
private val _mutableStateOf = item
private var firstRender = true

Expand Down Expand Up @@ -51,7 +48,8 @@ class ItemLine(item: MutableState<ItemStack>) : ILine<ItemStack> {
line.spawn(player)
this.update(player)

resetVelocity.send(player)
HoloEasy.packetImpl()
.velocity(player, line.entityID, 0.0, 0.0,0.0)

if(firstRender) {
firstRender = false
Expand All @@ -68,8 +66,8 @@ class ItemLine(item: MutableState<ItemStack>) : ILine<ItemStack> {
}

override fun update(player: Player) {
PacketType.METADATA_ITEM
.metadata(entityId, obj).send(player)
HoloEasy.packetImpl()
.metadataItem(player, entityId, item = obj)
}

}
Expand Down
21 changes: 6 additions & 15 deletions holoeasy-core/src/main/kotlin/org/holoeasy/line/Line.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package org.holoeasy.line

import com.comphenix.protocol.events.PacketContainer
import org.bukkit.Location
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import org.holoeasy.HoloEasy
import org.holoeasy.ext.invoke
import org.holoeasy.packet.IPacket
import org.holoeasy.packet.PacketType
import java.util.*
import java.util.concurrent.atomic.AtomicInteger

Expand All @@ -17,25 +12,21 @@ class Line( private val entityType: EntityType, var location: Location? = null)
companion object {
val IDs_COUNTER = AtomicInteger(Random().nextInt())
}
val plugin: Plugin = HoloEasy.plugin()
val entityID: Int = IDs_COUNTER.getAndIncrement()
private val entityDestroyPacket : PacketContainer = PacketType.DELETE.delete(entityID)

fun destroy(player: Player) {
entityDestroyPacket(player)
HoloEasy.packetImpl()
.deletePacket(player, entityID)
}

fun spawn(player: Player) {
val packet = PacketType.SPAWN
.spawn(entityID, entityType, location ?: throw RuntimeException("Forgot the location?"), plugin)
packet(player)
HoloEasy.packetImpl()
.spawn(player, entityID, entityType,location ?: throw RuntimeException("Forgot the location?") )
}

fun teleport(player: Player) {

val packet = PacketType.TELEPORT
.teleport(entityID, location ?: throw RuntimeException("Forgot the location?"))
packet(player)
HoloEasy.packetImpl()
.teleport(player, entityID, location ?: throw RuntimeException("Forgot the location?"))

}

Expand Down
21 changes: 9 additions & 12 deletions holoeasy-core/src/main/kotlin/org/holoeasy/line/TextLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ package org.holoeasy.line
import org.bukkit.Location
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import org.holoeasy.ext.send
import org.holoeasy.packet.IPacket
import org.holoeasy.packet.PacketType
import org.holoeasy.HoloEasy
import org.holoeasy.reactive.MutableState
import org.holoeasy.util.AABB
import java.util.LinkedHashMap

class TextLine(
obj: String,
Expand Down Expand Up @@ -90,8 +86,9 @@ class TextLine(
isEmpty = obj.isEmpty()
if (!isEmpty) {
line.spawn(player)
val packet = PacketType.METADATA_TEXT.metadata(entityId, parse(player))
packet.send(player)

HoloEasy.packetImpl()
.metadataText(player, entityId, nameTag = parse(player))
}
}

Expand Down Expand Up @@ -119,14 +116,14 @@ class TextLine(
0x01 -> {
line.spawn(player)
isEmpty = false
PacketType.METADATA_TEXT
.metadata(entityId, parse(player)).send(player)

HoloEasy.packetImpl()
.metadataText(player, entityId, nameTag = parse(player))
}

0x00 ->
PacketType.METADATA_TEXT
.metadata(entityId, parse(player), invisible = false)
.send(player)
HoloEasy.packetImpl()
.metadataText(player, entityId, nameTag = parse(player), invisible = false)
}
}

Expand Down
98 changes: 12 additions & 86 deletions holoeasy-core/src/main/kotlin/org/holoeasy/packet/IPacket.kt
Original file line number Diff line number Diff line change
@@ -1,101 +1,27 @@
package org.holoeasy.packet


import org.holoeasy.HoloEasy
import org.holoeasy.packet.delete.DeletePacketA
import org.holoeasy.packet.delete.DeletePacketB
import org.holoeasy.packet.equipment.EquipmentPacketA
import org.holoeasy.packet.equipment.EquipmentPacketB
import org.holoeasy.packet.equipment.EquipmentPacketC
import org.holoeasy.packet.equipment.IEquipmentPacket
import org.holoeasy.packet.metadata.item.*
import org.holoeasy.packet.metadata.text.*
import org.holoeasy.packet.rotate.IRotatePacket
import org.holoeasy.packet.rotate.RotatePacketA
import org.holoeasy.packet.spawn.*
import org.holoeasy.packet.teleport.TeleportPacketA
import org.holoeasy.packet.teleport.TeleportPacketB
import org.holoeasy.packet.velocity.IVelocityPacket
import org.holoeasy.packet.velocity.VelocityPacketA
import org.holoeasy.util.VersionEnum
import org.holoeasy.util.VersionUtil
import org.bukkit.Location
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack

interface IPacket {

val versionSupport: Array<out ClosedRange<VersionEnum>>
fun deletePacket(player: Player, entityId: Int)

fun isCurrentVersion(): Boolean {
for (range in versionSupport) {
if (VersionUtil.CLEAN_VERSION in range) {
return true
}
}
return false
}
fun equip(player: Player, entityId : Int, helmet : ItemStack)

}
fun metadataItem(player: Player,entityId: Int, item: ItemStack)

fun metadataText(player: Player, entityId: Int, nameTag: String?, invisible : Boolean = true)

object PacketType {
private fun <T : IPacket> getCurrImpl(vararg impls: T): T {
val rightImpl = impls.firstOrNull(IPacket::isCurrentVersion)
if (rightImpl != null) {
return rightImpl as T
}
fun rotate(player: Player, entityId : Int, yaw : Double)

if (HoloEasy.useLastSupportedVersion) {
return impls.last() as T
}
fun spawn(player: Player, entityId: Int, entityType: EntityType, location: Location)

throw RuntimeException(
"""
No version support for this packet
Set HoloEasy.useLastSupportedVersion to true or
open an issue at https://github.com/unldenis/holoeasy
""".trimIndent()
)
}
fun teleport(player: Player, entityId: Int, location: Location)

val DELETE by lazy { getCurrImpl(DeletePacketA, DeletePacketB) }

val METADATA_TEXT by lazy {
getCurrImpl(
MetadataTextPacketA,
MetadataTextPacketB,
MetadataTextPacketC,
MetadataTextPacketD,
MetadataTextPacketE
)
}

val METADATA_ITEM by lazy {
getCurrImpl(
MetadataItemPacketA,
MetadataItemPacketB,
MetadataItemPacketC,
MetadataItemPacketD,
MetadataItemPacketE
)
}

val SPAWN by lazy {
getCurrImpl(SpawnPacketA, SpawnPacketB, SpawnPacketC, SpawnPacketD)
}

val TELEPORT by lazy {
getCurrImpl(TeleportPacketA, TeleportPacketB)
}

val VELOCITY by lazy { getCurrImpl<IVelocityPacket>(VelocityPacketA) }

val ROTATE by lazy { getCurrImpl<IRotatePacket>(RotatePacketA) }

val EQUIPMENT by lazy {
getCurrImpl(
EquipmentPacketA,
EquipmentPacketB,
EquipmentPacketC
)
}
fun velocity(player: Player, entityId: Int, x: Double, y : Double, z : Double)

}
Loading

0 comments on commit 06cf396

Please sign in to comment.