From da6fbf6e4bd874244a3f801c1a7f0510fc7fe183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Fri, 30 May 2025 00:38:21 +0200 Subject: [PATCH] team api implementation --- build.gradle | 1 + .../eu/mhsl/craftattack/teamLobby/Lobby.java | 16 ++++- .../craftattack/teamLobby/LobbyManager.java | 28 +++++--- .../mhsl/craftattack/teamLobby/data/Team.java | 13 ++-- .../craftattack/teamLobby/http/ReqResp.java | 10 +++ .../craftattack/teamLobby/http/Request.java | 69 +++++++++++++++++++ src/main/resources/config.yml | 4 ++ 7 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 src/main/java/eu/mhsl/craftattack/teamLobby/http/ReqResp.java create mode 100644 src/main/java/eu/mhsl/craftattack/teamLobby/http/Request.java diff --git a/build.gradle b/build.gradle index 566b4f6..e8bcb7e 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/src/main/java/eu/mhsl/craftattack/teamLobby/Lobby.java b/src/main/java/eu/mhsl/craftattack/teamLobby/Lobby.java index 0b9745e..cb70de8 100644 --- a/src/main/java/eu/mhsl/craftattack/teamLobby/Lobby.java +++ b/src/main/java/eu/mhsl/craftattack/teamLobby/Lobby.java @@ -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); diff --git a/src/main/java/eu/mhsl/craftattack/teamLobby/LobbyManager.java b/src/main/java/eu/mhsl/craftattack/teamLobby/LobbyManager.java index 2e4ff62..9901393 100644 --- a/src/main/java/eu/mhsl/craftattack/teamLobby/LobbyManager.java +++ b/src/main/java/eu/mhsl/craftattack/teamLobby/LobbyManager.java @@ -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 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 teams = Set.of(); private final Map instances = new HashMap<>(); + private void loadTeams() { + HttpRequest request = Request.builder(Request.uri("team")) + .GET() + .build(); + + this.teams = Request.execute(request, new TypeToken>(){}.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); diff --git a/src/main/java/eu/mhsl/craftattack/teamLobby/data/Team.java b/src/main/java/eu/mhsl/craftattack/teamLobby/data/Team.java index 414ed6f..85f52ad 100644 --- a/src/main/java/eu/mhsl/craftattack/teamLobby/data/Team.java +++ b/src/main/java/eu/mhsl/craftattack/teamLobby/data/Team.java @@ -4,9 +4,14 @@ import java.util.List; import java.util.UUID; public record Team( - UUID teamId, - String teamName, - String hexColor, - List players + String name, + String color, + Object lastJoined, + Object strikeWeight, + List users ) { + public record User( + UUID uuid, + boolean dead + ) {} } diff --git a/src/main/java/eu/mhsl/craftattack/teamLobby/http/ReqResp.java b/src/main/java/eu/mhsl/craftattack/teamLobby/http/ReqResp.java new file mode 100644 index 0000000..5556874 --- /dev/null +++ b/src/main/java/eu/mhsl/craftattack/teamLobby/http/ReqResp.java @@ -0,0 +1,10 @@ +package eu.mhsl.craftattack.teamLobby.http; + + +public record ReqResp(int status, TData data) { + @SuppressWarnings("unchecked") + public T cast() { + System.out.println("Casting: " + this.data); + return (T) this.data; + } +} \ No newline at end of file diff --git a/src/main/java/eu/mhsl/craftattack/teamLobby/http/Request.java b/src/main/java/eu/mhsl/craftattack/teamLobby/http/Request.java new file mode 100644 index 0000000..b0c8ec8 --- /dev/null +++ b/src/main/java/eu/mhsl/craftattack/teamLobby/http/Request.java @@ -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) (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 parameters) { + try { + URIBuilder builder = new URIBuilder(basePath + "/" + command); + parameters.accept(builder); + return builder.build(); + } catch(URISyntaxException e) { + throw new RuntimeException(e); + } + } + + public static ReqResp execute(HttpRequest request, Type type) { + ReqResp rawResponse = sendHttp(request); + return new ReqResp<>(rawResponse.status(), gson.fromJson(rawResponse.data(), type)); + } + + private static ReqResp sendHttp(HttpRequest request) { + try(HttpClient client = HttpClient.newHttpClient()) { + HttpResponse httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); + return new ReqResp<>(httpResponse.statusCode(), httpResponse.body()); + } catch(IOException | InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 61440b9..48ad2fa 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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" \ No newline at end of file