Added Event api communication for creation of rooms

This commit is contained in:
Elias Müller 2023-11-18 15:27:50 +01:00
parent 281a2109e6
commit 5786b2409e
6 changed files with 63 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@ -68,16 +69,16 @@ public abstract class Appliance {
public void destruct(@NotNull JavaPlugin plugin) {
eventHandlers().forEach(HandlerList::unregisterAll);
commands().forEach(command -> setCommandExecutor(plugin, command.commandName, null));
}
private void setCommandExecutor(JavaPlugin plugin, String name, ApplianceCommand<?> executor) {
PluginCommand command = plugin.getCommand(name);
if(command != null) {
if(command != null && executor != null) {
command.setExecutor(executor);
command.setTabCompleter(executor);
} else {
Bukkit.getLogger().warning("Command " + name + " is not specified in plugin.yml!");
throw new RuntimeException("All commands must be registered in plugin.yml. Missing command: " + name);
}
}
}

View File

@ -53,7 +53,7 @@ public abstract class ApplianceCommand<T extends Appliance> extends ApplianceSup
return response.stream().filter(s -> s.startsWith(args[args.length-1])).toList();
}
protected abstract void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args);
protected abstract void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception;
/**
* Utility class for command which can only be used as a Player. You can access the executing player with the getPlayer() method.

View File

@ -1,5 +1,6 @@
package eu.mhsl.craftattack.spawn.appliances.event;
import com.google.gson.Gson;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.event.command.EventCommand;
@ -17,11 +18,21 @@ import org.bukkit.entity.Villager;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
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.List;
import java.util.UUID;
public class Event extends Appliance {
public DisplayVillager.ConfigBound villager;
private boolean isOpen = false;
private UUID roomId;
private HttpClient eventServerClient = HttpClient.newHttpClient();
public Event() {
super("event");
@ -37,11 +48,25 @@ public class Event extends Appliance {
villager.setVillagerType(Villager.Type.SNOW);
}
);
this.isOpen = localConfig().getBoolean("enabled", false);
if(this.isOpen) this.roomId = UUID.fromString(localConfig().getString("roomId", ""));
}
public void openEvent() {
public void openEvent() throws URISyntaxException, IOException, InterruptedException {
if(isOpen) throw new ApplianceCommand.Error("Es läuft derzeit bereits ein Event!");
HttpRequest createRoomRequest = HttpRequest.newBuilder()
.uri(new URI(localConfig().getString("api") + "/room"))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> rawResponse = eventServerClient.send(createRoomRequest, HttpResponse.BodyHandlers.ofString());
if(rawResponse.statusCode() != 200) throw new ApplianceCommand.Error("Event-Server meldet Fehler: " + rawResponse.statusCode());
record Response(UUID uuid) {}
Response response = new Gson().fromJson(rawResponse.body(), Response.class);
isOpen = true;
roomId = response.uuid;
}
public void joinEvent(Player p) {
@ -50,7 +75,27 @@ public class Event extends Appliance {
return;
}
PluginMessage.connect(p, localConfig().getString("connect-server-name"));
try {
p.sendMessage(Component.text("Authentifiziere...", NamedTextColor.GREEN));
record Request(UUID player, UUID room) {}
Request request = new Request(p.getUniqueId(), this.roomId);
HttpRequest queueRoomRequest = HttpRequest.newBuilder()
.uri(new URI(localConfig().getString("api") + "/queueRoom"))
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(request)))
.build();
HttpResponse<Void> rawResponse = eventServerClient.send(queueRoomRequest, HttpResponse.BodyHandlers.discarding());
if(rawResponse.statusCode() != 200) {
p.sendMessage(Component.text("Fehler beim beitreten: " + rawResponse.statusCode(), NamedTextColor.RED));
return;
}
p.sendMessage(Component.text("Betrete...", NamedTextColor.GREEN));
PluginMessage.connect(p, localConfig().getString("connect-server-name"));
} catch (URISyntaxException | IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
public void endEvent() {

View File

@ -12,7 +12,7 @@ public class EventCommand extends ApplianceCommand.PlayerChecked<Event> {
}
@Override
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
getAppliance().joinEvent(getPlayer());
}
}

View File

@ -2,6 +2,8 @@ package eu.mhsl.craftattack.spawn.appliances.event.command;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.event.Event;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@ -12,7 +14,8 @@ public class EventOpenSessionCommand extends ApplianceCommand<Event> {
}
@Override
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
getAppliance().openEvent();
sender.sendMessage(Component.text("Event-Server erfolgreich gestartet!", NamedTextColor.GREEN));
}
}

View File

@ -24,8 +24,10 @@ report:
api: https://mhsl.eu/craftattack/report
event:
api: http://10.20.0.1/
api: http://10.20.6.5:8080/
connect-server-name: event
enabled: false
roomId:
uuid:
villagerLocation:
world: world
@ -38,3 +40,6 @@ event:
help:
teamspeak: myserver.com
spawn: "Der Weltspawn befindet sich bei x:0 y:0 z:0"
playerLimit:
maxPlayers: 0