Skip to content

Commit

Permalink
NPC Interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRedMagic committed Jun 2, 2024
1 parent abae4de commit c01961e
Show file tree
Hide file tree
Showing 94 changed files with 227 additions and 15 deletions.
4 changes: 4 additions & 0 deletions api/src/main/java/com/redmagic/undefinedapi/nms/NMSPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ interface NMSPlayer: NMSEntity {
*/
fun resetPose()

fun interact(interact: PlayerInteract.() -> Unit)

fun getEntityID(): Int

/**
* Triggers a damage animation for the entity.
*
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/com/redmagic/undefinedapi/nms/PlayerInteract.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.redmagic.undefinedapi.nms

class PlayerInteract(val actionType: ClickType, val nmsPlayer: NMSPlayer)

enum class ClickType {

RIGHT_CLICK,
LEFT_CLICK

}
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.00"
val versionVar = "0.5.01"
val groupIdVar = "com.redmagic"
val artifactIdVar = "UndefinedAPI"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ object MenuManager : Listener {
if (!player.hasMenuOpen()) return
if (e.clickedInventory == null) return
if (e.clickedInventory!!.type != InventoryType.CHEST) return
e.view.title
val menu = player.getMenu()!!
if (menu is UndefinedPageMenu){
runPageMenu(menu, e)
Expand Down
2 changes: 1 addition & 1 deletion server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ tasks {
}

runServer {
minecraftVersion("1.20.5")
minecraftVersion("1.20.6")
}

}
Expand Down
23 changes: 17 additions & 6 deletions server/src/main/java/com/redmagic/undefinedapi/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import com.redmagic.undefinedapi.scheduler.delay
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.PlayerInventory
import org.bukkit.plugin.java.JavaPlugin

class Main: JavaPlugin() {
Expand All @@ -20,6 +23,20 @@ class Main: JavaPlugin() {
override fun onEnable() {
api = UndefinedAPI(this)

event<PlayerJoinEvent> {
val npc = api.createFakePlayer("the", "the")!!
npc.viewers.add(player)
npc.spawn(player.location)
npc.interact {

println(actionType)

}
}

}

fun test(player : Player){
event<PlayerJoinEvent> {

val npc = api.createFakePlayer("TheRedMagic", "TheRedMagic")!!
Expand All @@ -33,7 +50,6 @@ class Main: JavaPlugin() {

npc.resetPose()


delay(20) {
npc.teleport(newLoc)
delay(20) {
Expand Down Expand Up @@ -62,10 +78,5 @@ class Main: JavaPlugin() {
}

}

event<PlayerArmSwingEvent> {
println(hand)
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.redmagic.undefinedapi

import com.redmagic.undefinedapi.nms.NMSPlayer
import com.redmagic.undefinedapi.nms.PlayerInteract

object NMSManager1_20_4 {

val npcInteraction: HashMap<NMSPlayer, PlayerInteract.() -> Unit> = hashMapOf()

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer
import org.bukkit.entity.Player


fun Player.getConnection(): ServerGamePacketListenerImpl = (this as CraftPlayer).handle.connection
fun Player.getConnection(): ServerGamePacketListenerImpl = (this as CraftPlayer).handle.connection

fun isPaper(): Boolean {
val hasClass = { name: String -> try { Class.forName(name); true } catch (e: ClassNotFoundException) { false } }

return if (hasClass("com.destroystokyo.paper.PaperConfig") || hasClass("io.papermc.paper.configuration.Configuration")) true else false
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.redmagic.undefinedapi.event

import com.redmagic.undefinedapi.NMSManager1_20_4
import com.redmagic.undefinedapi.customEvents.PlayerArmSwingEvent
import com.redmagic.undefinedapi.getConnection
import com.redmagic.undefinedapi.isPaper
import com.redmagic.undefinedapi.nms.ClickType
import com.redmagic.undefinedapi.nms.PlayerInteract
import io.netty.channel.ChannelDuplexHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.ServerboundInteractPacket
import net.minecraft.network.protocol.game.ServerboundSwingPacket
import net.minecraft.server.network.ServerCommonPacketListenerImpl
import net.minecraft.world.InteractionHand
Expand Down Expand Up @@ -34,6 +39,10 @@ class PacketListenerManager1_20_4 {
val event = PlayerArmSwingEvent(player, if (msg.hand.equals(InteractionHand.MAIN_HAND)) EquipmentSlot.HAND else EquipmentSlot.OFF_HAND)
Bukkit.getPluginManager().callEvent(event)
if (event.isCancelled) return
}else if (msg is ServerboundInteractPacket){



}

super.channelRead(ctx, msg)
Expand Down Expand Up @@ -67,4 +76,34 @@ class PacketListenerManager1_20_4 {

}

}
fun handelNPCInteract(msg: ServerboundInteractPacket){
val actionID = ServerboundInteractPacket::class.java.getDeclaredField("b")
actionID.isAccessible = true
val ob: Any = actionID.get(msg)
val firstChar = ob.toString().split("$")[1][0]
val attacking = if (isPaper()) msg.isAttack else firstChar == '1'

if (firstChar == 'e') { return }

if (!attacking){

val handField = ob.javaClass.getDeclaredField("a")
handField.isAccessible = true
val string = handField.get(ob).toString()
if (string != "MAIN_HAND") { return }
}

val idField = ServerboundInteractPacket::class.java.getDeclaredField("a")
idField.isAccessible = true
val id = idField.get(msg) as Int
val action = if(attacking) ClickType.LEFT_CLICK else ClickType.RIGHT_CLICK

NMSManager1_20_4.npcInteraction.entries.forEach {
if (it.key.getEntityID() == id) {
it.value.invoke(PlayerInteract(action, it.key))
}
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,12 @@ class NMSPlayer1_20_4(name: String, skin: String): NMSPlayer {

}

override fun interact(interact: PlayerInteract.() -> Unit) {
NMSManager1_20_4.npcInteraction[this] = interact
}

override fun getEntityID(): Int = if (serverPlayer == null) 0 else serverPlayer!!.id

/**
* Resets the pose of the player.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.redmagic.undefinedapi

import com.redmagic.undefinedapi.nms.NMSPlayer
import com.redmagic.undefinedapi.nms.PlayerInteract

object NMSManager1_20_5 {

val npcInteraction: HashMap<NMSPlayer, PlayerInteract.() -> Unit> = hashMapOf()

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.redmagic.undefinedapi

import net.minecraft.network.protocol.game.ServerboundSwingPacket
import net.minecraft.server.network.ServerGamePacketListenerImpl
import org.bukkit.craftbukkit.entity.CraftPlayer
import org.bukkit.entity.Player


fun Player.getConnection(): ServerGamePacketListenerImpl = (this as CraftPlayer).handle.connection
fun Player.getConnection(): ServerGamePacketListenerImpl = (this as CraftPlayer).handle.connection

fun isRemapped(): Boolean = ServerboundSwingPacket::class.java.name == "net.minecraft.network.protocol.game.ServerboundSwingPacket"
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.redmagic.undefinedapi.event

import com.redmagic.undefinedapi.NMSManager1_20_5
import com.redmagic.undefinedapi.customEvents.PlayerArmSwingEvent
import com.redmagic.undefinedapi.getConnection
import com.redmagic.undefinedapi.isRemapped
import com.redmagic.undefinedapi.nms.ClickType
import com.redmagic.undefinedapi.nms.PlayerInteract
import io.netty.channel.ChannelDuplexHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import net.minecraft.network.Connection
import net.minecraft.network.protocol.game.ServerboundInteractPacket
import net.minecraft.network.protocol.game.ServerboundSwingPacket
import net.minecraft.server.network.ServerCommonPacketListenerImpl
import net.minecraft.world.InteractionHand
Expand Down Expand Up @@ -34,6 +39,8 @@ class PacketListenerManager1_20_5 {
val event = PlayerArmSwingEvent(player, if (msg.hand.equals(InteractionHand.MAIN_HAND)) EquipmentSlot.HAND else EquipmentSlot.OFF_HAND)
Bukkit.getPluginManager().callEvent(event)
if (event.isCancelled) return
}else if (msg is ServerboundInteractPacket){
handelNPCIntercart(msg)
}

super.channelRead(ctx, msg)
Expand Down Expand Up @@ -66,4 +73,39 @@ class PacketListenerManager1_20_5 {

}

fun handelNPCIntercart(msg: ServerboundInteractPacket){
val actionID = ServerboundInteractPacket::class.java.getDeclaredField("c")
actionID.isAccessible = true
val ob: Any = actionID.get(msg)

val firstChar = ob.toString().split("$")[1][0]
val attacking = if(isRemapped()) msg.isAttack else firstChar == '1'


if(isRemapped()){
if (ob.toString().contains("InteractionAction")) { return }
}else{
if (firstChar != 'e' && firstChar != '1') { return }
}

if (!attacking){

val handField = ob.javaClass.getDeclaredField("a")
handField.isAccessible = true
val string = handField.get(ob).toString()
if (string != "MAIN_HAND") { return }
}

val idField = ServerboundInteractPacket::class.java.getDeclaredField("b")
idField.isAccessible = true
val id = idField.get(msg) as Int
val action = if(attacking) ClickType.LEFT_CLICK else ClickType.RIGHT_CLICK

NMSManager1_20_5.npcInteraction.entries.forEach {
if (it.key.getEntityID() == id) {
it.value.invoke(PlayerInteract(action, it.key))
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.redmagic.undefinedapi.npc
import com.mojang.authlib.GameProfile
import com.mojang.authlib.properties.Property
import com.mojang.datafixers.util.Pair
import com.redmagic.undefinedapi.NMSManager1_20_5
import com.redmagic.undefinedapi.getConnection
import com.redmagic.undefinedapi.nms.*
import com.redmagic.undefinedapi.scheduler.delay
Expand Down Expand Up @@ -635,6 +636,11 @@ class NMSPlayer1_20_5(name: String, skin: String): NMSPlayer {
isCrouching = false
}

override fun interact(interact: PlayerInteract.() -> Unit) {
NMSManager1_20_5.npcInteraction[this] = interact
}
override fun getEntityID(): Int = if (serverPlayer == null) 0 else serverPlayer!!.id

/**
* Returns whether the player is alive.
*
Expand Down
4 changes: 2 additions & 2 deletions v1_20_6/.gradle/caches/paperweight/taskCache/reobfJar.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Command: C:\Users\lijnb\.jdks\corretto-21.0.3\bin\java.exe -Xmx1G -classpath C:\Users\lijnb\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.2\7b43b2f5baba09a38834e317ea020f82aa78ddd6\tiny-remapper-0.10.2-fat.jar net.fabricmc.tinyremapper.Main C:\Users\lijnb\IdeaProjects\UndefinedAPI\v1_20_6\build\libs\v1_20_6-0.4.49-dev.jar C:\Users\lijnb\IdeaProjects\UndefinedAPI\v1_20_6\build\libs\v1_20_6-0.4.49.jar C:\Users\lijnb\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.6-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\lijnb\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.6-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 1699,82 ms.
Command: C:\Users\lijnb\.jdks\corretto-21.0.3\bin\java.exe -Xmx1G -classpath C:\Users\lijnb\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.2\7b43b2f5baba09a38834e317ea020f82aa78ddd6\tiny-remapper-0.10.2-fat.jar net.fabricmc.tinyremapper.Main C:\Users\lijnb\IdeaProjects\UndefinedAPI\v1_20_6\build\libs\v1_20_6-0.5.00-dev.jar C:\Users\lijnb\IdeaProjects\UndefinedAPI\v1_20_6\build\libs\v1_20_6-0.5.00.jar C:\Users\lijnb\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.6-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\lijnb\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.6-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 1484,59 ms.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
5
14
0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified v1_20_6/build/kotlin/compileKotlin/cacheable/last-build.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.redmagic.undefinedapi

import com.redmagic.undefinedapi.nms.NMSPlayer
import com.redmagic.undefinedapi.nms.PlayerInteract

object NMSManager1_20_6 {

val npcInteraction: HashMap<NMSPlayer, PlayerInteract.() -> Unit> = hashMapOf()

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.redmagic.undefinedapi

import net.minecraft.network.protocol.game.ServerboundSwingPacket
import net.minecraft.server.network.ServerGamePacketListenerImpl
import org.bukkit.craftbukkit.entity.CraftPlayer
import org.bukkit.entity.Player


fun Player.getConnection(): ServerGamePacketListenerImpl = (this as CraftPlayer).handle.connection
fun Player.getConnection(): ServerGamePacketListenerImpl = (this as CraftPlayer).handle.connection

fun isRemapped(): Boolean = ServerboundSwingPacket::class.java.name == "net.minecraft.network.protocol.game.ServerboundSwingPacket"
Loading

0 comments on commit c01961e

Please sign in to comment.