Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create Nite class #9

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/kotlin/jp/trap/conqest/commands/CommandConQest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package jp.trap.conqest.commands

import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.tree.LiteralCommandNode
import io.papermc.paper.command.brigadier.CommandSourceStack
import jp.trap.conqest.Main
import jp.trap.conqest.game.nite.NormalNite
import net.kyori.adventure.text.Component
import org.bukkit.entity.*

// TODO 仮 本体はNiteManager的なところに保持するべき
var nite: NormalNite? = null

class CommandConQest(val plugin: Main) : Commands.Command {
override fun literalCommandNode(): LiteralCommandNode<CommandSourceStack> {
Expand All @@ -15,6 +21,19 @@ class CommandConQest(val plugin: Main) : Commands.Command {
)
0
}
.then(
io.papermc.paper.command.brigadier.Commands.literal("summon_nite").then(
io.papermc.paper.command.brigadier.Commands.argument("name", StringArgumentType.string())
.executes { ctx ->
if ((ctx.source.sender is Player).not()) return@executes 0
val player = ctx.source.sender as Player
val arg = ctx.getArgument("name", String::class.java)
// TODO: 仮 本来はNiteManager的なところに保持するべき
nite = NormalNite(player.location, arg, plugin)
0
}
)
)
.build()
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/kotlin/jp/trap/conqest/game/Nite.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jp.trap.conqest.game

import net.kyori.adventure.text.Component
import org.bukkit.Location
import org.bukkit.attribute.Attribute
import org.bukkit.entity.Entity
import org.bukkit.entity.EntityType
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Mob
import org.bukkit.plugin.Plugin

abstract class Nite<T>(location: Location, type: EntityType, name: String, plugin: Plugin)
where T : Entity, T : Mob {
private var entity: T = location.world.spawnEntity(
location, type, false
) as T
private var target: LivingEntity? = null
private var attack: Boolean = true
private val speed = 0.5
private val damage = 1.0

init {
entity.customName(Component.text(name))
if (entity.getAttribute(Attribute.ATTACK_DAMAGE) == null) {
entity.registerAttribute(Attribute.ATTACK_DAMAGE)
entity.getAttribute(Attribute.ATTACK_DAMAGE)?.baseValue = damage
}
plugin.server.scheduler.runTaskTimer(plugin, Runnable { update() }, 0, 1)
}

private fun update() {
if (target != null) {
entity.pathfinder.moveTo(target!!, speed)
if (attack && entity.location.distance(target!!.location) <= 3) {
entity.attack(target!!)
}
} else {
entity.pathfinder.stopPathfinding()
}
}

}
10 changes: 10 additions & 0 deletions src/main/kotlin/jp/trap/conqest/game/nite/NormalNite.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package jp.trap.conqest.game.nite

import jp.trap.conqest.game.Nite
import org.bukkit.Location
import org.bukkit.entity.EntityType
import org.bukkit.entity.Villager
import org.bukkit.plugin.Plugin

class NormalNite(location: Location, name: String, plugin: Plugin) :
Nite<Villager>(location, EntityType.VILLAGER, name, plugin)