Skip to content

Commit

Permalink
Merge branch 'main' into feat/profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Aug 26, 2024
2 parents 6686774 + 51eaaf0 commit ef27685
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 56 deletions.
11 changes: 6 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ repositories {

dependencies {
// Minestom
api("net.minestom:minestom-snapshots:17fd82a5c1")
// api("net.minestom:minestom-snapshots:0c9527118a") // todo update to this minestom - lots of breaking changes
api("net.kyori:adventure-text-minimessage:4.15.0") // Version that Minestom uses (https://github.com/Minestom/Minestom/blob/17fd82a5c1dc845d417ab2c60ae1248369e4fdff/gradle/libs.versions.toml#L7)
api("net.minestom:minestom-snapshots:789befee31")
api("net.kyori:adventure-text-minimessage:4.15.0")
implementation("io.pyroscope:agent:0.12.2")

// Logger
Expand All @@ -31,8 +30,8 @@ dependencies {

// APIs
api("dev.emortal.api:module-system:1.0.0")
api("dev.emortal.api:agones-sdk:1.0.7")
api("dev.emortal.api:common-proto-sdk:6a9ad1a")
api("dev.emortal.api:agones-sdk:1.1.0")
api("dev.emortal.api:common-proto-sdk:fee482e")
api("dev.emortal.api:live-config-parser:f0728b0")

api("io.kubernetes:client-java:18.0.1")
Expand All @@ -47,6 +46,8 @@ java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}

withSourcesJar()
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.inventory.condition.InventoryConditionResult;
import net.minestom.server.item.ItemHideFlag;
import net.minestom.server.item.ItemComponent;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.item.component.AttributeList;
import net.minestom.server.tag.Tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -120,11 +121,12 @@ private void onClick(@NotNull Player clicker, int slot, @NotNull ClickType click
return;
}

boolean isUnlocked = clickedItem.meta().getTag(BADGE_UNLOCKED_TAG);
boolean isActive = clickedItem.meta().getTag(BADGE_ACTIVE_TAG);

boolean isUnlocked = clickedItem.getTag(BADGE_UNLOCKED_TAG);
boolean isActive = clickedItem.getTag(BADGE_ACTIVE_TAG);
if (!isUnlocked || isActive) return;

String badgeId = clickedItem.meta().getTag(BADGE_ID_TAG);
String badgeId = clickedItem.getTag(BADGE_ID_TAG);
try {
this.badgeService.setActiveBadge(clicker.getUuid(), badgeId);
} catch (StatusRuntimeException exception) {
Expand All @@ -133,7 +135,7 @@ private void onClick(@NotNull Player clicker, int slot, @NotNull ClickType click
return;
}

String badgeName = clickedItem.meta().getTag(BADGE_NAME_TAG);
String badgeName = clickedItem.getTag(BADGE_NAME_TAG);
clicker.sendMessage(Component.text("Set active badge to " + badgeName, NamedTextColor.GREEN));
new BadgeGui(this.badgeService, clicker); // Reopen the gui
}
Expand Down Expand Up @@ -163,14 +165,13 @@ private void onClick(@NotNull Player clicker, int slot, @NotNull ClickType click
}

return ItemStack.builder(Material.fromNamespaceId(guiItem.getMaterial()))
.displayName(MINI_MESSAGE.deserialize(guiItem.getDisplayName()))
.lore(lore)
.meta(builder -> builder.hideFlag(ItemHideFlag.HIDE_ATTRIBUTES)
.set(BADGE_ID_TAG, badge.getId())
.set(BADGE_NAME_TAG, badge.getFriendlyName())
.set(BADGE_ACTIVE_TAG, isActive)
.set(BADGE_UNLOCKED_TAG, isOwned)
)
.build();
.set(ItemComponent.ITEM_NAME, MINI_MESSAGE.deserialize(guiItem.getDisplayName()))
.set(ItemComponent.LORE, lore)
.set(BADGE_ID_TAG, badge.getId())
.set(BADGE_NAME_TAG, badge.getFriendlyName())
.set(BADGE_ACTIVE_TAG, isActive)
.set(BADGE_UNLOCKED_TAG, isOwned)
.set(ItemComponent.ATTRIBUTE_MODIFIERS, new AttributeList(List.of(), false)
).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.agones.sdk.AgonesSDKProto;
import dev.agones.sdk.SDKGrpc;
import dev.agones.sdk.beta.BetaAgonesSDKProto;
import dev.emortal.api.agonessdk.AgonesUtils;
import dev.emortal.api.agonessdk.IgnoredStreamObserver;
import dev.emortal.api.modules.Module;
Expand All @@ -11,12 +12,15 @@
import dev.emortal.minestom.core.Environment;
import dev.emortal.minestom.core.module.kubernetes.command.agones.AgonesCommand;
import dev.emortal.minestom.core.module.kubernetes.command.currentserver.CurrentServerCommand;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.kubernetes.client.ProtoClient;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.util.Config;
import net.minestom.server.MinecraftServer;
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
import net.minestom.server.event.player.PlayerDisconnectEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -48,6 +52,7 @@ public final class KubernetesModule extends Module {
private ProtoClient protoClient;

private SDKGrpc.SDKStub sdk;
private dev.agones.sdk.beta.SDKGrpc.SDKStub betaSdk;

public KubernetesModule(@NotNull ModuleEnvironment environment, @NotNull AgonesSDKProto.KeyValue... additionalLabels) {
super(environment);
Expand Down Expand Up @@ -80,9 +85,11 @@ public boolean onLoad() {
}

private void loadAgones() {
this.sdk = SDKGrpc.newStub(ManagedChannelBuilder.forAddress(AGONES_ADDRESS, AGONES_GRPC_PORT).usePlaintext().build());
ManagedChannel channel = ManagedChannelBuilder.forAddress(AGONES_ADDRESS, AGONES_GRPC_PORT).usePlaintext().build();
this.sdk = SDKGrpc.newStub(channel);
this.betaSdk = dev.agones.sdk.beta.SDKGrpc.newStub(channel);

MinecraftServer.getCommandManager().register(new AgonesCommand(this.sdk));
MinecraftServer.getCommandManager().register(new AgonesCommand(this.sdk, this.betaSdk));

for (AgonesSDKProto.KeyValue label : this.additionalLabels) {
this.sdk.setLabel(label, new IgnoredStreamObserver<>());
Expand All @@ -103,6 +110,40 @@ private void loadAgones() {

this.sdk.setAnnotation(protocolVersion, new IgnoredStreamObserver<>());
this.sdk.setAnnotation(versionName, new IgnoredStreamObserver<>());

this.loadAgonesCountsAndLists();
}

private void loadAgonesCountsAndLists() {
MinecraftServer.getGlobalEventHandler().addListener(AsyncPlayerConfigurationEvent.class, event -> {
this.updateAgonesCounter("players", 1);
this.addToAgonesList("players", event.getPlayer().getUuid().toString());
}).addListener(PlayerDisconnectEvent.class, event -> {
this.updateAgonesCounter("players", -1);
this.removeFromAgonesList("players", event.getPlayer().getUuid().toString());
});
}

public void updateAgonesCounter(String name, long diff) {
this.betaSdk.updateCounter(BetaAgonesSDKProto.UpdateCounterRequest.newBuilder()
.setCounterUpdateRequest(BetaAgonesSDKProto.CounterUpdateRequest.newBuilder()
.setName(name)
.setCountDiff(diff))
.build(), new IgnoredStreamObserver<>());
}

public void addToAgonesList(String listName, String value) {
this.betaSdk.addListValue(BetaAgonesSDKProto.AddListValueRequest.newBuilder()
.setName(listName)
.setValue(value)
.build(), new IgnoredStreamObserver<>());
}

public void removeFromAgonesList(String listName, String value) {
this.betaSdk.removeListValue(BetaAgonesSDKProto.RemoveListValueRequest.newBuilder()
.setName(listName)
.setValue(value)
.build(), new IgnoredStreamObserver<>());
}

@Override
Expand Down Expand Up @@ -137,7 +178,11 @@ private void readyAgones() {
return this.protoClient;
}

public @Nullable SDKGrpc.SDKStub getSdk() {
public @Nullable SDKGrpc.SDKStub getAgonesSdk() {
return this.sdk;
}

public @Nullable dev.agones.sdk.beta.SDKGrpc.SDKStub getAgonesBetaSdk() {
return this.betaSdk;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@

public final class AgonesCommand extends Command {

public AgonesCommand(@NotNull SDKGrpc.SDKStub sdk) {
public AgonesCommand(@NotNull SDKGrpc.SDKStub sdk, dev.agones.sdk.beta.SDKGrpc.SDKStub betaSdk) {
super("magones");
this.setCondition(ExtraConditions.hasPermission("command.agones"));

var sdkSubs = new SdkSubCommands(sdk);
var sdkSubs = new SdkSubCommands(sdk, betaSdk);

// /magones get gameserver
this.addSyntax(sdkSubs::executeGetGameServer, new ArgumentLiteral("get"), new ArgumentLiteral("gameserver"));
// /magones reserve <duration>
this.addSyntax(sdkSubs::executeReserve, new ArgumentLiteral("reserve"), new ArgumentTime("duration"));
// /magones allocate
this.addSyntax(sdkSubs::executeAllocate, new ArgumentLiteral("allocate"));

// /magones get count [id]
this.addSyntax(sdkSubs::executeListCounters, new ArgumentLiteral("get"), new ArgumentLiteral("counters"));

// /magones get list [id]
this.addSyntax(sdkSubs::executeListLists, new ArgumentLiteral("get"), new ArgumentLiteral("list"));
this.addSyntax(sdkSubs::executeGetList, new ArgumentLiteral("get"), new ArgumentLiteral("list"), new ArgumentString("id"));

var set = new ArgumentLiteral("set");
var key = new ArgumentString("key");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package dev.emortal.minestom.core.module.kubernetes.command.agones;

import allocation.Allocation;
import dev.agones.sdk.AgonesSDKProto;
import dev.agones.sdk.SDKGrpc;
import dev.agones.sdk.beta.BetaAgonesSDKProto;
import io.grpc.stub.StreamObserver;

import java.util.Map;
import java.util.StringJoiner;
import java.util.function.Function;
import net.kyori.adventure.text.Component;
import net.minestom.server.command.CommandSender;
Expand All @@ -15,14 +20,20 @@ public final class SdkSubCommands {

private static @NotNull Component generateMessage(@NotNull String sdk, @NotNull String method, @NotNull AgonesCommand.RequestStatus status,
@NotNull String message) {
String text = "Agones >> [%s.%s] (%s) %s".formatted(sdk, method, status.name(), message);
String text = "Agones >> [%s.%s] (%s)".formatted(sdk, method, status.name());
if (!message.isEmpty()) {
text += "\n" + message;
}

return Component.text(text, status.getColor());
}

private final @NotNull SDKGrpc.SDKStub sdk;
private final @NotNull dev.agones.sdk.beta.SDKGrpc.SDKStub betaSdk;

public SdkSubCommands(@NotNull SDKGrpc.SDKStub sdk) {
public SdkSubCommands(@NotNull SDKGrpc.SDKStub sdk, dev.agones.sdk.beta.SDKGrpc.@NotNull SDKStub betaSdk) {
this.sdk = sdk;
this.betaSdk = betaSdk;
}

private <T> @NotNull StreamObserver<T> createCallback(@NotNull CommandSender sender, @NotNull String sdkMethod,
Expand Down Expand Up @@ -54,15 +65,40 @@ public void executeGetGameServer(@NotNull CommandSender sender, @NotNull Command
this.sdk.getGameServer(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "GetGameServer", Object::toString));
}

public void executeReserve(@NotNull CommandSender sender, @NotNull CommandContext context) {
Duration duration = context.get("duration");
AgonesSDKProto.Duration agonesDuration = AgonesSDKProto.Duration.newBuilder().setSeconds(duration.getSeconds()).build();
public void executeListCounters(@NotNull CommandSender sender, @NotNull CommandContext context) {
this.sdk.getGameServer(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "GetGameServer [Counters]", gameServer -> {
Map<String, AgonesSDKProto.GameServer.Status.CounterStatus> counters = gameServer.getStatus().getCountersMap();
StringJoiner joiner = new StringJoiner(", ", "Counters: \n", "");
for (Map.Entry<String, AgonesSDKProto.GameServer.Status.CounterStatus> entry : counters.entrySet()) {
String key = entry.getKey();
AgonesSDKProto.GameServer.Status.CounterStatus value = entry.getValue();
joiner.add(" - %s (%s/%s)".formatted(key, value.getCount(), value.getCapacity()));
}

return joiner.toString();
}));
}

public void executeListLists(@NotNull CommandSender sender, @NotNull CommandContext context) {
this.sdk.getGameServer(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "GetGameServer [Lists]", gameServer -> {
Map<String, AgonesSDKProto.GameServer.Status.ListStatus> lists = gameServer.getStatus().getListsMap();

StringJoiner joiner = new StringJoiner(", ", "Lists: \n", "");
for (Map.Entry<String, AgonesSDKProto.GameServer.Status.ListStatus> entry : lists.entrySet()) {
String key = entry.getKey();
AgonesSDKProto.GameServer.Status.ListStatus value = entry.getValue();
joiner.add(" - %s (%s/%s)".formatted(key, value.getValuesCount(), value.getCapacity()));
}

this.sdk.reserve(agonesDuration, this.createCallback(sender, "Reserve", v -> "Reserved for %s seconds".formatted(duration.getSeconds())));
return joiner.toString();
}));
}

public void executeAllocate(@NotNull CommandSender sender, @NotNull CommandContext context) {
this.sdk.allocate(AgonesSDKProto.Empty.getDefaultInstance(), this.createCallback(sender, "Allocate", v -> "Allocated"));
public void executeGetList(@NotNull CommandSender sender, @NotNull CommandContext context) {
String id = context.get("id");
AgonesSDKProto.KeyValue keyValue = AgonesSDKProto.KeyValue.newBuilder().setKey(id).build();
this.betaSdk.getList(BetaAgonesSDKProto.GetListRequest.newBuilder().setName(id).build(),
this.createCallback(sender, "GetList", Object::toString));
}

public void executeSetAnnotation(@NotNull CommandSender sender, @NotNull CommandContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
public final class CurrentServerCommand extends Command {
private static final Logger LOGGER = LoggerFactory.getLogger(CurrentServerCommand.class);

private static final String MESSAGE = """
<dark_purple>Proxy: <light_purple><proxy_id>
<dark_purple>Server: <light_purple><server_id>
<dark_purple>Fleet: <light_purple><fleet_id>""";
private static final String COPY_MESSAGE = """
Proxy: %s
Server: %s
Instance: %s
Position: %s""";

private final @NotNull PlayerTrackerService playerTracker;
Expand Down Expand Up @@ -59,21 +54,37 @@ private void onExecute(@NotNull CommandSender sender, @NotNull CommandContext co
return;
}

var serverId = Placeholder.unparsed("server_id", currentServer.getServerId());
var proxyId = Placeholder.unparsed("proxy_id", currentServer.getProxyId());
var fleetId = Placeholder.unparsed("fleet_id", currentServer.getFleetName());
Component message = Component.text().append(
Component.text("Proxy: ", NamedTextColor.DARK_PURPLE)
.append(Component.text(currentServer.getProxyId(), NamedTextColor.LIGHT_PURPLE))
.hoverEvent(HoverEvent.showText(Component.text("Click to copy Proxy ID", NamedTextColor.GREEN)))
.clickEvent(ClickEvent.copyToClipboard(currentServer.getProxyId()))
).append(
Component.newline()
.append(Component.text("Server: ", NamedTextColor.DARK_PURPLE))
.append(Component.text(currentServer.getServerId(), NamedTextColor.LIGHT_PURPLE))
.hoverEvent(HoverEvent.showText(Component.text("Click to copy Server ID", NamedTextColor.GREEN)))
.clickEvent(ClickEvent.copyToClipboard(currentServer.getServerId()))
).append(
Component.newline()
.append(Component.text("Fleet: ", NamedTextColor.DARK_PURPLE))
.append(Component.text(currentServer.getFleetName(), NamedTextColor.LIGHT_PURPLE))
.hoverEvent(HoverEvent.showText(Component.text("Click to copy Fleet Name", NamedTextColor.GREEN)))
.clickEvent(ClickEvent.copyToClipboard(currentServer.getFleetName()))
).append(
Component.newline()
.append(Component.text("⎘ Click to copy", NamedTextColor.GREEN))
.clickEvent(ClickEvent.copyToClipboard(this.createCopyableData(currentServer, player)))
.hoverEvent(HoverEvent.showText(Component.text("Click to copy all data", NamedTextColor.GREEN)))
).build();

var message = MiniMessage.miniMessage().deserialize(MESSAGE, serverId, proxyId, fleetId)
.clickEvent(ClickEvent.copyToClipboard(this.createCopyableData(currentServer, player)))
.hoverEvent(HoverEvent.showText(Component.text("Click to copy", NamedTextColor.GREEN)));
sender.sendMessage(message);
}

private @NotNull String createCopyableData(@NotNull CurrentServer server, @NotNull Player player) {
return COPY_MESSAGE.formatted(
server.getProxyId(),
server.getServerId(),
player.getInstance().getUniqueId(),
this.formatPos(player.getPosition())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minestom.server.entity.Player;
import net.minestom.server.timer.TaskSchedule;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
import java.util.concurrent.Executors;
Expand All @@ -33,7 +34,7 @@ public final class DefaultMatchmakingSessionImpl extends MatchmakingSession {
private final @NotNull ScheduledFuture<?> notificationTask;
private final @NotNull GameModeConfig gameMode;

public DefaultMatchmakingSessionImpl(@NotNull Player player, @NotNull GameModeConfig gameMode, @NotNull Ticket ticket) {
public DefaultMatchmakingSessionImpl(@NotNull Player player, @NotNull GameModeConfig gameMode, @NotNull Ticket ticket, @Nullable PendingMatch pendingMatch) {
super(player, ticket);

this.notificationTask = SCHEDULER.scheduleAtFixedRate(this::notifyPlayer, 30, 30, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.emortal.api.model.matchmaker.Ticket;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class MatchmakingSession {

Expand Down Expand Up @@ -54,6 +55,6 @@ public void setTicket(@NotNull Ticket ticket) {
@FunctionalInterface
public interface Creator {

@NotNull MatchmakingSession create(@NotNull Player player, @NotNull GameModeConfig gameMode, @NotNull Ticket ticket);
@NotNull MatchmakingSession create(@NotNull Player player, @NotNull GameModeConfig gameMode, @NotNull Ticket ticket, @Nullable PendingMatch pendingMatch);
}
}
Loading

0 comments on commit ef27685

Please sign in to comment.