From b3bdcaa8222981a6f0421c4895d617989db1b4b6 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 3 Sep 2019 21:17:54 +0200 Subject: [PATCH] Changed to class for every command --- .../kotlin/net/trivernis/superutils/Main.kt | 53 ++++++++----------- .../trivernis/superutils/commands/CommandC.kt | 27 ++++++++++ .../trivernis/superutils/commands/CommandH.kt | 32 +++++++++++ 3 files changed, 81 insertions(+), 31 deletions(-) create mode 100644 src/main/kotlin/net/trivernis/superutils/commands/CommandC.kt create mode 100644 src/main/kotlin/net/trivernis/superutils/commands/CommandH.kt diff --git a/src/main/kotlin/net/trivernis/superutils/Main.kt b/src/main/kotlin/net/trivernis/superutils/Main.kt index a9f8adc..2b950a1 100644 --- a/src/main/kotlin/net/trivernis/superutils/Main.kt +++ b/src/main/kotlin/net/trivernis/superutils/Main.kt @@ -1,49 +1,40 @@ package net.trivernis.superutils import com.earth2me.essentials.Essentials -import com.earth2me.essentials.Trade -import org.bukkit.GameMode -import org.bukkit.command.Command -import org.bukkit.command.CommandSender -import org.bukkit.entity.Player -import org.bukkit.event.player.PlayerTeleportEvent +import net.trivernis.superutils.commands.CommandC +import net.trivernis.superutils.commands.CommandH import org.bukkit.plugin.java.JavaPlugin -import org.bukkit.potion.PotionEffectType class Main : JavaPlugin() { + /** + * Executed on plugin enable + */ override fun onEnable() { + val c = getCommand("c") + if (c != null) { + c.setExecutor(CommandC()) + } + val essentials = getEssentials() + if (essentials != null) { + val h = getCommand("h") + if (h != null) { + h.setExecutor(CommandH(essentials)) + } + } } + /** + * Executed on plugin disable + */ override fun onDisable() { } - override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { - if (sender is Player) { - if (command.name.equals("c", ignoreCase = true) && sender.hasPermission("superutils.c")) { - if (sender.gameMode != GameMode.SPECTATOR) { - sender.gameMode = GameMode.SPECTATOR - sender.addPotionEffect(PotionEffectType.NIGHT_VISION.createEffect(1000000, 255)) - } else { - sender.removePotionEffect(PotionEffectType.NIGHT_VISION) - sender.gameMode = GameMode.SURVIVAL - } - return true - } else if (command.name.equals("h", ignoreCase = true) && sender.hasPermission("superutils.h")) { - val essentials = getEssentials() - if (essentials != null) { - val essUser = essentials.getUser(sender); - val userHome = essUser.getHome("home"); - essUser.teleport.teleport(userHome.block.location, null, PlayerTeleportEvent.TeleportCause.COMMAND); - essUser.sendMessage("You have been teleported home."); - } - } - } - return false; - } - + /** + * Returns instance of the essentials plugin + */ private fun getEssentials(): Essentials? { val essentials = server.pluginManager.getPlugin("Essentials"); return if (essentials != null && essentials is Essentials) diff --git a/src/main/kotlin/net/trivernis/superutils/commands/CommandC.kt b/src/main/kotlin/net/trivernis/superutils/commands/CommandC.kt new file mode 100644 index 0000000..83cc8e3 --- /dev/null +++ b/src/main/kotlin/net/trivernis/superutils/commands/CommandC.kt @@ -0,0 +1,27 @@ +package net.trivernis.superutils.commands + +import org.bukkit.GameMode +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.potion.PotionEffectType + +class CommandC: CommandExecutor { + /** + * Sets the users gamemode to spectator with nightvision or back to survival + */ + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (sender is Player && command.testPermission(sender)) { + if (sender.gameMode != GameMode.SPECTATOR) { + sender.gameMode = GameMode.SPECTATOR + sender.addPotionEffect(PotionEffectType.NIGHT_VISION.createEffect(1000000, 255)) + } else { + sender.removePotionEffect(PotionEffectType.NIGHT_VISION) + sender.gameMode = GameMode.SURVIVAL + } + return true; + } + return false; + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/trivernis/superutils/commands/CommandH.kt b/src/main/kotlin/net/trivernis/superutils/commands/CommandH.kt new file mode 100644 index 0000000..42e5bfa --- /dev/null +++ b/src/main/kotlin/net/trivernis/superutils/commands/CommandH.kt @@ -0,0 +1,32 @@ +package net.trivernis.superutils.commands + +import com.earth2me.essentials.Essentials +import com.earth2me.essentials.UserData +import org.bukkit.Location +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.event.player.PlayerTeleportEvent + +class CommandH(private var essentials: Essentials) : CommandExecutor { + + /** + * Teleports the user to the default home or a specified one + */ + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (sender is Player && command.testPermission(sender)) { + val essUser = essentials.getUser(sender) + val userHome: Location + userHome = if (args.isNotEmpty()) { + essUser.getHome(args[0]) + } else { + essUser.getHome("home") + } + essUser.teleport.teleport(userHome.block.location, null, PlayerTeleportEvent.TeleportCause.COMMAND) + essUser.sendMessage("You have been teleported home.") + return true + } + return false + } +} \ No newline at end of file