Skip to content

Commit

Permalink
Rework commands and move to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstaeding committed Nov 26, 2020
1 parent fc25f0e commit 284387c
Show file tree
Hide file tree
Showing 41 changed files with 1,102 additions and 1,089 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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<TString> {

public class VelocityOnTimeAddCommand
extends CommonOnTimeAddCommand<Player, Player, TextComponent, CommandSource>
implements Command {
TString getNoPermission();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.anvilpowered.ontime.common.command

package org.anvilpowered.ontime.common.command;

public class CommonOnTimeAddCommand<TUser, TPlayer, TString, TCommandSource>
extends CommonOnTimeTwoArgCommand<TUser, TPlayer, TString, TCommandSource> {

public void execute(TCommandSource source, String[] context) {
execute(source, context, "add", memberManager::addBonusTime);
open class CommonAddCommand<TUser, TPlayer, TString, TCommandSource> :
CommonTwoArgCommand<TUser, TPlayer, TString, TCommandSource>() {
open fun execute(source: TCommandSource, context: Array<String>) {
execute(source, context, "add", memberManager::addBonusTime)
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<TPlayer>
) {

@Inject
protected lateinit var memberManager: MemberManager<TString>

@Inject
private lateinit var permissionService: PermissionService

@Inject
private lateinit var pluginInfo: PluginInfo<TString>

@Inject
private lateinit var pluginMessages: PluginMessages<TString>

@Inject
protected lateinit var registry: Registry

@Inject
protected lateinit var textService: TextService<TString, TCommandSource>

@Inject
private lateinit var userService: UserService<TUser, TPlayer>

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<String>
) {
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<UUID> ->
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<String>): List<String> {
if (!testPermission(source)) return listOf()
return userService.matchPlayerNames(context, 0, 1)
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 284387c

Please sign in to comment.