Compare commits
3 Commits
cb45047f71
...
5786b2409e
Author | SHA1 | Date | |
---|---|---|---|
5786b2409e | |||
281a2109e6 | |||
9d79e8dc07 |
@ -7,9 +7,11 @@ import eu.mhsl.craftattack.spawn.appliances.countdown.Countdown;
|
||||
import eu.mhsl.craftattack.spawn.appliances.debug.Debug;
|
||||
import eu.mhsl.craftattack.spawn.appliances.event.Event;
|
||||
import eu.mhsl.craftattack.spawn.appliances.help.Help;
|
||||
import eu.mhsl.craftattack.spawn.appliances.playerlimit.PlayerLimit;
|
||||
import eu.mhsl.craftattack.spawn.appliances.report.Report;
|
||||
import eu.mhsl.craftattack.spawn.appliances.tablist.Tablist;
|
||||
import eu.mhsl.craftattack.spawn.appliances.titleClear.TitleClear;
|
||||
import eu.mhsl.craftattack.spawn.appliances.whitelist.Whitelist;
|
||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||
import eu.mhsl.craftattack.spawn.appliances.worldmuseum.WorldMuseum;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -39,6 +41,8 @@ public final class Main extends JavaPlugin {
|
||||
new Report(),
|
||||
new Event(),
|
||||
new Help(),
|
||||
new PlayerLimit(),
|
||||
new Whitelist(),
|
||||
new Debug()
|
||||
);
|
||||
Bukkit.getLogger().info("Loading appliances...");
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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() {
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.playerlimit;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.Main;
|
||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.playerlimit.command.SetPlayerLimitCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.playerlimit.listener.PlayerJoinLimiterListener;
|
||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerLimit extends Appliance {
|
||||
private static final String playerLimitKey = "maxPlayers";
|
||||
private int limit;
|
||||
public PlayerLimit() {
|
||||
super("playerLimit");
|
||||
this.limit = localConfig().getInt(playerLimitKey);
|
||||
Bukkit.setMaxPlayers(Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
public void setPlayerLimit(int limit) {
|
||||
this.limit = limit;
|
||||
localConfig().set(playerLimitKey, limit);
|
||||
Configuration.saveChanges();
|
||||
}
|
||||
|
||||
public int getLimit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<Listener> eventHandlers() {
|
||||
return List.of(
|
||||
new PlayerJoinLimiterListener()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||
return List.of(
|
||||
new SetPlayerLimitCommand()
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.playerlimit.command;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||
import eu.mhsl.craftattack.spawn.appliances.playerlimit.PlayerLimit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SetPlayerLimitCommand extends ApplianceCommand<PlayerLimit> {
|
||||
public SetPlayerLimitCommand() {
|
||||
super("setPlayerLimit");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
getAppliance().setPlayerLimit(Integer.parseInt(args[0]));
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.playerlimit.listener;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
|
||||
import eu.mhsl.craftattack.spawn.appliances.playerlimit.PlayerLimit;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
public class PlayerJoinLimiterListener extends ApplianceListener<PlayerLimit> {
|
||||
@EventHandler
|
||||
public void onLogin(PlayerLoginEvent playerPreLoginEvent) {
|
||||
playerPreLoginEvent.kickMessage(Component.text("Der Server ist derzeit voll! Versuche es bitte später erneut.", NamedTextColor.RED));
|
||||
if(Bukkit.getOnlinePlayers().size() >= getAppliance().getLimit()) playerPreLoginEvent.setResult(PlayerLoginEvent.Result.KICK_FULL);
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.whitelist;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
||||
|
||||
public class PlayerJoinListener extends ApplianceListener<Whitelist> {
|
||||
@EventHandler
|
||||
public void preLoginEvent(AsyncPlayerPreLoginEvent preLoginEvent) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.whitelist;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Whitelist extends Appliance {
|
||||
|
||||
|
||||
@Override
|
||||
protected @NotNull List<Listener> eventHandlers() {
|
||||
return List.of(
|
||||
new PlayerJoinListener()
|
||||
);
|
||||
}
|
||||
}
|
@ -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
|
||||
@ -37,4 +39,7 @@ event:
|
||||
|
||||
help:
|
||||
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
|
@ -29,3 +29,4 @@ commands:
|
||||
aliases: ["ts"]
|
||||
discord:
|
||||
aliases: ["dc"]
|
||||
setPlayerLimit:
|
||||
|
Loading…
x
Reference in New Issue
Block a user