Skip to content

Commit

Permalink
Spigot and Velocity implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstaeding committed Apr 23, 2020
1 parent 56e5e4e commit 43ef982
Show file tree
Hide file tree
Showing 48 changed files with 1,375 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public interface MemberRepository<
TDataStore>
extends Repository<TKey, Member<TKey>, TDataStore> {

CompletableFuture<Optional<Member<TKey>>> getOneOrGenerateForUser(UUID userUUID);
CompletableFuture<Optional<Member<TKey>>> getOneOrGenerateForUser(UUID userUUID, long time);

CompletableFuture<Optional<Member<TKey>>> generateUserFromConfig(UUID userUUID, long time);
CompletableFuture<Optional<Member<TKey>>> getOneOrGenerateForUser(UUID userUUID);

CompletableFuture<Optional<Member<TKey>>> getOneForUser(UUID userUUID);

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ subprojects {
maven { url 'https://jetbrains.bintray.com/xodus' }
}
dependencies {
implementation 'org.anvilpowered:anvil-api:1.0-SNAPSHOT'
implementation 'org.anvilpowered:anvil-api:0.1-SNAPSHOT'
}
if (project.hasProperty("buildNumber") && version.contains("-SNAPSHOT")) {
version = version.replace("-SNAPSHOT", "-RC${buildNumber}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

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

public void sendAdd(TCommandSource source, String[] context) {
send(source, context, "add", memberManager::addBonusTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.data.registry.Registry;
import org.anvilpowered.anvil.api.plugin.PluginInfo;
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.data.key.OnTimeKeys;
import org.anvilpowered.ontime.api.member.MemberManager;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@SuppressWarnings("unchecked")
public class CommonOnTimeCheckCommand<TUser, TPlayer, TSubject, TString, TCommandSource> {

@Inject
protected MemberManager<TString> memberManager;

@Inject
private PermissionService<TSubject> permissionService;

@Inject
private PluginInfo<TString> pluginInfo;

@Inject
protected Registry registry;

@Inject
private TextService<TString, TCommandSource> textService;

@Inject
private UserService<TUser, TPlayer> userService;

public void sendError(TCommandSource source) {
textService.builder()
.append(pluginInfo.getPrefix())
.red().append("Specify user or run as player!")
.sendTo(source);
}

public void sendCheck(TCommandSource source, String[] context,
Class<TPlayer> playerClass, Class<?> consoleClass) {
final boolean isConsole = consoleClass.isAssignableFrom(source.getClass());
final boolean isPlayer = playerClass.isAssignableFrom(source.getClass());
if (!isConsole && !(isPlayer && permissionService.hasPermission((TSubject) 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 = isConsole || permissionService.hasPermission((TSubject) source,
registry.getOrDefault(OnTimeKeys.CHECK_EXTENDED_PERMISSION));

CompletableFuture<Optional<UUID>> 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);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

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

public void sendSetBonus(TCommandSource source, String[] context) {
send(source, context, "setbonus", memberManager::setBonusTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

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

public void sendSetTotal(TCommandSource source, String[] context) {
send(source, context, "set", memberManager::setTotalTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.data.registry.Registry;
import org.anvilpowered.anvil.api.plugin.PluginInfo;
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.data.key.OnTimeKeys;
import org.anvilpowered.ontime.api.member.MemberManager;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;

public class CommonOnTimeTwoArgCommand<TUser, TPlayer, TSubject, TString, TCommandSource> {

@Inject
protected MemberManager<TString> memberManager;

@Inject
protected PermissionService<TSubject> permissionService;

@Inject
private PluginInfo<TString> pluginInfo;

@Inject
protected Registry registry;

@Inject
private TextService<TString, TCommandSource> textService;

@Inject
private UserService<TUser, TPlayer> userService;

public void send(TCommandSource source, String[] context, String command,
BiFunction<UUID, String, CompletableFuture<TString>> function) {
if (!permissionService.hasPermission((TSubject) source,
registry.getOrDefault(OnTimeKeys.EDIT_PERMISSION))) {
textService.builder()
.append(pluginInfo.getPrefix())
.red().append("You do not have permission for this command!")
.sendTo(source);
return;
}
if (context.length != 2) {
textService.builder()
.append(pluginInfo.getPrefix())
.red().append("Not enough arguments!")
.append("\n", command, " ").appendJoining(" ", context)
.append("\n^\nUsage: /ontime ", command, " <user> <time...>")
.sendTo(source);
return;
}
userService.getUUID(context[0]).thenAcceptAsync(userUUID -> {
if (!userUUID.isPresent()) {
textService.builder()
.append(pluginInfo.getPrefix())
.red().append("No values matching pattern '", context[0], "' present for user!")
.append("\n", command, " ").appendJoining(" ", context)
.append("\n").appendCount(command.length() + 1, " ")
.append("^\nUsage: /ontime ", command, " <user> <time...>")
.sendTo(source);
return;
}
function.apply(userUUID.get(), context[1])
.thenAcceptAsync(result -> textService.send(result, source));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

public class CommonMemberManager<
TUser,
TPlayer extends TCommandSource,
TPlayer,
TString,
TCommandSource>
extends BaseManager<MemberRepository<?, ?>>
Expand All @@ -64,8 +64,8 @@ public CommonMemberManager(Registry registry) {

@Override
public CompletableFuture<TString> info(UUID userUUID) {
String name = userService.getUserName(userUUID).orElse(userUUID.toString());
return getPrimaryComponent().getOneForUser(userUUID).thenApplyAsync(optionalMember -> {
String name = userService.getUserName(userUUID).join().orElse(userUUID.toString());
if (optionalMember.isPresent()) {
Member<?> member = optionalMember.get();
String totalTime = timeFormatService.format(
Expand All @@ -83,7 +83,7 @@ public CompletableFuture<TString> info(UUID userUUID) {
.append("\n\n", textService.builder()
.dark_gray().append("========= ")
.gold().append(pluginInfo.getPrefix())
.dark_gray().append(" ========="))
.dark_gray().append("========="))
.build();
}
return getNotFoundError(name);
Expand All @@ -92,8 +92,8 @@ public CompletableFuture<TString> info(UUID userUUID) {

@Override
public CompletableFuture<TString> infoExtended(UUID userUUID) {
String name = userService.getUserName(userUUID).orElse(userUUID.toString());
return getPrimaryComponent().getOneForUser(userUUID).thenApplyAsync(optionalMember -> {
String name = userService.getUserName(userUUID).join().orElse(userUUID.toString());
if (optionalMember.isPresent()) {
Member<?> member = optionalMember.get();
String playTime = timeFormatService.format(
Expand Down Expand Up @@ -154,8 +154,8 @@ public CompletableFuture<Optional<String>> sync(UUID userUUID) {

@Override
public CompletableFuture<TString> addBonusTime(UUID userUUID, long time) {
String name = userService.getUserName(userUUID).orElse(userUUID.toString());
return getPrimaryComponent().addBonusTimeForUser(userUUID, time).thenApplyAsync(result -> {
String name = userService.getUserName(userUUID).join().orElse(userUUID.toString());
if (result) {
return textService.builder()
.append(pluginInfo.getPrefix())
Expand Down Expand Up @@ -192,8 +192,8 @@ public CompletableFuture<TString> addBonusTime(UUID userUUID, String time) {

@Override
public CompletableFuture<TString> setBonusTime(UUID userUUID, long time) {
String name = userService.getUserName(userUUID).orElse(userUUID.toString());
return getPrimaryComponent().setBonusTimeForUser(userUUID, time).thenApplyAsync(result -> {
String name = userService.getUserName(userUUID).join().orElse(userUUID.toString());
if (result) {
return textService.builder()
.append(pluginInfo.getPrefix())
Expand All @@ -212,8 +212,8 @@ public CompletableFuture<TString> setBonusTime(UUID userUUID, String time) {

@Override
public CompletableFuture<TString> setTotalTime(UUID userUUID, long time) {
String name = userService.getUserName(userUUID).orElse(userUUID.toString());
return getPrimaryComponent().setTotalTimeForUser(userUUID, time).thenApplyAsync(result -> {
String name = userService.getUserName(userUUID).join().orElse(userUUID.toString());
if (result) {
return textService.builder()
.append(pluginInfo.getPrefix())
Expand Down
Loading

0 comments on commit 43ef982

Please sign in to comment.