team api implementation

This commit is contained in:
Elias Müller 2025-05-30 00:38:21 +02:00
parent df0f36fad4
commit da6fbf6e4b
7 changed files with 126 additions and 15 deletions

View File

@ -25,6 +25,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'org.spongepowered:configurate-yaml:4.1.2'
implementation 'com.google.guava:guava:32.0.1-android'
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
}
java {

View File

@ -100,10 +100,13 @@ public class Lobby extends InstanceContainer {
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> {
this.isJoining = false;
boolean wasComplete = this.isComplete;
var livingPlayers = this.team.users().stream()
.filter(user -> !user.dead())
.map(Team.User::uuid).toList();
this.isComplete = this.getPlayers().stream()
.map(Entity::getUuid)
.collect(Collectors.toSet())
.containsAll(this.team.players());
.containsAll(livingPlayers);
if(this.isComplete) {
this.playSound(Sound.sound(SoundEvent.ENTITY_EXPERIENCE_ORB_PICKUP, Sound.Source.PLAYER, 1f, 1f));
@ -149,6 +152,17 @@ public class Lobby extends InstanceContainer {
this.playSound(Sound.sound(SoundEvent.ENTITY_PLAYER_LEVELUP, Sound.Source.PLAYER, 1f, 1f));
MinecraftServer.getSchedulerManager().scheduleTask(() -> this.everyMember(p -> {
var isDead = this.team.users().stream()
.filter(user -> user.uuid().equals(p.getUuid()))
.findAny()
.orElseThrow()
.dead();
if(isDead) {
p.sendActionBar(Component.text("Du bist bereits aus dem Projekt ausgeschieden!", NamedTextColor.RED));
return;
}
p.addEffect(new Potion(PotionEffect.DARKNESS, 0, 5 * Ticks.TICKS_PER_SECOND));
p.sendActionBar(Component.text("Verbinde...", NamedTextColor.GREEN));
PluginMessageUtil.connect(p, this.targetServer);

View File

@ -1,28 +1,36 @@
package eu.mhsl.craftattack.teamLobby;
import com.google.common.reflect.TypeToken;
import eu.mhsl.craftattack.teamLobby.data.Team;
import eu.mhsl.craftattack.teamLobby.http.Request;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.net.http.HttpRequest;
import java.util.*;
public class LobbyManager {
private final Set<Team> teams = new HashSet<>() {
{
this.add(new Team(UUID.randomUUID(), "Testerr", "#123123", List.of(
UUID.fromString("c291290d-cffc-4649-aeec-d6f4417896ea"),
UUID.fromString("959ed433-14ea-38fe-918b-75b7d09466af")
)));
}
};
public LobbyManager() {
this.loadTeams();
}
private Set<Team> teams = Set.of();
private final Map<Team, Lobby> instances = new HashMap<>();
private void loadTeams() {
HttpRequest request = Request.builder(Request.uri("team"))
.GET()
.build();
this.teams = Request.execute(request, new TypeToken<Set<Team>>(){}.getType()).cast();
}
public synchronized @NotNull Lobby getPlayerInstance(Player player) {
UUID playerId = player.getUuid();
Team targetTeam = this.teams.stream()
.filter(team -> team.players().contains(playerId))
.filter(team -> team.users().stream().anyMatch(user -> user.uuid().equals(playerId)))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Player is not in any Team!"));
.orElseThrow(() -> new NoSuchElementException("Dein Account konnte nicht zugeordnet werden. Falls dies ein Fehler ist kontaktiere bitte einen Admin."));
if(!this.instances.containsKey(targetTeam)) {
Lobby instance = new Lobby(targetTeam);

View File

@ -4,9 +4,14 @@ import java.util.List;
import java.util.UUID;
public record Team(
UUID teamId,
String teamName,
String hexColor,
List<UUID> players
String name,
String color,
Object lastJoined,
Object strikeWeight,
List<User> users
) {
public record User(
UUID uuid,
boolean dead
) {}
}

View File

@ -0,0 +1,10 @@
package eu.mhsl.craftattack.teamLobby.http;
public record ReqResp<TData>(int status, TData data) {
@SuppressWarnings("unchecked")
public <T> T cast() {
System.out.println("Casting: " + this.data);
return (T) this.data;
}
}

View File

@ -0,0 +1,69 @@
package eu.mhsl.craftattack.teamLobby.http;
import com.google.gson.*;
import eu.mhsl.craftattack.teamLobby.Main;
import org.apache.http.client.utils.URIBuilder;
import org.spongepowered.configurate.ConfigurationNode;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;
public class Request {
private static final ConfigurationNode apiConfig = Main.getConfig().node("api");
private static final String endpoint = apiConfig.node("endpoint").getString();
private static final String auth = apiConfig.node("auth").getString();
private static final URI basePath = URI.create(Objects.requireNonNull(endpoint));
private static final Gson gson = new GsonBuilder()
.registerTypeAdapter(UUID.class, (JsonDeserializer<UUID>) (json, typeOfT, context) -> {
if (json.isJsonNull() || json.getAsString().isBlank()) {
return new UUID(0L, 0L);
}
return UUID.fromString(json.getAsString());
})
.create();
public static HttpRequest.Builder builder(URI endpoint) {
return HttpRequest.newBuilder()
.uri(endpoint)
.header("User-Agent", "teamLobby minestom")
.header("Content-Type", "application/json")
.header("Authorization", auth);
}
public static URI uri(String command) {
return uri(command, uriBuilder -> {});
}
public static URI uri(String command, Consumer<URIBuilder> parameters) {
try {
URIBuilder builder = new URIBuilder(basePath + "/" + command);
parameters.accept(builder);
return builder.build();
} catch(URISyntaxException e) {
throw new RuntimeException(e);
}
}
public static <TResponse> ReqResp<TResponse> execute(HttpRequest request, Type type) {
ReqResp<String> rawResponse = sendHttp(request);
return new ReqResp<>(rawResponse.status(), gson.fromJson(rawResponse.data(), type));
}
private static ReqResp<String> sendHttp(HttpRequest request) {
try(HttpClient client = HttpClient.newHttpClient()) {
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
return new ReqResp<>(httpResponse.statusCode(), httpResponse.body());
} catch(IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -11,3 +11,7 @@ lobby:
authentication:
method: 'NONE' # supported values: 'NONE', 'VANILLA', 'BUNGEECORD', 'VELOCITY'
secret: '' # only for VELOCITY proxies
api:
endpoint: "https://mhsl.eu/varo/api/"
auth: "Basic xxx"