diff --git a/ontime-velocity/src/main/java/org/anvilpowered/ontime/velocity/command/VelocityOnTimeAddCommand.java b/ontime-api/src/main/java/org/anvilpowered/ontime/api/plugin/PluginMessages.java similarity index 62% rename from ontime-velocity/src/main/java/org/anvilpowered/ontime/velocity/command/VelocityOnTimeAddCommand.java rename to ontime-api/src/main/java/org/anvilpowered/ontime/api/plugin/PluginMessages.java index 8ffc5a0..7222463 100644 --- a/ontime-velocity/src/main/java/org/anvilpowered/ontime/velocity/command/VelocityOnTimeAddCommand.java +++ b/ontime-api/src/main/java/org/anvilpowered/ontime/api/plugin/PluginMessages.java @@ -16,15 +16,9 @@ * along with this program. If not, see . */ -package org.anvilpowered.ontime.velocity.command; +package org.anvilpowered.ontime.api.plugin; -import com.velocitypowered.api.command.Command; -import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.proxy.Player; -import net.kyori.adventure.text.TextComponent; -import org.anvilpowered.ontime.common.command.CommonOnTimeAddCommand; +public interface PluginMessages { -public class VelocityOnTimeAddCommand - extends CommonOnTimeAddCommand - implements Command { + TString getNoPermission(); } diff --git a/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeAddCommand.java b/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonAddCommand.kt similarity index 74% rename from ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeAddCommand.java rename to ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonAddCommand.kt index a4c4989..38174b5 100644 --- a/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeAddCommand.java +++ b/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonAddCommand.kt @@ -15,13 +15,11 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ +package org.anvilpowered.ontime.common.command -package org.anvilpowered.ontime.common.command; - -public class CommonOnTimeAddCommand - extends CommonOnTimeTwoArgCommand { - - public void execute(TCommandSource source, String[] context) { - execute(source, context, "add", memberManager::addBonusTime); +open class CommonAddCommand : + CommonTwoArgCommand() { + open fun execute(source: TCommandSource, context: Array) { + execute(source, context, "add", memberManager::addBonusTime) } } diff --git a/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonCheckCommand.kt b/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonCheckCommand.kt new file mode 100644 index 0000000..a5a250d --- /dev/null +++ b/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonCheckCommand.kt @@ -0,0 +1,115 @@ +/* + * OnTime - AnvilPowered + * Copyright (C) 2020 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.anvilpowered.ontime.common.command + +import com.google.inject.Inject +import org.anvilpowered.anvil.api.plugin.PluginInfo +import org.anvilpowered.anvil.api.registry.Registry +import org.anvilpowered.anvil.api.util.PermissionService +import org.anvilpowered.anvil.api.util.TextService +import org.anvilpowered.anvil.api.util.UserService +import org.anvilpowered.ontime.api.member.MemberManager +import org.anvilpowered.ontime.api.plugin.PluginMessages +import org.anvilpowered.ontime.api.registry.OnTimeKeys +import java.util.Optional +import java.util.UUID +import java.util.concurrent.CompletableFuture + +open class CommonCheckCommand< + TUser : Any, + TPlayer : Any, + TString : Any, + TCommandSource : Any + >( + private val playerClass: Class +) { + + @Inject + protected lateinit var memberManager: MemberManager + + @Inject + private lateinit var permissionService: PermissionService + + @Inject + private lateinit var pluginInfo: PluginInfo + + @Inject + private lateinit var pluginMessages: PluginMessages + + @Inject + protected lateinit var registry: Registry + + @Inject + protected lateinit var textService: TextService + + @Inject + private lateinit var userService: UserService + + private val error: TString by lazy { + textService.builder() + .append(pluginInfo.prefix) + .red().append("Specify user or run as player!") + .build() + } + + private fun testPermission(source: Any?): Boolean { + return permissionService.hasPermission(source ?: return false, registry.getOrDefault(OnTimeKeys.CHECK_PERMISSION)) + } + + private fun hasNoPerms(source: TCommandSource): Boolean { + if (!testPermission(source)) { + textService.send(pluginMessages.noPermission, source) + return true + } + return false + } + + open fun execute( + source: TCommandSource, + context: Array + ) { + if (hasNoPerms(source)) return + val isPlayer = playerClass.isAssignableFrom(source.javaClass) + val hasExtended = permissionService.hasPermission(source, registry.getOrDefault(OnTimeKeys.CHECK_EXTENDED_PERMISSION)) + val futureUUID = if (context.size == 1 && hasExtended) { + userService.getUUID(context[0]) + } else { + CompletableFuture.completedFuture(Optional.empty()) + } + futureUUID.thenAcceptAsync { optionalUserUUID: Optional -> + if (optionalUserUUID.isPresent) { + memberManager.infoExtended(optionalUserUUID.get()) + .thenAcceptAsync { textService.send(it, source) } + } else if (isPlayer) { + val userUUID = userService.getUUID(source as TUser) + if (hasExtended) { + memberManager.infoExtended(userUUID) + } else { + memberManager.info(userUUID) + }.thenAcceptAsync { textService.send(it, source) } + } else { + textService.send(error, source) + } + } + } + + open fun suggest(source: TCommandSource, context: Array): List { + if (!testPermission(source)) return listOf() + return userService.matchPlayerNames(context, 0, 1) + } +} diff --git a/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeCheckCommand.java b/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeCheckCommand.java deleted file mode 100644 index eb8ab6a..0000000 --- a/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeCheckCommand.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * OnTime - AnvilPowered - * Copyright (C) 2020 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package org.anvilpowered.ontime.common.command; - -import com.google.inject.Inject; -import org.anvilpowered.anvil.api.plugin.PluginInfo; -import org.anvilpowered.anvil.api.registry.Registry; -import org.anvilpowered.anvil.api.util.PermissionService; -import org.anvilpowered.anvil.api.util.TextService; -import org.anvilpowered.anvil.api.util.UserService; -import org.anvilpowered.ontime.api.member.MemberManager; -import org.anvilpowered.ontime.api.registry.OnTimeKeys; - -import java.util.Optional; -import java.util.UUID; -import java.util.concurrent.CompletableFuture; - -@SuppressWarnings("unchecked") -public class CommonOnTimeCheckCommand { - - @Inject - protected MemberManager memberManager; - - @Inject - private PermissionService permissionService; - - @Inject - private PluginInfo pluginInfo; - - @Inject - protected Registry registry; - - @Inject - private TextService textService; - - @Inject - private UserService userService; - - public void sendError(TCommandSource source) { - textService.builder() - .append(pluginInfo.getPrefix()) - .red().append("Specify user or run as player!") - .sendTo(source); - } - - public void execute(TCommandSource source, String[] context, - Class playerClass) { - final boolean isPlayer = playerClass.isAssignableFrom(source.getClass()); - if (!permissionService.hasPermission(source, - registry.getOrDefault(OnTimeKeys.CHECK_PERMISSION))) { - textService.builder() - .append(pluginInfo.getPrefix()) - .red().append("You do not have permission for this command!") - .sendTo(source); - return; - } - final boolean hasExtended = permissionService.hasPermission(source, - registry.getOrDefault(OnTimeKeys.CHECK_EXTENDED_PERMISSION)); - - CompletableFuture> futureUUID; - if (context.length == 1 && hasExtended) { - futureUUID = userService.getUUID(context[0]); - } else { - futureUUID = CompletableFuture.completedFuture(Optional.empty()); - } - futureUUID.thenAcceptAsync(optionalUserUUID -> { - if (optionalUserUUID.isPresent()) { - memberManager.infoExtended(optionalUserUUID.get()) - .thenAcceptAsync(result -> textService.send(result, source)); - } else if (isPlayer) { - UUID userUUID = userService.getUUID((TUser) source); - if (hasExtended) { - memberManager.infoExtended(userUUID) - .thenAcceptAsync(result -> textService.send(result, source)); - } else { - memberManager.info(userUUID) - .thenAcceptAsync(result -> textService.send(result, source)); - } - } else { - sendError(source); - } - }); - } -} diff --git a/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeCommandNode.java b/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeCommandNode.java deleted file mode 100644 index b90eee7..0000000 --- a/ontime-common/src/main/java/org/anvilpowered/ontime/common/command/CommonOnTimeCommandNode.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * OnTime - AnvilPowered - * Copyright (C) 2020 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -package org.anvilpowered.ontime.common.command; - -import org.anvilpowered.anvil.api.Environment; -import org.anvilpowered.anvil.api.command.CommandNode; -import org.anvilpowered.anvil.api.command.CommandService; -import org.anvilpowered.anvil.api.registry.Registry; -import org.anvilpowered.ontime.common.plugin.OnTimePluginInfo; - -import javax.inject.Inject; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.Function; -import java.util.function.Predicate; - -public abstract class CommonOnTimeCommandNode - implements CommandNode { - - protected static final List ADD_ALIAS = Arrays.asList("add", "a"); - protected static final List CHECK_ALIAS = Arrays.asList("check", "c", "info", "i"); - protected static final List IMPORT_ALIAS = Collections.singletonList("import"); - protected static final List SET_BONUS_ALIAS = Arrays.asList("setbonus", "sb"); - protected static final List SET_TOTAL_ALIAS = Arrays.asList("set", "s", "settotal", "st"); - protected static final List HELP_ALIAS = Arrays.asList("help", "h"); - protected static final List VERSION_ALIAS = Arrays.asList("version", "v"); - - protected static final String ADD_DESCRIPTION = "Add bonus time to a player"; - protected static final String CHECK_DESCRIPTION = "Check playtime"; - protected static final String IMPORT_DESCRIPTION = "Import data from rankupper"; - protected static final String SET_BONUS_DESCRIPTION = "Set bonus playtime"; - protected static final String SET_TOTAL_DESCRIPTION = "Set total playtime"; - protected static final String HELP_DESCRIPTION = "Shows this help page."; - protected static final String VERSION_DESCRIPTION = "Shows the plugin version"; - protected static final String ROOT_DESCRIPTION = String.format("%s root command", OnTimePluginInfo.name); - - protected static final String ADD_USAGE = "