Skip to content

Commit

Permalink
Added EntityMoveEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRedMagic committed Jun 16, 2024
1 parent 38cfcaa commit 52309e0
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.redmagic.undefinedapi.customEvents

import com.redmagic.undefinedapi.event.UndefinedEvent
import org.bukkit.Location
import org.bukkit.entity.Entity

class EntityMoveEvent(val entity: Entity, val newLocation: Location): UndefinedEvent() {
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.redmagic.undefinedapi.customEvents

import com.redmagic.undefinedapi.event.UndefinedEvent
import org.bukkit.entity.Player
import org.bukkit.entity.Entity

/**
* Represents an event that is triggered when a player is ignited.
*
* @property player The player that is ignited.
* @constructor Creates a new instance of [PlayerIgniteEvent].
* @constructor Creates a new instance of [EntityIgniteEvent].
*/
class PlayerIgniteEvent(val player: Player): UndefinedEvent()
class EntityIgniteEvent(val entity: Entity): UndefinedEvent()

/**
* Represents a PlayerExtinguishEvent that is triggered when a player's fire is extinguished.
*
* @property player The player affected by the extinguish event.
*/
class PlayerExtinguishEvent(val player: Player): UndefinedEvent()
class EntityExtinguishEvent(val entity: Entity): UndefinedEvent()

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import com.redmagic.undefinedapi.nms.interfaces.NMSLivingEntity
import com.redmagic.undefinedapi.nms.interfaces.NMSPlayer
import java.io.InputStreamReader
import java.net.URI
import kotlin.math.floor


fun NMSLivingEntity.toDeltaValue(value: Double, newValue: Double) = (((newValue - value) * 32 * 128).toInt().toShort())

fun NMSLivingEntity.toRotationValue(rotation: Float) = (((rotation % 360) * 256 / 360).toInt().toByte())
fun NMSLivingEntity.toRotationValue(rotation: Float): Byte = floor(rotation * 256.0f / 360.0f).toInt().toByte()


fun NMSPlayer.getSkinTexture(name: String): Array<String> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.redmagic.undefinedapi.nms.extensions

fun <T : Any> Any.getPrivateField(fieldName: String): T = javaClass.getDeclaredField(fieldName).apply { isAccessible = true }[this] as T
fun <T : Any> Any.getPrivateField(fieldName: String): T = javaClass.getDeclaredField(fieldName).apply { isAccessible = true }[this] as T

fun <T : Any> Any.getPrivateFieldFromSuper(fieldName: String): T = javaClass.superclass.getDeclaredField(fieldName).apply { isAccessible = true }[this] as T
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

apply(plugin = "maven-publish")
val versionVar = "0.5.29"
val versionVar = "0.5.30"
val groupIdVar = "com.redmagic"
val artifactIdVar = "UndefinedAPI"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class UndefinedAPI(javaPlugin: JavaPlugin) {
API(plugin)
adventure = BukkitAudiences.create(javaPlugin)
MenuManager.setup(plugin)
PlayerMoveManager()
PlayerHitByPlayerManager()
BlockProgressManager()
PlayerArmorChangeManager()
Expand Down
5 changes: 2 additions & 3 deletions server/src/main/java/com/redmagic/undefinedapi/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ class Main: JavaPlugin() {
override fun onEnable() {
api = UndefinedAPI(this)

event<PlayerJoinEvent> {

player.location.block.glow(ChatColor.RED, listOf(player), 10000)
event<EntityMoveEvent> {

println("${entity.location}, ${newLocation}")
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object SpigotNMSMappings {
const val ClientboundContainerSetSlotPacketSlot = "e"
const val ClientboundContainerSetSlotPacketItemStack = "f"
const val ServerboundSetCarriedItemPacketSlot = "a"

const val ClientboundMoveEntityPacketEntityID = "a"

const val EntitySetRotMethod = "a"
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,10 @@ open class NMSEntity(open val entityType: EntityType): NMSEntity {

override fun addViewer(player: Player) {
viewers.add(player)
scoreboard.addPlayerToTeam(player.name, team)
}

override fun removeViewer(player: Player) {
viewers.remove(player)
scoreboard.removePlayerFromTeam(player.name)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package com.redmagic.undefinedapi.nms.v1_20_4.event
import com.redmagic.undefinedapi.customEvents.*
import com.redmagic.undefinedapi.nms.v1_20_4.NMSManager
import com.redmagic.undefinedapi.event.event
import com.redmagic.undefinedapi.nms.extensions.getMetaDataInfo
import com.redmagic.undefinedapi.nms.extensions.removeMetaData
import com.redmagic.undefinedapi.nms.extensions.setMetaData
import com.redmagic.undefinedapi.nms.ClickType
import com.redmagic.undefinedapi.nms.EntityInteract
import com.redmagic.undefinedapi.nms.extensions.*
import com.redmagic.undefinedapi.nms.v1_20_4.SpigotNMSMappings
import com.redmagic.undefinedapi.nms.v1_20_4.extensions.*
import com.redmagic.undefinedapi.scheduler.sync
import net.minecraft.network.protocol.Packet
import net.minecraft.network.protocol.game.*
import net.minecraft.world.InteractionHand
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.inventory.EquipmentSlot
import java.util.UUID

/**
* This class represents a packet listener for Minecraft version 1.20.4.
Expand All @@ -30,12 +32,15 @@ class PacketListenerManager {

private val armorSlots: HashMap<Int, Int> = hashMapOf(Pair(4, 39), Pair(5, 38), Pair(6, 37), Pair(7, 36))

private val onFire: MutableList<UUID> = mutableListOf()

private val que = ArrayDeque<Packet<*>>(6)

init {

event<PlayerJoinEvent> {

if (player.fireTicks > 0) {
player.setMetaData("onFire", true)
onFire.add(player.uniqueId)
}

val fakeConnection = player.getConnection().getConnection()
Expand All @@ -61,6 +66,7 @@ class PacketListenerManager {
when (this@UndefinedDuplexHandler) {
is ClientboundSetEntityDataPacket -> handleDataPacket(player, this@UndefinedDuplexHandler)
is ClientboundContainerSetSlotPacket -> handleArmorChange(player, this@UndefinedDuplexHandler)
is ClientboundMoveEntityPacket -> handleEntityMove(this, player.world as CraftWorld)
}

return@UndefinedDuplexHandler false
Expand All @@ -82,6 +88,20 @@ class PacketListenerManager {

}

private fun handleEntityMove(msg: ClientboundMoveEntityPacket, craftWorld: CraftWorld) {

if (!checkQue(msg)) return

val entityID = msg.getPrivateFieldFromSuper<Int>(SpigotNMSMappings.ClientboundMoveEntityPacketEntityID)

sync {
craftWorld.handle.getEntity(entityID)?.bukkitEntity?.apply {

Bukkit.getPluginManager().callEvent(EntityMoveEvent(this@apply, this.location))

}
}
}

private fun handleMainHandSwitch(msg: ServerboundSetCarriedItemPacket, player: Player) {

Expand Down Expand Up @@ -130,7 +150,7 @@ class PacketListenerManager {
list.filter { it.value is Byte }.forEach {

when (it.id) {
0 -> handleFire(player, it.value as Byte)
0 -> handleFire(msg, it.value as Byte, player.world as CraftWorld)
8 -> handleUsingItem(player, (it.value as Byte).toInt())
}
}
Expand All @@ -150,16 +170,21 @@ class PacketListenerManager {
}
}

private fun handleFire(player: Player, value: Byte){
private fun handleFire(msg: ClientboundSetEntityDataPacket, value: Byte, craftWorld: CraftWorld){

if (!checkQue(msg)) return

val entityID = msg.getEntityID()

val entity = craftWorld.handle.getEntity(entityID)

sync {
if (value == 0.toByte()) {
player.getMetaDataInfo("onFire")?.also {
Bukkit.getPluginManager().callEvent(PlayerExtinguishEvent(player))
player.removeMetaData("onFire")
}
} else if (value == 1.toByte() && player.getMetaDataInfo("onFire") == null) {
Bukkit.getPluginManager().callEvent(PlayerIgniteEvent(player))
player.setMetaData("onFire", true)
if (value == 0.toByte() && onFire.contains(entity!!.uuid)) {
Bukkit.getPluginManager().callEvent(EntityExtinguishEvent(entity.bukkitEntity))
onFire.remove(entity.uuid)
} else if (value == 1.toByte() && !onFire.contains(entity!!.uuid)) {
Bukkit.getPluginManager().callEvent(EntityIgniteEvent(entity.bukkitEntity))
onFire.add(entity.uuid)
}
}
}
Expand Down Expand Up @@ -190,5 +215,17 @@ class PacketListenerManager {
}
}


private fun checkQue(packet: Packet<*>): Boolean {
if (que.size == 6) que.removeLast()

for (p in que) {
if (p == packet) return false
}

que.addFirst(packet)
return true
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ object SpigotNMSMappings {
const val ClientboundContainerSetSlotPacketSlot = "f"
const val ClientboundContainerSetSlotPacketItemStack = "g"
const val ServerboundSetCarriedItemPacketSlot = "b"
const val ClientboundMoveEntityPacketEntityID = "a"

const val EntitySetRotMethod = "a"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ import com.redmagic.undefinedapi.event.event
import com.redmagic.undefinedapi.nms.ClickType
import com.redmagic.undefinedapi.nms.EntityInteract
import com.redmagic.undefinedapi.nms.extensions.getMetaDataInfo
import com.redmagic.undefinedapi.nms.extensions.getPrivateFieldFromSuper
import com.redmagic.undefinedapi.nms.extensions.removeMetaData
import com.redmagic.undefinedapi.nms.extensions.setMetaData
import com.redmagic.undefinedapi.nms.v1_20_5.NMSManager
import com.redmagic.undefinedapi.nms.v1_20_5.SpigotNMSMappings
import com.redmagic.undefinedapi.nms.v1_20_5.extensions.*
import com.redmagic.undefinedapi.scheduler.sync
import net.minecraft.network.protocol.Packet
import net.minecraft.network.protocol.game.*
import net.minecraft.world.InteractionHand
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.CraftWorld
import org.bukkit.craftbukkit.entity.CraftPlayer
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.inventory.EquipmentSlot
import java.util.*
import kotlin.collections.ArrayDeque
import kotlin.collections.HashMap

/**
* This class represents a packet listener for Minecraft version 1.20.5.
Expand All @@ -30,6 +37,9 @@ class PacketListenerManager {

private val armorSlots: HashMap<Int, Int> = hashMapOf(Pair(4, 39), Pair(5, 38), Pair(6, 37), Pair(7, 36))

private val onFire: MutableList<UUID> = mutableListOf()

private val que = ArrayDeque<Packet<*>>(6)

init {

Expand Down Expand Up @@ -85,6 +95,21 @@ class PacketListenerManager {
}


private fun handleEntityMove(msg: ClientboundMoveEntityPacket, craftWorld: CraftWorld) {

if (!checkQue(msg)) return

val entityID = msg.getPrivateFieldFromSuper<Int>(SpigotNMSMappings.ClientboundMoveEntityPacketEntityID)

sync {
craftWorld.handle.getEntity(entityID)?.bukkitEntity?.apply {

Bukkit.getPluginManager().callEvent(EntityMoveEvent(this@apply, this.location))

}
}
}

private fun handleMainHandSwitch(msg: ServerboundSetCarriedItemPacket, player: Player) {

val slot = msg.getEntityID()
Expand Down Expand Up @@ -158,11 +183,11 @@ class PacketListenerManager {
sync {
if (value == 0.toByte()) {
player.getMetaDataInfo("onFire")?.also {
Bukkit.getPluginManager().callEvent(PlayerExtinguishEvent(player))
Bukkit.getPluginManager().callEvent(EntityExtinguishEvent(player))
player.removeMetaData("onFire")
}
} else if (value == 1.toByte() && player.getMetaDataInfo("onFire") == null) {
Bukkit.getPluginManager().callEvent(PlayerIgniteEvent(player))
Bukkit.getPluginManager().callEvent(EntityIgniteEvent(player))
player.setMetaData("onFire", true)
}
}
Expand Down Expand Up @@ -199,4 +224,15 @@ class PacketListenerManager {
}
}

private fun checkQue(packet: Packet<*>): Boolean {
if (que.size == 6) que.removeLast()

for (p in que) {
if (p == packet) return false
}

que.addFirst(packet)
return true
}

}

0 comments on commit 52309e0

Please sign in to comment.