From 35aafedac09f551aeb8d88f9ebdc80dc144034d2 Mon Sep 17 00:00:00 2001 From: Yannick Lamprecht Date: Sat, 12 Jan 2019 01:02:03 +0100 Subject: [PATCH] Get rid of interface --- .../github/ysl3000/bukkit/test/ICommand.kt | 11 -- .../bukkit/test/PathfinderTestPlugin.kt | 151 +++++++++--------- 2 files changed, 72 insertions(+), 90 deletions(-) delete mode 100644 PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/ICommand.kt diff --git a/PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/ICommand.kt b/PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/ICommand.kt deleted file mode 100644 index fe5dc81..0000000 --- a/PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/ICommand.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.ysl3000.bukkit.test - -import org.bukkit.entity.Player - -/** - * Created by ysl3000 - */ -interface ICommand { - - fun execute(p: Player, args: List): Boolean -} diff --git a/PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/PathfinderTestPlugin.kt b/PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/PathfinderTestPlugin.kt index 1db0d45..cb87c67 100644 --- a/PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/PathfinderTestPlugin.kt +++ b/PathfinderTestPlugin/src/main/java/com/github/ysl3000/bukkit/test/PathfinderTestPlugin.kt @@ -1,6 +1,7 @@ package com.github.ysl3000.bukkit.test import com.github.ysl3000.bukkit.pathfinding.PathfinderGoalAPI +import com.github.ysl3000.bukkit.pathfinding.entity.Insentient import com.github.ysl3000.bukkit.pathfinding.goals.PathfinderGoalGimmiCookie import com.github.ysl3000.bukkit.pathfinding.goals.PathfinderGoalMoveToLocation import com.github.ysl3000.bukkit.pathfinding.goals.TalkToStrangers @@ -25,16 +26,16 @@ class PathfinderTestPlugin : JavaPlugin(), Listener { private var pathfinderManager: PathfinderManager? = null - private val commandMap = HashMap() + private val commandMap = HashMap) -> Boolean>() override fun onEnable() { this.pathfinderManager = PathfinderGoalAPI.getAPI() - commandMap["chat"] = Chat(pathfinderManager!!) - commandMap["cookie"] = DeliverCookie(pathfinderManager!!) - commandMap["move"] = MoveToLocation(pathfinderManager!!) - commandMap["print"] = PrintGoal(pathfinderManager!!) + commandMap["chat"] = this::chatCommand + commandMap["cookie"] = this::deliverCookieCommand + commandMap["move"] = this::moveToLocationCommand + commandMap["print"] = this::printGoalCommand Bukkit.getServer().pluginManager.registerEvents(this, this) } @@ -60,108 +61,100 @@ class PathfinderTestPlugin : JavaPlugin(), Listener { } val finalParam = param - Bukkit.getScheduler().runTask(this) { - - iCommand?.execute(player, finalParam) - - } + Bukkit.getScheduler().runTask(this, { + iCommand?.invoke(player, finalParam) + } as Runnable) } } - private inner class Chat internal constructor(private val pathfinderManager: PathfinderManager) : ICommand { - - - override fun execute(p: Player, args: List): Boolean { - - val creature = p.world.spawn(p.location, Zombie::class.java) - val pathfinderGoalEntity = this.pathfinderManager.getPathfindeGoalEntity(creature) - pathfinderGoalEntity.clearPathfinderGoals() - pathfinderGoalEntity.clearTargetPathfinderGoals() - pathfinderGoalEntity.addPathfinderGoal(0, - TalkToStrangers(pathfinderGoalEntity, args, TimeUnit.SECONDS.toMillis(20))) - return true + private fun clear(insentient: Insentient?) { + insentient?.let { + insentient.clearPathfinderGoals() + insentient.clearTargetPathfinderGoals() } } - private inner class DeliverCookie internal constructor(private val pathfinderManager: PathfinderManager) : ICommand { + private fun chatCommand(p: Player, args: List): Boolean { + val creature = p.world.spawn(p.location, Zombie::class.java) + val pathfinderGoalEntity = this.pathfinderManager?.getPathfindeGoalEntity(creature) + clear(pathfinderGoalEntity) + pathfinderGoalEntity?.let { insentient: Insentient -> + { - override fun execute(p: Player, args: List): Boolean { + insentient.addPathfinderGoal(0, + TalkToStrangers(insentient, args, TimeUnit.SECONDS.toMillis(20))) + } + } - val creature = p.world - .spawn(Location(p.world, 235.0, 70.0, 246.0), Zombie::class.java) - val pathfinderGoalEntity = this.pathfinderManager.getPathfindeGoalEntity(creature) + return true + } - pathfinderGoalEntity.clearPathfinderGoals() - pathfinderGoalEntity.clearTargetPathfinderGoals() - pathfinderGoalEntity - .addPathfinderGoal(0, PathfinderGoalGimmiCookie(pathfinderGoalEntity, creature)) + private fun deliverCookieCommand(p: Player, args: List): Boolean { - return true - } + val creature = p.world + .spawn(Location(p.world, 235.0, 70.0, 246.0), Zombie::class.java) + val pathfinderGoalEntity = this.pathfinderManager?.getPathfindeGoalEntity(creature) + clear(pathfinderGoalEntity) + pathfinderGoalEntity?.addPathfinderGoal(0, PathfinderGoalGimmiCookie(pathfinderGoalEntity, creature)) + return true } - private inner class MoveToLocation internal constructor(private val pathfinderManager: PathfinderManager) : ICommand { - override fun execute(p: Player, args: List): Boolean { + private fun moveToLocationCommand(p: Player, args: List): Boolean { - val creature = p.world.spawn(p.location, Zombie::class.java) - val pathfinderGoalEntity = this.pathfinderManager.getPathfindeGoalEntity(creature) - pathfinderGoalEntity.clearPathfinderGoals() - pathfinderGoalEntity.clearTargetPathfinderGoals() - pathfinderGoalEntity.addPathfinderGoal(0, - PathfinderGoalMoveToLocation( - pathfinderGoalEntity, Location(p.world, 235.0, 70.0, 246.0), - 2.0, 0.0) - ) + val creature = p.world.spawn(p.location, Zombie::class.java) + val pathfinderGoalEntity = this.pathfinderManager?.getPathfindeGoalEntity(creature) - return true - } + clear(pathfinderGoalEntity) + pathfinderGoalEntity?.addPathfinderGoal(0, + PathfinderGoalMoveToLocation( + pathfinderGoalEntity, Location(p.world, 235.0, 70.0, 246.0), + 2.0, 0.0)) + return true } - private inner class PrintGoal(private val pathfinderManager: PathfinderManager) : ICommand { - override fun execute(p: Player, args: List): Boolean { - val creature = p.world.spawn(p.location, Zombie::class.java) + private fun printGoalCommand(p: Player, args: List): Boolean { + val creature = p.world.spawn(p.location, Zombie::class.java) - val pathfinderGoalEntity = this.pathfinderManager.getPathfindeGoalEntity(creature) - pathfinderGoalEntity.clearPathfinderGoals() - pathfinderGoalEntity.clearTargetPathfinderGoals() - pathfinderGoalEntity.addPathfinderGoal(0, - PathfinderGoalPrint() - ) + val pathfinderGoalEntity = this.pathfinderManager?.getPathfindeGoalEntity(creature) + clear(pathfinderGoalEntity) + pathfinderGoalEntity?.addPathfinderGoal(0, + PathfinderGoalPrint() + ) - return true - } + return true + } - private inner class PathfinderGoalPrint : PathfinderGoal { + private inner class PathfinderGoalPrint : PathfinderGoal { - private var shE = true - private var shT = true + private var shE = true + private var shT = true - override fun shouldExecute(): Boolean { - println("called should execute") - shE = shE.not() - return shE - } + override fun shouldExecute(): Boolean { + println("called should execute") + shE = shE.not() + return shE + } - override fun shouldTerminate(): Boolean { - println("Called should terminate") - shT = shE.not() - return shT - } + override fun shouldTerminate(): Boolean { + println("Called should terminate") + shT = shE.not() + return shT + } - override fun init() { - println("Called Init") - } + override fun init() { + println("Called Init") + } - override fun execute() { - println("Called execute") - } + override fun execute() { + println("Called execute") + } - override fun reset() { - println("Called reset") - } + override fun reset() { + println("Called reset") } } + }