Added Event api communication for creation of rooms
This commit is contained in:
		| @@ -8,6 +8,7 @@ import org.bukkit.event.HandlerList; | |||||||
| import org.bukkit.event.Listener; | import org.bukkit.event.Listener; | ||||||
| import org.bukkit.plugin.java.JavaPlugin; | import org.bukkit.plugin.java.JavaPlugin; | ||||||
| import org.jetbrains.annotations.NotNull; | import org.jetbrains.annotations.NotNull; | ||||||
|  | import org.jetbrains.annotations.Nullable; | ||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| @@ -68,16 +69,16 @@ public abstract class Appliance { | |||||||
|  |  | ||||||
|     public void destruct(@NotNull JavaPlugin plugin) { |     public void destruct(@NotNull JavaPlugin plugin) { | ||||||
|         eventHandlers().forEach(HandlerList::unregisterAll); |         eventHandlers().forEach(HandlerList::unregisterAll); | ||||||
|         commands().forEach(command -> setCommandExecutor(plugin, command.commandName, null)); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void setCommandExecutor(JavaPlugin plugin, String name, ApplianceCommand<?> executor) { |     private void setCommandExecutor(JavaPlugin plugin, String name, ApplianceCommand<?> executor) { | ||||||
|         PluginCommand command = plugin.getCommand(name); |         PluginCommand command = plugin.getCommand(name); | ||||||
|         if(command != null) { |         if(command != null && executor != null) { | ||||||
|             command.setExecutor(executor); |             command.setExecutor(executor); | ||||||
|             command.setTabCompleter(executor); |             command.setTabCompleter(executor); | ||||||
|         } else { |         } else { | ||||||
|             Bukkit.getLogger().warning("Command " + name + " is not specified in plugin.yml!"); |             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); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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(); |         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. |      * Utility class for command which can only be used as a Player. You can access the executing player with the getPlayer() method. | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| package eu.mhsl.craftattack.spawn.appliances.event; | 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.Appliance; | ||||||
| import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand; | import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand; | ||||||
| import eu.mhsl.craftattack.spawn.appliances.event.command.EventCommand; | 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.bukkit.event.Listener; | ||||||
| import org.jetbrains.annotations.NotNull; | 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.List; | ||||||
|  | import java.util.UUID; | ||||||
|  |  | ||||||
| public class Event extends Appliance { | public class Event extends Appliance { | ||||||
|     public DisplayVillager.ConfigBound villager; |     public DisplayVillager.ConfigBound villager; | ||||||
|     private boolean isOpen = false; |     private boolean isOpen = false; | ||||||
|  |     private UUID roomId; | ||||||
|  |     private HttpClient eventServerClient = HttpClient.newHttpClient(); | ||||||
|  |  | ||||||
|  |  | ||||||
|     public Event() { |     public Event() { | ||||||
|         super("event"); |         super("event"); | ||||||
| @@ -37,11 +48,25 @@ public class Event extends Appliance { | |||||||
|                     villager.setVillagerType(Villager.Type.SNOW); |                     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!"); |         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; |         isOpen = true; | ||||||
|  |         roomId = response.uuid; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void joinEvent(Player p) { |     public void joinEvent(Player p) { | ||||||
| @@ -50,7 +75,27 @@ public class Event extends Appliance { | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         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")); |             PluginMessage.connect(p, localConfig().getString("connect-server-name")); | ||||||
|  |  | ||||||
|  |         } catch (URISyntaxException | IOException | InterruptedException e) { | ||||||
|  |             throw new RuntimeException(e); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void endEvent() { |     public void endEvent() { | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ public class EventCommand extends ApplianceCommand.PlayerChecked<Event> { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @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()); |         getAppliance().joinEvent(getPlayer()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,6 +2,8 @@ package eu.mhsl.craftattack.spawn.appliances.event.command; | |||||||
|  |  | ||||||
| import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand; | import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand; | ||||||
| import eu.mhsl.craftattack.spawn.appliances.event.Event; | 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.Command; | ||||||
| import org.bukkit.command.CommandSender; | import org.bukkit.command.CommandSender; | ||||||
| import org.jetbrains.annotations.NotNull; | import org.jetbrains.annotations.NotNull; | ||||||
| @@ -12,7 +14,8 @@ public class EventOpenSessionCommand extends ApplianceCommand<Event> { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @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(); |         getAppliance().openEvent(); | ||||||
|  |         sender.sendMessage(Component.text("Event-Server erfolgreich gestartet!", NamedTextColor.GREEN)); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -24,8 +24,10 @@ report: | |||||||
|   api: https://mhsl.eu/craftattack/report |   api: https://mhsl.eu/craftattack/report | ||||||
|  |  | ||||||
| event: | event: | ||||||
|   api: http://10.20.0.1/ |   api: http://10.20.6.5:8080/ | ||||||
|   connect-server-name: event |   connect-server-name: event | ||||||
|  |   enabled: false | ||||||
|  |   roomId: | ||||||
|   uuid: |   uuid: | ||||||
|   villagerLocation: |   villagerLocation: | ||||||
|     world: world |     world: world | ||||||
| @@ -38,3 +40,6 @@ event: | |||||||
| help: | help: | ||||||
|   teamspeak: myserver.com |   teamspeak: myserver.com | ||||||
|   spawn: "Der Weltspawn befindet sich bei x:0 y:0 z:0" |   spawn: "Der Weltspawn befindet sich bei x:0 y:0 z:0" | ||||||
|  |  | ||||||
|  | playerLimit: | ||||||
|  |   maxPlayers: 0 | ||||||
		Reference in New Issue
	
	Block a user