This commit is contained in:
kento2 2026-02-23 20:14:08 +01:00
parent f1e9ac5ea9
commit 179f94085f
15 changed files with 167 additions and 67 deletions

View file

@ -1,6 +1,7 @@
package de.kentoj.scrow.bukkit;
import de.kentoj.scrow.bukkit.economy.command.CoinsCommand;
import de.kentoj.scrow.bukkit.economy.CoinsCommand;
import de.kentoj.scrow.bukkit.friends.command.FriendCommand;
import org.bukkit.plugin.java.JavaPlugin;
import reactor.core.publisher.Mono;
@ -14,6 +15,7 @@ public class CoreImplPlugin extends JavaPlugin {
{
ScrowAPI.getCommandManager().register(new CoinsCommand().create(), this);
ScrowAPI.getCommandManager().register(new FriendCommand().create(), this);
}
{
@ -33,9 +35,4 @@ public class CoreImplPlugin extends JavaPlugin {
}
ScrowAPISurface.destroyScrowAPI();
}
@Override
public void onDisable() {
}
}

View file

@ -47,7 +47,7 @@ public class ScrowAPISurface {
ScrowAPI.getDatabase() != null ?
new MongoEconomyService(ScrowAPI.getDatabase()) : new InMemoryEconomyService()
);
// TODO friends service
Commands.setFactory(new CommandFactoryImpl());
ScrowAPI.setCommandManager(new BukkitCommandManager());
}

View file

@ -27,7 +27,7 @@ public class ArgumentReaderImpl implements ArgumentReader {
final var str = new StringBuilder();
while (true) {
if (buf.position() >= buf.limit()) break;
char c = buf.get();
final char c = buf.get();
if (c == ' ') break;
str.append(c);
}
@ -40,7 +40,7 @@ public class ArgumentReaderImpl implements ArgumentReader {
var quoting = false;
while (true) {
if (buf.position() >= buf.limit()) break;
char c = buf.get();
final char c = buf.get();
if (c == '"') quoting = !quoting;
if (c == ' ' && !quoting) break;
str.append(c);

View file

@ -1,17 +1,18 @@
package de.kentoj.scrow.bukkit.cmds2.parsing;
import de.kentoj.scrow.bukkit.cmds2.CommandContextImpl;
import de.kentoj.scrow.bukkit.services.command.literal.ArgumentReader;
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
import de.kentoj.scrow.bukkit.services.command.execution.ArgumentData;
import de.kentoj.scrow.bukkit.services.command.literal.ArgumentReader;
import de.kentoj.scrow.bukkit.services.command.literal.CommandArgument;
import de.kentoj.scrow.bukkit.services.command.literal.Literal;
import de.kentoj.scrow.bukkit.services.command.literal.RootLiteral;
import de.kentoj.scrow.bukkit.services.command.exception.CommandInvocationException;
import lombok.RequiredArgsConstructor;
import org.bukkit.command.CommandSender;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public class CommandProcessor {
@ -22,28 +23,35 @@ public class CommandProcessor {
private final ArgumentReader argumentReader;
public void execute(RootLiteral rootLiteral) {
Literal lastLiteral = rootLiteral;
Literal literal = rootLiteral;
do {
processArguments(lastLiteral);
processArguments(literal);
if (argumentReader.isEOF()) {
if (lastLiteral.getExecutor() == null) throw new CommandInvocationException("Incomplete command -- literal expected");
lastLiteral.getExecutor().execute(new CommandContextImpl(sender, arguments));
if (literal.getExecutor() == null) {
throw new CommandInvocationException("Incomplete command -- literal expected");
}
literal.getExecutor().execute(new CommandContextImpl(sender, arguments));
return;
}
final var literalName = argumentReader.readWord();
lastLiteral = lastLiteral.getLiteral(literalName);
if (lastLiteral == null) throw new CommandInvocationException("Unknown literal -- " + literalName);
final var nextLiteral = literal.getLiteral(literalName);
if (nextLiteral == null) {
final String validLiterals = literal.getLiterals().stream().map(Literal::getName).collect(Collectors.joining(", "));
throw new CommandInvocationException("Unknown literal -- " + literalName +
"\nValid literals: " + validLiterals);
}
literal = nextLiteral;
final boolean hasSenderPermission = lastLiteral.getRequiredPermission() == null || sender.hasPermission(lastLiteral.getRequiredPermission());
final boolean hasSenderPermission = literal.getRequiredPermission() == null || sender.hasPermission(literal.getRequiredPermission());
if (!hasSenderPermission) throw new CommandInvocationException("No permission");
} while (lastLiteral.hasLiteral());
} while (literal.hasLiteral());
processArguments(lastLiteral);
processArguments(literal);
if (lastLiteral.getExecutor() == null) throw new CommandInvocationException("Incomplete command.");
lastLiteral.getExecutor().execute(new CommandContextImpl(sender, arguments));
if (literal.getExecutor() == null) throw new CommandInvocationException("Incomplete command.");
literal.getExecutor().execute(new CommandContextImpl(sender, arguments));
}
private void processArguments(Literal node) {
@ -55,13 +63,13 @@ public class CommandProcessor {
private <T> ArgumentData<T> parseArgument(CommandArgument<T> argument) {
if (argumentReader.isEOF()) {
var defaultValue = argument.getDefaultValue(new CommandContextImpl(sender, arguments));
final var defaultValue = argument.getDefaultValue(new CommandContextImpl(sender, arguments));
if (defaultValue == null) throw new CommandInvocationException("Missing argument -- " + argument.getName());
return new ArgumentDataImpl<>(defaultValue, argument, null);
}
final var rawInput = argument.getType().readRawInput(argumentReader);
T value = argument.getType().parseInput(rawInput);
final T value = argument.getType().parseInput(rawInput);
argument.getType().checkValue(new CommandContextImpl(sender, arguments), value, rawInput);
return new ArgumentDataImpl<>(value, argument, rawInput);
}

View file

@ -1,4 +1,4 @@
package de.kentoj.scrow.bukkit.economy.command;
package de.kentoj.scrow.bukkit.economy;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.services.command.literal.CommandArgument;

View file

@ -1,36 +0,0 @@
package de.kentoj.scrow.bukkit.friends;
import de.kentoj.scrow.bukkit.services.friends.FriendRequest;
import de.kentoj.scrow.bukkit.services.friends.Friendship;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.UUID;
public interface FriendshipService {
Flux<@NotNull Friendship> getFriendships(UUID playerId);
/**
* @param initiator id of player who wished to end the friendship
* @param other id of the player the initiator wants to end the friendship with
* @return true on success, false if no such friendship existed.
*/
Mono<@NotNull Boolean> endFriendship(UUID initiator, UUID other);
Flux<@NotNull FriendRequest> getIncomingFriendRequests(UUID playerId);
Flux<@NotNull FriendRequest> getOutgoingFriendRequests(UUID playerId);
/**
* @param initiator id of player sending the friend request
* @param to id of the player the initiator wants to befriend
* @return true on success, false if already requested
*/
Mono<@NotNull Boolean> sendFriendRequest(UUID initiator, UUID to);
Mono<@NotNull Friendship> acceptFriendRequest(UUID from, UUID to);
Mono<@NotNull Friendship> denyFriendRequest(UUID from, UUID to);
}

View file

@ -0,0 +1,20 @@
package de.kentoj.scrow.bukkit.friends.command;
import de.kentoj.scrow.bukkit.services.command.Commands;
import de.kentoj.scrow.bukkit.services.command.literal.RootLiteral;
public class FriendCommand {
private final RootLiteral rootLiteral;
public FriendCommand() {
rootLiteral = Commands.root("friend");
rootLiteral.addAliases("f", "friends");
rootLiteral.addLiteral(new FriendListLiteral().create());
}
public RootLiteral create() {
return rootLiteral;
}
}

View file

@ -0,0 +1,48 @@
package de.kentoj.scrow.bukkit.friends.command;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.services.command.Commands;
import de.kentoj.scrow.bukkit.services.command.execution.CommandContext;
import de.kentoj.scrow.bukkit.services.command.literal.Literal;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import reactor.core.scheduler.Schedulers;
import java.util.UUID;
public class FriendListLiteral {
private final Literal literal;
public FriendListLiteral() {
literal = Commands.literal("list");
literal.setExecutor(this::listFriends);
}
public Literal create() {
return literal;
}
private void listFriends(CommandContext ctx) {
ScrowAPI.getFriendsService().getFriendships(ctx.getSenderPlayer().getUniqueId())
.subscribeOn(Schedulers.boundedElastic())
.collectList()
.publishOn(ScrowAPI.getMinecraftScheduler())
.subscribe(friendships -> {
if (friendships.isEmpty()) {
ctx.getSender().sendMessage("You have no friends.");
} else {
ctx.getSender().sendMessage("You have " + friendships.size() + " friends:");
friendships.forEach(friendship -> {
final UUID friendId = friendship.getOther(ctx.getSenderPlayer().getUniqueId());
// TODO get name even if never online on the network
final OfflinePlayer friend = Bukkit.getOfflinePlayer(friendId);
ctx.getSender().sendMessage("- " + friend.getName());
});
}
}, error -> {
ctx.getSender().sendMessage("Error fetching friends");
// TODO improve handling
});
}
}