This commit is contained in:
kento2 2026-08-29 03:06:29 +02:00
parent ce56c6e105
commit 35a0784e40
46 changed files with 597 additions and 673 deletions

View file

@ -9,11 +9,11 @@ java_library(
deps = [
"//core/common",
artifact("io.papermc.paper:paper-api"),
artifact("de.kentoj.scrow:kencommandapi-bukkit"),
artifact("site.lab0x13.scrow:commands-bukkit"),
],
exports = [
"//core/common",
artifact("de.kentoj.scrow:kencommandapi-bukkit"),
artifact("site.lab0x13.scrow:commands-bukkit"),
artifact("org.mongodb:bson"),
],
neverlink = True,
@ -27,7 +27,7 @@ java_binary(
resource_strip_prefix = "core/bukkit/plugin/main/resources",
deps = [
"//core/common",
artifact("de.kentoj.scrow:kencommandapi-bukkit"),
artifact("site.lab0x13.scrow:commands-bukkit"),
artifact("net.luckperms:api"),
artifact("io.papermc.paper:paper-api"),
artifact("io.nats:jnats"),

View file

@ -1,6 +1,6 @@
package de.kentoj.scrow.bukkit;
import de.kentoj.kencommandapi.CommandAPI;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import de.kentoj.scrow.bukkit.minigame.MinigameManager;
import de.kentoj.scrow.bukkit.region.RegionManager;
import de.kentoj.scrowlib.economy.EconomyService;
@ -12,12 +12,11 @@ import de.kentoj.scrowlib.player.NetworkPlayer;
import de.kentoj.scrowlib.player.NetworkPlayerFactory;
import de.kentoj.scrowlib.player.PlayerPrefixProvider;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jdbi.v3.core.Jdbi;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import site.lab0x13.scrow.commands.bukkit.ScrowCommands;
import java.util.UUID;
@ -38,8 +37,7 @@ public class ScrowAPI {
private static MessageBroker messageBroker;
private static InstanceManager instanceManager;
private static RegionManager regionManager;
private static CommandAPI<Player> playerCommands;
private static CommandAPI<CommandSender> commands;
private static ScrowCommands<ScrowBukkitCC> commands;
private static PlayerPrefixProvider playerPrefixProvider;
private static final MinigameManager minigameManager = new MinigameManager();
private static NetworkPlayerFactory playerFactory;
@ -103,13 +101,8 @@ public class ScrowAPI {
}
@ApiStatus.Internal
public static void setPlayerCommands(CommandAPI<Player> playerCommands) {
ScrowAPI.playerCommands = playerCommands;
}
@ApiStatus.Internal
public static void setCommands(CommandAPI<CommandSender> commands) {
ScrowAPI.commands = commands;
public static void setCommands(ScrowCommands<ScrowBukkitCC> commandsManager) {
ScrowAPI.commands = commandsManager;
}
public static @Nullable Jdbi jdbi() {
@ -143,17 +136,15 @@ public class ScrowAPI {
/**
* Regions are not a reliable enough to detect fast movements.
* The way regions work is that every few ticks, all regions are checked for players.
*
* @see org.bukkit.event.player.PlayerMoveEvent
*/
public static RegionManager regionManager() {
return regionManager;
}
public static CommandAPI<Player> playerCommands() {
return playerCommands;
}
public static CommandAPI<CommandSender> commands() {
@ApiStatus.Internal
public static ScrowCommands<ScrowBukkitCC> commands() {
return commands;
}

View file

@ -0,0 +1,18 @@
package de.kentoj.scrow.bukkit.command;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import org.bukkit.command.CommandSender;
import site.lab0x13.scrow.commands.bukkit.BukkitCommandContext;
public final class ScrowBukkitCC extends BukkitCommandContext<ScrowBukkitCC> {
private final ScrowMessageStyle style;
public ScrowBukkitCC(CommandSender sender, ScrowMessageStyle style) {
super(sender);
this.style = style;
}
public ScrowMessageStyle style() {
return style;
}
}

View file

@ -1,11 +1,11 @@
package de.kentoj.scrow.bukkit.minigame.phaseflow;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.scrow.bukkit.minigame.Minigame;
import de.kentoj.scrow.bukkit.minigame.PlayerManager;
import de.kentoj.scrow.bukkit.minigame.phase.PhaseFlow;
import de.kentoj.scrow.bukkit.minigame.phase.event.PhaseListeners;
import de.kentoj.scrow.bukkit.minigame.phase.event.PhaseListenersImpl;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
public class PhaseContext<T extends Minigame> {
private final T game;
@ -29,7 +29,7 @@ public class PhaseContext<T extends Minigame> {
return game.playerManager();
}
public MessageStyle style() {
public ScrowMessageStyle style() {
return game.messageStyle();
}

View file

@ -21,8 +21,8 @@ public final class CoreImplPlugin extends JavaPlugin {
ScrowAPISurface.initScrowAPI(this, cachedPrefixProvider);
log.info("Initialized ScrowAPI");
ScrowAPI.playerCommands().register(new CoinsCommand().rootLiteral());
ScrowAPI.playerCommands().register(new FriendCommand().rootLiteral());
ScrowAPI.commands().register(new CoinsCommand().rootLiteral());
ScrowAPI.commands().register(new FriendCommand().rootLiteral());
var luckperms = LuckPermsProvider.get();
new LuckPermsPrefixSetter(luckperms, cachedPrefixProvider).init(this);

View file

@ -1,10 +1,11 @@
package de.kentoj.scrow.bukkit;
import de.kentoj.kencommandapi.CommandAPI;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import de.kentoj.scrow.bukkit.internal.player.NetworkPlayerWatchdog;
import de.kentoj.scrow.bukkit.internal.region.RegionManagerImpl;
import de.kentoj.scrow.bukkit.internal.region.RegionPlayerTracker;
import de.kentoj.scrow.bukkit.internal.region.RegionTask;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import de.kentoj.scrowlib.economy.InMemoryEconomyService;
import de.kentoj.scrowlib.economy.PostgresEconomyService;
import de.kentoj.scrowlib.friends.friendship.InMemoryFriendshipService;
@ -18,12 +19,11 @@ import de.kentoj.scrowlib.player.NetworkPlayerImpl;
import de.kentoj.scrowlib.player.PlayerPrefixProvider;
import io.nats.client.Nats;
import io.nats.client.Options;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jdbi.v3.core.Jdbi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import site.lab0x13.scrow.commands.bukkit.ScrowCommands;
import java.io.IOException;
import java.util.Properties;
@ -70,8 +70,7 @@ public class ScrowAPISurface {
ScrowAPI.setPlayerFactory(playerId -> new NetworkPlayerImpl(ScrowAPI.messageBroker(), playerId));
ScrowAPI.setRegionManager(new RegionManagerImpl());
ScrowAPI.setCommands(new CommandAPI<>(plugin, CommandSender.class));
ScrowAPI.setPlayerCommands(new CommandAPI<>(plugin, Player.class));
ScrowAPI.setCommands(new ScrowCommands<>(plugin, (literal, sender) -> new ScrowBukkitCC(sender, (ScrowMessageStyle) literal.style())));
ScrowAPI.setInstanceManager(new InstanceManagerImpl(ScrowAPI.messageBroker()));
ScrowAPI.setEconomyService(ScrowAPI.jdbi() != null ?
new PostgresEconomyService(ScrowAPI.jdbi()) : new InMemoryEconomyService());

View file

@ -1,31 +1,29 @@
package de.kentoj.scrow.bukkit.command.economy;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import org.bukkit.entity.Player;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
import java.util.concurrent.CompletableFuture;
public final class CoinsCommand {
private final RootLiteral<Player> rootLiteral;
private final RootLiteral<ScrowBukkitCC> rootLiteral;
public CoinsCommand() {
rootLiteral = Literal.<Player>builder("coins", "bal", "balance")
rootLiteral = Literal.<ScrowBukkitCC>builder("coins", "bal", "balance")
.withPermission("command.coins.get.self")
.withExecutor(this::execute)
.withLiteral(new CoinsSetLiteral().literal())
.withLiteral(new CoinsGetLiteral().literal())
.withSubLiteral(new CoinsSetLiteral().literal())
.withSubLiteral(new CoinsGetLiteral().literal())
.asRootLiteral(ScrowMessageStyle.GENERIC);
}
public CompletableFuture<?> execute(CommandContext<Player> ctx) {
var player = ctx.sender();
public CompletableFuture<?> execute(ScrowBukkitCC ctx) {
var player = ctx.player();
return Tasks.supplyAsync(() ->
ScrowAPI.economyService().getCoins(player.getUniqueId()))
.thenSync(amount ->
@ -33,7 +31,7 @@ public final class CoinsCommand {
.toFuture();
}
public RootLiteral<Player> rootLiteral() {
public RootLiteral<ScrowBukkitCC> rootLiteral() {
return rootLiteral;
}
}

View file

@ -1,34 +1,32 @@
package de.kentoj.scrow.bukkit.command.economy;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import site.lab0x13.scrow.commands.bukkit.type.OfflinePlayerArgumentType;
import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.literal.Literal;
import java.util.concurrent.CompletableFuture;
public final class CoinsGetLiteral {
private final Literal<Player> literal;
private final CommandArgumentSpec<Player, OfflinePlayer> playerArg;
private final Literal<ScrowBukkitCC> literal;
private final Argument<ScrowBukkitCC, OfflinePlayer> playerArg;
public CoinsGetLiteral() {
playerArg = CommandArgumentSpec.builder("player", new OfflinePlayerArgumentType<Player>())
.withDefaultValue(CommandContext::sender)
playerArg = Argument.builder("player", new OfflinePlayerArgumentType<ScrowBukkitCC>())
.withDefaultValue(ScrowBukkitCC::player)
.build();
literal = Literal.<Player>builder("get")
literal = Literal.<ScrowBukkitCC>builder("get")
.withPermission("command.coins.get.others")
.withArgument(playerArg)
.withExecutor(this::execute)
.build();
}
private CompletableFuture<?> execute(CommandContext<Player> ctx) {
private CompletableFuture<?> execute(ScrowBukkitCC ctx) {
var player = ctx.getArg(playerArg);
return Tasks.supplyAsync(() ->
ScrowAPI.economyService().getCoins(player.getUniqueId()))
@ -37,7 +35,7 @@ public final class CoinsGetLiteral {
.toFuture();
}
public Literal<Player> literal() {
public Literal<ScrowBukkitCC> literal() {
return literal;
}
}

View file

@ -1,11 +1,10 @@
package de.kentoj.scrow.bukkit.command.economy;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.argument.types.IntegerArgumentType;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.argument.types.IntegerArgumentType;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.bukkit.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import org.bukkit.OfflinePlayer;
@ -15,17 +14,17 @@ import java.util.concurrent.CompletableFuture;
public final class CoinsSetLiteral {
private final Literal<Player> literal;
private final CommandArgumentSpec<Player, OfflinePlayer> playerArg;
private final CommandArgumentSpec<Player, Integer> amountArg;
private final Literal<ScrowBukkitCC> literal;
private final Argument<ScrowBukkitCC, OfflinePlayer> playerArg;
private final Argument<ScrowBukkitCC, Integer> amountArg;
CoinsSetLiteral() {
playerArg = CommandArgumentSpec.builder("player", new OfflinePlayerArgumentType<Player>())
.withDefaultValue(CommandContext::sender)
playerArg = Argument.builder("player", new OfflinePlayerArgumentType<ScrowBukkitCC>())
.withDefaultValue(ScrowBukkitCC::player)
.build();
amountArg = CommandArgumentSpec.builder("amount", new IntegerArgumentType<Player>())
amountArg = Argument.builder("amount", new IntegerArgumentType<ScrowBukkitCC>())
.build();
literal = Literal.<Player>builder("set")
literal = Literal.<ScrowBukkitCC>builder("set")
.withPermission("command.coins.set")
.withArgument(playerArg)
.withArgument(amountArg)
@ -33,7 +32,7 @@ public final class CoinsSetLiteral {
.build();
}
private CompletableFuture<?> execute(CommandContext<Player> ctx) {
private CompletableFuture<?> execute(ScrowBukkitCC ctx) {
var player = ctx.getArg(playerArg);
var amount = ctx.getArg(amountArg);
return Tasks.runAsync(() ->
@ -43,7 +42,7 @@ public final class CoinsSetLiteral {
.toFuture();
}
public Literal<Player> literal() {
public Literal<ScrowBukkitCC> literal() {
return literal;
}
}

View file

@ -2,15 +2,13 @@ package de.kentoj.scrow.bukkit.command.friends;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType;
import site.lab0x13.scrow.commands.model.argument.Argument;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.bukkit.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.scheduler.Task;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import de.kentoj.scrowlib.friends.FriendRequestService;
import de.kentoj.scrowlib.friends.FriendshipService;
import de.kentoj.scrowlib.friends.friendship.FriendshipImpl;
@ -18,6 +16,7 @@ import de.kentoj.scrowlib.friends.request.FriendRequestImpl;
import net.kyori.adventure.text.Component;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import site.lab0x13.scrow.commands.model.literal.MessageStyle;
import java.time.Instant;
import java.util.Objects;
@ -26,22 +25,22 @@ import java.util.concurrent.CompletableFuture;
public final class FriendAddLiteral {
private final Literal<Player> literal;
private final CommandArgumentSpec<Player, OfflinePlayer> playerArg;
private final Literal<ScrowBukkitCC> literal;
private final Argument<ScrowBukkitCC, OfflinePlayer> playerArg;
private final FriendRequestService requestService = ScrowAPI.friendRequestService();
private final FriendshipService friendsService = ScrowAPI.friendshipService();
FriendAddLiteral() {
playerArg = CommandArgumentSpec.builder("player", new OfflinePlayerArgumentType<Player>())
playerArg = Argument.builder("player", new OfflinePlayerArgumentType<ScrowBukkitCC>())
.build();
literal = Literal.<Player>builder("add")
literal = Literal.<ScrowBukkitCC>builder("add")
.withArgument(playerArg)
.withExecutor(this::execute)
.build();
}
private CompletableFuture<?> execute(CommandContext<Player> ctx) {
var self = ctx.sender();
private CompletableFuture<?> execute(ScrowBukkitCC ctx) {
var self = ctx.player();
var other = ctx.getArg(playerArg);
return Tasks.supplyAsync(() -> findAction(self, other)
.mapSuccess(action -> {
@ -53,6 +52,10 @@ public final class FriendAddLiteral {
}
private void sendRequest(MessageStyle style, Player self, OfflinePlayer other) {
if (other.equals(self)) {
self.sendMessage(style.ok("You can't send yourself a friend request."));
return;
}
Tasks.runAsync(() ->
requestService.saveFriendRequest(new FriendRequestImpl(self.getUniqueId(), other.getUniqueId())))
.runSync(() -> {
@ -110,7 +113,7 @@ public final class FriendAddLiteral {
.mapSync(Objects::nonNull);
}
public Literal<Player> literal() {
public Literal<ScrowBukkitCC> literal() {
return literal;
}

View file

@ -1,25 +1,25 @@
package de.kentoj.scrow.bukkit.command.friends;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
public final class FriendCommand {
private final RootLiteral<Player> rootLiteral;
private final RootLiteral<ScrowBukkitCC> rootLiteral;
public FriendCommand() {
rootLiteral = Literal.<Player>builder("f", "friend")
.withLiteral(new FriendListLiteral().literal())
.withLiteral(new FriendAddLiteral().literal())
.withLiteral(new FriendRemoveLiteral().literal())
.withLiteral(new FriendRequestDeclineLiteral().literal())
.withLiteral(new FriendRequestsLiteral().literal())
rootLiteral = Literal.<ScrowBukkitCC>builder("f", "friend")
.withSubLiteral(new FriendListLiteral().literal())
.withSubLiteral(new FriendAddLiteral().literal())
.withSubLiteral(new FriendRemoveLiteral().literal())
.withSubLiteral(new FriendRequestDeclineLiteral().literal())
.withSubLiteral(new FriendRequestsLiteral().literal())
.asRootLiteral(new ScrowMessageStyle("Friends", NamedTextColor.AQUA));
}
@ -38,7 +38,7 @@ public final class FriendCommand {
.color(NamedTextColor.RED));
}
public RootLiteral<Player> rootLiteral() {
public RootLiteral<ScrowBukkitCC> rootLiteral() {
return rootLiteral;
}
}

View file

@ -1,8 +1,7 @@
package de.kentoj.scrow.bukkit.command.friends;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import site.lab0x13.scrow.commands.model.literal.Literal;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
@ -11,7 +10,6 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@ -20,16 +18,16 @@ import static java.util.Objects.requireNonNull;
import static net.kyori.adventure.text.Component.text;
public final class FriendListLiteral {
private final Literal<Player> literal;
private final Literal<ScrowBukkitCC> literal;
FriendListLiteral() {
literal = Literal.<Player>builder("list")
literal = Literal.<ScrowBukkitCC>builder("list")
.withExecutor(this::displayFriendList)
.build();
}
private CompletableFuture<?> displayFriendList(CommandContext<Player> ctx) {
var sender = ctx.sender();
private CompletableFuture<?> displayFriendList(ScrowBukkitCC ctx) {
var sender = ctx.player();
return Tasks.supplyAsync(() ->
ScrowAPI.friendshipService().getFriendships(sender.getUniqueId()))
.thenSync(friendships -> {
@ -67,7 +65,7 @@ public final class FriendListLiteral {
return message;
}
public Literal<Player> literal() {
public Literal<ScrowBukkitCC> literal() {
return literal;
}
}

View file

@ -1,36 +1,33 @@
package de.kentoj.scrow.bukkit.command.friends;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import site.lab0x13.scrow.commands.bukkit.type.OfflinePlayerArgumentType;
import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.literal.Literal;
import java.util.concurrent.CompletableFuture;
public final class FriendRemoveLiteral {
private final Literal<Player> literal;
private final CommandArgumentSpec<Player, OfflinePlayer> playerArg;
private final Literal<ScrowBukkitCC> literal;
private final Argument<ScrowBukkitCC, OfflinePlayer> playerArg;
FriendRemoveLiteral() {
playerArg = CommandArgumentSpec.<Player, OfflinePlayer>builder("player", new OfflinePlayerArgumentType<>())
playerArg = Argument.<ScrowBukkitCC, OfflinePlayer>builder("player", new OfflinePlayerArgumentType<>())
.build();
literal = Literal.<Player>builder("remove")
literal = Literal.<ScrowBukkitCC>builder("remove")
.withArgument(playerArg)
.withExecutor(this::removeFriend)
.build();
}
private CompletableFuture<?> removeFriend(CommandContext<Player> ctx) {
private CompletableFuture<?> removeFriend(ScrowBukkitCC ctx) {
var friend = ctx.getArg(playerArg);
return Tasks.supplyAsync(() ->
ScrowAPI.friendshipService().deleteFriendship(friend.getUniqueId(), ctx.sender().getUniqueId()))
ScrowAPI.friendshipService().deleteFriendship(friend.getUniqueId(), ctx.player().getUniqueId()))
.thenSync(deleted -> {
if (!deleted) {
ctx.sender().sendMessage(ctx.style().err("You are not friends with " + friend.getName() + "."));
@ -40,7 +37,7 @@ public final class FriendRemoveLiteral {
}).toFuture();
}
public Literal<Player> literal() {
public Literal<ScrowBukkitCC> literal() {
return literal;
}
}

View file

@ -1,50 +1,46 @@
package de.kentoj.scrow.bukkit.command.friends;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import de.kentoj.scrowlib.friends.FriendRequestService;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import site.lab0x13.scrow.commands.bukkit.type.OfflinePlayerArgumentType;
import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.literal.Literal;
import java.util.concurrent.CompletableFuture;
public final class FriendRequestDeclineLiteral {
private final Literal<Player> literal;
private final CommandArgumentSpec<Player, OfflinePlayer> playerArg;
private final Literal<ScrowBukkitCC> literal;
private final Argument<ScrowBukkitCC, OfflinePlayer> playerArg;
private final FriendRequestService requestService = ScrowAPI.friendRequestService();
FriendRequestDeclineLiteral() {
playerArg = CommandArgumentSpec.builder("player", new OfflinePlayerArgumentType<Player>())
playerArg = Argument.builder("player", new OfflinePlayerArgumentType<ScrowBukkitCC>())
.build();
literal = Literal.<Player>builder("reject", "decline")
literal = Literal.<ScrowBukkitCC>builder("reject", "decline")
.withArgument(playerArg)
.withExecutor(this::declineFriendRequest)
.build();
}
private CompletableFuture<?> declineFriendRequest(CommandContext<Player> ctx) {
private CompletableFuture<?> declineFriendRequest(ScrowBukkitCC ctx) {
var other = ctx.getArg(playerArg);
return Tasks.supplyAsync(() ->
requestService.deleteFriendRequest(other.getUniqueId(), ctx.sender().getUniqueId()))
requestService.deleteFriendRequest(other.getUniqueId(), ctx.player().getUniqueId()))
.thenSync(deleted -> {
if (!deleted) {
ctx.sender().sendMessage(ctx.style().err("There's no incoming friend request from " + other.getName() + "."));
ctx.player().sendMessage(ctx.style().err("There's no incoming friend request from " + other.getName() + "."));
return;
}
ctx.sender().sendMessage(ctx.style().ok("Friend request declined."));
ScrowAPI.getPlayer(other).sendMessage(ctx.style().ok(ctx.sender().getName() + " has declined your friend request."));
ctx.player().sendMessage(ctx.style().ok("Friend request declined."));
ScrowAPI.getPlayer(other).sendMessage(ctx.style().ok(ctx.player().getName() + " has declined your friend request."));
}).toFuture();
}
public Literal<Player> literal() {
public Literal<ScrowBukkitCC> literal() {
return literal;
}
}

View file

@ -1,8 +1,7 @@
package de.kentoj.scrow.bukkit.command.friends;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.scrow.bukkit.command.ScrowBukkitCC;
import site.lab0x13.scrow.commands.model.literal.Literal;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.scheduler.Tasks;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
@ -11,7 +10,6 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@ -20,26 +18,26 @@ import static net.kyori.adventure.text.Component.text;
public final class FriendRequestsLiteral {
private final Literal<Player> literal;
private final Literal<ScrowBukkitCC> literal;
FriendRequestsLiteral() {
literal = Literal.<Player>builder("requests")
literal = Literal.<ScrowBukkitCC>builder("requests")
.withExecutor(this::displayFriendRequests)
.build();
}
private CompletableFuture<?> displayFriendRequests(CommandContext<Player> ctx) {
private CompletableFuture<?> displayFriendRequests(ScrowBukkitCC ctx) {
return Tasks.supplyAsync(() ->
ScrowAPI.friendRequestService().getFriendRequestsTo(ctx.sender().getUniqueId()))
ScrowAPI.friendRequestService().getFriendRequestsTo(ctx.player().getUniqueId()))
.thenSync(list -> {
if (list.isEmpty()) {
ctx.sender().sendMessage(ctx.style().err("You have no friend requests."));
ctx.player().sendMessage(ctx.style().err("You have no friend requests."));
return;
}
var requesters = list.stream()
.sorted(this::compareByAge)
.map(fr -> Bukkit.getOfflinePlayer(fr.from()).getName());
ctx.sender().sendMessage(formatList(ctx.style(), requesters.toList()));
ctx.player().sendMessage(formatList(ctx.style(), requesters.toList()));
}).toFuture();
}
@ -59,7 +57,7 @@ public final class FriendRequestsLiteral {
return message;
}
public Literal<Player> literal() {
public Literal<ScrowBukkitCC> literal() {
return literal;
}
}

View file

@ -7,7 +7,7 @@ shared_deps = [
artifact("org.jetbrains:annotations"),
artifact("com.google.guava:guava"),
artifact("org.slf4j.slf4j:api"),
artifact("de.kentoj.scrow:kencommandapi-core"),
artifact("site.lab0x13.scrow:commands-common"),
artifact("net.kyori:adventure-api"),
artifact("net.kyori:adventure-text-minimessage"),
artifact("org.jdbi:jdbi3-core"),

View file

@ -1,10 +1,10 @@
package de.kentoj.scrowlib.convention;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import site.lab0x13.scrow.commands.model.literal.MessageStyle;
public record ScrowMessageStyle(
Component prefix,

View file

@ -9,12 +9,14 @@ java_library(
visibility = ["//visibility:public"],
deps = [
"//core/common",
artifact("site.lab0x13.scrow:commands-common"),
artifact("com.velocitypowered:velocity-api"),
artifact("de.kentoj.scrow:kencommandapi-velocity"),
artifact("site.lab0x13.scrow:commands-velocity"),
],
exports = [
"//core/common",
artifact("de.kentoj.scrow:kencommandapi-velocity"),
artifact("site.lab0x13.scrow:commands-common"),
artifact("site.lab0x13.scrow:commands-velocity"),
artifact("org.mongodb:bson"),
],
neverlink = True,
@ -25,11 +27,13 @@ java_binary(
srcs = glob(["plugin/main/java/**/*.java", "api/main/java/**/*.java"]),
create_executable = False,
deps = [
":api",
"//core/common",
artifact("site.lab0x13.scrow:commands-common"),
artifact("com.velocitypowered:velocity-api"),
artifact("com.google.inject:guice"),
artifact("io.nats:jnats"),
artifact("de.kentoj.scrow:kencommandapi-velocity"),
artifact("site.lab0x13.scrow:commands-velocity"),
artifact("org.mongodb:bson"),
artifact("org.postgresql:postgresql"),
artifact("com.google.code.gson:gson"),

View file

@ -1,22 +1,21 @@
package de.kentoj.scrow.velocity;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import de.kentoj.kencommandapi.CommandAPI;
import de.kentoj.scrow.velocity.commands.ScrowVelocityCC;
import de.kentoj.scrowlib.instancemanager.InstanceManager;
import de.kentoj.scrowlib.messaging.MessageBroker;
import de.kentoj.scrowlib.player.NetworkPlayer;
import de.kentoj.scrowlib.player.NetworkPlayerFactory;
import org.jdbi.v3.core.Jdbi;
import org.jetbrains.annotations.ApiStatus;
import site.lab0x13.scrow.commands.velocity.ScrowCommands;
import java.util.UUID;
public class ScrowAPI {
private static Jdbi jdbi;
private static CommandAPI<CommandSource> commands;
private static CommandAPI<Player> playerCommands;
private static ScrowCommands<ScrowVelocityCC> commands;
private static MessageBroker messageBroker;
private static InstanceManager instanceManager;
private static NetworkPlayerFactory playerFactory;
@ -30,32 +29,27 @@ public class ScrowAPI {
}
@ApiStatus.Internal
public static void initMessageBroker(MessageBroker messageBroker) {
public static void setMessageBroker(MessageBroker messageBroker) {
ScrowAPI.messageBroker = messageBroker;
}
@ApiStatus.Internal
public static void initInstanceManager(InstanceManager instanceManager) {
public static void setInstanceManager(InstanceManager instanceManager) {
ScrowAPI.instanceManager = instanceManager;
}
@ApiStatus.Internal
public static void initJdbi(Jdbi jdbi) {
public static void setJdbi(Jdbi jdbi) {
ScrowAPI.jdbi = jdbi;
}
@ApiStatus.Internal
public static void initPlayerFactory(NetworkPlayerFactory playerFactory) {
public static void setPlayerFactory(NetworkPlayerFactory playerFactory) {
ScrowAPI.playerFactory = playerFactory;
}
@ApiStatus.Internal
public static void initPlayerCommands(CommandAPI<Player> playerCommands) {
ScrowAPI.playerCommands = playerCommands;
}
@ApiStatus.Internal
public static void initCommands(CommandAPI<CommandSource> commands) {
public static void setCommands(ScrowCommands<ScrowVelocityCC> commands) {
ScrowAPI.commands = commands;
}
@ -63,14 +57,10 @@ public class ScrowAPI {
return jdbi;
}
public static CommandAPI<CommandSource> commands() {
public static ScrowCommands<ScrowVelocityCC> commands() {
return commands;
}
public static CommandAPI<Player> playerCommands() {
return playerCommands;
}
public static MessageBroker messageBroker() {
return messageBroker;
}

View file

@ -0,0 +1,19 @@
package de.kentoj.scrow.velocity.commands;
import com.velocitypowered.api.command.CommandSource;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import site.lab0x13.scrow.commands.velocity.VelocityCommandContext;
public class ScrowVelocityCC extends VelocityCommandContext<ScrowVelocityCC> {
private final ScrowMessageStyle style;
public ScrowVelocityCC(CommandSource sender, ScrowMessageStyle style) {
super(sender);
this.style = style;
}
public ScrowMessageStyle style() {
return style;
}
}

View file

@ -3,6 +3,7 @@ package de.kentoj.scrow.corevelocity;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.scrow.corevelocity.misc.OnlineCommand;
@ -12,14 +13,12 @@ import de.kentoj.scrow.corevelocity.privmsg.MsgCommand;
import de.kentoj.scrow.corevelocity.privmsg.PrivMsgHandler;
import de.kentoj.scrow.corevelocity.privmsg.ReplyCommand;
import de.kentoj.scrow.corevelocity.serverregistration.ConsulV1ServerInstanceScanner;
import de.kentoj.scrow.corevelocity.serverregistration.RegistrationTask;
import de.kentoj.scrow.corevelocity.serverregistration.ServerInstanceScanner;
import de.kentoj.scrow.corevelocity.serverregistration.PlayerTryJoinLobbyListener;
import de.kentoj.scrow.velocity.ScrowAPI;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import de.kentoj.scrowlib.utils.EnvUtils;
import net.kyori.adventure.text.format.NamedTextColor;
import java.time.Duration;
import site.lab0x13.scrow.commands.model.literal.MessageStyle;
@Plugin(
id = "corevelocity",
@ -31,6 +30,7 @@ public final class CoreVelocityPlugin {
@Inject
private ProxyServer server;
private ConsulV1ServerInstanceScanner instanceScanner;
@Subscribe
private void onInit(ProxyInitializeEvent __) {
@ -39,15 +39,21 @@ public final class CoreVelocityPlugin {
var lastTargetCache = new LastTargetCache();
var style = new ScrowMessageStyle("MSG", NamedTextColor.LIGHT_PURPLE);
var privmsgHelper = new PrivMsgHandler(style);
ScrowAPI.playerCommands().register(new ReplyCommand(server, lastTargetCache, privmsgHelper, style).rootLiteral());
ScrowAPI.playerCommands().register(new MsgCommand(server, lastTargetCache, privmsgHelper, style).rootLiteral());
ScrowAPI.commands().register(new ReplyCommand(server, lastTargetCache, privmsgHelper, style).rootLiteral());
ScrowAPI.commands().register(new MsgCommand(server, lastTargetCache, privmsgHelper, style).rootLiteral());
ScrowAPI.commands().register(new OnlineCommand(server).rootLiteral());
var sendPlayerWatchdog = new SendPlayerWatchdog(server);
sendPlayerWatchdog.listen();
ServerInstanceScanner instanceScanner = new ConsulV1ServerInstanceScanner(EnvUtils.envOrThrow("HOST_CONSUL"));
server.getScheduler().buildTask(this, new RegistrationTask(server, instanceScanner))
.repeat(Duration.ofSeconds(3))
instanceScanner = new ConsulV1ServerInstanceScanner(server, EnvUtils.envOrThrow("HOST_CONSUL"));
server.getEventManager().register(this, new PlayerTryJoinLobbyListener(server));
server.getScheduler()
.buildTask(this, () -> instanceScanner.start())
.schedule();
}
@Subscribe
private void onShutdown(ProxyShutdownEvent __) {
instanceScanner.close();
}
}

View file

@ -1,10 +1,9 @@
package de.kentoj.scrow.corevelocity;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.CommandAPI;
import de.kentoj.scrow.velocity.ScrowAPI;
import de.kentoj.scrow.velocity.commands.ScrowVelocityCC;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import de.kentoj.scrowlib.instancemanager.InstanceManagerImpl;
import de.kentoj.scrowlib.messaging.InMemoyMessageBroker;
import de.kentoj.scrowlib.messaging.NatsMessageBroker;
@ -14,6 +13,7 @@ import io.nats.client.Options;
import org.jdbi.v3.core.Jdbi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import site.lab0x13.scrow.commands.velocity.ScrowCommands;
import java.io.IOException;
import java.util.Properties;
@ -30,13 +30,13 @@ public class ScrowAPISurface {
.server(natsHost)
.pedantic()
.build();
ScrowAPI.initMessageBroker(new NatsMessageBroker(Nats.connect(options)));
ScrowAPI.setMessageBroker(new NatsMessageBroker(Nats.connect(options)));
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
log.error("HOST_NATS not set -> Using in-memory dummy message broker");
ScrowAPI.initMessageBroker(new InMemoyMessageBroker());
ScrowAPI.setMessageBroker(new InMemoyMessageBroker());
}
var hostPostgres = System.getenv().get("HOST_POSTGRES");
@ -45,7 +45,7 @@ public class ScrowAPISurface {
Class.forName("org.postgresql.Driver", true, ScrowAPISurface.class.getClassLoader());
var props = new Properties();
props.setProperty("user", "postgres");
ScrowAPI.initJdbi(Jdbi.create(hostPostgres, props));
ScrowAPI.setJdbi(Jdbi.create(hostPostgres, props));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
@ -53,10 +53,9 @@ public class ScrowAPISurface {
log.error("HOST_POSTGRES not set -> Using in-memory database");
}
ScrowAPI.initInstanceManager(new InstanceManagerImpl(ScrowAPI.messageBroker()));
ScrowAPI.initCommands(new CommandAPI<>(server, CommandSource.class));
ScrowAPI.initPlayerCommands(new CommandAPI<>(server, Player.class));
ScrowAPI.initPlayerFactory(playerId -> new NetworkPlayerImpl(ScrowAPI.messageBroker(), playerId));
ScrowAPI.setInstanceManager(new InstanceManagerImpl(ScrowAPI.messageBroker()));
ScrowAPI.setCommands(new ScrowCommands<>(server, (literal, sender) -> new ScrowVelocityCC(sender, (ScrowMessageStyle) literal.style())));
ScrowAPI.setPlayerFactory(playerId -> new NetworkPlayerImpl(ScrowAPI.messageBroker(), playerId));
}
public static void destroyScrowAPI() {

View file

@ -1,38 +1,33 @@
package de.kentoj.scrow.corevelocity.misc;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.invocation.SyncCommandExecutor;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.scrow.velocity.commands.ScrowVelocityCC;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
public final class OnlineCommand {
private final RootLiteral<CommandSource> rootLiteral;
private final RootLiteral<ScrowVelocityCC> rootLiteral;
private final ScrowMessageStyle style = ScrowMessageStyle.GENERIC;
private final ProxyServer server;
public OnlineCommand(ProxyServer server) {
this.server = server;
rootLiteral = Literal.<CommandSource>builder("online")
rootLiteral = Literal.<ScrowVelocityCC>builder("online")
.withSyncExecutor(this::executeSync)
.asRootLiteral(ScrowMessageStyle.GENERIC);
}
private Result<Object, String> executeSync(CommandContext<CommandSource> ctx) {
private void executeSync(ScrowVelocityCC ctx) {
int cnt = server.getPlayerCount();
var msg = cnt != 1
? "There are currently " + cnt + " players online."
: "There is currently 1 player online.";
ctx.sender().sendMessage(style.ok(msg));
return Results.success(new Object());
}
public RootLiteral<CommandSource> rootLiteral() {
public RootLiteral<ScrowVelocityCC> rootLiteral() {
return rootLiteral;
}
}

View file

@ -1,54 +1,53 @@
package de.kentoj.scrow.corevelocity.privmsg;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.argument.types.StringArgumentType;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.kencommandapi.type.PlayerArgumentType;
import de.kentoj.scrow.velocity.commands.ScrowVelocityCC;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.argument.types.StringArgumentType;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
import site.lab0x13.scrow.commands.velocity.type.PlayerArgumentType;
public class MsgCommand {
private final RootLiteral<Player> rootLiteral;
private final CommandArgumentSpec<Player, Player> targetArg;
private final CommandArgumentSpec<Player, String> msgArg;
private final RootLiteral<ScrowVelocityCC> rootLiteral;
private final Argument<ScrowVelocityCC, Player> targetArg;
private final Argument<ScrowVelocityCC, String> msgArg;
private final LastTargetCache cache;
private final PrivMsgHandler privMsgHandler;
public MsgCommand(ProxyServer server, LastTargetCache cache, PrivMsgHandler helper, MessageStyle style) {
public MsgCommand(ProxyServer server, LastTargetCache cache, PrivMsgHandler helper, ScrowMessageStyle style) {
this.cache = cache;
this.privMsgHandler = helper;
targetArg = CommandArgumentSpec.builder("target", new PlayerArgumentType<Player>(server)).build();
msgArg = CommandArgumentSpec.builder("msg", new StringArgumentType<Player>(true)).build();
targetArg = Argument.builder("target", new PlayerArgumentType<ScrowVelocityCC>(server)).build();
msgArg = Argument.builder("msg", new StringArgumentType<ScrowVelocityCC>(true)).build();
rootLiteral = Literal.<Player>builder("msg", "w", "privmsg")
rootLiteral = Literal.<ScrowVelocityCC>builder("msg", "w", "privmsg")
.withArgument(targetArg)
.withArgument(msgArg)
.withSyncExecutor(this::executeSync)
.asRootLiteral(style);
}
private Result<Object, String> executeSync(CommandContext<Player> ctx) {
var target = ctx.getArg(targetArg);
private void executeSync(ScrowVelocityCC ctx) {
var target = ctx.getarg(targetArg);
var msg = ctx.getArg(msgArg);
var isMessagingSelf = target.getUniqueId().equals(ctx.sender().getUniqueId());
if (isMessagingSelf) return Results.failure("You can't message yourself.");
var isMessagingSelf = target.getUniqueId().equals(ctx.player().getUniqueId());
if (isMessagingSelf) {
ctx.player().sendMessage(ctx.style().err("You can't message yourself."));
return;
}
cache.setLastTarget(ctx.sender().getUniqueId(), target.getUniqueId());
privMsgHandler.handle(ctx.sender(), target, msg);
return Results.success(new Object());
cache.setLastTarget(ctx.player().getUniqueId(), target.getUniqueId());
privMsgHandler.handle(ctx.player(), target, msg);
}
public RootLiteral<Player> rootLiteral() {
public RootLiteral<ScrowVelocityCC> rootLiteral() {
return rootLiteral;
}
}

View file

@ -1,53 +1,51 @@
package de.kentoj.scrow.corevelocity.privmsg;
import com.leakyabstractions.result.api.Result;
import com.leakyabstractions.result.core.Results;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import de.kentoj.kencommandapi.api.argument.CommandArgumentSpec;
import de.kentoj.kencommandapi.api.argument.types.StringArgumentType;
import de.kentoj.kencommandapi.api.invocation.CommandContext;
import de.kentoj.kencommandapi.api.literal.Literal;
import de.kentoj.kencommandapi.api.literal.RootLiteral;
import de.kentoj.kencommandapi.api.platform.MessageStyle;
import de.kentoj.kencommandapi.type.PlayerArgumentType;
import de.kentoj.scrow.velocity.commands.ScrowVelocityCC;
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.argument.types.StringArgumentType;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
public class ReplyCommand {
private final RootLiteral<Player> rootLiteral;
private final CommandArgumentSpec<Player, String> msgArg;
private final RootLiteral<ScrowVelocityCC> rootLiteral;
private final Argument<ScrowVelocityCC, String> msgArg;
private final LastTargetCache cache;
private final ProxyServer server;
private final PrivMsgHandler privMsgHandler;
public ReplyCommand(ProxyServer server, LastTargetCache cache, PrivMsgHandler privMsgHandler, MessageStyle style) {
public ReplyCommand(ProxyServer server, LastTargetCache cache, PrivMsgHandler privMsgHandler, ScrowMessageStyle style) {
this.cache = cache;
this.server = server;
this.privMsgHandler = privMsgHandler;
msgArg = CommandArgumentSpec.builder("msg", new StringArgumentType<Player>(true)).build();
rootLiteral = Literal.<Player>builder("reply", "r")
msgArg = Argument.builder("msg", new StringArgumentType<ScrowVelocityCC>(true)).build();
rootLiteral = Literal.<ScrowVelocityCC>builder("reply", "r")
.withArgument(msgArg)
.withSyncExecutor(this::executeSync)
.asRootLiteral(style);
}
private Result<Object, String> executeSync(CommandContext<Player> ctx) {
var targetId = cache.getLastTarget(ctx.sender().getUniqueId());
if (targetId == null)
return Results.failure("You didn't messaged anyone recently. Use /msg first");
private void executeSync(ScrowVelocityCC ctx) {
var targetId = cache.getLastTarget(ctx.player().getUniqueId());
if (targetId == null) {
ctx.player().sendMessage(ctx.style().err("You didn't messaged anyone recently. Use /msg first"));
return;
}
var target = server.getPlayer(targetId).orElse(null);
var msg = ctx.getArg(msgArg);
if (target == null)
return Results.failure("The player you last messaged is no longer online.");
if (target == null) {
ctx.player().sendMessage(ctx.style().err("The player you last messaged is no longer online."));
return;
}
privMsgHandler.handle(ctx.sender(), target, msg);
return Results.success(new Object());
}
public RootLiteral<Player> rootLiteral() {
public RootLiteral<ScrowVelocityCC> rootLiteral() {
return rootLiteral;
}
}

View file

@ -1,50 +1,105 @@
package de.kentoj.scrow.corevelocity.serverregistration;
import com.google.common.hash.Hashing;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.stream.Stream;
import java.util.List;
import java.util.stream.StreamSupport;
public class ConsulV1ServerInstanceScanner implements ServerInstanceScanner, Closeable {
public class ConsulV1ServerInstanceScanner implements Closeable {
private static final Logger log = LoggerFactory.getLogger("server registration");
private final ProxyServer server;
private final String consulUrl;
private final HttpClient client;
public ConsulV1ServerInstanceScanner(String consulUrl) {
// see https://developer.hashicorp.com/consul/api-docs/features/blocking
private long lastIndex = -1;
public ConsulV1ServerInstanceScanner(ProxyServer server, String consulUrl) {
this.server = server;
this.consulUrl = consulUrl;
this.client = HttpClient.newHttpClient();
}
@Override
// FIXME query cluster-wide
public Stream<ServerInfo> queryMinecraftServers() throws IOException, InterruptedException {
var url = consulUrl + "/v1/agent/services/?filter=" + URLEncoder.encode("\"minecraft\" in Tags", StandardCharsets.UTF_8);
public void start() {
while (!Thread.currentThread().isInterrupted()) {
try {
log.info("Querying servers...");
var knownServers = server.getAllServers().stream()
.map(RegisteredServer::getServerInfo)
.toList();
var currentServers = queryMinecraftServers();
registerNewServers(knownServers, currentServers);
unregisterDeadServers(knownServers, currentServers);
} catch (IOException e) {
throw new RuntimeException("failed querying Minecraft servers", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
private void unregisterDeadServers(List<ServerInfo> knownServers, List<ServerInfo> currentServers) {
var currentServerIds = currentServers.stream().map(ServerInfo::getName).toList();
knownServers.stream()
.filter(s -> !currentServerIds.contains(s.getName()))
.forEach(s -> {
server.unregisterServer(s);
log.info("Unregistered server {}", s.getName());
});
}
private void registerNewServers(List<ServerInfo> knownServers, List<ServerInfo> currentServers) {
var knownServerIds = knownServers.stream()
.map(ServerInfo::getName)
.toList();
currentServers.forEach(s -> {
if (knownServerIds.contains(s.getName()))
return;
server.registerServer(s);
log.info("registered server {}", s.getName());
});
}
private List<ServerInfo> queryMinecraftServers() throws IOException, InterruptedException {
var url = consulUrl + "/v1/health/service/minecraft?passing=true";
if (lastIndex >= 0)
url += "&index=" + lastIndex;
var request = HttpRequest.newBuilder(URI.create(url))
.GET()
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
var jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject();
return jsonResponse.entrySet().stream()
.map(Map.Entry::getValue)
.map(JsonElement::getAsJsonObject)
var jsonResponse = JsonParser.parseString(response.body()).getAsJsonArray();
lastIndex = response.headers().firstValueAsLong("X-Consul-Index").orElseThrow();
return StreamSupport.stream(jsonResponse.spliterator(), false)
.map(j -> j.getAsJsonObject().get("Service").getAsJsonObject())
.map(service -> {
var name = toServerName(service.get("Service").getAsString(), service.get("ID").getAsString());
var host = InetSocketAddress.createUnresolved(service.get("Address").getAsString(),
service.get("Port").getAsInt());
return new ServerInfo(name, host);
});
var gamemodeName = service.get("Meta").getAsJsonObject().get("mode").getAsString();
var address = InetSocketAddress.createUnresolved(
service.get("Address").getAsString(), service.get("Port").getAsInt());
var id = toServerName(gamemodeName, service.get("ID").getAsString());
return new ServerInfo(id, address);
})
.toList();
}
private String toServerName(String serviceName, String id) {
@ -54,7 +109,7 @@ public class ConsulV1ServerInstanceScanner implements ServerInstanceScanner, Clo
}
@Override
public void close() throws IOException {
public void close() {
client.close();
}
}

View file

@ -0,0 +1,22 @@
package de.kentoj.scrow.corevelocity.serverregistration;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent;
import com.velocitypowered.api.proxy.ProxyServer;
public class PlayerTryJoinLobbyListener {
private final ProxyServer server;
public PlayerTryJoinLobbyListener(ProxyServer server) {
this.server = server;
}
@Subscribe
private void onChooseInitialServer(PlayerChooseInitialServerEvent ev) {
var lobby = server.getAllServers().stream()
.filter(n -> n.getServerInfo().getName().startsWith("lobby-"))
.findAny();
lobby.ifPresent(ev::setInitialServer);
}
}

View file

@ -1,29 +0,0 @@
package de.kentoj.scrow.corevelocity.serverregistration;
import com.velocitypowered.api.proxy.ProxyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class RegistrationTask implements Runnable {
private static final Logger log = LoggerFactory.getLogger(RegistrationTask.class);
private final ProxyServer server;
private final ServerInstanceScanner watcher;
public RegistrationTask(ProxyServer server, ServerInstanceScanner watcher) {
this.server = server;
this.watcher = watcher;
}
@Override
public void run() {
try {
watcher.queryMinecraftServers().forEach(server::registerServer);
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Failed querying available servers", e);
}
}
}

View file

@ -1,11 +0,0 @@
package de.kentoj.scrow.corevelocity.serverregistration;
import com.velocitypowered.api.proxy.server.ServerInfo;
import java.io.IOException;
import java.util.stream.Stream;
public interface ServerInstanceScanner {
Stream<ServerInfo> queryMinecraftServers() throws IOException, InterruptedException;
}