added EventRepository
This commit is contained in:
parent
696c4bc260
commit
184617e9c3
@ -0,0 +1,29 @@
|
||||
package eu.mhsl.craftattack.spawn.api.client.repositories;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.util.api.EventApiUtil;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class EventRepository extends HttpRepository {
|
||||
public EventRepository() {
|
||||
super(EventApiUtil.getBaseUri());
|
||||
}
|
||||
|
||||
public record CreatedRoom(UUID uuid) {
|
||||
}
|
||||
|
||||
public record QueueRoom(UUID player, UUID room) {
|
||||
public record Response(String error) {
|
||||
}
|
||||
}
|
||||
|
||||
public ReqResp<CreatedRoom> createSession() {
|
||||
return this.post("room", null, CreatedRoom.class);
|
||||
}
|
||||
|
||||
public ReqResp<QueueRoom.Response> queueRoom(QueueRoom request) {
|
||||
return this.post("queueRoom", request, QueueRoom.Response.class);
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ package eu.mhsl.craftattack.spawn.api.client.repositories;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
||||
import eu.mhsl.craftattack.spawn.util.api.WebsiteApiUtil;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
@ -12,7 +12,7 @@ import java.util.UUID;
|
||||
|
||||
public class FeedbackRepository extends HttpRepository {
|
||||
public FeedbackRepository() {
|
||||
super(ApiUtil.getBaseUri(), ApiUtil::withAuthorizationSecret);
|
||||
super(WebsiteApiUtil.getBaseUri(), WebsiteApiUtil::withAuthorizationSecret);
|
||||
}
|
||||
|
||||
public record Request(String event, List<UUID> users) {}
|
||||
|
@ -2,7 +2,7 @@ package eu.mhsl.craftattack.spawn.api.client.repositories;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
||||
import eu.mhsl.craftattack.spawn.util.api.WebsiteApiUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ -11,7 +11,7 @@ import java.util.UUID;
|
||||
|
||||
public class ReportRepository extends HttpRepository {
|
||||
public ReportRepository() {
|
||||
super(ApiUtil.getBaseUri(), ApiUtil::withAuthorizationSecret);
|
||||
super(WebsiteApiUtil.getBaseUri(), WebsiteApiUtil::withAuthorizationSecret);
|
||||
}
|
||||
|
||||
public record ReportCreationInfo(@NotNull UUID reporter, @Nullable UUID reported, String reason) {
|
||||
|
@ -2,13 +2,13 @@ package eu.mhsl.craftattack.spawn.api.client.repositories;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
||||
import eu.mhsl.craftattack.spawn.util.api.WebsiteApiUtil;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class WhitelistRepository extends HttpRepository {
|
||||
public WhitelistRepository() {
|
||||
super(ApiUtil.getBaseUri(), ApiUtil::withAuthorizationSecret);
|
||||
super(WebsiteApiUtil.getBaseUri(), WebsiteApiUtil::withAuthorizationSecret);
|
||||
}
|
||||
|
||||
public record UserData(
|
||||
|
@ -1,7 +1,8 @@
|
||||
package eu.mhsl.craftattack.spawn.appliances.event;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import eu.mhsl.craftattack.spawn.Main;
|
||||
import eu.mhsl.craftattack.spawn.api.client.ReqResp;
|
||||
import eu.mhsl.craftattack.spawn.api.client.repositories.EventRepository;
|
||||
import eu.mhsl.craftattack.spawn.api.server.HttpServer;
|
||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||
@ -10,6 +11,7 @@ import eu.mhsl.craftattack.spawn.appliances.customAdvancements.CustomAdvancement
|
||||
import eu.mhsl.craftattack.spawn.appliances.event.command.*;
|
||||
import eu.mhsl.craftattack.spawn.appliances.event.listener.ApplyPendingRewardsListener;
|
||||
import eu.mhsl.craftattack.spawn.util.IteratorUtil;
|
||||
import eu.mhsl.craftattack.spawn.util.api.HttpStatus;
|
||||
import eu.mhsl.craftattack.spawn.util.entity.DisplayVillager;
|
||||
import eu.mhsl.craftattack.spawn.util.listener.DismissInventoryOpenFromHolder;
|
||||
import eu.mhsl.craftattack.spawn.util.listener.PlayerInteractAtEntityEventListener;
|
||||
@ -27,12 +29,6 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
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.*;
|
||||
|
||||
public class Event extends Appliance {
|
||||
@ -57,7 +53,6 @@ public class Event extends Appliance {
|
||||
private boolean isOpen = false;
|
||||
private AdvertisementStatus advertiseStatus = AdvertisementStatus.BEFORE;
|
||||
private UUID roomId;
|
||||
private final HttpClient eventServerClient = HttpClient.newHttpClient();
|
||||
private final List<Reward> pendingRewards = new ArrayList<>();
|
||||
|
||||
record RewardConfiguration(String memorialMaterial, String memorialTitle, String memorialLore, List<UUID> memorials,
|
||||
@ -74,85 +69,62 @@ public class Event extends Appliance {
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.villager = new DisplayVillager.ConfigBound(
|
||||
localConfig(),
|
||||
this.localConfig(),
|
||||
villager -> {
|
||||
villager.customName(Component.text("Events", NamedTextColor.GOLD));
|
||||
villager.setProfession(Villager.Profession.LIBRARIAN);
|
||||
villager.setVillagerType(Villager.Type.SNOW);
|
||||
}
|
||||
);
|
||||
this.isOpen = localConfig().getBoolean("enabled", false);
|
||||
if(this.isOpen) this.roomId = UUID.fromString(localConfig().getString("roomId", ""));
|
||||
this.isOpen = this.localConfig().getBoolean("enabled", false);
|
||||
if(this.isOpen) this.roomId = UUID.fromString(this.localConfig().getString("roomId", ""));
|
||||
}
|
||||
|
||||
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();
|
||||
public void openEvent() {
|
||||
if(this.isOpen) throw new ApplianceCommand.Error("Es läuft derzeit bereits ein Event!");
|
||||
|
||||
HttpResponse<String> rawResponse = eventServerClient.send(createRoomRequest, HttpResponse.BodyHandlers.ofString());
|
||||
if(rawResponse.statusCode() != 200)
|
||||
throw new ApplianceCommand.Error("Event-Server meldet Fehler: " + rawResponse.statusCode());
|
||||
ReqResp<EventRepository.CreatedRoom> sessionResponse = this.queryRepository(EventRepository.class).createSession();
|
||||
|
||||
record Response(UUID uuid) {
|
||||
}
|
||||
Response response = new Gson().fromJson(rawResponse.body(), Response.class);
|
||||
if(sessionResponse.status() != HttpStatus.OK)
|
||||
throw new ApplianceCommand.Error("Event-Server meldet Fehler: " + sessionResponse.status());
|
||||
|
||||
isOpen = true;
|
||||
roomId = response.uuid;
|
||||
this.isOpen = true;
|
||||
this.roomId = sessionResponse.data().uuid();
|
||||
}
|
||||
|
||||
public void joinEvent(Player p) {
|
||||
if(!isOpen) {
|
||||
if(!this.isOpen) {
|
||||
p.sendMessage(Component.text("Zurzeit ist kein Event geöffnet.", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
if(!p.hasPermission("admin") && advertiseStatus == AdvertisementStatus.BEFORE) {
|
||||
if(!p.hasPermission("admin") && this.advertiseStatus == AdvertisementStatus.BEFORE) {
|
||||
p.sendMessage(Component.text("Die Event befinden sich noch in der Vorbereitung.", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
if(!p.hasPermission("admin") && advertiseStatus == AdvertisementStatus.DONE) {
|
||||
if(!p.hasPermission("admin") && this.advertiseStatus == AdvertisementStatus.DONE) {
|
||||
p.sendMessage(Component.text("Die Events laufen bereits. Ein nachträgliches Beitreten ist nicht möglich.", NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Main.instance().getLogger().info("Verbinde mit eventserver: " + p.getName());
|
||||
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();
|
||||
Main.instance().getLogger().info("Verbinde mit eventserver: " + p.getName());
|
||||
p.sendMessage(Component.text("Authentifiziere...", NamedTextColor.GREEN));
|
||||
ReqResp<EventRepository.QueueRoom.Response> queueRespone = this.queryRepository(EventRepository.class)
|
||||
.queueRoom(new EventRepository.QueueRoom(p.getUniqueId(), this.roomId));
|
||||
|
||||
record Response(String error) {
|
||||
}
|
||||
HttpResponse<String> rawResponse = eventServerClient.send(queueRoomRequest, HttpResponse.BodyHandlers.ofString());
|
||||
Main.instance().getLogger().info("Response: " + rawResponse.body());
|
||||
Response response = new Gson().fromJson(rawResponse.body(), Response.class);
|
||||
|
||||
if(rawResponse.statusCode() != 200 || response.error != null) {
|
||||
p.sendMessage(Component.text("Fehler beim Betreten: " + response.error, 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);
|
||||
if(queueRespone.status() != HttpStatus.OK || queueRespone.data().error() != null) {
|
||||
p.sendMessage(Component.text("Fehler beim Betreten: " + queueRespone.data().error(), NamedTextColor.RED));
|
||||
return;
|
||||
}
|
||||
|
||||
p.sendMessage(Component.text("Betrete...", NamedTextColor.GREEN));
|
||||
PluginMessage.connect(p, this.localConfig().getString("connect-server-name"));
|
||||
}
|
||||
|
||||
public void endEvent() {
|
||||
if(!isOpen) throw new ApplianceCommand.Error("Es läuft derzeit kein Event!");
|
||||
isOpen = false;
|
||||
if(!this.isOpen) throw new ApplianceCommand.Error("Es läuft derzeit kein Event!");
|
||||
this.isOpen = false;
|
||||
}
|
||||
|
||||
private void rewardPlayers(RewardConfiguration rewardConfiguration) {
|
||||
@ -160,10 +132,10 @@ public class Event extends Appliance {
|
||||
Reward reward = new Reward(uuid, new ItemStack(Objects.requireNonNull(Material.matchMaterial(rewardConfiguration.material)), amount));
|
||||
|
||||
if(Bukkit.getPlayer(uuid) == null) {
|
||||
pendingRewards.add(reward);
|
||||
this.pendingRewards.add(reward);
|
||||
return;
|
||||
}
|
||||
giveReward(reward);
|
||||
this.giveReward(reward);
|
||||
});
|
||||
|
||||
rewardConfiguration.memorials.forEach(uuid -> {
|
||||
@ -177,10 +149,10 @@ public class Event extends Appliance {
|
||||
Main.instance().getAppliance(CustomAdvancements.class).grantAdvancement(Advancements.participateEvent, uuid);
|
||||
|
||||
if(Bukkit.getPlayer(uuid) == null) {
|
||||
pendingRewards.add(memorial);
|
||||
this.pendingRewards.add(memorial);
|
||||
return;
|
||||
}
|
||||
giveReward(memorial);
|
||||
this.giveReward(memorial);
|
||||
});
|
||||
|
||||
rewardConfiguration.rewards.keySet().stream()
|
||||
@ -208,7 +180,7 @@ public class Event extends Appliance {
|
||||
}
|
||||
|
||||
public void advertise() {
|
||||
advertiseCountdown.cancelIfRunning();
|
||||
this.advertiseCountdown.cancelIfRunning();
|
||||
this.advertiseStatus = AdvertisementStatus.ADVERTISED;
|
||||
IteratorUtil.onlinePlayers(player -> player.sendMessage(
|
||||
Component.text()
|
||||
@ -219,7 +191,7 @@ public class Event extends Appliance {
|
||||
.append(Component.text(", um dem Event beizutreten!")).appendNewline()
|
||||
.append(Component.text("-".repeat(10), NamedTextColor.GRAY)).appendNewline()
|
||||
));
|
||||
advertiseCountdown.start();
|
||||
this.advertiseCountdown.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -247,7 +219,7 @@ public class Event extends Appliance {
|
||||
protected List<Listener> listeners() {
|
||||
return List.of(
|
||||
new ApplyPendingRewardsListener(),
|
||||
new PlayerInteractAtEntityEventListener(this.villager.getUniqueId(), playerInteractAtEntityEvent -> joinEvent(playerInteractAtEntityEvent.getPlayer())),
|
||||
new PlayerInteractAtEntityEventListener(this.villager.getUniqueId(), playerInteractAtEntityEvent -> this.joinEvent(playerInteractAtEntityEvent.getPlayer())),
|
||||
new DismissInventoryOpenFromHolder(this.villager.getUniqueId())
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package eu.mhsl.craftattack.spawn.util.api;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class EventApiUtil {
|
||||
private final static ConfigurationSection apiConfig = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("event"));
|
||||
public final static String basePath = apiConfig.getString("api");
|
||||
|
||||
public static URI getBaseUri() {
|
||||
Objects.requireNonNull(basePath);
|
||||
try {
|
||||
return new URI(basePath);
|
||||
} catch(URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -8,7 +8,7 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ApiUtil {
|
||||
public class WebsiteApiUtil {
|
||||
private final static ConfigurationSection apiConfig = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api"));
|
||||
public final static String basePath = apiConfig.getString("baseurl");
|
||||
public final static String apiSecret = apiConfig.getString("secret");
|
Loading…
x
Reference in New Issue
Block a user