connectInfo
This commit is contained in:
@ -1,10 +1,7 @@
|
||||
package eu.mhsl.craftattack.teamLobby;
|
||||
|
||||
import eu.mhsl.craftattack.teamLobby.data.Team;
|
||||
import eu.mhsl.craftattack.teamLobby.util.CommonHandler;
|
||||
import eu.mhsl.craftattack.teamLobby.util.PluginMessageUtil;
|
||||
import eu.mhsl.craftattack.teamLobby.util.PosSerializer;
|
||||
import eu.mhsl.craftattack.teamLobby.util.ResourceExtractor;
|
||||
import eu.mhsl.craftattack.teamLobby.util.*;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -51,7 +48,7 @@ public class Lobby extends InstanceContainer {
|
||||
private final String targetServer = this.lobbyConfig.node("connectsTo").getString();
|
||||
public final Pos spawnPoint = PosSerializer.deserialize(this.lobbyConfig.node("spawnpoint").getString());
|
||||
|
||||
private final Team team;
|
||||
public final Team team;
|
||||
private boolean isComplete;
|
||||
private boolean isJoining;
|
||||
|
||||
@ -92,8 +89,6 @@ public class Lobby extends InstanceContainer {
|
||||
Path worldPath = Path.of(ResourceExtractor.commonRessourcePath.toString(), "world");
|
||||
ResourceExtractor.extractResourceFolder("/world", worldPath);
|
||||
this.setChunkLoader(new AnvilLoader(worldPath));
|
||||
|
||||
this.setBlock(this.spawnPoint.sub(0, 1 ,0), Block.DIAMOND_BLOCK);
|
||||
}
|
||||
|
||||
private <TEvent> void onPlayerChange(TEvent event) {
|
||||
@ -149,6 +144,11 @@ public class Lobby extends InstanceContainer {
|
||||
|
||||
private void connect() {
|
||||
if(!this.isJoining) return;
|
||||
if(ConnectInfo.availableTickets < 1) {
|
||||
this.everyMember(p -> p.sendActionBar(Component.text("Keine Spielzeit verfügbar!", NamedTextColor.RED)));
|
||||
return;
|
||||
}
|
||||
|
||||
this.playSound(Sound.sound(SoundEvent.ENTITY_PLAYER_LEVELUP, Sound.Source.PLAYER, 1f, 1f));
|
||||
|
||||
MinecraftServer.getSchedulerManager().scheduleTask(() -> this.everyMember(p -> {
|
||||
|
@ -25,7 +25,7 @@ public class LobbyManager {
|
||||
private final Map<Team, Lobby> instances = new HashMap<>();
|
||||
|
||||
private void loadTeams() {
|
||||
HttpRequest request = Request.builder(Request.uri("team"))
|
||||
HttpRequest request = Request.builder(Request.website("team"))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
package eu.mhsl.craftattack.teamLobby;
|
||||
|
||||
import eu.mhsl.craftattack.teamLobby.util.ConnectInfo;
|
||||
import eu.mhsl.craftattack.teamLobby.util.DisconnectInfo;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
|
||||
import net.minestom.server.timer.TaskSchedule;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
@ -65,6 +67,13 @@ public class Main {
|
||||
Lobby lobby = lobbyManager.getPlayerInstance(p);
|
||||
event.setSpawningInstance(lobby);
|
||||
p.setRespawnPoint(lobby.spawnPoint);
|
||||
MinecraftServer.getSchedulerManager().scheduleTask(
|
||||
() -> {
|
||||
ConnectInfo.tickets(lobby.team.name(), p);
|
||||
},
|
||||
TaskSchedule.seconds(3),
|
||||
TaskSchedule.stop()
|
||||
);
|
||||
} catch(Exception e) {
|
||||
new DisconnectInfo(
|
||||
"Login abgelehnt",
|
||||
|
@ -18,10 +18,13 @@ 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 websiteEndpoint = apiConfig.node("websiteEndpoint").getString();
|
||||
private static final String serverEndpoint = apiConfig.node("serverEndpoint").getString();
|
||||
private static final String auth = apiConfig.node("auth").getString();
|
||||
|
||||
private static final URI basePath = URI.create(Objects.requireNonNull(endpoint));
|
||||
private static final URI websiteBasePath = URI.create(Objects.requireNonNull(websiteEndpoint));
|
||||
private static final URI serverBasePath = URI.create(Objects.requireNonNull(serverEndpoint) + "/api/");
|
||||
|
||||
private static final Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(UUID.class, (JsonDeserializer<UUID>) (json, typeOfT, context) -> {
|
||||
if (json.isJsonNull() || json.getAsString().isBlank()) {
|
||||
@ -39,13 +42,23 @@ public class Request {
|
||||
.header("Authorization", auth);
|
||||
}
|
||||
|
||||
public static URI uri(String command) {
|
||||
return uri(command, uriBuilder -> {});
|
||||
public static URI website(String command) {
|
||||
return website(command, uriBuilder -> {});
|
||||
}
|
||||
|
||||
public static URI uri(String command, Consumer<URIBuilder> parameters) {
|
||||
public static URI website(String command, Consumer<URIBuilder> parameters) {
|
||||
try {
|
||||
URIBuilder builder = new URIBuilder(basePath + "/" + command);
|
||||
URIBuilder builder = new URIBuilder(websiteBasePath + "/" + command);
|
||||
parameters.accept(builder);
|
||||
return builder.build();
|
||||
} catch(URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static URI server(String command, Consumer<URIBuilder> parameters) {
|
||||
try {
|
||||
URIBuilder builder = new URIBuilder(serverBasePath + "/" + command);
|
||||
parameters.accept(builder);
|
||||
return builder.build();
|
||||
} catch(URISyntaxException e) {
|
||||
@ -55,6 +68,7 @@ public class Request {
|
||||
|
||||
public static <TResponse> ReqResp<TResponse> execute(HttpRequest request, Type type) {
|
||||
ReqResp<String> rawResponse = sendHttp(request);
|
||||
System.out.printf("Requesting '%s', got %d with '%s'\n", request.uri(), rawResponse.status(), rawResponse.data());
|
||||
return new ReqResp<>(rawResponse.status(), gson.fromJson(rawResponse.data(), type));
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,47 @@
|
||||
package eu.mhsl.craftattack.teamLobby.util;
|
||||
|
||||
import eu.mhsl.craftattack.teamLobby.http.ReqResp;
|
||||
import eu.mhsl.craftattack.teamLobby.http.Request;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.timer.TaskSchedule;
|
||||
|
||||
import java.net.http.HttpRequest;
|
||||
|
||||
public class ConnectInfo {
|
||||
public static int availableTickets = 0;
|
||||
public static void tickets(String teamName, Player player) {
|
||||
var task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
HttpRequest request = Request.builder(Request.server(
|
||||
"playtimer/tickets",
|
||||
uriBuilder -> uriBuilder.addParameter("team", teamName)
|
||||
)).GET().build();
|
||||
|
||||
record Ticket(String team, int tickets) {}
|
||||
record Status(String status, Ticket response) {}
|
||||
ReqResp<Status> ticket = Request.execute(request, Status.class);
|
||||
availableTickets = ticket.data().response.tickets;
|
||||
|
||||
MinecraftServer.getSchedulerManager().submitTask(
|
||||
() -> {
|
||||
String playtimeOverview = String.format(
|
||||
"Dein Team hat noch %d Beitritte, also %dx%d=%d Minuten übrig.",
|
||||
availableTickets,
|
||||
availableTickets,
|
||||
30,
|
||||
availableTickets * 30
|
||||
);
|
||||
player.sendMessage(Component.text(playtimeOverview, NamedTextColor.DARK_GREEN));
|
||||
return TaskSchedule.stop();
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
new Thread(task).start();
|
||||
}
|
||||
}
|
@ -13,5 +13,6 @@ authentication:
|
||||
secret: '' # only for VELOCITY proxies
|
||||
|
||||
api:
|
||||
endpoint: "https://mhsl.eu/varo/api/"
|
||||
auth: "Basic xxx"
|
||||
websiteEndpoint: "https://mhsl.eu/varo/api/"
|
||||
auth: "Basic xxx"
|
||||
serverEndpoint: "http://localhost:8080/"
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user